1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.polls.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import com.liferay.portlet.polls.NoSuchChoiceException;
43  import com.liferay.portlet.polls.model.PollsChoice;
44  import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
45  import com.liferay.portlet.polls.model.impl.PollsChoiceModelImpl;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  /**
56   * <a href="PollsChoicePersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class PollsChoicePersistenceImpl extends BasePersistenceImpl
62      implements PollsChoicePersistence {
63      public PollsChoice create(long choiceId) {
64          PollsChoice pollsChoice = new PollsChoiceImpl();
65  
66          pollsChoice.setNew(true);
67          pollsChoice.setPrimaryKey(choiceId);
68  
69          String uuid = PortalUUIDUtil.generate();
70  
71          pollsChoice.setUuid(uuid);
72  
73          return pollsChoice;
74      }
75  
76      public PollsChoice remove(long choiceId)
77          throws NoSuchChoiceException, SystemException {
78          Session session = null;
79  
80          try {
81              session = openSession();
82  
83              PollsChoice pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
84                      new Long(choiceId));
85  
86              if (pollsChoice == null) {
87                  if (_log.isWarnEnabled()) {
88                      _log.warn("No PollsChoice exists with the primary key " +
89                          choiceId);
90                  }
91  
92                  throw new NoSuchChoiceException(
93                      "No PollsChoice exists with the primary key " + choiceId);
94              }
95  
96              return remove(pollsChoice);
97          }
98          catch (NoSuchChoiceException nsee) {
99              throw nsee;
100         }
101         catch (Exception e) {
102             throw processException(e);
103         }
104         finally {
105             closeSession(session);
106         }
107     }
108 
109     public PollsChoice remove(PollsChoice pollsChoice)
110         throws SystemException {
111         if (_listeners.length > 0) {
112             for (ModelListener listener : _listeners) {
113                 listener.onBeforeRemove(pollsChoice);
114             }
115         }
116 
117         pollsChoice = removeImpl(pollsChoice);
118 
119         if (_listeners.length > 0) {
120             for (ModelListener listener : _listeners) {
121                 listener.onAfterRemove(pollsChoice);
122             }
123         }
124 
125         return pollsChoice;
126     }
127 
128     protected PollsChoice removeImpl(PollsChoice pollsChoice)
129         throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             session.delete(pollsChoice);
136 
137             session.flush();
138 
139             return pollsChoice;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(PollsChoice.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(PollsChoice pollsChoice, boolean merge)</code>.
153      */
154     public PollsChoice update(PollsChoice pollsChoice)
155         throws SystemException {
156         if (_log.isWarnEnabled()) {
157             _log.warn(
158                 "Using the deprecated update(PollsChoice pollsChoice) method. Use update(PollsChoice pollsChoice, boolean merge) instead.");
159         }
160 
161         return update(pollsChoice, false);
162     }
163 
164     /**
165      * Add, update, or merge, the entity. This method also calls the model
166      * listeners to trigger the proper events associated with adding, deleting,
167      * or updating an entity.
168      *
169      * @param        pollsChoice the entity to add, update, or merge
170      * @param        merge boolean value for whether to merge the entity. The
171      *                default value is false. Setting merge to true is more
172      *                expensive and should only be true when pollsChoice is
173      *                transient. See LEP-5473 for a detailed discussion of this
174      *                method.
175      * @return        true if the portlet can be displayed via Ajax
176      */
177     public PollsChoice update(PollsChoice pollsChoice, boolean merge)
178         throws SystemException {
179         boolean isNew = pollsChoice.isNew();
180 
181         if (_listeners.length > 0) {
182             for (ModelListener listener : _listeners) {
183                 if (isNew) {
184                     listener.onBeforeCreate(pollsChoice);
185                 }
186                 else {
187                     listener.onBeforeUpdate(pollsChoice);
188                 }
189             }
190         }
191 
192         pollsChoice = updateImpl(pollsChoice, merge);
193 
194         if (_listeners.length > 0) {
195             for (ModelListener listener : _listeners) {
196                 if (isNew) {
197                     listener.onAfterCreate(pollsChoice);
198                 }
199                 else {
200                     listener.onAfterUpdate(pollsChoice);
201                 }
202             }
203         }
204 
205         return pollsChoice;
206     }
207 
208     public PollsChoice updateImpl(
209         com.liferay.portlet.polls.model.PollsChoice pollsChoice, boolean merge)
210         throws SystemException {
211         if (Validator.isNull(pollsChoice.getUuid())) {
212             String uuid = PortalUUIDUtil.generate();
213 
214             pollsChoice.setUuid(uuid);
215         }
216 
217         Session session = null;
218 
219         try {
220             session = openSession();
221 
222             if (merge) {
223                 session.merge(pollsChoice);
224             }
225             else {
226                 if (pollsChoice.isNew()) {
227                     session.save(pollsChoice);
228                 }
229             }
230 
231             session.flush();
232 
233             pollsChoice.setNew(false);
234 
235             return pollsChoice;
236         }
237         catch (Exception e) {
238             throw processException(e);
239         }
240         finally {
241             closeSession(session);
242 
243             FinderCacheUtil.clearCache(PollsChoice.class.getName());
244         }
245     }
246 
247     public PollsChoice findByPrimaryKey(long choiceId)
248         throws NoSuchChoiceException, SystemException {
249         PollsChoice pollsChoice = fetchByPrimaryKey(choiceId);
250 
251         if (pollsChoice == null) {
252             if (_log.isWarnEnabled()) {
253                 _log.warn("No PollsChoice exists with the primary key " +
254                     choiceId);
255             }
256 
257             throw new NoSuchChoiceException(
258                 "No PollsChoice exists with the primary key " + choiceId);
259         }
260 
261         return pollsChoice;
262     }
263 
264     public PollsChoice fetchByPrimaryKey(long choiceId)
265         throws SystemException {
266         Session session = null;
267 
268         try {
269             session = openSession();
270 
271             return (PollsChoice)session.get(PollsChoiceImpl.class,
272                 new Long(choiceId));
273         }
274         catch (Exception e) {
275             throw processException(e);
276         }
277         finally {
278             closeSession(session);
279         }
280     }
281 
282     public List<PollsChoice> findByUuid(String uuid) throws SystemException {
283         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
284         String finderClassName = PollsChoice.class.getName();
285         String finderMethodName = "findByUuid";
286         String[] finderParams = new String[] { String.class.getName() };
287         Object[] finderArgs = new Object[] { uuid };
288 
289         Object result = null;
290 
291         if (finderClassNameCacheEnabled) {
292             result = FinderCacheUtil.getResult(finderClassName,
293                     finderMethodName, finderParams, finderArgs, this);
294         }
295 
296         if (result == null) {
297             Session session = null;
298 
299             try {
300                 session = openSession();
301 
302                 StringBuilder query = new StringBuilder();
303 
304                 query.append(
305                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
306 
307                 if (uuid == null) {
308                     query.append("uuid_ IS NULL");
309                 }
310                 else {
311                     query.append("uuid_ = ?");
312                 }
313 
314                 query.append(" ");
315 
316                 query.append("ORDER BY ");
317 
318                 query.append("questionId ASC, ");
319                 query.append("name ASC");
320 
321                 Query q = session.createQuery(query.toString());
322 
323                 QueryPos qPos = QueryPos.getInstance(q);
324 
325                 if (uuid != null) {
326                     qPos.add(uuid);
327                 }
328 
329                 List<PollsChoice> list = q.list();
330 
331                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
332                     finderClassName, finderMethodName, finderParams,
333                     finderArgs, list);
334 
335                 return list;
336             }
337             catch (Exception e) {
338                 throw processException(e);
339             }
340             finally {
341                 closeSession(session);
342             }
343         }
344         else {
345             return (List<PollsChoice>)result;
346         }
347     }
348 
349     public List<PollsChoice> findByUuid(String uuid, int start, int end)
350         throws SystemException {
351         return findByUuid(uuid, start, end, null);
352     }
353 
354     public List<PollsChoice> findByUuid(String uuid, int start, int end,
355         OrderByComparator obc) throws SystemException {
356         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
357         String finderClassName = PollsChoice.class.getName();
358         String finderMethodName = "findByUuid";
359         String[] finderParams = new String[] {
360                 String.class.getName(),
361                 
362                 "java.lang.Integer", "java.lang.Integer",
363                 "com.liferay.portal.kernel.util.OrderByComparator"
364             };
365         Object[] finderArgs = new Object[] {
366                 uuid,
367                 
368                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
369             };
370 
371         Object result = null;
372 
373         if (finderClassNameCacheEnabled) {
374             result = FinderCacheUtil.getResult(finderClassName,
375                     finderMethodName, finderParams, finderArgs, this);
376         }
377 
378         if (result == null) {
379             Session session = null;
380 
381             try {
382                 session = openSession();
383 
384                 StringBuilder query = new StringBuilder();
385 
386                 query.append(
387                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
388 
389                 if (uuid == null) {
390                     query.append("uuid_ IS NULL");
391                 }
392                 else {
393                     query.append("uuid_ = ?");
394                 }
395 
396                 query.append(" ");
397 
398                 if (obc != null) {
399                     query.append("ORDER BY ");
400                     query.append(obc.getOrderBy());
401                 }
402 
403                 else {
404                     query.append("ORDER BY ");
405 
406                     query.append("questionId ASC, ");
407                     query.append("name ASC");
408                 }
409 
410                 Query q = session.createQuery(query.toString());
411 
412                 QueryPos qPos = QueryPos.getInstance(q);
413 
414                 if (uuid != null) {
415                     qPos.add(uuid);
416                 }
417 
418                 List<PollsChoice> list = (List<PollsChoice>)QueryUtil.list(q,
419                         getDialect(), start, end);
420 
421                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
422                     finderClassName, finderMethodName, finderParams,
423                     finderArgs, list);
424 
425                 return list;
426             }
427             catch (Exception e) {
428                 throw processException(e);
429             }
430             finally {
431                 closeSession(session);
432             }
433         }
434         else {
435             return (List<PollsChoice>)result;
436         }
437     }
438 
439     public PollsChoice findByUuid_First(String uuid, OrderByComparator obc)
440         throws NoSuchChoiceException, SystemException {
441         List<PollsChoice> list = findByUuid(uuid, 0, 1, obc);
442 
443         if (list.size() == 0) {
444             StringBuilder msg = new StringBuilder();
445 
446             msg.append("No PollsChoice exists with the key {");
447 
448             msg.append("uuid=" + uuid);
449 
450             msg.append(StringPool.CLOSE_CURLY_BRACE);
451 
452             throw new NoSuchChoiceException(msg.toString());
453         }
454         else {
455             return list.get(0);
456         }
457     }
458 
459     public PollsChoice findByUuid_Last(String uuid, OrderByComparator obc)
460         throws NoSuchChoiceException, SystemException {
461         int count = countByUuid(uuid);
462 
463         List<PollsChoice> list = findByUuid(uuid, count - 1, count, obc);
464 
465         if (list.size() == 0) {
466             StringBuilder msg = new StringBuilder();
467 
468             msg.append("No PollsChoice exists with the key {");
469 
470             msg.append("uuid=" + uuid);
471 
472             msg.append(StringPool.CLOSE_CURLY_BRACE);
473 
474             throw new NoSuchChoiceException(msg.toString());
475         }
476         else {
477             return list.get(0);
478         }
479     }
480 
481     public PollsChoice[] findByUuid_PrevAndNext(long choiceId, String uuid,
482         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
483         PollsChoice pollsChoice = findByPrimaryKey(choiceId);
484 
485         int count = countByUuid(uuid);
486 
487         Session session = null;
488 
489         try {
490             session = openSession();
491 
492             StringBuilder query = new StringBuilder();
493 
494             query.append(
495                 "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
496 
497             if (uuid == null) {
498                 query.append("uuid_ IS NULL");
499             }
500             else {
501                 query.append("uuid_ = ?");
502             }
503 
504             query.append(" ");
505 
506             if (obc != null) {
507                 query.append("ORDER BY ");
508                 query.append(obc.getOrderBy());
509             }
510 
511             else {
512                 query.append("ORDER BY ");
513 
514                 query.append("questionId ASC, ");
515                 query.append("name ASC");
516             }
517 
518             Query q = session.createQuery(query.toString());
519 
520             QueryPos qPos = QueryPos.getInstance(q);
521 
522             if (uuid != null) {
523                 qPos.add(uuid);
524             }
525 
526             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
527                     pollsChoice);
528 
529             PollsChoice[] array = new PollsChoiceImpl[3];
530 
531             array[0] = (PollsChoice)objArray[0];
532             array[1] = (PollsChoice)objArray[1];
533             array[2] = (PollsChoice)objArray[2];
534 
535             return array;
536         }
537         catch (Exception e) {
538             throw processException(e);
539         }
540         finally {
541             closeSession(session);
542         }
543     }
544 
545     public List<PollsChoice> findByQuestionId(long questionId)
546         throws SystemException {
547         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
548         String finderClassName = PollsChoice.class.getName();
549         String finderMethodName = "findByQuestionId";
550         String[] finderParams = new String[] { Long.class.getName() };
551         Object[] finderArgs = new Object[] { new Long(questionId) };
552 
553         Object result = null;
554 
555         if (finderClassNameCacheEnabled) {
556             result = FinderCacheUtil.getResult(finderClassName,
557                     finderMethodName, finderParams, finderArgs, this);
558         }
559 
560         if (result == null) {
561             Session session = null;
562 
563             try {
564                 session = openSession();
565 
566                 StringBuilder query = new StringBuilder();
567 
568                 query.append(
569                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
570 
571                 query.append("questionId = ?");
572 
573                 query.append(" ");
574 
575                 query.append("ORDER BY ");
576 
577                 query.append("questionId ASC, ");
578                 query.append("name ASC");
579 
580                 Query q = session.createQuery(query.toString());
581 
582                 QueryPos qPos = QueryPos.getInstance(q);
583 
584                 qPos.add(questionId);
585 
586                 List<PollsChoice> list = q.list();
587 
588                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
589                     finderClassName, finderMethodName, finderParams,
590                     finderArgs, list);
591 
592                 return list;
593             }
594             catch (Exception e) {
595                 throw processException(e);
596             }
597             finally {
598                 closeSession(session);
599             }
600         }
601         else {
602             return (List<PollsChoice>)result;
603         }
604     }
605 
606     public List<PollsChoice> findByQuestionId(long questionId, int start,
607         int end) throws SystemException {
608         return findByQuestionId(questionId, start, end, null);
609     }
610 
611     public List<PollsChoice> findByQuestionId(long questionId, int start,
612         int end, OrderByComparator obc) throws SystemException {
613         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
614         String finderClassName = PollsChoice.class.getName();
615         String finderMethodName = "findByQuestionId";
616         String[] finderParams = new String[] {
617                 Long.class.getName(),
618                 
619                 "java.lang.Integer", "java.lang.Integer",
620                 "com.liferay.portal.kernel.util.OrderByComparator"
621             };
622         Object[] finderArgs = new Object[] {
623                 new Long(questionId),
624                 
625                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
626             };
627 
628         Object result = null;
629 
630         if (finderClassNameCacheEnabled) {
631             result = FinderCacheUtil.getResult(finderClassName,
632                     finderMethodName, finderParams, finderArgs, this);
633         }
634 
635         if (result == null) {
636             Session session = null;
637 
638             try {
639                 session = openSession();
640 
641                 StringBuilder query = new StringBuilder();
642 
643                 query.append(
644                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
645 
646                 query.append("questionId = ?");
647 
648                 query.append(" ");
649 
650                 if (obc != null) {
651                     query.append("ORDER BY ");
652                     query.append(obc.getOrderBy());
653                 }
654 
655                 else {
656                     query.append("ORDER BY ");
657 
658                     query.append("questionId ASC, ");
659                     query.append("name ASC");
660                 }
661 
662                 Query q = session.createQuery(query.toString());
663 
664                 QueryPos qPos = QueryPos.getInstance(q);
665 
666                 qPos.add(questionId);
667 
668                 List<PollsChoice> list = (List<PollsChoice>)QueryUtil.list(q,
669                         getDialect(), start, end);
670 
671                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
672                     finderClassName, finderMethodName, finderParams,
673                     finderArgs, list);
674 
675                 return list;
676             }
677             catch (Exception e) {
678                 throw processException(e);
679             }
680             finally {
681                 closeSession(session);
682             }
683         }
684         else {
685             return (List<PollsChoice>)result;
686         }
687     }
688 
689     public PollsChoice findByQuestionId_First(long questionId,
690         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
691         List<PollsChoice> list = findByQuestionId(questionId, 0, 1, obc);
692 
693         if (list.size() == 0) {
694             StringBuilder msg = new StringBuilder();
695 
696             msg.append("No PollsChoice exists with the key {");
697 
698             msg.append("questionId=" + questionId);
699 
700             msg.append(StringPool.CLOSE_CURLY_BRACE);
701 
702             throw new NoSuchChoiceException(msg.toString());
703         }
704         else {
705             return list.get(0);
706         }
707     }
708 
709     public PollsChoice findByQuestionId_Last(long questionId,
710         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
711         int count = countByQuestionId(questionId);
712 
713         List<PollsChoice> list = findByQuestionId(questionId, count - 1, count,
714                 obc);
715 
716         if (list.size() == 0) {
717             StringBuilder msg = new StringBuilder();
718 
719             msg.append("No PollsChoice exists with the key {");
720 
721             msg.append("questionId=" + questionId);
722 
723             msg.append(StringPool.CLOSE_CURLY_BRACE);
724 
725             throw new NoSuchChoiceException(msg.toString());
726         }
727         else {
728             return list.get(0);
729         }
730     }
731 
732     public PollsChoice[] findByQuestionId_PrevAndNext(long choiceId,
733         long questionId, OrderByComparator obc)
734         throws NoSuchChoiceException, SystemException {
735         PollsChoice pollsChoice = findByPrimaryKey(choiceId);
736 
737         int count = countByQuestionId(questionId);
738 
739         Session session = null;
740 
741         try {
742             session = openSession();
743 
744             StringBuilder query = new StringBuilder();
745 
746             query.append(
747                 "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
748 
749             query.append("questionId = ?");
750 
751             query.append(" ");
752 
753             if (obc != null) {
754                 query.append("ORDER BY ");
755                 query.append(obc.getOrderBy());
756             }
757 
758             else {
759                 query.append("ORDER BY ");
760 
761                 query.append("questionId ASC, ");
762                 query.append("name ASC");
763             }
764 
765             Query q = session.createQuery(query.toString());
766 
767             QueryPos qPos = QueryPos.getInstance(q);
768 
769             qPos.add(questionId);
770 
771             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
772                     pollsChoice);
773 
774             PollsChoice[] array = new PollsChoiceImpl[3];
775 
776             array[0] = (PollsChoice)objArray[0];
777             array[1] = (PollsChoice)objArray[1];
778             array[2] = (PollsChoice)objArray[2];
779 
780             return array;
781         }
782         catch (Exception e) {
783             throw processException(e);
784         }
785         finally {
786             closeSession(session);
787         }
788     }
789 
790     public PollsChoice findByQ_N(long questionId, String name)
791         throws NoSuchChoiceException, SystemException {
792         PollsChoice pollsChoice = fetchByQ_N(questionId, name);
793 
794         if (pollsChoice == null) {
795             StringBuilder msg = new StringBuilder();
796 
797             msg.append("No PollsChoice exists with the key {");
798 
799             msg.append("questionId=" + questionId);
800 
801             msg.append(", ");
802             msg.append("name=" + name);
803 
804             msg.append(StringPool.CLOSE_CURLY_BRACE);
805 
806             if (_log.isWarnEnabled()) {
807                 _log.warn(msg.toString());
808             }
809 
810             throw new NoSuchChoiceException(msg.toString());
811         }
812 
813         return pollsChoice;
814     }
815 
816     public PollsChoice fetchByQ_N(long questionId, String name)
817         throws SystemException {
818         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
819         String finderClassName = PollsChoice.class.getName();
820         String finderMethodName = "fetchByQ_N";
821         String[] finderParams = new String[] {
822                 Long.class.getName(), String.class.getName()
823             };
824         Object[] finderArgs = new Object[] { new Long(questionId), name };
825 
826         Object result = null;
827 
828         if (finderClassNameCacheEnabled) {
829             result = FinderCacheUtil.getResult(finderClassName,
830                     finderMethodName, finderParams, finderArgs, this);
831         }
832 
833         if (result == null) {
834             Session session = null;
835 
836             try {
837                 session = openSession();
838 
839                 StringBuilder query = new StringBuilder();
840 
841                 query.append(
842                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
843 
844                 query.append("questionId = ?");
845 
846                 query.append(" AND ");
847 
848                 if (name == null) {
849                     query.append("name IS NULL");
850                 }
851                 else {
852                     query.append("name = ?");
853                 }
854 
855                 query.append(" ");
856 
857                 query.append("ORDER BY ");
858 
859                 query.append("questionId ASC, ");
860                 query.append("name ASC");
861 
862                 Query q = session.createQuery(query.toString());
863 
864                 QueryPos qPos = QueryPos.getInstance(q);
865 
866                 qPos.add(questionId);
867 
868                 if (name != null) {
869                     qPos.add(name);
870                 }
871 
872                 List<PollsChoice> list = q.list();
873 
874                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
875                     finderClassName, finderMethodName, finderParams,
876                     finderArgs, list);
877 
878                 if (list.size() == 0) {
879                     return null;
880                 }
881                 else {
882                     return list.get(0);
883                 }
884             }
885             catch (Exception e) {
886                 throw processException(e);
887             }
888             finally {
889                 closeSession(session);
890             }
891         }
892         else {
893             List<PollsChoice> list = (List<PollsChoice>)result;
894 
895             if (list.size() == 0) {
896                 return null;
897             }
898             else {
899                 return list.get(0);
900             }
901         }
902     }
903 
904     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
905         throws SystemException {
906         Session session = null;
907 
908         try {
909             session = openSession();
910 
911             dynamicQuery.compile(session);
912 
913             return dynamicQuery.list();
914         }
915         catch (Exception e) {
916             throw processException(e);
917         }
918         finally {
919             closeSession(session);
920         }
921     }
922 
923     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
924         int start, int end) throws SystemException {
925         Session session = null;
926 
927         try {
928             session = openSession();
929 
930             dynamicQuery.setLimit(start, end);
931 
932             dynamicQuery.compile(session);
933 
934             return dynamicQuery.list();
935         }
936         catch (Exception e) {
937             throw processException(e);
938         }
939         finally {
940             closeSession(session);
941         }
942     }
943 
944     public List<PollsChoice> findAll() throws SystemException {
945         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
946     }
947 
948     public List<PollsChoice> findAll(int start, int end)
949         throws SystemException {
950         return findAll(start, end, null);
951     }
952 
953     public List<PollsChoice> findAll(int start, int end, OrderByComparator obc)
954         throws SystemException {
955         boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
956         String finderClassName = PollsChoice.class.getName();
957         String finderMethodName = "findAll";
958         String[] finderParams = new String[] {
959                 "java.lang.Integer", "java.lang.Integer",
960                 "com.liferay.portal.kernel.util.OrderByComparator"
961             };
962         Object[] finderArgs = new Object[] {
963                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
964             };
965 
966         Object result = null;
967 
968         if (finderClassNameCacheEnabled) {
969             result = FinderCacheUtil.getResult(finderClassName,
970                     finderMethodName, finderParams, finderArgs, this);
971         }
972 
973         if (result == null) {
974             Session session = null;
975 
976             try {
977                 session = openSession();
978 
979                 StringBuilder query = new StringBuilder();
980 
981                 query.append(
982                     "FROM com.liferay.portlet.polls.model.PollsChoice ");
983 
984                 if (obc != null) {
985                     query.append("ORDER BY ");
986                     query.append(obc.getOrderBy());
987                 }
988 
989                 else {
990                     query.append("ORDER BY ");
991 
992                     query.append("questionId ASC, ");
993                     query.append("name ASC");
994                 }
995 
996                 Query q = session.createQuery(query.toString());
997 
998                 List<PollsChoice> list = (List<PollsChoice>)QueryUtil.list(q,
999                         getDialect(), start, end);
1000
1001                if (obc == null) {
1002                    Collections.sort(list);
1003                }
1004
1005                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1006                    finderClassName, finderMethodName, finderParams,
1007                    finderArgs, list);
1008
1009                return list;
1010            }
1011            catch (Exception e) {
1012                throw processException(e);
1013            }
1014            finally {
1015                closeSession(session);
1016            }
1017        }
1018        else {
1019            return (List<PollsChoice>)result;
1020        }
1021    }
1022
1023    public void removeByUuid(String uuid) throws SystemException {
1024        for (PollsChoice pollsChoice : findByUuid(uuid)) {
1025            remove(pollsChoice);
1026        }
1027    }
1028
1029    public void removeByQuestionId(long questionId) throws SystemException {
1030        for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1031            remove(pollsChoice);
1032        }
1033    }
1034
1035    public void removeByQ_N(long questionId, String name)
1036        throws NoSuchChoiceException, SystemException {
1037        PollsChoice pollsChoice = findByQ_N(questionId, name);
1038
1039        remove(pollsChoice);
1040    }
1041
1042    public void removeAll() throws SystemException {
1043        for (PollsChoice pollsChoice : findAll()) {
1044            remove(pollsChoice);
1045        }
1046    }
1047
1048    public int countByUuid(String uuid) throws SystemException {
1049        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1050        String finderClassName = PollsChoice.class.getName();
1051        String finderMethodName = "countByUuid";
1052        String[] finderParams = new String[] { String.class.getName() };
1053        Object[] finderArgs = new Object[] { uuid };
1054
1055        Object result = null;
1056
1057        if (finderClassNameCacheEnabled) {
1058            result = FinderCacheUtil.getResult(finderClassName,
1059                    finderMethodName, finderParams, finderArgs, this);
1060        }
1061
1062        if (result == null) {
1063            Session session = null;
1064
1065            try {
1066                session = openSession();
1067
1068                StringBuilder query = new StringBuilder();
1069
1070                query.append("SELECT COUNT(*) ");
1071                query.append(
1072                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1073
1074                if (uuid == null) {
1075                    query.append("uuid_ IS NULL");
1076                }
1077                else {
1078                    query.append("uuid_ = ?");
1079                }
1080
1081                query.append(" ");
1082
1083                Query q = session.createQuery(query.toString());
1084
1085                QueryPos qPos = QueryPos.getInstance(q);
1086
1087                if (uuid != null) {
1088                    qPos.add(uuid);
1089                }
1090
1091                Long count = null;
1092
1093                Iterator<Long> itr = q.list().iterator();
1094
1095                if (itr.hasNext()) {
1096                    count = itr.next();
1097                }
1098
1099                if (count == null) {
1100                    count = new Long(0);
1101                }
1102
1103                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1104                    finderClassName, finderMethodName, finderParams,
1105                    finderArgs, count);
1106
1107                return count.intValue();
1108            }
1109            catch (Exception e) {
1110                throw processException(e);
1111            }
1112            finally {
1113                closeSession(session);
1114            }
1115        }
1116        else {
1117            return ((Long)result).intValue();
1118        }
1119    }
1120
1121    public int countByQuestionId(long questionId) throws SystemException {
1122        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1123        String finderClassName = PollsChoice.class.getName();
1124        String finderMethodName = "countByQuestionId";
1125        String[] finderParams = new String[] { Long.class.getName() };
1126        Object[] finderArgs = new Object[] { new Long(questionId) };
1127
1128        Object result = null;
1129
1130        if (finderClassNameCacheEnabled) {
1131            result = FinderCacheUtil.getResult(finderClassName,
1132                    finderMethodName, finderParams, finderArgs, this);
1133        }
1134
1135        if (result == null) {
1136            Session session = null;
1137
1138            try {
1139                session = openSession();
1140
1141                StringBuilder query = new StringBuilder();
1142
1143                query.append("SELECT COUNT(*) ");
1144                query.append(
1145                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1146
1147                query.append("questionId = ?");
1148
1149                query.append(" ");
1150
1151                Query q = session.createQuery(query.toString());
1152
1153                QueryPos qPos = QueryPos.getInstance(q);
1154
1155                qPos.add(questionId);
1156
1157                Long count = null;
1158
1159                Iterator<Long> itr = q.list().iterator();
1160
1161                if (itr.hasNext()) {
1162                    count = itr.next();
1163                }
1164
1165                if (count == null) {
1166                    count = new Long(0);
1167                }
1168
1169                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1170                    finderClassName, finderMethodName, finderParams,
1171                    finderArgs, count);
1172
1173                return count.intValue();
1174            }
1175            catch (Exception e) {
1176                throw processException(e);
1177            }
1178            finally {
1179                closeSession(session);
1180            }
1181        }
1182        else {
1183            return ((Long)result).intValue();
1184        }
1185    }
1186
1187    public int countByQ_N(long questionId, String name)
1188        throws SystemException {
1189        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1190        String finderClassName = PollsChoice.class.getName();
1191        String finderMethodName = "countByQ_N";
1192        String[] finderParams = new String[] {
1193                Long.class.getName(), String.class.getName()
1194            };
1195        Object[] finderArgs = new Object[] { new Long(questionId), name };
1196
1197        Object result = null;
1198
1199        if (finderClassNameCacheEnabled) {
1200            result = FinderCacheUtil.getResult(finderClassName,
1201                    finderMethodName, finderParams, finderArgs, this);
1202        }
1203
1204        if (result == null) {
1205            Session session = null;
1206
1207            try {
1208                session = openSession();
1209
1210                StringBuilder query = new StringBuilder();
1211
1212                query.append("SELECT COUNT(*) ");
1213                query.append(
1214                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1215
1216                query.append("questionId = ?");
1217
1218                query.append(" AND ");
1219
1220                if (name == null) {
1221                    query.append("name IS NULL");
1222                }
1223                else {
1224                    query.append("name = ?");
1225                }
1226
1227                query.append(" ");
1228
1229                Query q = session.createQuery(query.toString());
1230
1231                QueryPos qPos = QueryPos.getInstance(q);
1232
1233                qPos.add(questionId);
1234
1235                if (name != null) {
1236                    qPos.add(name);
1237                }
1238
1239                Long count = null;
1240
1241                Iterator<Long> itr = q.list().iterator();
1242
1243                if (itr.hasNext()) {
1244                    count = itr.next();
1245                }
1246
1247                if (count == null) {
1248                    count = new Long(0);
1249                }
1250
1251                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1252                    finderClassName, finderMethodName, finderParams,
1253                    finderArgs, count);
1254
1255                return count.intValue();
1256            }
1257            catch (Exception e) {
1258                throw processException(e);
1259            }
1260            finally {
1261                closeSession(session);
1262            }
1263        }
1264        else {
1265            return ((Long)result).intValue();
1266        }
1267    }
1268
1269    public int countAll() throws SystemException {
1270        boolean finderClassNameCacheEnabled = PollsChoiceModelImpl.CACHE_ENABLED;
1271        String finderClassName = PollsChoice.class.getName();
1272        String finderMethodName = "countAll";
1273        String[] finderParams = new String[] {  };
1274        Object[] finderArgs = new Object[] {  };
1275
1276        Object result = null;
1277
1278        if (finderClassNameCacheEnabled) {
1279            result = FinderCacheUtil.getResult(finderClassName,
1280                    finderMethodName, finderParams, finderArgs, this);
1281        }
1282
1283        if (result == null) {
1284            Session session = null;
1285
1286            try {
1287                session = openSession();
1288
1289                Query q = session.createQuery(
1290                        "SELECT COUNT(*) FROM com.liferay.portlet.polls.model.PollsChoice");
1291
1292                Long count = null;
1293
1294                Iterator<Long> itr = q.list().iterator();
1295
1296                if (itr.hasNext()) {
1297                    count = itr.next();
1298                }
1299
1300                if (count == null) {
1301                    count = new Long(0);
1302                }
1303
1304                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1305                    finderClassName, finderMethodName, finderParams,
1306                    finderArgs, count);
1307
1308                return count.intValue();
1309            }
1310            catch (Exception e) {
1311                throw processException(e);
1312            }
1313            finally {
1314                closeSession(session);
1315            }
1316        }
1317        else {
1318            return ((Long)result).intValue();
1319        }
1320    }
1321
1322    public void registerListener(ModelListener listener) {
1323        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1324
1325        listeners.add(listener);
1326
1327        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1328    }
1329
1330    public void unregisterListener(ModelListener listener) {
1331        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1332
1333        listeners.remove(listener);
1334
1335        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1336    }
1337
1338    public void afterPropertiesSet() {
1339        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1340                    com.liferay.portal.util.PropsUtil.get(
1341                        "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1342
1343        if (listenerClassNames.length > 0) {
1344            try {
1345                List<ModelListener> listeners = new ArrayList<ModelListener>();
1346
1347                for (String listenerClassName : listenerClassNames) {
1348                    listeners.add((ModelListener)Class.forName(
1349                            listenerClassName).newInstance());
1350                }
1351
1352                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1353            }
1354            catch (Exception e) {
1355                _log.error(e);
1356            }
1357        }
1358    }
1359
1360    private static Log _log = LogFactory.getLog(PollsChoicePersistenceImpl.class);
1361    private ModelListener[] _listeners = new ModelListener[0];
1362}