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