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 if (pollsVote.isCachedModel() || BatchSessionUtil.isEnabled()) {
254 Object staleObject = session.get(PollsVoteImpl.class,
255 pollsVote.getPrimaryKeyObj());
256
257 if (staleObject != null) {
258 session.evict(staleObject);
259 }
260 }
261
262 session.delete(pollsVote);
263
264 session.flush();
265 }
266 catch (Exception e) {
267 throw processException(e);
268 }
269 finally {
270 closeSession(session);
271 }
272
273 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
274
275 PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
276
277 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
278 new Object[] {
279 new Long(pollsVoteModelImpl.getOriginalQuestionId()),
280 new Long(pollsVoteModelImpl.getOriginalUserId())
281 });
282
283 EntityCacheUtil.removeResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
284 PollsVoteImpl.class, pollsVote.getPrimaryKey());
285
286 return pollsVote;
287 }
288
289 public PollsVote updateImpl(
290 com.liferay.portlet.polls.model.PollsVote pollsVote, boolean merge)
291 throws SystemException {
292 pollsVote = toUnwrappedModel(pollsVote);
293
294 boolean isNew = pollsVote.isNew();
295
296 PollsVoteModelImpl pollsVoteModelImpl = (PollsVoteModelImpl)pollsVote;
297
298 Session session = null;
299
300 try {
301 session = openSession();
302
303 BatchSessionUtil.update(session, pollsVote, merge);
304
305 pollsVote.setNew(false);
306 }
307 catch (Exception e) {
308 throw processException(e);
309 }
310 finally {
311 closeSession(session);
312 }
313
314 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
315
316 EntityCacheUtil.putResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
317 PollsVoteImpl.class, pollsVote.getPrimaryKey(), pollsVote);
318
319 if (!isNew &&
320 ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
321 (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
322 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_U,
323 new Object[] {
324 new Long(pollsVoteModelImpl.getOriginalQuestionId()),
325 new Long(pollsVoteModelImpl.getOriginalUserId())
326 });
327 }
328
329 if (isNew ||
330 ((pollsVote.getQuestionId() != pollsVoteModelImpl.getOriginalQuestionId()) ||
331 (pollsVote.getUserId() != pollsVoteModelImpl.getOriginalUserId()))) {
332 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
333 new Object[] {
334 new Long(pollsVote.getQuestionId()),
335 new Long(pollsVote.getUserId())
336 }, pollsVote);
337 }
338
339 return pollsVote;
340 }
341
342 protected PollsVote toUnwrappedModel(PollsVote pollsVote) {
343 if (pollsVote instanceof PollsVoteImpl) {
344 return pollsVote;
345 }
346
347 PollsVoteImpl pollsVoteImpl = new PollsVoteImpl();
348
349 pollsVoteImpl.setNew(pollsVote.isNew());
350 pollsVoteImpl.setPrimaryKey(pollsVote.getPrimaryKey());
351
352 pollsVoteImpl.setVoteId(pollsVote.getVoteId());
353 pollsVoteImpl.setUserId(pollsVote.getUserId());
354 pollsVoteImpl.setQuestionId(pollsVote.getQuestionId());
355 pollsVoteImpl.setChoiceId(pollsVote.getChoiceId());
356 pollsVoteImpl.setVoteDate(pollsVote.getVoteDate());
357
358 return pollsVoteImpl;
359 }
360
361
369 public PollsVote findByPrimaryKey(Serializable primaryKey)
370 throws NoSuchModelException, SystemException {
371 return findByPrimaryKey(((Long)primaryKey).longValue());
372 }
373
374
382 public PollsVote findByPrimaryKey(long voteId)
383 throws NoSuchVoteException, SystemException {
384 PollsVote pollsVote = fetchByPrimaryKey(voteId);
385
386 if (pollsVote == null) {
387 if (_log.isWarnEnabled()) {
388 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + voteId);
389 }
390
391 throw new NoSuchVoteException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
392 voteId);
393 }
394
395 return pollsVote;
396 }
397
398
405 public PollsVote fetchByPrimaryKey(Serializable primaryKey)
406 throws SystemException {
407 return fetchByPrimaryKey(((Long)primaryKey).longValue());
408 }
409
410
417 public PollsVote fetchByPrimaryKey(long voteId) throws SystemException {
418 PollsVote pollsVote = (PollsVote)EntityCacheUtil.getResult(PollsVoteModelImpl.ENTITY_CACHE_ENABLED,
419 PollsVoteImpl.class, voteId, this);
420
421 if (pollsVote == null) {
422 Session session = null;
423
424 try {
425 session = openSession();
426
427 pollsVote = (PollsVote)session.get(PollsVoteImpl.class,
428 new Long(voteId));
429 }
430 catch (Exception e) {
431 throw processException(e);
432 }
433 finally {
434 if (pollsVote != null) {
435 cacheResult(pollsVote);
436 }
437
438 closeSession(session);
439 }
440 }
441
442 return pollsVote;
443 }
444
445
452 public List<PollsVote> findByQuestionId(long questionId)
453 throws SystemException {
454 return findByQuestionId(questionId, QueryUtil.ALL_POS,
455 QueryUtil.ALL_POS, null);
456 }
457
458
471 public List<PollsVote> findByQuestionId(long questionId, int start, int end)
472 throws SystemException {
473 return findByQuestionId(questionId, start, end, null);
474 }
475
476
490 public List<PollsVote> findByQuestionId(long questionId, int start,
491 int end, OrderByComparator orderByComparator) throws SystemException {
492 Object[] finderArgs = new Object[] {
493 questionId,
494
495 String.valueOf(start), String.valueOf(end),
496 String.valueOf(orderByComparator)
497 };
498
499 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
500 finderArgs, this);
501
502 if (list == null) {
503 Session session = null;
504
505 try {
506 session = openSession();
507
508 StringBundler query = null;
509
510 if (orderByComparator != null) {
511 query = new StringBundler(3 +
512 (orderByComparator.getOrderByFields().length * 3));
513 }
514 else {
515 query = new StringBundler(2);
516 }
517
518 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
519
520 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
521
522 if (orderByComparator != null) {
523 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
524 orderByComparator);
525 }
526
527 String sql = query.toString();
528
529 Query q = session.createQuery(sql);
530
531 QueryPos qPos = QueryPos.getInstance(q);
532
533 qPos.add(questionId);
534
535 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
536 end);
537 }
538 catch (Exception e) {
539 throw processException(e);
540 }
541 finally {
542 if (list == null) {
543 list = new ArrayList<PollsVote>();
544 }
545
546 cacheResult(list);
547
548 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
549 finderArgs, list);
550
551 closeSession(session);
552 }
553 }
554
555 return list;
556 }
557
558
571 public PollsVote findByQuestionId_First(long questionId,
572 OrderByComparator orderByComparator)
573 throws NoSuchVoteException, SystemException {
574 List<PollsVote> list = findByQuestionId(questionId, 0, 1,
575 orderByComparator);
576
577 if (list.isEmpty()) {
578 StringBundler msg = new StringBundler(4);
579
580 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
581
582 msg.append("questionId=");
583 msg.append(questionId);
584
585 msg.append(StringPool.CLOSE_CURLY_BRACE);
586
587 throw new NoSuchVoteException(msg.toString());
588 }
589 else {
590 return list.get(0);
591 }
592 }
593
594
607 public PollsVote findByQuestionId_Last(long questionId,
608 OrderByComparator orderByComparator)
609 throws NoSuchVoteException, SystemException {
610 int count = countByQuestionId(questionId);
611
612 List<PollsVote> list = findByQuestionId(questionId, count - 1, count,
613 orderByComparator);
614
615 if (list.isEmpty()) {
616 StringBundler msg = new StringBundler(4);
617
618 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
619
620 msg.append("questionId=");
621 msg.append(questionId);
622
623 msg.append(StringPool.CLOSE_CURLY_BRACE);
624
625 throw new NoSuchVoteException(msg.toString());
626 }
627 else {
628 return list.get(0);
629 }
630 }
631
632
646 public PollsVote[] findByQuestionId_PrevAndNext(long voteId,
647 long questionId, OrderByComparator orderByComparator)
648 throws NoSuchVoteException, SystemException {
649 PollsVote pollsVote = findByPrimaryKey(voteId);
650
651 Session session = null;
652
653 try {
654 session = openSession();
655
656 PollsVote[] array = new PollsVoteImpl[3];
657
658 array[0] = getByQuestionId_PrevAndNext(session, pollsVote,
659 questionId, orderByComparator, true);
660
661 array[1] = pollsVote;
662
663 array[2] = getByQuestionId_PrevAndNext(session, pollsVote,
664 questionId, orderByComparator, false);
665
666 return array;
667 }
668 catch (Exception e) {
669 throw processException(e);
670 }
671 finally {
672 closeSession(session);
673 }
674 }
675
676 protected PollsVote getByQuestionId_PrevAndNext(Session session,
677 PollsVote pollsVote, long questionId,
678 OrderByComparator orderByComparator, boolean previous) {
679 StringBundler query = null;
680
681 if (orderByComparator != null) {
682 query = new StringBundler(6 +
683 (orderByComparator.getOrderByFields().length * 6));
684 }
685 else {
686 query = new StringBundler(3);
687 }
688
689 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
690
691 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
692
693 if (orderByComparator != null) {
694 String[] orderByFields = orderByComparator.getOrderByFields();
695
696 if (orderByFields.length > 0) {
697 query.append(WHERE_AND);
698 }
699
700 for (int i = 0; i < orderByFields.length; i++) {
701 query.append(_ORDER_BY_ENTITY_ALIAS);
702 query.append(orderByFields[i]);
703
704 if ((i + 1) < orderByFields.length) {
705 if (orderByComparator.isAscending() ^ previous) {
706 query.append(WHERE_GREATER_THAN_HAS_NEXT);
707 }
708 else {
709 query.append(WHERE_LESSER_THAN_HAS_NEXT);
710 }
711 }
712 else {
713 if (orderByComparator.isAscending() ^ previous) {
714 query.append(WHERE_GREATER_THAN);
715 }
716 else {
717 query.append(WHERE_LESSER_THAN);
718 }
719 }
720 }
721
722 query.append(ORDER_BY_CLAUSE);
723
724 for (int i = 0; i < orderByFields.length; i++) {
725 query.append(_ORDER_BY_ENTITY_ALIAS);
726 query.append(orderByFields[i]);
727
728 if ((i + 1) < orderByFields.length) {
729 if (orderByComparator.isAscending() ^ previous) {
730 query.append(ORDER_BY_ASC_HAS_NEXT);
731 }
732 else {
733 query.append(ORDER_BY_DESC_HAS_NEXT);
734 }
735 }
736 else {
737 if (orderByComparator.isAscending() ^ previous) {
738 query.append(ORDER_BY_ASC);
739 }
740 else {
741 query.append(ORDER_BY_DESC);
742 }
743 }
744 }
745 }
746
747 String sql = query.toString();
748
749 Query q = session.createQuery(sql);
750
751 q.setFirstResult(0);
752 q.setMaxResults(2);
753
754 QueryPos qPos = QueryPos.getInstance(q);
755
756 qPos.add(questionId);
757
758 if (orderByComparator != null) {
759 Object[] values = orderByComparator.getOrderByValues(pollsVote);
760
761 for (Object value : values) {
762 qPos.add(value);
763 }
764 }
765
766 List<PollsVote> list = q.list();
767
768 if (list.size() == 2) {
769 return list.get(1);
770 }
771 else {
772 return null;
773 }
774 }
775
776
783 public List<PollsVote> findByChoiceId(long choiceId)
784 throws SystemException {
785 return findByChoiceId(choiceId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
786 null);
787 }
788
789
802 public List<PollsVote> findByChoiceId(long choiceId, int start, int end)
803 throws SystemException {
804 return findByChoiceId(choiceId, start, end, null);
805 }
806
807
821 public List<PollsVote> findByChoiceId(long choiceId, int start, int end,
822 OrderByComparator orderByComparator) throws SystemException {
823 Object[] finderArgs = new Object[] {
824 choiceId,
825
826 String.valueOf(start), String.valueOf(end),
827 String.valueOf(orderByComparator)
828 };
829
830 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CHOICEID,
831 finderArgs, this);
832
833 if (list == null) {
834 Session session = null;
835
836 try {
837 session = openSession();
838
839 StringBundler query = null;
840
841 if (orderByComparator != null) {
842 query = new StringBundler(3 +
843 (orderByComparator.getOrderByFields().length * 3));
844 }
845 else {
846 query = new StringBundler(2);
847 }
848
849 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
850
851 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
852
853 if (orderByComparator != null) {
854 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
855 orderByComparator);
856 }
857
858 String sql = query.toString();
859
860 Query q = session.createQuery(sql);
861
862 QueryPos qPos = QueryPos.getInstance(q);
863
864 qPos.add(choiceId);
865
866 list = (List<PollsVote>)QueryUtil.list(q, getDialect(), start,
867 end);
868 }
869 catch (Exception e) {
870 throw processException(e);
871 }
872 finally {
873 if (list == null) {
874 list = new ArrayList<PollsVote>();
875 }
876
877 cacheResult(list);
878
879 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CHOICEID,
880 finderArgs, list);
881
882 closeSession(session);
883 }
884 }
885
886 return list;
887 }
888
889
902 public PollsVote findByChoiceId_First(long choiceId,
903 OrderByComparator orderByComparator)
904 throws NoSuchVoteException, SystemException {
905 List<PollsVote> list = findByChoiceId(choiceId, 0, 1, orderByComparator);
906
907 if (list.isEmpty()) {
908 StringBundler msg = new StringBundler(4);
909
910 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
911
912 msg.append("choiceId=");
913 msg.append(choiceId);
914
915 msg.append(StringPool.CLOSE_CURLY_BRACE);
916
917 throw new NoSuchVoteException(msg.toString());
918 }
919 else {
920 return list.get(0);
921 }
922 }
923
924
937 public PollsVote findByChoiceId_Last(long choiceId,
938 OrderByComparator orderByComparator)
939 throws NoSuchVoteException, SystemException {
940 int count = countByChoiceId(choiceId);
941
942 List<PollsVote> list = findByChoiceId(choiceId, count - 1, count,
943 orderByComparator);
944
945 if (list.isEmpty()) {
946 StringBundler msg = new StringBundler(4);
947
948 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
949
950 msg.append("choiceId=");
951 msg.append(choiceId);
952
953 msg.append(StringPool.CLOSE_CURLY_BRACE);
954
955 throw new NoSuchVoteException(msg.toString());
956 }
957 else {
958 return list.get(0);
959 }
960 }
961
962
976 public PollsVote[] findByChoiceId_PrevAndNext(long voteId, long choiceId,
977 OrderByComparator orderByComparator)
978 throws NoSuchVoteException, SystemException {
979 PollsVote pollsVote = findByPrimaryKey(voteId);
980
981 Session session = null;
982
983 try {
984 session = openSession();
985
986 PollsVote[] array = new PollsVoteImpl[3];
987
988 array[0] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
989 orderByComparator, true);
990
991 array[1] = pollsVote;
992
993 array[2] = getByChoiceId_PrevAndNext(session, pollsVote, choiceId,
994 orderByComparator, false);
995
996 return array;
997 }
998 catch (Exception e) {
999 throw processException(e);
1000 }
1001 finally {
1002 closeSession(session);
1003 }
1004 }
1005
1006 protected PollsVote getByChoiceId_PrevAndNext(Session session,
1007 PollsVote pollsVote, long choiceId,
1008 OrderByComparator orderByComparator, boolean previous) {
1009 StringBundler query = null;
1010
1011 if (orderByComparator != null) {
1012 query = new StringBundler(6 +
1013 (orderByComparator.getOrderByFields().length * 6));
1014 }
1015 else {
1016 query = new StringBundler(3);
1017 }
1018
1019 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1020
1021 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1022
1023 if (orderByComparator != null) {
1024 String[] orderByFields = orderByComparator.getOrderByFields();
1025
1026 if (orderByFields.length > 0) {
1027 query.append(WHERE_AND);
1028 }
1029
1030 for (int i = 0; i < orderByFields.length; i++) {
1031 query.append(_ORDER_BY_ENTITY_ALIAS);
1032 query.append(orderByFields[i]);
1033
1034 if ((i + 1) < orderByFields.length) {
1035 if (orderByComparator.isAscending() ^ previous) {
1036 query.append(WHERE_GREATER_THAN_HAS_NEXT);
1037 }
1038 else {
1039 query.append(WHERE_LESSER_THAN_HAS_NEXT);
1040 }
1041 }
1042 else {
1043 if (orderByComparator.isAscending() ^ previous) {
1044 query.append(WHERE_GREATER_THAN);
1045 }
1046 else {
1047 query.append(WHERE_LESSER_THAN);
1048 }
1049 }
1050 }
1051
1052 query.append(ORDER_BY_CLAUSE);
1053
1054 for (int i = 0; i < orderByFields.length; i++) {
1055 query.append(_ORDER_BY_ENTITY_ALIAS);
1056 query.append(orderByFields[i]);
1057
1058 if ((i + 1) < orderByFields.length) {
1059 if (orderByComparator.isAscending() ^ previous) {
1060 query.append(ORDER_BY_ASC_HAS_NEXT);
1061 }
1062 else {
1063 query.append(ORDER_BY_DESC_HAS_NEXT);
1064 }
1065 }
1066 else {
1067 if (orderByComparator.isAscending() ^ previous) {
1068 query.append(ORDER_BY_ASC);
1069 }
1070 else {
1071 query.append(ORDER_BY_DESC);
1072 }
1073 }
1074 }
1075 }
1076
1077 String sql = query.toString();
1078
1079 Query q = session.createQuery(sql);
1080
1081 q.setFirstResult(0);
1082 q.setMaxResults(2);
1083
1084 QueryPos qPos = QueryPos.getInstance(q);
1085
1086 qPos.add(choiceId);
1087
1088 if (orderByComparator != null) {
1089 Object[] values = orderByComparator.getOrderByValues(pollsVote);
1090
1091 for (Object value : values) {
1092 qPos.add(value);
1093 }
1094 }
1095
1096 List<PollsVote> list = q.list();
1097
1098 if (list.size() == 2) {
1099 return list.get(1);
1100 }
1101 else {
1102 return null;
1103 }
1104 }
1105
1106
1115 public PollsVote findByQ_U(long questionId, long userId)
1116 throws NoSuchVoteException, SystemException {
1117 PollsVote pollsVote = fetchByQ_U(questionId, userId);
1118
1119 if (pollsVote == null) {
1120 StringBundler msg = new StringBundler(6);
1121
1122 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1123
1124 msg.append("questionId=");
1125 msg.append(questionId);
1126
1127 msg.append(", userId=");
1128 msg.append(userId);
1129
1130 msg.append(StringPool.CLOSE_CURLY_BRACE);
1131
1132 if (_log.isWarnEnabled()) {
1133 _log.warn(msg.toString());
1134 }
1135
1136 throw new NoSuchVoteException(msg.toString());
1137 }
1138
1139 return pollsVote;
1140 }
1141
1142
1150 public PollsVote fetchByQ_U(long questionId, long userId)
1151 throws SystemException {
1152 return fetchByQ_U(questionId, userId, true);
1153 }
1154
1155
1163 public PollsVote fetchByQ_U(long questionId, long userId,
1164 boolean retrieveFromCache) throws SystemException {
1165 Object[] finderArgs = new Object[] { questionId, userId };
1166
1167 Object result = null;
1168
1169 if (retrieveFromCache) {
1170 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_U,
1171 finderArgs, this);
1172 }
1173
1174 if (result == null) {
1175 Session session = null;
1176
1177 try {
1178 session = openSession();
1179
1180 StringBundler query = new StringBundler(3);
1181
1182 query.append(_SQL_SELECT_POLLSVOTE_WHERE);
1183
1184 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1185
1186 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1187
1188 String sql = query.toString();
1189
1190 Query q = session.createQuery(sql);
1191
1192 QueryPos qPos = QueryPos.getInstance(q);
1193
1194 qPos.add(questionId);
1195
1196 qPos.add(userId);
1197
1198 List<PollsVote> list = q.list();
1199
1200 result = list;
1201
1202 PollsVote pollsVote = null;
1203
1204 if (list.isEmpty()) {
1205 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1206 finderArgs, list);
1207 }
1208 else {
1209 pollsVote = list.get(0);
1210
1211 cacheResult(pollsVote);
1212
1213 if ((pollsVote.getQuestionId() != questionId) ||
1214 (pollsVote.getUserId() != userId)) {
1215 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1216 finderArgs, pollsVote);
1217 }
1218 }
1219
1220 return pollsVote;
1221 }
1222 catch (Exception e) {
1223 throw processException(e);
1224 }
1225 finally {
1226 if (result == null) {
1227 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_U,
1228 finderArgs, new ArrayList<PollsVote>());
1229 }
1230
1231 closeSession(session);
1232 }
1233 }
1234 else {
1235 if (result instanceof List<?>) {
1236 return null;
1237 }
1238 else {
1239 return (PollsVote)result;
1240 }
1241 }
1242 }
1243
1244
1250 public List<PollsVote> findAll() throws SystemException {
1251 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1252 }
1253
1254
1266 public List<PollsVote> findAll(int start, int end)
1267 throws SystemException {
1268 return findAll(start, end, null);
1269 }
1270
1271
1284 public List<PollsVote> findAll(int start, int end,
1285 OrderByComparator orderByComparator) throws SystemException {
1286 Object[] finderArgs = new Object[] {
1287 String.valueOf(start), String.valueOf(end),
1288 String.valueOf(orderByComparator)
1289 };
1290
1291 List<PollsVote> list = (List<PollsVote>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1292 finderArgs, this);
1293
1294 if (list == null) {
1295 Session session = null;
1296
1297 try {
1298 session = openSession();
1299
1300 StringBundler query = null;
1301 String sql = null;
1302
1303 if (orderByComparator != null) {
1304 query = new StringBundler(2 +
1305 (orderByComparator.getOrderByFields().length * 3));
1306
1307 query.append(_SQL_SELECT_POLLSVOTE);
1308
1309 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1310 orderByComparator);
1311
1312 sql = query.toString();
1313 }
1314 else {
1315 sql = _SQL_SELECT_POLLSVOTE;
1316 }
1317
1318 Query q = session.createQuery(sql);
1319
1320 if (orderByComparator == null) {
1321 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1322 start, end, false);
1323
1324 Collections.sort(list);
1325 }
1326 else {
1327 list = (List<PollsVote>)QueryUtil.list(q, getDialect(),
1328 start, end);
1329 }
1330 }
1331 catch (Exception e) {
1332 throw processException(e);
1333 }
1334 finally {
1335 if (list == null) {
1336 list = new ArrayList<PollsVote>();
1337 }
1338
1339 cacheResult(list);
1340
1341 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1342
1343 closeSession(session);
1344 }
1345 }
1346
1347 return list;
1348 }
1349
1350
1356 public void removeByQuestionId(long questionId) throws SystemException {
1357 for (PollsVote pollsVote : findByQuestionId(questionId)) {
1358 remove(pollsVote);
1359 }
1360 }
1361
1362
1368 public void removeByChoiceId(long choiceId) throws SystemException {
1369 for (PollsVote pollsVote : findByChoiceId(choiceId)) {
1370 remove(pollsVote);
1371 }
1372 }
1373
1374
1381 public void removeByQ_U(long questionId, long userId)
1382 throws NoSuchVoteException, SystemException {
1383 PollsVote pollsVote = findByQ_U(questionId, userId);
1384
1385 remove(pollsVote);
1386 }
1387
1388
1393 public void removeAll() throws SystemException {
1394 for (PollsVote pollsVote : findAll()) {
1395 remove(pollsVote);
1396 }
1397 }
1398
1399
1406 public int countByQuestionId(long questionId) throws SystemException {
1407 Object[] finderArgs = new Object[] { questionId };
1408
1409 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1410 finderArgs, this);
1411
1412 if (count == null) {
1413 Session session = null;
1414
1415 try {
1416 session = openSession();
1417
1418 StringBundler query = new StringBundler(2);
1419
1420 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1421
1422 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1423
1424 String sql = query.toString();
1425
1426 Query q = session.createQuery(sql);
1427
1428 QueryPos qPos = QueryPos.getInstance(q);
1429
1430 qPos.add(questionId);
1431
1432 count = (Long)q.uniqueResult();
1433 }
1434 catch (Exception e) {
1435 throw processException(e);
1436 }
1437 finally {
1438 if (count == null) {
1439 count = Long.valueOf(0);
1440 }
1441
1442 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1443 finderArgs, count);
1444
1445 closeSession(session);
1446 }
1447 }
1448
1449 return count.intValue();
1450 }
1451
1452
1459 public int countByChoiceId(long choiceId) throws SystemException {
1460 Object[] finderArgs = new Object[] { choiceId };
1461
1462 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CHOICEID,
1463 finderArgs, this);
1464
1465 if (count == null) {
1466 Session session = null;
1467
1468 try {
1469 session = openSession();
1470
1471 StringBundler query = new StringBundler(2);
1472
1473 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1474
1475 query.append(_FINDER_COLUMN_CHOICEID_CHOICEID_2);
1476
1477 String sql = query.toString();
1478
1479 Query q = session.createQuery(sql);
1480
1481 QueryPos qPos = QueryPos.getInstance(q);
1482
1483 qPos.add(choiceId);
1484
1485 count = (Long)q.uniqueResult();
1486 }
1487 catch (Exception e) {
1488 throw processException(e);
1489 }
1490 finally {
1491 if (count == null) {
1492 count = Long.valueOf(0);
1493 }
1494
1495 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CHOICEID,
1496 finderArgs, count);
1497
1498 closeSession(session);
1499 }
1500 }
1501
1502 return count.intValue();
1503 }
1504
1505
1513 public int countByQ_U(long questionId, long userId)
1514 throws SystemException {
1515 Object[] finderArgs = new Object[] { questionId, userId };
1516
1517 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_U,
1518 finderArgs, this);
1519
1520 if (count == null) {
1521 Session session = null;
1522
1523 try {
1524 session = openSession();
1525
1526 StringBundler query = new StringBundler(3);
1527
1528 query.append(_SQL_COUNT_POLLSVOTE_WHERE);
1529
1530 query.append(_FINDER_COLUMN_Q_U_QUESTIONID_2);
1531
1532 query.append(_FINDER_COLUMN_Q_U_USERID_2);
1533
1534 String sql = query.toString();
1535
1536 Query q = session.createQuery(sql);
1537
1538 QueryPos qPos = QueryPos.getInstance(q);
1539
1540 qPos.add(questionId);
1541
1542 qPos.add(userId);
1543
1544 count = (Long)q.uniqueResult();
1545 }
1546 catch (Exception e) {
1547 throw processException(e);
1548 }
1549 finally {
1550 if (count == null) {
1551 count = Long.valueOf(0);
1552 }
1553
1554 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_U, finderArgs,
1555 count);
1556
1557 closeSession(session);
1558 }
1559 }
1560
1561 return count.intValue();
1562 }
1563
1564
1570 public int countAll() throws SystemException {
1571 Object[] finderArgs = new Object[0];
1572
1573 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1574 finderArgs, this);
1575
1576 if (count == null) {
1577 Session session = null;
1578
1579 try {
1580 session = openSession();
1581
1582 Query q = session.createQuery(_SQL_COUNT_POLLSVOTE);
1583
1584 count = (Long)q.uniqueResult();
1585 }
1586 catch (Exception e) {
1587 throw processException(e);
1588 }
1589 finally {
1590 if (count == null) {
1591 count = Long.valueOf(0);
1592 }
1593
1594 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1595 count);
1596
1597 closeSession(session);
1598 }
1599 }
1600
1601 return count.intValue();
1602 }
1603
1604
1607 public void afterPropertiesSet() {
1608 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1609 com.liferay.portal.util.PropsUtil.get(
1610 "value.object.listener.com.liferay.portlet.polls.model.PollsVote")));
1611
1612 if (listenerClassNames.length > 0) {
1613 try {
1614 List<ModelListener<PollsVote>> listenersList = new ArrayList<ModelListener<PollsVote>>();
1615
1616 for (String listenerClassName : listenerClassNames) {
1617 listenersList.add((ModelListener<PollsVote>)InstanceFactory.newInstance(
1618 listenerClassName));
1619 }
1620
1621 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1622 }
1623 catch (Exception e) {
1624 _log.error(e);
1625 }
1626 }
1627 }
1628
1629 @BeanReference(type = PollsChoicePersistence.class)
1630 protected PollsChoicePersistence pollsChoicePersistence;
1631 @BeanReference(type = PollsQuestionPersistence.class)
1632 protected PollsQuestionPersistence pollsQuestionPersistence;
1633 @BeanReference(type = PollsVotePersistence.class)
1634 protected PollsVotePersistence pollsVotePersistence;
1635 @BeanReference(type = ResourcePersistence.class)
1636 protected ResourcePersistence resourcePersistence;
1637 @BeanReference(type = UserPersistence.class)
1638 protected UserPersistence userPersistence;
1639 private static final String _SQL_SELECT_POLLSVOTE = "SELECT pollsVote FROM PollsVote pollsVote";
1640 private static final String _SQL_SELECT_POLLSVOTE_WHERE = "SELECT pollsVote FROM PollsVote pollsVote WHERE ";
1641 private static final String _SQL_COUNT_POLLSVOTE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote";
1642 private static final String _SQL_COUNT_POLLSVOTE_WHERE = "SELECT COUNT(pollsVote) FROM PollsVote pollsVote WHERE ";
1643 private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsVote.questionId = ?";
1644 private static final String _FINDER_COLUMN_CHOICEID_CHOICEID_2 = "pollsVote.choiceId = ?";
1645 private static final String _FINDER_COLUMN_Q_U_QUESTIONID_2 = "pollsVote.questionId = ? AND ";
1646 private static final String _FINDER_COLUMN_Q_U_USERID_2 = "pollsVote.userId = ?";
1647 private static final String _ORDER_BY_ENTITY_ALIAS = "pollsVote.";
1648 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsVote exists with the primary key ";
1649 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsVote exists with the key {";
1650 private static Log _log = LogFactoryUtil.getLog(PollsVotePersistenceImpl.class);
1651 }