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(
401                     "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
402 
403                 query.append("questionId = ?");
404 
405                 query.append(" ");
406 
407                 Query q = session.createQuery(query.toString());
408 
409                 QueryPos qPos = QueryPos.getInstance(q);
410 
411                 qPos.add(questionId);
412 
413                 list = q.list();
414             }
415             catch (Exception e) {
416                 throw processException(e);
417             }
418             finally {
419                 if (list == null) {
420                     list = new ArrayList<PollsVote>();
421                 }
422 
423                 cacheResult(list);
424 
425                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
426                     finderArgs, list);
427 
428                 closeSession(session);
429             }
430         }
431 
432         return list;
433     }
434 
435     public List<PollsVote> findByQuestionId(long questionId, int start, int end)
436         throws SystemException {
437         return findByQuestionId(questionId, start, end, null);
438     }
439 
440     public List<PollsVote> findByQuestionId(long questionId, int start,
441         int end, OrderByComparator obc) throws SystemException {
442         Object[] finderArgs = new Object[] {
443                 new Long(questionId),
444                 
445                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
446             };
447 
448         List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
449                 finderArgs, this);
450 
451         if (list == null) {
452             Session session = null;
453 
454             try {
455                 session = openSession();
456 
457                 StringBuilder query = new StringBuilder();
458 
459                 query.append(
460                     "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
461 
462                 query.append("questionId = ?");
463 
464                 query.append(" ");
465 
466                 if (obc != null) {
467                     query.append("ORDER BY ");
468                     query.append(obc.getOrderBy());
469                 }
470 
471                 Query q = session.createQuery(query.toString());
472 
473                 QueryPos qPos = QueryPos.getInstance(q);
474 
475                 qPos.add(questionId);
476 
477                 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
478                         end);
479             }
480             catch (Exception e) {
481                 throw processException(e);
482             }
483             finally {
484                 if (list == null) {
485                     list = new ArrayList<PollsVote>();
486                 }
487 
488                 cacheResult(list);
489 
490                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
491                     finderArgs, list);
492 
493                 closeSession(session);
494             }
495         }
496 
497         return list;
498     }
499 
500     public PollsVote findByQuestionId_First(long questionId,
501         OrderByComparator obc) throws NoSuchVoteException, SystemException {
502         List<PollsVote> list = findByQuestionId(questionId, 0, 1, obc);
503 
504         if (list.isEmpty()) {
505             StringBuilder msg = new StringBuilder();
506 
507             msg.append("No PollsVote exists with the key {");
508 
509             msg.append("questionId=" + questionId);
510 
511             msg.append(StringPool.CLOSE_CURLY_BRACE);
512 
513             throw new NoSuchVoteException(msg.toString());
514         }
515         else {
516             return list.get(0);
517         }
518     }
519 
520     public PollsVote findByQuestionId_Last(long questionId,
521         OrderByComparator obc) throws NoSuchVoteException, SystemException {
522         int count = countByQuestionId(questionId);
523 
524         List<PollsVote> list = findByQuestionId(questionId, count - 1, count,
525                 obc);
526 
527         if (list.isEmpty()) {
528             StringBuilder msg = new StringBuilder();
529 
530             msg.append("No PollsVote exists with the key {");
531 
532             msg.append("questionId=" + questionId);
533 
534             msg.append(StringPool.CLOSE_CURLY_BRACE);
535 
536             throw new NoSuchVoteException(msg.toString());
537         }
538         else {
539             return list.get(0);
540         }
541     }
542 
543     public PollsVote[] findByQuestionId_PrevAndNext(long voteId,
544         long questionId, OrderByComparator obc)
545         throws NoSuchVoteException, SystemException {
546         PollsVote pollsVote = findByPrimaryKey(voteId);
547 
548         int count = countByQuestionId(questionId);
549 
550         Session session = null;
551 
552         try {
553             session = openSession();
554 
555             StringBuilder query = new StringBuilder();
556 
557             query.append(
558                 "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
559 
560             query.append("questionId = ?");
561 
562             query.append(" ");
563 
564             if (obc != null) {
565                 query.append("ORDER BY ");
566                 query.append(obc.getOrderBy());
567             }
568 
569             Query q = session.createQuery(query.toString());
570 
571             QueryPos qPos = QueryPos.getInstance(q);
572 
573             qPos.add(questionId);
574 
575             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
576                     pollsVote);
577 
578             PollsVote[] array = new PollsVoteImpl[3];
579 
580             array[0] = (PollsVote)objArray[0];
581             array[1] = (PollsVote)objArray[1];
582             array[2] = (PollsVote)objArray[2];
583 
584             return array;
585         }
586         catch (Exception e) {
587             throw processException(e);
588         }
589         finally {
590             closeSession(session);
591         }
592     }
593 
594     public List<PollsVote> findByChoiceId(long choiceId)
595         throws SystemException {
596         Object[] finderArgs = new Object[] { new Long(choiceId) };
597 
598         List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CHOICEID,
599                 finderArgs, this);
600 
601         if (list == null) {
602             Session session = null;
603 
604             try {
605                 session = openSession();
606 
607                 StringBuilder query = new StringBuilder();
608 
609                 query.append(
610                     "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
611 
612                 query.append("choiceId = ?");
613 
614                 query.append(" ");
615 
616                 Query q = session.createQuery(query.toString());
617 
618                 QueryPos qPos = QueryPos.getInstance(q);
619 
620                 qPos.add(choiceId);
621 
622                 list = q.list();
623             }
624             catch (Exception e) {
625                 throw processException(e);
626             }
627             finally {
628                 if (list == null) {
629                     list = new ArrayList<PollsVote>();
630                 }
631 
632                 cacheResult(list);
633 
634                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CHOICEID,
635                     finderArgs, list);
636 
637                 closeSession(session);
638             }
639         }
640 
641         return list;
642     }
643 
644     public List<PollsVote> findByChoiceId(long choiceId, int start, int end)
645         throws SystemException {
646         return findByChoiceId(choiceId, start, end, null);
647     }
648 
649     public List<PollsVote> findByChoiceId(long choiceId, int start, int end,
650         OrderByComparator obc) throws SystemException {
651         Object[] finderArgs = new Object[] {
652                 new Long(choiceId),
653                 
654                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
655             };
656 
657         List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CHOICEID,
658                 finderArgs, this);
659 
660         if (list == null) {
661             Session session = null;
662 
663             try {
664                 session = openSession();
665 
666                 StringBuilder query = new StringBuilder();
667 
668                 query.append(
669                     "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
670 
671                 query.append("choiceId = ?");
672 
673                 query.append(" ");
674 
675                 if (obc != null) {
676                     query.append("ORDER BY ");
677                     query.append(obc.getOrderBy());
678                 }
679 
680                 Query q = session.createQuery(query.toString());
681 
682                 QueryPos qPos = QueryPos.getInstance(q);
683 
684                 qPos.add(choiceId);
685 
686                 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
687                         end);
688             }
689             catch (Exception e) {
690                 throw processException(e);
691             }
692             finally {
693                 if (list == null) {
694                     list = new ArrayList<PollsVote>();
695                 }
696 
697                 cacheResult(list);
698 
699                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CHOICEID,
700                     finderArgs, list);
701 
702                 closeSession(session);
703             }
704         }
705 
706         return list;
707     }
708 
709     public PollsVote findByChoiceId_First(long choiceId, OrderByComparator obc)
710         throws NoSuchVoteException, SystemException {
711         List<PollsVote> list = findByChoiceId(choiceId, 0, 1, obc);
712 
713         if (list.isEmpty()) {
714             StringBuilder msg = new StringBuilder();
715 
716             msg.append("No PollsVote exists with the key {");
717 
718             msg.append("choiceId=" + choiceId);
719 
720             msg.append(StringPool.CLOSE_CURLY_BRACE);
721 
722             throw new NoSuchVoteException(msg.toString());
723         }
724         else {
725             return list.get(0);
726         }
727     }
728 
729     public PollsVote findByChoiceId_Last(long choiceId, OrderByComparator obc)
730         throws NoSuchVoteException, SystemException {
731         int count = countByChoiceId(choiceId);
732 
733         List<PollsVote> list = findByChoiceId(choiceId, count - 1, count, obc);
734 
735         if (list.isEmpty()) {
736             StringBuilder msg = new StringBuilder();
737 
738             msg.append("No PollsVote exists with the key {");
739 
740             msg.append("choiceId=" + choiceId);
741 
742             msg.append(StringPool.CLOSE_CURLY_BRACE);
743 
744             throw new NoSuchVoteException(msg.toString());
745         }
746         else {
747             return list.get(0);
748         }
749     }
750 
751     public PollsVote[] findByChoiceId_PrevAndNext(long voteId, long choiceId,
752         OrderByComparator obc) throws NoSuchVoteException, SystemException {
753         PollsVote pollsVote = findByPrimaryKey(voteId);
754 
755         int count = countByChoiceId(choiceId);
756 
757         Session session = null;
758 
759         try {
760             session = openSession();
761 
762             StringBuilder query = new StringBuilder();
763 
764             query.append(
765                 "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
766 
767             query.append("choiceId = ?");
768 
769             query.append(" ");
770 
771             if (obc != null) {
772                 query.append("ORDER BY ");
773                 query.append(obc.getOrderBy());
774             }
775 
776             Query q = session.createQuery(query.toString());
777 
778             QueryPos qPos = QueryPos.getInstance(q);
779 
780             qPos.add(choiceId);
781 
782             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
783                     pollsVote);
784 
785             PollsVote[] array = new PollsVoteImpl[3];
786 
787             array[0] = (PollsVote)objArray[0];
788             array[1] = (PollsVote)objArray[1];
789             array[2] = (PollsVote)objArray[2];
790 
791             return array;
792         }
793         catch (Exception e) {
794             throw processException(e);
795         }
796         finally {
797             closeSession(session);
798         }
799     }
800 
801     public PollsVote findByQ_U(long questionId, long userId)
802         throws NoSuchVoteException, SystemException {
803         PollsVote pollsVote = fetchByQ_U(questionId, userId);
804 
805         if (pollsVote == null) {
806             StringBuilder msg = new StringBuilder();
807 
808             msg.append("No PollsVote exists with the key {");
809 
810             msg.append("questionId=" + questionId);
811 
812             msg.append(", ");
813             msg.append("userId=" + userId);
814 
815             msg.append(StringPool.CLOSE_CURLY_BRACE);
816 
817             if (_log.isWarnEnabled()) {
818                 _log.warn(msg.toString());
819             }
820 
821             throw new NoSuchVoteException(msg.toString());
822         }
823 
824         return pollsVote;
825     }
826 
827     public PollsVote fetchByQ_U(long questionId, long userId)
828         throws SystemException {
829         return fetchByQ_U(questionId, userId, true);
830     }
831 
832     public PollsVote fetchByQ_U(long questionId, long userId,
833         boolean retrieveFromCache) throws SystemException {
834         Object[] finderArgs = new Object[] {
835                 new Long(questionId), new Long(userId)
836             };
837 
838         Object result = null;
839 
840         if (retrieveFromCache) {
841             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_U,
842                     finderArgs, this);
843         }
844 
845         if (result == null) {
846             Session session = null;
847 
848             try {
849                 session = openSession();
850 
851                 StringBuilder query = new StringBuilder();
852 
853                 query.append(
854                     "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
855 
856                 query.append("questionId = ?");
857 
858                 query.append(" AND ");
859 
860                 query.append("userId = ?");
861 
862                 query.append(" ");
863 
864                 Query q = session.createQuery(query.toString());
865 
866                 QueryPos qPos = QueryPos.getInstance(q);
867 
868                 qPos.add(questionId);
869 
870                 qPos.add(userId);
871 
872                 List<PollsVote> list = q.list();
873 
874                 result = list;
875 
876                 PollsVote pollsVote = null;
877 
878                 if (list.isEmpty()) {
879                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
880                         finderArgs, list);
881                 }
882                 else {
883                     pollsVote = list.get(0);
884 
885                     cacheResult(pollsVote);
886 
887                     if ((pollsVote.getQuestionId() != questionId) ||
888                             (pollsVote.getUserId() != userId)) {
889                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
890                             finderArgs, pollsVote);
891                     }
892                 }
893 
894                 return pollsVote;
895             }
896             catch (Exception e) {
897                 throw processException(e);
898             }
899             finally {
900                 if (result == null) {
901                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
902                         finderArgs, new ArrayList<PollsVote>());
903                 }
904 
905                 closeSession(session);
906             }
907         }
908         else {
909             if (result instanceof List) {
910                 return null;
911             }
912             else {
913                 return (PollsVote)result;
914             }
915         }
916     }
917 
918     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
919         throws SystemException {
920         Session session = null;
921 
922         try {
923             session = openSession();
924 
925             dynamicQuery.compile(session);
926 
927             return dynamicQuery.list();
928         }
929         catch (Exception e) {
930             throw processException(e);
931         }
932         finally {
933             closeSession(session);
934         }
935     }
936 
937     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
938         int start, int end) throws SystemException {
939         Session session = null;
940 
941         try {
942             session = openSession();
943 
944             dynamicQuery.setLimit(start, end);
945 
946             dynamicQuery.compile(session);
947 
948             return dynamicQuery.list();
949         }
950         catch (Exception e) {
951             throw processException(e);
952         }
953         finally {
954             closeSession(session);
955         }
956     }
957 
958     public List<PollsVote> findAll() throws SystemException {
959         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
960     }
961 
962     public List<PollsVote> findAll(int start, int end)
963         throws SystemException {
964         return findAll(start, end, null);
965     }
966 
967     public List<PollsVote> findAll(int start, int end, OrderByComparator obc)
968         throws SystemException {
969         Object[] finderArgs = new Object[] {
970                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
971             };
972 
973         List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
974                 finderArgs, this);
975 
976         if (list == null) {
977             Session session = null;
978 
979             try {
980                 session = openSession();
981 
982                 StringBuilder query = new StringBuilder();
983 
984                 query.append("FROM com.liferay.portlet.polls.model.PollsVote ");
985 
986                 if (obc != null) {
987                     query.append("ORDER BY ");
988                     query.append(obc.getOrderBy());
989                 }
990 
991                 Query q = session.createQuery(query.toString());
992 
993                 if (obc == null) {
994                     list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
995                             start, end, false);
996 
997                     Collections.sort(list);
998                 }
999                 else {
1000                    list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1001                            start, end);
1002                }
1003            }
1004            catch (Exception e) {
1005                throw processException(e);
1006            }
1007            finally {
1008                if (list == null) {
1009                    list = new ArrayList<PollsVote>();
1010                }
1011
1012                cacheResult(list);
1013
1014                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1015
1016                closeSession(session);
1017            }
1018        }
1019
1020        return list;
1021    }
1022
1023    public void removeByQuestionId(long questionId) throws SystemException {
1024        for (PollsVote pollsVote : findByQuestionId(questionId)) {
1025            remove(pollsVote);
1026        }
1027    }
1028
1029    public void removeByChoiceId(long choiceId) throws SystemException {
1030        for (PollsVote pollsVote : findByChoiceId(choiceId)) {
1031            remove(pollsVote);
1032        }
1033    }
1034
1035    public void removeByQ_U(long questionId, long userId)
1036        throws NoSuchVoteException, SystemException {
1037        PollsVote pollsVote = findByQ_U(questionId, userId);
1038
1039        remove(pollsVote);
1040    }
1041
1042    public void removeAll() throws SystemException {
1043        for (PollsVote pollsVote : findAll()) {
1044            remove(pollsVote);
1045        }
1046    }
1047
1048    public int countByQuestionId(long questionId) throws SystemException {
1049        Object[] finderArgs = new Object[] { new Long(questionId) };
1050
1051        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1052                finderArgs, this);
1053
1054        if (count == null) {
1055            Session session = null;
1056
1057            try {
1058                session = openSession();
1059
1060                StringBuilder query = new StringBuilder();
1061
1062                query.append("SELECT COUNT(*) ");
1063                query.append(
1064                    "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
1065
1066                query.append("questionId = ?");
1067
1068                query.append(" ");
1069
1070                Query q = session.createQuery(query.toString());
1071
1072                QueryPos qPos = QueryPos.getInstance(q);
1073
1074                qPos.add(questionId);
1075
1076                count = (Long)q.uniqueResult();
1077            }
1078            catch (Exception e) {
1079                throw processException(e);
1080            }
1081            finally {
1082                if (count == null) {
1083                    count = Long.valueOf(0);
1084                }
1085
1086                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1087                    finderArgs, count);
1088
1089                closeSession(session);
1090            }
1091        }
1092
1093        return count.intValue();
1094    }
1095
1096    public int countByChoiceId(long choiceId) throws SystemException {
1097        Object[] finderArgs = new Object[] { new Long(choiceId) };
1098
1099        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CHOICEID,
1100                finderArgs, this);
1101
1102        if (count == null) {
1103            Session session = null;
1104
1105            try {
1106                session = openSession();
1107
1108                StringBuilder query = new StringBuilder();
1109
1110                query.append("SELECT COUNT(*) ");
1111                query.append(
1112                    "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
1113
1114                query.append("choiceId = ?");
1115
1116                query.append(" ");
1117
1118                Query q = session.createQuery(query.toString());
1119
1120                QueryPos qPos = QueryPos.getInstance(q);
1121
1122                qPos.add(choiceId);
1123
1124                count = (Long)q.uniqueResult();
1125            }
1126            catch (Exception e) {
1127                throw processException(e);
1128            }
1129            finally {
1130                if (count == null) {
1131                    count = Long.valueOf(0);
1132                }
1133
1134                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CHOICEID,
1135                    finderArgs, count);
1136
1137                closeSession(session);
1138            }
1139        }
1140
1141        return count.intValue();
1142    }
1143
1144    public int countByQ_U(long questionId, long userId)
1145        throws SystemException {
1146        Object[] finderArgs = new Object[] {
1147                new Long(questionId), new Long(userId)
1148            };
1149
1150        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_U,
1151                finderArgs, this);
1152
1153        if (count == null) {
1154            Session session = null;
1155
1156            try {
1157                session = openSession();
1158
1159                StringBuilder query = new StringBuilder();
1160
1161                query.append("SELECT COUNT(*) ");
1162                query.append(
1163                    "FROM com.liferay.portlet.polls.model.PollsVote WHERE ");
1164
1165                query.append("questionId = ?");
1166
1167                query.append(" AND ");
1168
1169                query.append("userId = ?");
1170
1171                query.append(" ");
1172
1173                Query q = session.createQuery(query.toString());
1174
1175                QueryPos qPos = QueryPos.getInstance(q);
1176
1177                qPos.add(questionId);
1178
1179                qPos.add(userId);
1180
1181                count = (Long)q.uniqueResult();
1182            }
1183            catch (Exception e) {
1184                throw processException(e);
1185            }
1186            finally {
1187                if (count == null) {
1188                    count = Long.valueOf(0);
1189                }
1190
1191                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_U, finderArgs,
1192                    count);
1193
1194                closeSession(session);
1195            }
1196        }
1197
1198        return count.intValue();
1199    }
1200
1201    public int countAll() throws SystemException {
1202        Object[] finderArgs = new Object[0];
1203
1204        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1205                finderArgs, this);
1206
1207        if (count == null) {
1208            Session session = null;
1209
1210            try {
1211                session = openSession();
1212
1213                Query q = session.createQuery(
1214                        "SELECT COUNT(*) FROM com.liferay.portlet.polls.model.PollsVote");
1215
1216                count = (Long)q.uniqueResult();
1217            }
1218            catch (Exception e) {
1219                throw processException(e);
1220            }
1221            finally {
1222                if (count == null) {
1223                    count = Long.valueOf(0);
1224                }
1225
1226                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1227                    count);
1228
1229                closeSession(session);
1230            }
1231        }
1232
1233        return count.intValue();
1234    }
1235
1236    public void afterPropertiesSet() {
1237        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1238                    com.liferay.portal.util.PropsUtil.get(
1239                        "value.object.listener.com.liferay.portlet.polls.model.PollsVote")));
1240
1241        if (listenerClassNames.length > 0) {
1242            try {
1243                List<ModelListener<PollsVote>> listenersList = new ArrayList<ModelListener<PollsVote>>();
1244
1245                for (String listenerClassName : listenerClassNames) {
1246                    listenersList.add((ModelListener<PollsVote>)Class.forName(
1247                            listenerClassName).newInstance());
1248                }
1249
1250                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1251            }
1252            catch (Exception e) {
1253                _log.error(e);
1254            }
1255        }
1256    }
1257
1258    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsChoicePersistence.impl")
1259    protected com.liferay.portlet.polls.service.persistence.PollsChoicePersistence pollsChoicePersistence;
1260    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence.impl")
1261    protected com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence pollsQuestionPersistence;
1262    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsVotePersistence.impl")
1263    protected com.liferay.portlet.polls.service.persistence.PollsVotePersistence pollsVotePersistence;
1264    private static Log _log = LogFactoryUtil.getLog(PollsVotePersistenceImpl.class);
1265}