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