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