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