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