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