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