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