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