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