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.getOriginalQuestionId()),
269 new Long(pollsVoteModelImpl.getOriginalUserId())
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 Session session = null;
493
494 try {
495 session = openSession();
496
497 StringBundler query = null;
498
499 if (orderByComparator != null) {
500 query = new StringBundler(3 +
501 (orderByComparator.getOrderByFields().length * 3));
502 }
503 else {
504 query = new StringBundler(2);
505 }
506
507 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
508
509 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
510
511 if (orderByComparator != null) {
512 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
513 orderByComparator);
514 }
515
516 String sql = query.toString();
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 list = new ArrayList<PollsVote>();
533 }
534
535 cacheResult(list);
536
537 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
538 finderArgs, list);
539
540 closeSession(session);
541 }
542 }
543
544 return list;
545 }
546
547
560 public PollsVote findByQuestionId_First(long questionId,
561 OrderByComparator orderByComparator)
562 throws NoSuchVoteException, SystemException {
563 List<PollsVote> list = findByQuestionId(questionId, 0, 1,
564 orderByComparator);
565
566 if (list.isEmpty()) {
567 StringBundler msg = new StringBundler(4);
568
569 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
570
571 msg.append("questionId=");
572 msg.append(questionId);
573
574 msg.append(StringPool.CLOSE_CURLY_BRACE);
575
576 throw new NoSuchVoteException(msg.toString());
577 }
578 else {
579 return list.get(0);
580 }
581 }
582
583
596 public PollsVote findByQuestionId_Last(long questionId,
597 OrderByComparator orderByComparator)
598 throws NoSuchVoteException, SystemException {
599 int count = countByQuestionId(questionId);
600
601 List<PollsVote> list = findByQuestionId(questionId, count - 1, count,
602 orderByComparator);
603
604 if (list.isEmpty()) {
605 StringBundler msg = new StringBundler(4);
606
607 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
608
609 msg.append("questionId=");
610 msg.append(questionId);
611
612 msg.append(StringPool.CLOSE_CURLY_BRACE);
613
614 throw new NoSuchVoteException(msg.toString());
615 }
616 else {
617 return list.get(0);
618 }
619 }
620
621
635 public PollsVote[] findByQuestionId_PrevAndNext(long voteId,
636 long questionId, OrderByComparator orderByComparator)
637 throws NoSuchVoteException, SystemException {
638 PollsVote pollsVote = findByPrimaryKey(voteId);
639
640 Session session = null;
641
642 try {
643 session = openSession();
644
645 PollsVote[] array = new PollsVoteImpl[3];
646
647 array[0] = getByQuestionId_PrevAndNext(session, pollsVote,
648 questionId, orderByComparator, true);
649
650 array[1] = pollsVote;
651
652 array[2] = getByQuestionId_PrevAndNext(session, pollsVote,
653 questionId, orderByComparator, false);
654
655 return array;
656 }
657 catch (Exception e) {
658 throw processException(e);
659 }
660 finally {
661 closeSession(session);
662 }
663 }
664
665 protected PollsVote getByQuestionId_PrevAndNext(Session session,
666 PollsVote pollsVote, long questionId,
667 OrderByComparator orderByComparator, boolean previous) {
668 StringBundler query = null;
669
670 if (orderByComparator != null) {
671 query = new StringBundler(6 +
672 (orderByComparator.getOrderByFields().length * 6));
673 }
674 else {
675 query = new StringBundler(3);
676 }
677
678 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
679
680 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
681
682 if (orderByComparator != null) {
683 String[] orderByFields = orderByComparator.getOrderByFields();
684
685 if (orderByFields.length > 0) {
686 query.append(WHERE_AND);
687 }
688
689 for (int i = 0; i < orderByFields.length; i++) {
690 query.append(_ORDER_BY_ENTITY_ALIAS);
691 query.append(orderByFields[i]);
692
693 if ((i + 1) < orderByFields.length) {
694 if (orderByComparator.isAscending() ^ previous) {
695 query.append(WHERE_GREATER_THAN_HAS_NEXT);
696 }
697 else {
698 query.append(WHERE_LESSER_THAN_HAS_NEXT);
699 }
700 }
701 else {
702 if (orderByComparator.isAscending() ^ previous) {
703 query.append(WHERE_GREATER_THAN);
704 }
705 else {
706 query.append(WHERE_LESSER_THAN);
707 }
708 }
709 }
710
711 query.append(ORDER_BY_CLAUSE);
712
713 for (int i = 0; i < orderByFields.length; i++) {
714 query.append(_ORDER_BY_ENTITY_ALIAS);
715 query.append(orderByFields[i]);
716
717 if ((i + 1) < orderByFields.length) {
718 if (orderByComparator.isAscending() ^ previous) {
719 query.append(ORDER_BY_ASC_HAS_NEXT);
720 }
721 else {
722 query.append(ORDER_BY_DESC_HAS_NEXT);
723 }
724 }
725 else {
726 if (orderByComparator.isAscending() ^ previous) {
727 query.append(ORDER_BY_ASC);
728 }
729 else {
730 query.append(ORDER_BY_DESC);
731 }
732 }
733 }
734 }
735
736 String sql = query.toString();
737
738 Query q = session.createQuery(sql);
739
740 q.setFirstResult(0);
741 q.setMaxResults(2);
742
743 QueryPos qPos = QueryPos.getInstance(q);
744
745 qPos.add(questionId);
746
747 if (orderByComparator != null) {
748 Object[] values = orderByComparator.getOrderByValues(pollsVote);
749
750 for (Object value : values) {
751 qPos.add(value);
752 }
753 }
754
755 List<PollsVote> list = q.list();
756
757 if (list.size() == 2) {
758 return list.get(1);
759 }
760 else {
761 return null;
762 }
763 }
764
765
772 public List<PollsVote> findByChoiceId(long choiceId)
773 throws SystemException {
774 return findByChoiceId(choiceId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
775 null);
776 }
777
778
791 public List<PollsVote> findByChoiceId(long choiceId, int start, int end)
792 throws SystemException {
793 return findByChoiceId(choiceId, start, end, null);
794 }
795
796
810 public List<PollsVote> findByChoiceId(long choiceId, int start, int end,
811 OrderByComparator orderByComparator) throws SystemException {
812 Object[] finderArgs = new Object[] {
813 choiceId,
814
815 String.valueOf(start), String.valueOf(end),
816 String.valueOf(orderByComparator)
817 };
818
819 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CHOICEID,
820 finderArgs, this);
821
822 if (list == null) {
823 Session session = null;
824
825 try {
826 session = openSession();
827
828 StringBundler query = null;
829
830 if (orderByComparator != null) {
831 query = new StringBundler(3 +
832 (orderByComparator.getOrderByFields().length * 3));
833 }
834 else {
835 query = new StringBundler(2);
836 }
837
838 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
839
840 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
841
842 if (orderByComparator != null) {
843 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
844 orderByComparator);
845 }
846
847 String sql = query.toString();
848
849 Query q = session.createQuery(sql);
850
851 QueryPos qPos = QueryPos.getInstance(q);
852
853 qPos.add(choiceId);
854
855 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
856 end);
857 }
858 catch (Exception e) {
859 throw processException(e);
860 }
861 finally {
862 if (list == null) {
863 list = new ArrayList<PollsVote>();
864 }
865
866 cacheResult(list);
867
868 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CHOICEID,
869 finderArgs, list);
870
871 closeSession(session);
872 }
873 }
874
875 return list;
876 }
877
878
891 public PollsVote findByChoiceId_First(long choiceId,
892 OrderByComparator orderByComparator)
893 throws NoSuchVoteException, SystemException {
894 List<PollsVote> list = findByChoiceId(choiceId, 0, 1, orderByComparator);
895
896 if (list.isEmpty()) {
897 StringBundler msg = new StringBundler(4);
898
899 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
900
901 msg.append("choiceId=");
902 msg.append(choiceId);
903
904 msg.append(StringPool.CLOSE_CURLY_BRACE);
905
906 throw new NoSuchVoteException(msg.toString());
907 }
908 else {
909 return list.get(0);
910 }
911 }
912
913
926 public PollsVote findByChoiceId_Last(long choiceId,
927 OrderByComparator orderByComparator)
928 throws NoSuchVoteException, SystemException {
929 int count = countByChoiceId(choiceId);
930
931 List<PollsVote> list = findByChoiceId(choiceId, count - 1, count,
932 orderByComparator);
933
934 if (list.isEmpty()) {
935 StringBundler msg = new StringBundler(4);
936
937 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
938
939 msg.append("choiceId=");
940 msg.append(choiceId);
941
942 msg.append(StringPool.CLOSE_CURLY_BRACE);
943
944 throw new NoSuchVoteException(msg.toString());
945 }
946 else {
947 return list.get(0);
948 }
949 }
950
951
965 public PollsVote[] findByChoiceId_PrevAndNext(long voteId, long choiceId,
966 OrderByComparator orderByComparator)
967 throws NoSuchVoteException, SystemException {
968 PollsVote pollsVote = findByPrimaryKey(voteId);
969
970 Session session = null;
971
972 try {
973 session = openSession();
974
975 PollsVote[] array = new PollsVoteImpl[3];
976
977 array[0] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
978 orderByComparator, true);
979
980 array[1] = pollsVote;
981
982 array[2] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
983 orderByComparator, false);
984
985 return array;
986 }
987 catch (Exception e) {
988 throw processException(e);
989 }
990 finally {
991 closeSession(session);
992 }
993 }
994
995 protected PollsVote getByChoiceId_PrevAndNext(Session session,
996 PollsVote pollsVote, long choiceId,
997 OrderByComparator orderByComparator, boolean previous) {
998 StringBundler query = null;
999
1000 if (orderByComparator != null) {
1001 query = new StringBundler(6 +
1002 (orderByComparator.getOrderByFields().length * 6));
1003 }
1004 else {
1005 query = new StringBundler(3);
1006 }
1007
1008 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1009
1010 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1011
1012 if (orderByComparator != null) {
1013 String[] orderByFields = orderByComparator.getOrderByFields();
1014
1015 if (orderByFields.length > 0) {
1016 query.append(WHERE_AND);
1017 }
1018
1019 for (int i = 0; i < orderByFields.length; i++) {
1020 query.append(_ORDER_BY_ENTITY_ALIAS);
1021 query.append(orderByFields[i]);
1022
1023 if ((i + 1) < orderByFields.length) {
1024 if (orderByComparator.isAscending() ^ previous) {
1025 query.append(WHERE_GREATER_THAN_HAS_NEXT);
1026 }
1027 else {
1028 query.append(WHERE_LESSER_THAN_HAS_NEXT);
1029 }
1030 }
1031 else {
1032 if (orderByComparator.isAscending() ^ previous) {
1033 query.append(WHERE_GREATER_THAN);
1034 }
1035 else {
1036 query.append(WHERE_LESSER_THAN);
1037 }
1038 }
1039 }
1040
1041 query.append(ORDER_BY_CLAUSE);
1042
1043 for (int i = 0; i < orderByFields.length; i++) {
1044 query.append(_ORDER_BY_ENTITY_ALIAS);
1045 query.append(orderByFields[i]);
1046
1047 if ((i + 1) < orderByFields.length) {
1048 if (orderByComparator.isAscending() ^ previous) {
1049 query.append(ORDER_BY_ASC_HAS_NEXT);
1050 }
1051 else {
1052 query.append(ORDER_BY_DESC_HAS_NEXT);
1053 }
1054 }
1055 else {
1056 if (orderByComparator.isAscending() ^ previous) {
1057 query.append(ORDER_BY_ASC);
1058 }
1059 else {
1060 query.append(ORDER_BY_DESC);
1061 }
1062 }
1063 }
1064 }
1065
1066 String sql = query.toString();
1067
1068 Query q = session.createQuery(sql);
1069
1070 q.setFirstResult(0);
1071 q.setMaxResults(2);
1072
1073 QueryPos qPos = QueryPos.getInstance(q);
1074
1075 qPos.add(choiceId);
1076
1077 if (orderByComparator != null) {
1078 Object[] values = orderByComparator.getOrderByValues(pollsVote);
1079
1080 for (Object value : values) {
1081 qPos.add(value);
1082 }
1083 }
1084
1085 List<PollsVote> list = q.list();
1086
1087 if (list.size() == 2) {
1088 return list.get(1);
1089 }
1090 else {
1091 return null;
1092 }
1093 }
1094
1095
1104 public PollsVote findByQ_U(long questionId, long userId)
1105 throws NoSuchVoteException, SystemException {
1106 PollsVote pollsVote = fetchByQ_U(questionId, userId);
1107
1108 if (pollsVote == null) {
1109 StringBundler msg = new StringBundler(6);
1110
1111 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1112
1113 msg.append("questionId=");
1114 msg.append(questionId);
1115
1116 msg.append(", userId=");
1117 msg.append(userId);
1118
1119 msg.append(StringPool.CLOSE_CURLY_BRACE);
1120
1121 if (_log.isWarnEnabled()) {
1122 _log.warn(msg.toString());
1123 }
1124
1125 throw new NoSuchVoteException(msg.toString());
1126 }
1127
1128 return pollsVote;
1129 }
1130
1131
1139 public PollsVote fetchByQ_U(long questionId, long userId)
1140 throws SystemException {
1141 return fetchByQ_U(questionId, userId, true);
1142 }
1143
1144
1152 public PollsVote fetchByQ_U(long questionId, long userId,
1153 boolean retrieveFromCache) throws SystemException {
1154 Object[] finderArgs = new Object[] { questionId, userId };
1155
1156 Object result = null;
1157
1158 if (retrieveFromCache) {
1159 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_U,
1160 finderArgs, this);
1161 }
1162
1163 if (result == null) {
1164 Session session = null;
1165
1166 try {
1167 session = openSession();
1168
1169 StringBundler query = new StringBundler(3);
1170
1171 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1172
1173 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1174
1175 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1176
1177 String sql = query.toString();
1178
1179 Query q = session.createQuery(sql);
1180
1181 QueryPos qPos = QueryPos.getInstance(q);
1182
1183 qPos.add(questionId);
1184
1185 qPos.add(userId);
1186
1187 List<PollsVote> list = q.list();
1188
1189 result = list;
1190
1191 PollsVote pollsVote = null;
1192
1193 if (list.isEmpty()) {
1194 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1195 finderArgs, list);
1196 }
1197 else {
1198 pollsVote = list.get(0);
1199
1200 cacheResult(pollsVote);
1201
1202 if ((pollsVote.getQuestionId() != questionId) ||
1203 (pollsVote.getUserId() != userId)) {
1204 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1205 finderArgs, pollsVote);
1206 }
1207 }
1208
1209 return pollsVote;
1210 }
1211 catch (Exception e) {
1212 throw processException(e);
1213 }
1214 finally {
1215 if (result == null) {
1216 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1217 finderArgs, new ArrayList<PollsVote>());
1218 }
1219
1220 closeSession(session);
1221 }
1222 }
1223 else {
1224 if (result instanceof List<?>) {
1225 return null;
1226 }
1227 else {
1228 return (PollsVote)result;
1229 }
1230 }
1231 }
1232
1233
1239 public List<PollsVote> findAll() throws SystemException {
1240 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1241 }
1242
1243
1255 public List<PollsVote> findAll(int start, int end)
1256 throws SystemException {
1257 return findAll(start, end, null);
1258 }
1259
1260
1273 public List<PollsVote> findAll(int start, int end,
1274 OrderByComparator orderByComparator) throws SystemException {
1275 Object[] finderArgs = new Object[] {
1276 String.valueOf(start), String.valueOf(end),
1277 String.valueOf(orderByComparator)
1278 };
1279
1280 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1281 finderArgs, this);
1282
1283 if (list == null) {
1284 Session session = null;
1285
1286 try {
1287 session = openSession();
1288
1289 StringBundler query = null;
1290 String sql = null;
1291
1292 if (orderByComparator != null) {
1293 query = new StringBundler(2 +
1294 (orderByComparator.getOrderByFields().length * 3));
1295
1296 query.append(_SQL_SELECT_POLLSVOTE);
1297
1298 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1299 orderByComparator);
1300
1301 sql = query.toString();
1302 }
1303 else {
1304 sql = _SQL_SELECT_POLLSVOTE;
1305 }
1306
1307 Query q = session.createQuery(sql);
1308
1309 if (orderByComparator == null) {
1310 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1311 start, end, false);
1312
1313 Collections.sort(list);
1314 }
1315 else {
1316 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1317 start, end);
1318 }
1319 }
1320 catch (Exception e) {
1321 throw processException(e);
1322 }
1323 finally {
1324 if (list == null) {
1325 list = new ArrayList<PollsVote>();
1326 }
1327
1328 cacheResult(list);
1329
1330 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1331
1332 closeSession(session);
1333 }
1334 }
1335
1336 return list;
1337 }
1338
1339
1345 public void removeByQuestionId(long questionId) throws SystemException {
1346 for (PollsVote pollsVote : findByQuestionId(questionId)) {
1347 remove(pollsVote);
1348 }
1349 }
1350
1351
1357 public void removeByChoiceId(long choiceId) throws SystemException {
1358 for (PollsVote pollsVote : findByChoiceId(choiceId)) {
1359 remove(pollsVote);
1360 }
1361 }
1362
1363
1370 public void removeByQ_U(long questionId, long userId)
1371 throws NoSuchVoteException, SystemException {
1372 PollsVote pollsVote = findByQ_U(questionId, userId);
1373
1374 remove(pollsVote);
1375 }
1376
1377
1382 public void removeAll() throws SystemException {
1383 for (PollsVote pollsVote : findAll()) {
1384 remove(pollsVote);
1385 }
1386 }
1387
1388
1395 public int countByQuestionId(long questionId) throws SystemException {
1396 Object[] finderArgs = new Object[] { questionId };
1397
1398 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1399 finderArgs, this);
1400
1401 if (count == null) {
1402 Session session = null;
1403
1404 try {
1405 session = openSession();
1406
1407 StringBundler query = new StringBundler(2);
1408
1409 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1410
1411 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1412
1413 String sql = query.toString();
1414
1415 Query q = session.createQuery(sql);
1416
1417 QueryPos qPos = QueryPos.getInstance(q);
1418
1419 qPos.add(questionId);
1420
1421 count = (Long)q.uniqueResult();
1422 }
1423 catch (Exception e) {
1424 throw processException(e);
1425 }
1426 finally {
1427 if (count == null) {
1428 count = Long.valueOf(0);
1429 }
1430
1431 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1432 finderArgs, count);
1433
1434 closeSession(session);
1435 }
1436 }
1437
1438 return count.intValue();
1439 }
1440
1441
1448 public int countByChoiceId(long choiceId) throws SystemException {
1449 Object[] finderArgs = new Object[] { choiceId };
1450
1451 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CHOICEID,
1452 finderArgs, this);
1453
1454 if (count == null) {
1455 Session session = null;
1456
1457 try {
1458 session = openSession();
1459
1460 StringBundler query = new StringBundler(2);
1461
1462 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1463
1464 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1465
1466 String sql = query.toString();
1467
1468 Query q = session.createQuery(sql);
1469
1470 QueryPos qPos = QueryPos.getInstance(q);
1471
1472 qPos.add(choiceId);
1473
1474 count = (Long)q.uniqueResult();
1475 }
1476 catch (Exception e) {
1477 throw processException(e);
1478 }
1479 finally {
1480 if (count == null) {
1481 count = Long.valueOf(0);
1482 }
1483
1484 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CHOICEID,
1485 finderArgs, count);
1486
1487 closeSession(session);
1488 }
1489 }
1490
1491 return count.intValue();
1492 }
1493
1494
1502 public int countByQ_U(long questionId, long userId)
1503 throws SystemException {
1504 Object[] finderArgs = new Object[] { questionId, userId };
1505
1506 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_U,
1507 finderArgs, this);
1508
1509 if (count == null) {
1510 Session session = null;
1511
1512 try {
1513 session = openSession();
1514
1515 StringBundler query = new StringBundler(3);
1516
1517 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1518
1519 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1520
1521 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1522
1523 String sql = query.toString();
1524
1525 Query q = session.createQuery(sql);
1526
1527 QueryPos qPos = QueryPos.getInstance(q);
1528
1529 qPos.add(questionId);
1530
1531 qPos.add(userId);
1532
1533 count = (Long)q.uniqueResult();
1534 }
1535 catch (Exception e) {
1536 throw processException(e);
1537 }
1538 finally {
1539 if (count == null) {
1540 count = Long.valueOf(0);
1541 }
1542
1543 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_U, finderArgs,
1544 count);
1545
1546 closeSession(session);
1547 }
1548 }
1549
1550 return count.intValue();
1551 }
1552
1553
1559 public int countAll() throws SystemException {
1560 Object[] finderArgs = new Object[0];
1561
1562 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1563 finderArgs, this);
1564
1565 if (count == null) {
1566 Session session = null;
1567
1568 try {
1569 session = openSession();
1570
1571 Query q = session.createQuery(_SQL_COUNT_POLLSVOTE);
1572
1573 count = (Long)q.uniqueResult();
1574 }
1575 catch (Exception e) {
1576 throw processException(e);
1577 }
1578 finally {
1579 if (count == null) {
1580 count = Long.valueOf(0);
1581 }
1582
1583 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1584 count);
1585
1586 closeSession(session);
1587 }
1588 }
1589
1590 return count.intValue();
1591 }
1592
1593
1596 public void afterPropertiesSet() {
1597 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1598 com.liferay.portal.util.PropsUtil.get(
1599 "value.object.listener.com.liferay.portlet.polls.model.PollsVote")));
1600
1601 if (listenerClassNames.length > 0) {
1602 try {
1603 List<ModelListener<PollsVote>> listenersList = new ArrayList<ModelListener<PollsVote>>();
1604
1605 for (String listenerClassName : listenerClassNames) {
1606 listenersList.add((ModelListener<PollsVote>)InstanceFactory.newInstance(
1607 listenerClassName));
1608 }
1609
1610 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1611 }
1612 catch (Exception e) {
1613 _log.error(e);
1614 }
1615 }
1616 }
1617
1618 public void destroy() {
1619 EntityCacheUtil.removeCache(PollsVoteImpl.class.getName());
1620 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1621 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1622 }
1623
1624 @BeanReference(type = PollsChoicePersistence.class)
1625 protected PollsChoicePersistence pollsChoicePersistence;
1626 @BeanReference(type = PollsQuestionPersistence.class)
1627 protected PollsQuestionPersistence pollsQuestionPersistence;
1628 @BeanReference(type = PollsVotePersistence.class)
1629 protected PollsVotePersistence pollsVotePersistence;
1630 @BeanReference(type = ResourcePersistence.class)
1631 protected ResourcePersistence resourcePersistence;
1632 @BeanReference(type = UserPersistence.class)
1633 protected UserPersistence userPersistence;
1634 private static final String _SQL_SELECT_POLLSVOTE = "SELECT pollsVote FROM PollsVote pollsVote";
1635 private static final String _SQL_SELECT_POLLSVOTE_WHERE = "SELECT pollsVote FROM PollsVote pollsVote WHERE ";
1636 private static final String _SQL_COUNT_POLLSVOTE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote";
1637 private static final String _SQL_COUNT_POLLSVOTE_WHERE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote WHERE ";
1638 private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsVote.questionId = ?";
1639 private static final String _FINDER_COLUMN_CHOICEID_CHOICEID_2 = "pollsVote.choiceId = ?";
1640 private static final String _FINDER_COLUMN_Q_U_QUESTIONID_2 = "pollsVote.questionId = ? AND ";
1641 private static final String _FINDER_COLUMN_Q_U_USERID_2 = "pollsVote.userId = ?";
1642 private static final String _ORDER_BY_ENTITY_ALIAS = "pollsVote.";
1643 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsVote exists with the primary key ";
1644 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsVote exists with the key {";
1645 private static Log _log = LogFactoryUtil.getLog(PollsVotePersistenceImpl.class);
1646 }