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