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