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