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