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