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