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