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                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
423 
424                 if (uuid == null) {
425                     query.append("uuid_ IS NULL");
426                 }
427                 else {
428                     query.append("uuid_ = ?");
429                 }
430 
431                 query.append(" ");
432 
433                 query.append("ORDER BY ");
434 
435                 query.append("questionId ASC, ");
436                 query.append("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                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
494 
495                 if (uuid == null) {
496                     query.append("uuid_ IS NULL");
497                 }
498                 else {
499                     query.append("uuid_ = ?");
500                 }
501 
502                 query.append(" ");
503 
504                 if (obc != null) {
505                     query.append("ORDER BY ");
506                     query.append(obc.getOrderBy());
507                 }
508 
509                 else {
510                     query.append("ORDER BY ");
511 
512                     query.append("questionId ASC, ");
513                     query.append("name ASC");
514                 }
515 
516                 Query q = session.createQuery(query.toString());
517 
518                 QueryPos qPos = QueryPos.getInstance(q);
519 
520                 if (uuid != null) {
521                     qPos.add(uuid);
522                 }
523 
524                 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
525                         start, end);
526             }
527             catch (Exception e) {
528                 throw processException(e);
529             }
530             finally {
531                 if (list == null) {
532                     list = new ArrayList<PollsChoice>();
533                 }
534 
535                 cacheResult(list);
536 
537                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_UUID,
538                     finderArgs, list);
539 
540                 closeSession(session);
541             }
542         }
543 
544         return list;
545     }
546 
547     public PollsChoice findByUuid_First(String uuid, OrderByComparator obc)
548         throws NoSuchChoiceException, SystemException {
549         List<PollsChoice> list = findByUuid(uuid, 0, 1, obc);
550 
551         if (list.isEmpty()) {
552             StringBuilder msg = new StringBuilder();
553 
554             msg.append("No PollsChoice exists with the key {");
555 
556             msg.append("uuid=" + uuid);
557 
558             msg.append(StringPool.CLOSE_CURLY_BRACE);
559 
560             throw new NoSuchChoiceException(msg.toString());
561         }
562         else {
563             return list.get(0);
564         }
565     }
566 
567     public PollsChoice findByUuid_Last(String uuid, OrderByComparator obc)
568         throws NoSuchChoiceException, SystemException {
569         int count = countByUuid(uuid);
570 
571         List<PollsChoice> list = findByUuid(uuid, count - 1, count, obc);
572 
573         if (list.isEmpty()) {
574             StringBuilder msg = new StringBuilder();
575 
576             msg.append("No PollsChoice exists with the key {");
577 
578             msg.append("uuid=" + uuid);
579 
580             msg.append(StringPool.CLOSE_CURLY_BRACE);
581 
582             throw new NoSuchChoiceException(msg.toString());
583         }
584         else {
585             return list.get(0);
586         }
587     }
588 
589     public PollsChoice[] findByUuid_PrevAndNext(long choiceId, String uuid,
590         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
591         PollsChoice pollsChoice = findByPrimaryKey(choiceId);
592 
593         int count = countByUuid(uuid);
594 
595         Session session = null;
596 
597         try {
598             session = openSession();
599 
600             StringBuilder query = new StringBuilder();
601 
602             query.append(
603                 "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
604 
605             if (uuid == null) {
606                 query.append("uuid_ IS NULL");
607             }
608             else {
609                 query.append("uuid_ = ?");
610             }
611 
612             query.append(" ");
613 
614             if (obc != null) {
615                 query.append("ORDER BY ");
616                 query.append(obc.getOrderBy());
617             }
618 
619             else {
620                 query.append("ORDER BY ");
621 
622                 query.append("questionId ASC, ");
623                 query.append("name ASC");
624             }
625 
626             Query q = session.createQuery(query.toString());
627 
628             QueryPos qPos = QueryPos.getInstance(q);
629 
630             if (uuid != null) {
631                 qPos.add(uuid);
632             }
633 
634             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
635                     pollsChoice);
636 
637             PollsChoice[] array = new PollsChoiceImpl[3];
638 
639             array[0] = (PollsChoice)objArray[0];
640             array[1] = (PollsChoice)objArray[1];
641             array[2] = (PollsChoice)objArray[2];
642 
643             return array;
644         }
645         catch (Exception e) {
646             throw processException(e);
647         }
648         finally {
649             closeSession(session);
650         }
651     }
652 
653     public List<PollsChoice> findByQuestionId(long questionId)
654         throws SystemException {
655         Object[] finderArgs = new Object[] { new Long(questionId) };
656 
657         List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
658                 finderArgs, this);
659 
660         if (list == null) {
661             Session session = null;
662 
663             try {
664                 session = openSession();
665 
666                 StringBuilder query = new StringBuilder();
667 
668                 query.append(
669                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
670 
671                 query.append("questionId = ?");
672 
673                 query.append(" ");
674 
675                 query.append("ORDER BY ");
676 
677                 query.append("questionId ASC, ");
678                 query.append("name ASC");
679 
680                 Query q = session.createQuery(query.toString());
681 
682                 QueryPos qPos = QueryPos.getInstance(q);
683 
684                 qPos.add(questionId);
685 
686                 list = q.list();
687             }
688             catch (Exception e) {
689                 throw processException(e);
690             }
691             finally {
692                 if (list == null) {
693                     list = new ArrayList<PollsChoice>();
694                 }
695 
696                 cacheResult(list);
697 
698                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
699                     finderArgs, list);
700 
701                 closeSession(session);
702             }
703         }
704 
705         return list;
706     }
707 
708     public List<PollsChoice> findByQuestionId(long questionId, int start,
709         int end) throws SystemException {
710         return findByQuestionId(questionId, start, end, null);
711     }
712 
713     public List<PollsChoice> findByQuestionId(long questionId, int start,
714         int end, OrderByComparator obc) throws SystemException {
715         Object[] finderArgs = new Object[] {
716                 new Long(questionId),
717                 
718                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
719             };
720 
721         List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
722                 finderArgs, this);
723 
724         if (list == null) {
725             Session session = null;
726 
727             try {
728                 session = openSession();
729 
730                 StringBuilder query = new StringBuilder();
731 
732                 query.append(
733                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
734 
735                 query.append("questionId = ?");
736 
737                 query.append(" ");
738 
739                 if (obc != null) {
740                     query.append("ORDER BY ");
741                     query.append(obc.getOrderBy());
742                 }
743 
744                 else {
745                     query.append("ORDER BY ");
746 
747                     query.append("questionId ASC, ");
748                     query.append("name ASC");
749                 }
750 
751                 Query q = session.createQuery(query.toString());
752 
753                 QueryPos qPos = QueryPos.getInstance(q);
754 
755                 qPos.add(questionId);
756 
757                 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
758                         start, end);
759             }
760             catch (Exception e) {
761                 throw processException(e);
762             }
763             finally {
764                 if (list == null) {
765                     list = new ArrayList<PollsChoice>();
766                 }
767 
768                 cacheResult(list);
769 
770                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
771                     finderArgs, list);
772 
773                 closeSession(session);
774             }
775         }
776 
777         return list;
778     }
779 
780     public PollsChoice findByQuestionId_First(long questionId,
781         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
782         List<PollsChoice> list = findByQuestionId(questionId, 0, 1, obc);
783 
784         if (list.isEmpty()) {
785             StringBuilder msg = new StringBuilder();
786 
787             msg.append("No PollsChoice exists with the key {");
788 
789             msg.append("questionId=" + questionId);
790 
791             msg.append(StringPool.CLOSE_CURLY_BRACE);
792 
793             throw new NoSuchChoiceException(msg.toString());
794         }
795         else {
796             return list.get(0);
797         }
798     }
799 
800     public PollsChoice findByQuestionId_Last(long questionId,
801         OrderByComparator obc) throws NoSuchChoiceException, SystemException {
802         int count = countByQuestionId(questionId);
803 
804         List<PollsChoice> list = findByQuestionId(questionId, count - 1, count,
805                 obc);
806 
807         if (list.isEmpty()) {
808             StringBuilder msg = new StringBuilder();
809 
810             msg.append("No PollsChoice exists with the key {");
811 
812             msg.append("questionId=" + questionId);
813 
814             msg.append(StringPool.CLOSE_CURLY_BRACE);
815 
816             throw new NoSuchChoiceException(msg.toString());
817         }
818         else {
819             return list.get(0);
820         }
821     }
822 
823     public PollsChoice[] findByQuestionId_PrevAndNext(long choiceId,
824         long questionId, OrderByComparator obc)
825         throws NoSuchChoiceException, SystemException {
826         PollsChoice pollsChoice = findByPrimaryKey(choiceId);
827 
828         int count = countByQuestionId(questionId);
829 
830         Session session = null;
831 
832         try {
833             session = openSession();
834 
835             StringBuilder query = new StringBuilder();
836 
837             query.append(
838                 "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
839 
840             query.append("questionId = ?");
841 
842             query.append(" ");
843 
844             if (obc != null) {
845                 query.append("ORDER BY ");
846                 query.append(obc.getOrderBy());
847             }
848 
849             else {
850                 query.append("ORDER BY ");
851 
852                 query.append("questionId ASC, ");
853                 query.append("name ASC");
854             }
855 
856             Query q = session.createQuery(query.toString());
857 
858             QueryPos qPos = QueryPos.getInstance(q);
859 
860             qPos.add(questionId);
861 
862             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
863                     pollsChoice);
864 
865             PollsChoice[] array = new PollsChoiceImpl[3];
866 
867             array[0] = (PollsChoice)objArray[0];
868             array[1] = (PollsChoice)objArray[1];
869             array[2] = (PollsChoice)objArray[2];
870 
871             return array;
872         }
873         catch (Exception e) {
874             throw processException(e);
875         }
876         finally {
877             closeSession(session);
878         }
879     }
880 
881     public PollsChoice findByQ_N(long questionId, String name)
882         throws NoSuchChoiceException, SystemException {
883         PollsChoice pollsChoice = fetchByQ_N(questionId, name);
884 
885         if (pollsChoice == null) {
886             StringBuilder msg = new StringBuilder();
887 
888             msg.append("No PollsChoice exists with the key {");
889 
890             msg.append("questionId=" + questionId);
891 
892             msg.append(", ");
893             msg.append("name=" + name);
894 
895             msg.append(StringPool.CLOSE_CURLY_BRACE);
896 
897             if (_log.isWarnEnabled()) {
898                 _log.warn(msg.toString());
899             }
900 
901             throw new NoSuchChoiceException(msg.toString());
902         }
903 
904         return pollsChoice;
905     }
906 
907     public PollsChoice fetchByQ_N(long questionId, String name)
908         throws SystemException {
909         return fetchByQ_N(questionId, name, true);
910     }
911 
912     public PollsChoice fetchByQ_N(long questionId, String name,
913         boolean retrieveFromCache) throws SystemException {
914         Object[] finderArgs = new Object[] { new Long(questionId), name };
915 
916         Object result = null;
917 
918         if (retrieveFromCache) {
919             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_N,
920                     finderArgs, this);
921         }
922 
923         if (result == null) {
924             Session session = null;
925 
926             try {
927                 session = openSession();
928 
929                 StringBuilder query = new StringBuilder();
930 
931                 query.append(
932                     "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
933 
934                 query.append("questionId = ?");
935 
936                 query.append(" AND ");
937 
938                 if (name == null) {
939                     query.append("name IS NULL");
940                 }
941                 else {
942                     query.append("name = ?");
943                 }
944 
945                 query.append(" ");
946 
947                 query.append("ORDER BY ");
948 
949                 query.append("questionId ASC, ");
950                 query.append("name ASC");
951 
952                 Query q = session.createQuery(query.toString());
953 
954                 QueryPos qPos = QueryPos.getInstance(q);
955 
956                 qPos.add(questionId);
957 
958                 if (name != null) {
959                     qPos.add(name);
960                 }
961 
962                 List<PollsChoice> list = q.list();
963 
964                 result = list;
965 
966                 PollsChoice pollsChoice = null;
967 
968                 if (list.isEmpty()) {
969                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
970                         finderArgs, list);
971                 }
972                 else {
973                     pollsChoice = list.get(0);
974 
975                     cacheResult(pollsChoice);
976 
977                     if ((pollsChoice.getQuestionId() != questionId) ||
978                             (pollsChoice.getName() == null) ||
979                             !pollsChoice.getName().equals(name)) {
980                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
981                             finderArgs, pollsChoice);
982                     }
983                 }
984 
985                 return pollsChoice;
986             }
987             catch (Exception e) {
988                 throw processException(e);
989             }
990             finally {
991                 if (result == null) {
992                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
993                         finderArgs, new ArrayList<PollsChoice>());
994                 }
995 
996                 closeSession(session);
997             }
998         }
999         else {
1000            if (result instanceof List) {
1001                return null;
1002            }
1003            else {
1004                return (PollsChoice)result;
1005            }
1006        }
1007    }
1008
1009    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1010        throws SystemException {
1011        Session session = null;
1012
1013        try {
1014            session = openSession();
1015
1016            dynamicQuery.compile(session);
1017
1018            return dynamicQuery.list();
1019        }
1020        catch (Exception e) {
1021            throw processException(e);
1022        }
1023        finally {
1024            closeSession(session);
1025        }
1026    }
1027
1028    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1029        int start, int end) throws SystemException {
1030        Session session = null;
1031
1032        try {
1033            session = openSession();
1034
1035            dynamicQuery.setLimit(start, end);
1036
1037            dynamicQuery.compile(session);
1038
1039            return dynamicQuery.list();
1040        }
1041        catch (Exception e) {
1042            throw processException(e);
1043        }
1044        finally {
1045            closeSession(session);
1046        }
1047    }
1048
1049    public List<PollsChoice> findAll() throws SystemException {
1050        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1051    }
1052
1053    public List<PollsChoice> findAll(int start, int end)
1054        throws SystemException {
1055        return findAll(start, end, null);
1056    }
1057
1058    public List<PollsChoice> findAll(int start, int end, OrderByComparator obc)
1059        throws SystemException {
1060        Object[] finderArgs = new Object[] {
1061                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1062            };
1063
1064        List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1065                finderArgs, this);
1066
1067        if (list == null) {
1068            Session session = null;
1069
1070            try {
1071                session = openSession();
1072
1073                StringBuilder query = new StringBuilder();
1074
1075                query.append(
1076                    "FROM com.liferay.portlet.polls.model.PollsChoice ");
1077
1078                if (obc != null) {
1079                    query.append("ORDER BY ");
1080                    query.append(obc.getOrderBy());
1081                }
1082
1083                else {
1084                    query.append("ORDER BY ");
1085
1086                    query.append("questionId ASC, ");
1087                    query.append("name ASC");
1088                }
1089
1090                Query q = session.createQuery(query.toString());
1091
1092                if (obc == null) {
1093                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1094                            start, end, false);
1095
1096                    Collections.sort(list);
1097                }
1098                else {
1099                    list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1100                            start, end);
1101                }
1102            }
1103            catch (Exception e) {
1104                throw processException(e);
1105            }
1106            finally {
1107                if (list == null) {
1108                    list = new ArrayList<PollsChoice>();
1109                }
1110
1111                cacheResult(list);
1112
1113                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1114
1115                closeSession(session);
1116            }
1117        }
1118
1119        return list;
1120    }
1121
1122    public void removeByUuid(String uuid) throws SystemException {
1123        for (PollsChoice pollsChoice : findByUuid(uuid)) {
1124            remove(pollsChoice);
1125        }
1126    }
1127
1128    public void removeByQuestionId(long questionId) throws SystemException {
1129        for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1130            remove(pollsChoice);
1131        }
1132    }
1133
1134    public void removeByQ_N(long questionId, String name)
1135        throws NoSuchChoiceException, SystemException {
1136        PollsChoice pollsChoice = findByQ_N(questionId, name);
1137
1138        remove(pollsChoice);
1139    }
1140
1141    public void removeAll() throws SystemException {
1142        for (PollsChoice pollsChoice : findAll()) {
1143            remove(pollsChoice);
1144        }
1145    }
1146
1147    public int countByUuid(String uuid) throws SystemException {
1148        Object[] finderArgs = new Object[] { uuid };
1149
1150        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1151                finderArgs, this);
1152
1153        if (count == null) {
1154            Session session = null;
1155
1156            try {
1157                session = openSession();
1158
1159                StringBuilder query = new StringBuilder();
1160
1161                query.append("SELECT COUNT(*) ");
1162                query.append(
1163                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1164
1165                if (uuid == null) {
1166                    query.append("uuid_ IS NULL");
1167                }
1168                else {
1169                    query.append("uuid_ = ?");
1170                }
1171
1172                query.append(" ");
1173
1174                Query q = session.createQuery(query.toString());
1175
1176                QueryPos qPos = QueryPos.getInstance(q);
1177
1178                if (uuid != null) {
1179                    qPos.add(uuid);
1180                }
1181
1182                count = (Long)q.uniqueResult();
1183            }
1184            catch (Exception e) {
1185                throw processException(e);
1186            }
1187            finally {
1188                if (count == null) {
1189                    count = Long.valueOf(0);
1190                }
1191
1192                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1193                    finderArgs, count);
1194
1195                closeSession(session);
1196            }
1197        }
1198
1199        return count.intValue();
1200    }
1201
1202    public int countByQuestionId(long questionId) throws SystemException {
1203        Object[] finderArgs = new Object[] { new Long(questionId) };
1204
1205        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1206                finderArgs, this);
1207
1208        if (count == null) {
1209            Session session = null;
1210
1211            try {
1212                session = openSession();
1213
1214                StringBuilder query = new StringBuilder();
1215
1216                query.append("SELECT COUNT(*) ");
1217                query.append(
1218                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1219
1220                query.append("questionId = ?");
1221
1222                query.append(" ");
1223
1224                Query q = session.createQuery(query.toString());
1225
1226                QueryPos qPos = QueryPos.getInstance(q);
1227
1228                qPos.add(questionId);
1229
1230                count = (Long)q.uniqueResult();
1231            }
1232            catch (Exception e) {
1233                throw processException(e);
1234            }
1235            finally {
1236                if (count == null) {
1237                    count = Long.valueOf(0);
1238                }
1239
1240                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1241                    finderArgs, count);
1242
1243                closeSession(session);
1244            }
1245        }
1246
1247        return count.intValue();
1248    }
1249
1250    public int countByQ_N(long questionId, String name)
1251        throws SystemException {
1252        Object[] finderArgs = new Object[] { new Long(questionId), name };
1253
1254        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_N,
1255                finderArgs, this);
1256
1257        if (count == null) {
1258            Session session = null;
1259
1260            try {
1261                session = openSession();
1262
1263                StringBuilder query = new StringBuilder();
1264
1265                query.append("SELECT COUNT(*) ");
1266                query.append(
1267                    "FROM com.liferay.portlet.polls.model.PollsChoice WHERE ");
1268
1269                query.append("questionId = ?");
1270
1271                query.append(" AND ");
1272
1273                if (name == null) {
1274                    query.append("name IS NULL");
1275                }
1276                else {
1277                    query.append("name = ?");
1278                }
1279
1280                query.append(" ");
1281
1282                Query q = session.createQuery(query.toString());
1283
1284                QueryPos qPos = QueryPos.getInstance(q);
1285
1286                qPos.add(questionId);
1287
1288                if (name != null) {
1289                    qPos.add(name);
1290                }
1291
1292                count = (Long)q.uniqueResult();
1293            }
1294            catch (Exception e) {
1295                throw processException(e);
1296            }
1297            finally {
1298                if (count == null) {
1299                    count = Long.valueOf(0);
1300                }
1301
1302                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_N, finderArgs,
1303                    count);
1304
1305                closeSession(session);
1306            }
1307        }
1308
1309        return count.intValue();
1310    }
1311
1312    public int countAll() throws SystemException {
1313        Object[] finderArgs = new Object[0];
1314
1315        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1316                finderArgs, this);
1317
1318        if (count == null) {
1319            Session session = null;
1320
1321            try {
1322                session = openSession();
1323
1324                Query q = session.createQuery(
1325                        "SELECT COUNT(*) FROM com.liferay.portlet.polls.model.PollsChoice");
1326
1327                count = (Long)q.uniqueResult();
1328            }
1329            catch (Exception e) {
1330                throw processException(e);
1331            }
1332            finally {
1333                if (count == null) {
1334                    count = Long.valueOf(0);
1335                }
1336
1337                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1338                    count);
1339
1340                closeSession(session);
1341            }
1342        }
1343
1344        return count.intValue();
1345    }
1346
1347    public void afterPropertiesSet() {
1348        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1349                    com.liferay.portal.util.PropsUtil.get(
1350                        "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1351
1352        if (listenerClassNames.length > 0) {
1353            try {
1354                List<ModelListener<PollsChoice>> listenersList = new ArrayList<ModelListener<PollsChoice>>();
1355
1356                for (String listenerClassName : listenerClassNames) {
1357                    listenersList.add((ModelListener<PollsChoice>)Class.forName(
1358                            listenerClassName).newInstance());
1359                }
1360
1361                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1362            }
1363            catch (Exception e) {
1364                _log.error(e);
1365            }
1366        }
1367    }
1368
1369    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsChoicePersistence.impl")
1370    protected com.liferay.portlet.polls.service.persistence.PollsChoicePersistence pollsChoicePersistence;
1371    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence.impl")
1372    protected com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence pollsQuestionPersistence;
1373    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsVotePersistence.impl")
1374    protected com.liferay.portlet.polls.service.persistence.PollsVotePersistence pollsVotePersistence;
1375    private static Log _log = LogFactoryUtil.getLog(PollsChoicePersistenceImpl.class);
1376}