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