001
014
015 package com.liferay.portlet.polls.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.kernel.annotation.BeanReference;
019 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderPath;
023 import com.liferay.portal.kernel.dao.orm.Query;
024 import com.liferay.portal.kernel.dao.orm.QueryPos;
025 import com.liferay.portal.kernel.dao.orm.QueryUtil;
026 import com.liferay.portal.kernel.dao.orm.Session;
027 import com.liferay.portal.kernel.exception.SystemException;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.InstanceFactory;
032 import com.liferay.portal.kernel.util.OrderByComparator;
033 import com.liferay.portal.kernel.util.StringBundler;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.StringUtil;
036 import com.liferay.portal.model.ModelListener;
037 import com.liferay.portal.service.persistence.BatchSessionUtil;
038 import com.liferay.portal.service.persistence.ResourcePersistence;
039 import com.liferay.portal.service.persistence.UserPersistence;
040 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041
042 import com.liferay.portlet.polls.NoSuchVoteException;
043 import com.liferay.portlet.polls.model.PollsVote;
044 import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
045 import com.liferay.portlet.polls.model.impl.PollsVoteModelImpl;
046
047 import java.io.Serializable;
048
049 import java.util.ArrayList;
050 import java.util.Collections;
051 import java.util.List;
052
053
069 public class PollsVotePersistenceImpl extends BasePersistenceImpl<PollsVote>
070 implements PollsVotePersistence {
071 public static final String FINDER_CLASS_NAME_ENTITY = PollsVoteImpl.class.getName();
072 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
073 ".List";
074 public static final FinderPath FINDER_PATH_FIND_BY_QUESTIONID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
075 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
076 "findByQuestionId",
077 new String[] {
078 Long.class.getName(),
079
080 "java.lang.Integer", "java.lang.Integer",
081 "com.liferay.portal.kernel.util.OrderByComparator"
082 });
083 public static final FinderPath FINDER_PATH_COUNT_BY_QUESTIONID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
084 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
085 "countByQuestionId", new String[] { Long.class.getName() });
086 public static final FinderPath FINDER_PATH_FIND_BY_CHOICEID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
087 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
088 "findByChoiceId",
089 new String[] {
090 Long.class.getName(),
091
092 "java.lang.Integer", "java.lang.Integer",
093 "com.liferay.portal.kernel.util.OrderByComparator"
094 });
095 public static final FinderPath FINDER_PATH_COUNT_BY_CHOICEID = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
096 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
097 "countByChoiceId", new String[] { Long.class.getName() });
098 public static final FinderPath FINDER_PATH_FETCH_BY_Q_U = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
099 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
100 "fetchByQ_U",
101 new String[] { Long.class.getName(), Long.class.getName() });
102 public static final FinderPath FINDER_PATH_COUNT_BY_Q_U = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
103 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104 "countByQ_U",
105 new String[] { Long.class.getName(), Long.class.getName() });
106 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
107 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
108 "findAll", new String[0]);
109 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
110 PollsVoteModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
111 "countAll", new String[0]);
112
113
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
134 public void cacheResult(List<PollsVote> pollsVotes) {
135 for (PollsVote pollsVote : pollsVotes) {
136 if (EntityCacheUtil.getResult(
137 PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
138 PollsVoteImpl.class, pollsVote.getPrimaryKey(), this) == null) {
139 cacheResult(pollsVote);
140 }
141 }
142 }
143
144
151 public void clearCache() {
152 CacheRegistryUtil.clear(PollsVoteImpl.class.getName());
153 EntityCacheUtil.clearCache(PollsVoteImpl.class.getName());
154 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
155 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
156 }
157
158
165 public void clearCache(PollsVote pollsVote) {
166 EntityCacheUtil.removeResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
167 PollsVoteImpl.class, pollsVote.getPrimaryKey());
168
169 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
170 new Object[] {
171 new Long(pollsVote.getQuestionId()),
172 new Long(pollsVote.getUserId())
173 });
174 }
175
176
182 public PollsVote create(long voteId) {
183 PollsVote pollsVote = new PollsVoteImpl();
184
185 pollsVote.setNew(true);
186 pollsVote.setPrimaryKey(voteId);
187
188 return pollsVote;
189 }
190
191
199 public PollsVote remove(Serializable primaryKey)
200 throws NoSuchModelException, SystemException {
201 return remove(((Long)primaryKey).longValue());
202 }
203
204
212 public PollsVote remove(long voteId)
213 throws NoSuchVoteException, SystemException {
214 Session session = null;
215
216 try {
217 session = openSession();
218
219 PollsVote pollsVote = (PollsVote)session.get(PollsVoteImpl.class,
220 new Long(voteId));
221
222 if (pollsVote == null) {
223 if (_log.isWarnEnabled()) {
224 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + voteId);
225 }
226
227 throw new NoSuchVoteException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
228 voteId);
229 }
230
231 return remove(pollsVote);
232 }
233 catch (NoSuchVoteException nsee) {
234 throw nsee;
235 }
236 catch (Exception e) {
237 throw processException(e);
238 }
239 finally {
240 closeSession(session);
241 }
242 }
243
244 protected PollsVote removeImpl(PollsVote pollsVote)
245 throws SystemException {
246 pollsVote = toUnwrappedModel(pollsVote);
247
248 Session session = null;
249
250 try {
251 session = openSession();
252
253 BatchSessionUtil.delete(session, pollsVote);
254 }
255 catch (Exception e) {
256 throw processException(e);
257 }
258 finally {
259 closeSession(session);
260 }
261
262 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
263
264 PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
265
266 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
267 new Object[] {
268 new Long(pollsVoteModelImpl.getQuestionId()),
269 new Long(pollsVoteModelImpl.getUserId())
270 });
271
272 EntityCacheUtil.removeResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
273 PollsVoteImpl.class, pollsVote.getPrimaryKey());
274
275 return pollsVote;
276 }
277
278 public PollsVote updateImpl(
279 com.liferay.portlet.polls.model.PollsVote pollsVote, boolean merge)
280 throws SystemException {
281 pollsVote = toUnwrappedModel(pollsVote);
282
283 boolean isNew = pollsVote.isNew();
284
285 PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
286
287 Session session = null;
288
289 try {
290 session = openSession();
291
292 BatchSessionUtil.update(session, pollsVote, merge);
293
294 pollsVote.setNew(false);
295 }
296 catch (Exception e) {
297 throw processException(e);
298 }
299 finally {
300 closeSession(session);
301 }
302
303 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
304
305 EntityCacheUtil.putResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
306 PollsVoteImpl.class, pollsVote.getPrimaryKey(), pollsVote);
307
308 if (!isNew &&
309 ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
310 (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
311 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
312 new Object[] {
313 new Long(pollsVoteModelImpl.getOriginalQuestionId()),
314 new Long(pollsVoteModelImpl.getOriginalUserId())
315 });
316 }
317
318 if (isNew ||
319 ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
320 (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
321 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
322 new Object[] {
323 new Long(pollsVote.getQuestionId()),
324 new Long(pollsVote.getUserId())
325 }, pollsVote);
326 }
327
328 return pollsVote;
329 }
330
331 protected PollsVote toUnwrappedModel(PollsVote pollsVote) {
332 if (pollsVote instanceof PollsVoteImpl) {
333 return pollsVote;
334 }
335
336 PollsVoteImpl pollsVoteImpl = new PollsVoteImpl();
337
338 pollsVoteImpl.setNew(pollsVote.isNew());
339 pollsVoteImpl.setPrimaryKey(pollsVote.getPrimaryKey());
340
341 pollsVoteImpl.setVoteId(pollsVote.getVoteId());
342 pollsVoteImpl.setUserId(pollsVote.getUserId());
343 pollsVoteImpl.setQuestionId(pollsVote.getQuestionId());
344 pollsVoteImpl.setChoiceId(pollsVote.getChoiceId());
345 pollsVoteImpl.setVoteDate(pollsVote.getVoteDate());
346
347 return pollsVoteImpl;
348 }
349
350
358 public PollsVote findByPrimaryKey(Serializable primaryKey)
359 throws NoSuchModelException, SystemException {
360 return findByPrimaryKey(((Long)primaryKey).longValue());
361 }
362
363
371 public PollsVote findByPrimaryKey(long voteId)
372 throws NoSuchVoteException, SystemException {
373 PollsVote pollsVote = fetchByPrimaryKey(voteId);
374
375 if (pollsVote == null) {
376 if (_log.isWarnEnabled()) {
377 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + voteId);
378 }
379
380 throw new NoSuchVoteException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
381 voteId);
382 }
383
384 return pollsVote;
385 }
386
387
394 public PollsVote fetchByPrimaryKey(Serializable primaryKey)
395 throws SystemException {
396 return fetchByPrimaryKey(((Long)primaryKey).longValue());
397 }
398
399
406 public PollsVote fetchByPrimaryKey(long voteId) throws SystemException {
407 PollsVote pollsVote = (PollsVote)EntityCacheUtil.getResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
408 PollsVoteImpl.class, voteId, this);
409
410 if (pollsVote == null) {
411 Session session = null;
412
413 try {
414 session = openSession();
415
416 pollsVote = (PollsVote)session.get(PollsVoteImpl.class,
417 new Long(voteId));
418 }
419 catch (Exception e) {
420 throw processException(e);
421 }
422 finally {
423 if (pollsVote != null) {
424 cacheResult(pollsVote);
425 }
426
427 closeSession(session);
428 }
429 }
430
431 return pollsVote;
432 }
433
434
441 public List<PollsVote> findByQuestionId(long questionId)
442 throws SystemException {
443 return findByQuestionId(questionId, QueryUtil.ALL_POS,
444 QueryUtil.ALL_POS, null);
445 }
446
447
460 public List<PollsVote> findByQuestionId(long questionId, int start, int end)
461 throws SystemException {
462 return findByQuestionId(questionId, start, end, null);
463 }
464
465
479 public List<PollsVote> findByQuestionId(long questionId, int start,
480 int end, OrderByComparator orderByComparator) throws SystemException {
481 Object[] finderArgs = new Object[] {
482 questionId,
483
484 String.valueOf(start), String.valueOf(end),
485 String.valueOf(orderByComparator)
486 };
487
488 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
489 finderArgs, this);
490
491 if (list == null) {
492 StringBundler query = null;
493
494 if (orderByComparator != null) {
495 query = new StringBundler(3 +
496 (orderByComparator.getOrderByFields().length * 3));
497 }
498 else {
499 query = new StringBundler(2);
500 }
501
502 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
503
504 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
505
506 if (orderByComparator != null) {
507 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
508 orderByComparator);
509 }
510
511 String sql = query.toString();
512
513 Session session = null;
514
515 try {
516 session = openSession();
517
518 Query q = session.createQuery(sql);
519
520 QueryPos qPos = QueryPos.getInstance(q);
521
522 qPos.add(questionId);
523
524 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
525 end);
526 }
527 catch (Exception e) {
528 throw processException(e);
529 }
530 finally {
531 if (list == null) {
532 FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_QUESTIONID,
533 finderArgs);
534 }
535 else {
536 cacheResult(list);
537
538 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
539 finderArgs, list);
540 }
541
542 closeSession(session);
543 }
544 }
545
546 return list;
547 }
548
549
562 public PollsVote findByQuestionId_First(long questionId,
563 OrderByComparator orderByComparator)
564 throws NoSuchVoteException, SystemException {
565 List<PollsVote> list = findByQuestionId(questionId, 0, 1,
566 orderByComparator);
567
568 if (list.isEmpty()) {
569 StringBundler msg = new StringBundler(4);
570
571 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
572
573 msg.append("questionId=");
574 msg.append(questionId);
575
576 msg.append(StringPool.CLOSE_CURLY_BRACE);
577
578 throw new NoSuchVoteException(msg.toString());
579 }
580 else {
581 return list.get(0);
582 }
583 }
584
585
598 public PollsVote findByQuestionId_Last(long questionId,
599 OrderByComparator orderByComparator)
600 throws NoSuchVoteException, SystemException {
601 int count = countByQuestionId(questionId);
602
603 List<PollsVote> list = findByQuestionId(questionId, count - 1, count,
604 orderByComparator);
605
606 if (list.isEmpty()) {
607 StringBundler msg = new StringBundler(4);
608
609 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
610
611 msg.append("questionId=");
612 msg.append(questionId);
613
614 msg.append(StringPool.CLOSE_CURLY_BRACE);
615
616 throw new NoSuchVoteException(msg.toString());
617 }
618 else {
619 return list.get(0);
620 }
621 }
622
623
637 public PollsVote[] findByQuestionId_PrevAndNext(long voteId,
638 long questionId, OrderByComparator orderByComparator)
639 throws NoSuchVoteException, SystemException {
640 PollsVote pollsVote = findByPrimaryKey(voteId);
641
642 Session session = null;
643
644 try {
645 session = openSession();
646
647 PollsVote[] array = new PollsVoteImpl[3];
648
649 array[0] = getByQuestionId_PrevAndNext(session, pollsVote,
650 questionId, orderByComparator, true);
651
652 array[1] = pollsVote;
653
654 array[2] = getByQuestionId_PrevAndNext(session, pollsVote,
655 questionId, orderByComparator, false);
656
657 return array;
658 }
659 catch (Exception e) {
660 throw processException(e);
661 }
662 finally {
663 closeSession(session);
664 }
665 }
666
667 protected PollsVote getByQuestionId_PrevAndNext(Session session,
668 PollsVote pollsVote, long questionId,
669 OrderByComparator orderByComparator, boolean previous) {
670 StringBundler query = null;
671
672 if (orderByComparator != null) {
673 query = new StringBundler(6 +
674 (orderByComparator.getOrderByFields().length * 6));
675 }
676 else {
677 query = new StringBundler(3);
678 }
679
680 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
681
682 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
683
684 if (orderByComparator != null) {
685 String[] orderByFields = orderByComparator.getOrderByFields();
686
687 if (orderByFields.length > 0) {
688 query.append(WHERE_AND);
689 }
690
691 for (int i = 0; i < orderByFields.length; i++) {
692 query.append(_ORDER_BY_ENTITY_ALIAS);
693 query.append(orderByFields[i]);
694
695 if ((i + 1) < orderByFields.length) {
696 if (orderByComparator.isAscending() ^ previous) {
697 query.append(WHERE_GREATER_THAN_HAS_NEXT);
698 }
699 else {
700 query.append(WHERE_LESSER_THAN_HAS_NEXT);
701 }
702 }
703 else {
704 if (orderByComparator.isAscending() ^ previous) {
705 query.append(WHERE_GREATER_THAN);
706 }
707 else {
708 query.append(WHERE_LESSER_THAN);
709 }
710 }
711 }
712
713 query.append(ORDER_BY_CLAUSE);
714
715 for (int i = 0; i < orderByFields.length; i++) {
716 query.append(_ORDER_BY_ENTITY_ALIAS);
717 query.append(orderByFields[i]);
718
719 if ((i + 1) < orderByFields.length) {
720 if (orderByComparator.isAscending() ^ previous) {
721 query.append(ORDER_BY_ASC_HAS_NEXT);
722 }
723 else {
724 query.append(ORDER_BY_DESC_HAS_NEXT);
725 }
726 }
727 else {
728 if (orderByComparator.isAscending() ^ previous) {
729 query.append(ORDER_BY_ASC);
730 }
731 else {
732 query.append(ORDER_BY_DESC);
733 }
734 }
735 }
736 }
737
738 String sql = query.toString();
739
740 Query q = session.createQuery(sql);
741
742 q.setFirstResult(0);
743 q.setMaxResults(2);
744
745 QueryPos qPos = QueryPos.getInstance(q);
746
747 qPos.add(questionId);
748
749 if (orderByComparator != null) {
750 Object[] values = orderByComparator.getOrderByValues(pollsVote);
751
752 for (Object value : values) {
753 qPos.add(value);
754 }
755 }
756
757 List<PollsVote> list = q.list();
758
759 if (list.size() == 2) {
760 return list.get(1);
761 }
762 else {
763 return null;
764 }
765 }
766
767
774 public List<PollsVote> findByChoiceId(long choiceId)
775 throws SystemException {
776 return findByChoiceId(choiceId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
777 null);
778 }
779
780
793 public List<PollsVote> findByChoiceId(long choiceId, int start, int end)
794 throws SystemException {
795 return findByChoiceId(choiceId, start, end, null);
796 }
797
798
812 public List<PollsVote> findByChoiceId(long choiceId, int start, int end,
813 OrderByComparator orderByComparator) throws SystemException {
814 Object[] finderArgs = new Object[] {
815 choiceId,
816
817 String.valueOf(start), String.valueOf(end),
818 String.valueOf(orderByComparator)
819 };
820
821 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CHOICEID,
822 finderArgs, this);
823
824 if (list == null) {
825 StringBundler query = null;
826
827 if (orderByComparator != null) {
828 query = new StringBundler(3 +
829 (orderByComparator.getOrderByFields().length * 3));
830 }
831 else {
832 query = new StringBundler(2);
833 }
834
835 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
836
837 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
838
839 if (orderByComparator != null) {
840 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
841 orderByComparator);
842 }
843
844 String sql = query.toString();
845
846 Session session = null;
847
848 try {
849 session = openSession();
850
851 Query q = session.createQuery(sql);
852
853 QueryPos qPos = QueryPos.getInstance(q);
854
855 qPos.add(choiceId);
856
857 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
858 end);
859 }
860 catch (Exception e) {
861 throw processException(e);
862 }
863 finally {
864 if (list == null) {
865 FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_CHOICEID,
866 finderArgs);
867 }
868 else {
869 cacheResult(list);
870
871 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CHOICEID,
872 finderArgs, list);
873 }
874
875 closeSession(session);
876 }
877 }
878
879 return list;
880 }
881
882
895 public PollsVote findByChoiceId_First(long choiceId,
896 OrderByComparator orderByComparator)
897 throws NoSuchVoteException, SystemException {
898 List<PollsVote> list = findByChoiceId(choiceId, 0, 1, orderByComparator);
899
900 if (list.isEmpty()) {
901 StringBundler msg = new StringBundler(4);
902
903 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
904
905 msg.append("choiceId=");
906 msg.append(choiceId);
907
908 msg.append(StringPool.CLOSE_CURLY_BRACE);
909
910 throw new NoSuchVoteException(msg.toString());
911 }
912 else {
913 return list.get(0);
914 }
915 }
916
917
930 public PollsVote findByChoiceId_Last(long choiceId,
931 OrderByComparator orderByComparator)
932 throws NoSuchVoteException, SystemException {
933 int count = countByChoiceId(choiceId);
934
935 List<PollsVote> list = findByChoiceId(choiceId, count - 1, count,
936 orderByComparator);
937
938 if (list.isEmpty()) {
939 StringBundler msg = new StringBundler(4);
940
941 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
942
943 msg.append("choiceId=");
944 msg.append(choiceId);
945
946 msg.append(StringPool.CLOSE_CURLY_BRACE);
947
948 throw new NoSuchVoteException(msg.toString());
949 }
950 else {
951 return list.get(0);
952 }
953 }
954
955
969 public PollsVote[] findByChoiceId_PrevAndNext(long voteId, long choiceId,
970 OrderByComparator orderByComparator)
971 throws NoSuchVoteException, SystemException {
972 PollsVote pollsVote = findByPrimaryKey(voteId);
973
974 Session session = null;
975
976 try {
977 session = openSession();
978
979 PollsVote[] array = new PollsVoteImpl[3];
980
981 array[0] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
982 orderByComparator, true);
983
984 array[1] = pollsVote;
985
986 array[2] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
987 orderByComparator, false);
988
989 return array;
990 }
991 catch (Exception e) {
992 throw processException(e);
993 }
994 finally {
995 closeSession(session);
996 }
997 }
998
999 protected PollsVote getByChoiceId_PrevAndNext(Session session,
1000 PollsVote pollsVote, long choiceId,
1001 OrderByComparator orderByComparator, boolean previous) {
1002 StringBundler query = null;
1003
1004 if (orderByComparator != null) {
1005 query = new StringBundler(6 +
1006 (orderByComparator.getOrderByFields().length * 6));
1007 }
1008 else {
1009 query = new StringBundler(3);
1010 }
1011
1012 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1013
1014 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1015
1016 if (orderByComparator != null) {
1017 String[] orderByFields = orderByComparator.getOrderByFields();
1018
1019 if (orderByFields.length > 0) {
1020 query.append(WHERE_AND);
1021 }
1022
1023 for (int i = 0; i < orderByFields.length; i++) {
1024 query.append(_ORDER_BY_ENTITY_ALIAS);
1025 query.append(orderByFields[i]);
1026
1027 if ((i + 1) < orderByFields.length) {
1028 if (orderByComparator.isAscending() ^ previous) {
1029 query.append(WHERE_GREATER_THAN_HAS_NEXT);
1030 }
1031 else {
1032 query.append(WHERE_LESSER_THAN_HAS_NEXT);
1033 }
1034 }
1035 else {
1036 if (orderByComparator.isAscending() ^ previous) {
1037 query.append(WHERE_GREATER_THAN);
1038 }
1039 else {
1040 query.append(WHERE_LESSER_THAN);
1041 }
1042 }
1043 }
1044
1045 query.append(ORDER_BY_CLAUSE);
1046
1047 for (int i = 0; i < orderByFields.length; i++) {
1048 query.append(_ORDER_BY_ENTITY_ALIAS);
1049 query.append(orderByFields[i]);
1050
1051 if ((i + 1) < orderByFields.length) {
1052 if (orderByComparator.isAscending() ^ previous) {
1053 query.append(ORDER_BY_ASC_HAS_NEXT);
1054 }
1055 else {
1056 query.append(ORDER_BY_DESC_HAS_NEXT);
1057 }
1058 }
1059 else {
1060 if (orderByComparator.isAscending() ^ previous) {
1061 query.append(ORDER_BY_ASC);
1062 }
1063 else {
1064 query.append(ORDER_BY_DESC);
1065 }
1066 }
1067 }
1068 }
1069
1070 String sql = query.toString();
1071
1072 Query q = session.createQuery(sql);
1073
1074 q.setFirstResult(0);
1075 q.setMaxResults(2);
1076
1077 QueryPos qPos = QueryPos.getInstance(q);
1078
1079 qPos.add(choiceId);
1080
1081 if (orderByComparator != null) {
1082 Object[] values = orderByComparator.getOrderByValues(pollsVote);
1083
1084 for (Object value : values) {
1085 qPos.add(value);
1086 }
1087 }
1088
1089 List<PollsVote> list = q.list();
1090
1091 if (list.size() == 2) {
1092 return list.get(1);
1093 }
1094 else {
1095 return null;
1096 }
1097 }
1098
1099
1108 public PollsVote findByQ_U(long questionId, long userId)
1109 throws NoSuchVoteException, SystemException {
1110 PollsVote pollsVote = fetchByQ_U(questionId, userId);
1111
1112 if (pollsVote == null) {
1113 StringBundler msg = new StringBundler(6);
1114
1115 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1116
1117 msg.append("questionId=");
1118 msg.append(questionId);
1119
1120 msg.append(", userId=");
1121 msg.append(userId);
1122
1123 msg.append(StringPool.CLOSE_CURLY_BRACE);
1124
1125 if (_log.isWarnEnabled()) {
1126 _log.warn(msg.toString());
1127 }
1128
1129 throw new NoSuchVoteException(msg.toString());
1130 }
1131
1132 return pollsVote;
1133 }
1134
1135
1143 public PollsVote fetchByQ_U(long questionId, long userId)
1144 throws SystemException {
1145 return fetchByQ_U(questionId, userId, true);
1146 }
1147
1148
1156 public PollsVote fetchByQ_U(long questionId, long userId,
1157 boolean retrieveFromCache) throws SystemException {
1158 Object[] finderArgs = new Object[] { questionId, userId };
1159
1160 Object result = null;
1161
1162 if (retrieveFromCache) {
1163 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_U,
1164 finderArgs, this);
1165 }
1166
1167 if (result == null) {
1168 StringBundler query = new StringBundler(3);
1169
1170 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1171
1172 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1173
1174 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1175
1176 String sql = query.toString();
1177
1178 Session session = null;
1179
1180 try {
1181 session = openSession();
1182
1183 Query q = session.createQuery(sql);
1184
1185 QueryPos qPos = QueryPos.getInstance(q);
1186
1187 qPos.add(questionId);
1188
1189 qPos.add(userId);
1190
1191 List<PollsVote> list = q.list();
1192
1193 result = list;
1194
1195 PollsVote pollsVote = null;
1196
1197 if (list.isEmpty()) {
1198 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1199 finderArgs, list);
1200 }
1201 else {
1202 pollsVote = list.get(0);
1203
1204 cacheResult(pollsVote);
1205
1206 if ((pollsVote.getQuestionId() != questionId) ||
1207 (pollsVote.getUserId() != userId)) {
1208 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1209 finderArgs, pollsVote);
1210 }
1211 }
1212
1213 return pollsVote;
1214 }
1215 catch (Exception e) {
1216 throw processException(e);
1217 }
1218 finally {
1219 if (result == null) {
1220 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
1221 finderArgs);
1222 }
1223
1224 closeSession(session);
1225 }
1226 }
1227 else {
1228 if (result instanceof List<?>) {
1229 return null;
1230 }
1231 else {
1232 return (PollsVote)result;
1233 }
1234 }
1235 }
1236
1237
1243 public List<PollsVote> findAll() throws SystemException {
1244 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1245 }
1246
1247
1259 public List<PollsVote> findAll(int start, int end)
1260 throws SystemException {
1261 return findAll(start, end, null);
1262 }
1263
1264
1277 public List<PollsVote> findAll(int start, int end,
1278 OrderByComparator orderByComparator) throws SystemException {
1279 Object[] finderArgs = new Object[] {
1280 String.valueOf(start), String.valueOf(end),
1281 String.valueOf(orderByComparator)
1282 };
1283
1284 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1285 finderArgs, this);
1286
1287 if (list == null) {
1288 StringBundler query = null;
1289 String sql = null;
1290
1291 if (orderByComparator != null) {
1292 query = new StringBundler(2 +
1293 (orderByComparator.getOrderByFields().length * 3));
1294
1295 query.append(_SQL_SELECT_POLLSVOTE);
1296
1297 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1298 orderByComparator);
1299
1300 sql = query.toString();
1301 }
1302 else {
1303 sql = _SQL_SELECT_POLLSVOTE;
1304 }
1305
1306 Session session = null;
1307
1308 try {
1309 session = openSession();
1310
1311 Query q = session.createQuery(sql);
1312
1313 if (orderByComparator == null) {
1314 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1315 start, end, false);
1316
1317 Collections.sort(list);
1318 }
1319 else {
1320 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1321 start, end);
1322 }
1323 }
1324 catch (Exception e) {
1325 throw processException(e);
1326 }
1327 finally {
1328 if (list == null) {
1329 FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
1330 finderArgs);
1331 }
1332 else {
1333 cacheResult(list);
1334
1335 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
1336 list);
1337 }
1338
1339 closeSession(session);
1340 }
1341 }
1342
1343 return list;
1344 }
1345
1346
1352 public void removeByQuestionId(long questionId) throws SystemException {
1353 for (PollsVote pollsVote : findByQuestionId(questionId)) {
1354 remove(pollsVote);
1355 }
1356 }
1357
1358
1364 public void removeByChoiceId(long choiceId) throws SystemException {
1365 for (PollsVote pollsVote : findByChoiceId(choiceId)) {
1366 remove(pollsVote);
1367 }
1368 }
1369
1370
1377 public void removeByQ_U(long questionId, long userId)
1378 throws NoSuchVoteException, SystemException {
1379 PollsVote pollsVote = findByQ_U(questionId, userId);
1380
1381 remove(pollsVote);
1382 }
1383
1384
1389 public void removeAll() throws SystemException {
1390 for (PollsVote pollsVote : findAll()) {
1391 remove(pollsVote);
1392 }
1393 }
1394
1395
1402 public int countByQuestionId(long questionId) throws SystemException {
1403 Object[] finderArgs = new Object[] { questionId };
1404
1405 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1406 finderArgs, this);
1407
1408 if (count == null) {
1409 StringBundler query = new StringBundler(2);
1410
1411 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1412
1413 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1414
1415 String sql = query.toString();
1416
1417 Session session = null;
1418
1419 try {
1420 session = openSession();
1421
1422 Query q = session.createQuery(sql);
1423
1424 QueryPos qPos = QueryPos.getInstance(q);
1425
1426 qPos.add(questionId);
1427
1428 count = (Long)q.uniqueResult();
1429 }
1430 catch (Exception e) {
1431 throw processException(e);
1432 }
1433 finally {
1434 if (count == null) {
1435 count = Long.valueOf(0);
1436 }
1437
1438 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1439 finderArgs, count);
1440
1441 closeSession(session);
1442 }
1443 }
1444
1445 return count.intValue();
1446 }
1447
1448
1455 public int countByChoiceId(long choiceId) throws SystemException {
1456 Object[] finderArgs = new Object[] { choiceId };
1457
1458 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CHOICEID,
1459 finderArgs, this);
1460
1461 if (count == null) {
1462 StringBundler query = new StringBundler(2);
1463
1464 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1465
1466 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1467
1468 String sql = query.toString();
1469
1470 Session session = null;
1471
1472 try {
1473 session = openSession();
1474
1475 Query q = session.createQuery(sql);
1476
1477 QueryPos qPos = QueryPos.getInstance(q);
1478
1479 qPos.add(choiceId);
1480
1481 count = (Long)q.uniqueResult();
1482 }
1483 catch (Exception e) {
1484 throw processException(e);
1485 }
1486 finally {
1487 if (count == null) {
1488 count = Long.valueOf(0);
1489 }
1490
1491 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CHOICEID,
1492 finderArgs, count);
1493
1494 closeSession(session);
1495 }
1496 }
1497
1498 return count.intValue();
1499 }
1500
1501
1509 public int countByQ_U(long questionId, long userId)
1510 throws SystemException {
1511 Object[] finderArgs = new Object[] { questionId, userId };
1512
1513 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_U,
1514 finderArgs, this);
1515
1516 if (count == null) {
1517 StringBundler query = new StringBundler(3);
1518
1519 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1520
1521 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1522
1523 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1524
1525 String sql = query.toString();
1526
1527 Session session = null;
1528
1529 try {
1530 session = openSession();
1531
1532 Query q = session.createQuery(sql);
1533
1534 QueryPos qPos = QueryPos.getInstance(q);
1535
1536 qPos.add(questionId);
1537
1538 qPos.add(userId);
1539
1540 count = (Long)q.uniqueResult();
1541 }
1542 catch (Exception e) {
1543 throw processException(e);
1544 }
1545 finally {
1546 if (count == null) {
1547 count = Long.valueOf(0);
1548 }
1549
1550 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_U, finderArgs,
1551 count);
1552
1553 closeSession(session);
1554 }
1555 }
1556
1557 return count.intValue();
1558 }
1559
1560
1566 public int countAll() throws SystemException {
1567 Object[] finderArgs = new Object[0];
1568
1569 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1570 finderArgs, this);
1571
1572 if (count == null) {
1573 Session session = null;
1574
1575 try {
1576 session = openSession();
1577
1578 Query q = session.createQuery(_SQL_COUNT_POLLSVOTE);
1579
1580 count = (Long)q.uniqueResult();
1581 }
1582 catch (Exception e) {
1583 throw processException(e);
1584 }
1585 finally {
1586 if (count == null) {
1587 count = Long.valueOf(0);
1588 }
1589
1590 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1591 count);
1592
1593 closeSession(session);
1594 }
1595 }
1596
1597 return count.intValue();
1598 }
1599
1600
1603 public void afterPropertiesSet() {
1604 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1605 com.liferay.portal.util.PropsUtil.get(
1606 "value.object.listener.com.liferay.portlet.polls.model.PollsVote")));
1607
1608 if (listenerClassNames.length > 0) {
1609 try {
1610 List<ModelListener<PollsVote>> listenersList = new ArrayList<ModelListener<PollsVote>>();
1611
1612 for (String listenerClassName : listenerClassNames) {
1613 listenersList.add((ModelListener<PollsVote>)InstanceFactory.newInstance(
1614 listenerClassName));
1615 }
1616
1617 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1618 }
1619 catch (Exception e) {
1620 _log.error(e);
1621 }
1622 }
1623 }
1624
1625 public void destroy() {
1626 EntityCacheUtil.removeCache(PollsVoteImpl.class.getName());
1627 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1628 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1629 }
1630
1631 @BeanReference(type = PollsChoicePersistence.class)
1632 protected PollsChoicePersistence pollsChoicePersistence;
1633 @BeanReference(type = PollsQuestionPersistence.class)
1634 protected PollsQuestionPersistence pollsQuestionPersistence;
1635 @BeanReference(type = PollsVotePersistence.class)
1636 protected PollsVotePersistence pollsVotePersistence;
1637 @BeanReference(type = ResourcePersistence.class)
1638 protected ResourcePersistence resourcePersistence;
1639 @BeanReference(type = UserPersistence.class)
1640 protected UserPersistence userPersistence;
1641 private static final String _SQL_SELECT_POLLSVOTE = "SELECT pollsVote FROM PollsVote pollsVote";
1642 private static final String _SQL_SELECT_POLLSVOTE_WHERE = "SELECT pollsVote FROM PollsVote pollsVote WHERE ";
1643 private static final String _SQL_COUNT_POLLSVOTE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote";
1644 private static final String _SQL_COUNT_POLLSVOTE_WHERE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote WHERE ";
1645 private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsVote.questionId = ?";
1646 private static final String _FINDER_COLUMN_CHOICEID_CHOICEID_2 = "pollsVote.choiceId = ?";
1647 private static final String _FINDER_COLUMN_Q_U_QUESTIONID_2 = "pollsVote.questionId = ? AND ";
1648 private static final String _FINDER_COLUMN_Q_U_USERID_2 = "pollsVote.userId = ?";
1649 private static final String _ORDER_BY_ENTITY_ALIAS = "pollsVote.";
1650 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsVote exists with the primary key ";
1651 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsVote exists with the key {";
1652 private static Log _log = LogFactoryUtil.getLog(PollsVotePersistenceImpl.class);
1653 }