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