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