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