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