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