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