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