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.kernel.util.Validator;
037 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
038 import com.liferay.portal.model.ModelListener;
039 import com.liferay.portal.service.persistence.BatchSessionUtil;
040 import com.liferay.portal.service.persistence.ResourcePersistence;
041 import com.liferay.portal.service.persistence.UserPersistence;
042 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043
044 import com.liferay.portlet.polls.NoSuchChoiceException;
045 import com.liferay.portlet.polls.model.PollsChoice;
046 import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
047 import com.liferay.portlet.polls.model.impl.PollsChoiceModelImpl;
048
049 import java.io.Serializable;
050
051 import java.util.ArrayList;
052 import java.util.Collections;
053 import java.util.List;
054
055
071 public class PollsChoicePersistenceImpl extends BasePersistenceImpl<PollsChoice>
072 implements PollsChoicePersistence {
073 public static final String FINDER_CLASS_NAME_ENTITY = PollsChoiceImpl.class.getName();
074 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
075 ".List";
076 public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
077 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
078 "findByUuid",
079 new String[] {
080 String.class.getName(),
081
082 "java.lang.Integer", "java.lang.Integer",
083 "com.liferay.portal.kernel.util.OrderByComparator"
084 });
085 public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
086 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087 "countByUuid", new String[] { String.class.getName() });
088 public static final FinderPath FINDER_PATH_FIND_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
089 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
090 "findByQuestionId",
091 new String[] {
092 Long.class.getName(),
093
094 "java.lang.Integer", "java.lang.Integer",
095 "com.liferay.portal.kernel.util.OrderByComparator"
096 });
097 public static final FinderPath FINDER_PATH_COUNT_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
098 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
099 "countByQuestionId", new String[] { Long.class.getName() });
100 public static final FinderPath FINDER_PATH_FETCH_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
101 PollsChoiceModelImpl.FINDER_CACHE_ENABLED,
102 FINDER_CLASS_NAME_ENTITY, "fetchByQ_N",
103 new String[] { Long.class.getName(), String.class.getName() });
104 public static final FinderPath FINDER_PATH_COUNT_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
105 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
106 "countByQ_N",
107 new String[] { Long.class.getName(), String.class.getName() });
108 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
109 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
110 "findAll", new String[0]);
111 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
112 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113 "countAll", new String[0]);
114
115
120 public void cacheResult(PollsChoice pollsChoice) {
121 EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
122 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
123
124 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
125 new Object[] {
126 new Long(pollsChoice.getQuestionId()),
127
128 pollsChoice.getName()
129 }, pollsChoice);
130 }
131
132
137 public void cacheResult(List<PollsChoice> pollsChoices) {
138 for (PollsChoice pollsChoice : pollsChoices) {
139 if (EntityCacheUtil.getResult(
140 PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
141 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), this) == null) {
142 cacheResult(pollsChoice);
143 }
144 }
145 }
146
147
154 public void clearCache() {
155 CacheRegistryUtil.clear(PollsChoiceImpl.class.getName());
156 EntityCacheUtil.clearCache(PollsChoiceImpl.class.getName());
157 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
158 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
159 }
160
161
168 public void clearCache(PollsChoice pollsChoice) {
169 EntityCacheUtil.removeResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
170 PollsChoiceImpl.class, pollsChoice.getPrimaryKey());
171
172 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
173 new Object[] {
174 new Long(pollsChoice.getQuestionId()),
175
176 pollsChoice.getName()
177 });
178 }
179
180
186 public PollsChoice create(long choiceId) {
187 PollsChoice pollsChoice = new PollsChoiceImpl();
188
189 pollsChoice.setNew(true);
190 pollsChoice.setPrimaryKey(choiceId);
191
192 String uuid = PortalUUIDUtil.generate();
193
194 pollsChoice.setUuid(uuid);
195
196 return pollsChoice;
197 }
198
199
207 public PollsChoice remove(Serializable primaryKey)
208 throws NoSuchModelException, SystemException {
209 return remove(((Long)primaryKey).longValue());
210 }
211
212
220 public PollsChoice remove(long choiceId)
221 throws NoSuchChoiceException, SystemException {
222 Session session = null;
223
224 try {
225 session = openSession();
226
227 PollsChoice pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
228 new Long(choiceId));
229
230 if (pollsChoice == null) {
231 if (_log.isWarnEnabled()) {
232 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
233 }
234
235 throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
236 choiceId);
237 }
238
239 return remove(pollsChoice);
240 }
241 catch (NoSuchChoiceException nsee) {
242 throw nsee;
243 }
244 catch (Exception e) {
245 throw processException(e);
246 }
247 finally {
248 closeSession(session);
249 }
250 }
251
252 protected PollsChoice removeImpl(PollsChoice pollsChoice)
253 throws SystemException {
254 pollsChoice = toUnwrappedModel(pollsChoice);
255
256 Session session = null;
257
258 try {
259 session = openSession();
260
261 BatchSessionUtil.delete(session, pollsChoice);
262 }
263 catch (Exception e) {
264 throw processException(e);
265 }
266 finally {
267 closeSession(session);
268 }
269
270 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
271
272 PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
273
274 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
275 new Object[] {
276 new Long(pollsChoiceModelImpl.getOriginalQuestionId()),
277
278 pollsChoiceModelImpl.getOriginalName()
279 });
280
281 EntityCacheUtil.removeResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
282 PollsChoiceImpl.class, pollsChoice.getPrimaryKey());
283
284 return pollsChoice;
285 }
286
287 public PollsChoice updateImpl(
288 com.liferay.portlet.polls.model.PollsChoice pollsChoice, boolean merge)
289 throws SystemException {
290 pollsChoice = toUnwrappedModel(pollsChoice);
291
292 boolean isNew = pollsChoice.isNew();
293
294 PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
295
296 if (Validator.isNull(pollsChoice.getUuid())) {
297 String uuid = PortalUUIDUtil.generate();
298
299 pollsChoice.setUuid(uuid);
300 }
301
302 Session session = null;
303
304 try {
305 session = openSession();
306
307 BatchSessionUtil.update(session, pollsChoice, merge);
308
309 pollsChoice.setNew(false);
310 }
311 catch (Exception e) {
312 throw processException(e);
313 }
314 finally {
315 closeSession(session);
316 }
317
318 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
319
320 EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
321 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
322
323 if (!isNew &&
324 ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
325 !Validator.equals(pollsChoice.getName(),
326 pollsChoiceModelImpl.getOriginalName()))) {
327 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
328 new Object[] {
329 new Long(pollsChoiceModelImpl.getOriginalQuestionId()),
330
331 pollsChoiceModelImpl.getOriginalName()
332 });
333 }
334
335 if (isNew ||
336 ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
337 !Validator.equals(pollsChoice.getName(),
338 pollsChoiceModelImpl.getOriginalName()))) {
339 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
340 new Object[] {
341 new Long(pollsChoice.getQuestionId()),
342
343 pollsChoice.getName()
344 }, pollsChoice);
345 }
346
347 return pollsChoice;
348 }
349
350 protected PollsChoice toUnwrappedModel(PollsChoice pollsChoice) {
351 if (pollsChoice instanceof PollsChoiceImpl) {
352 return pollsChoice;
353 }
354
355 PollsChoiceImpl pollsChoiceImpl = new PollsChoiceImpl();
356
357 pollsChoiceImpl.setNew(pollsChoice.isNew());
358 pollsChoiceImpl.setPrimaryKey(pollsChoice.getPrimaryKey());
359
360 pollsChoiceImpl.setUuid(pollsChoice.getUuid());
361 pollsChoiceImpl.setChoiceId(pollsChoice.getChoiceId());
362 pollsChoiceImpl.setQuestionId(pollsChoice.getQuestionId());
363 pollsChoiceImpl.setName(pollsChoice.getName());
364 pollsChoiceImpl.setDescription(pollsChoice.getDescription());
365
366 return pollsChoiceImpl;
367 }
368
369
377 public PollsChoice findByPrimaryKey(Serializable primaryKey)
378 throws NoSuchModelException, SystemException {
379 return findByPrimaryKey(((Long)primaryKey).longValue());
380 }
381
382
390 public PollsChoice findByPrimaryKey(long choiceId)
391 throws NoSuchChoiceException, SystemException {
392 PollsChoice pollsChoice = fetchByPrimaryKey(choiceId);
393
394 if (pollsChoice == null) {
395 if (_log.isWarnEnabled()) {
396 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
397 }
398
399 throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
400 choiceId);
401 }
402
403 return pollsChoice;
404 }
405
406
413 public PollsChoice fetchByPrimaryKey(Serializable primaryKey)
414 throws SystemException {
415 return fetchByPrimaryKey(((Long)primaryKey).longValue());
416 }
417
418
425 public PollsChoice fetchByPrimaryKey(long choiceId)
426 throws SystemException {
427 PollsChoice pollsChoice = (PollsChoice)EntityCacheUtil.getResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
428 PollsChoiceImpl.class, choiceId, this);
429
430 if (pollsChoice == null) {
431 Session session = null;
432
433 try {
434 session = openSession();
435
436 pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
437 new Long(choiceId));
438 }
439 catch (Exception e) {
440 throw processException(e);
441 }
442 finally {
443 if (pollsChoice != null) {
444 cacheResult(pollsChoice);
445 }
446
447 closeSession(session);
448 }
449 }
450
451 return pollsChoice;
452 }
453
454
461 public List<PollsChoice> findByUuid(String uuid) throws SystemException {
462 return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
463 }
464
465
478 public List<PollsChoice> findByUuid(String uuid, int start, int end)
479 throws SystemException {
480 return findByUuid(uuid, start, end, null);
481 }
482
483
497 public List<PollsChoice> findByUuid(String uuid, int start, int end,
498 OrderByComparator orderByComparator) throws SystemException {
499 Object[] finderArgs = new Object[] {
500 uuid,
501
502 String.valueOf(start), String.valueOf(end),
503 String.valueOf(orderByComparator)
504 };
505
506 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
507 finderArgs, this);
508
509 if (list == null) {
510 Session session = null;
511
512 try {
513 session = openSession();
514
515 StringBundler query = null;
516
517 if (orderByComparator != null) {
518 query = new StringBundler(3 +
519 (orderByComparator.getOrderByFields().length * 3));
520 }
521 else {
522 query = new StringBundler(3);
523 }
524
525 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
526
527 if (uuid == null) {
528 query.append(_FINDER_COLUMN_UUID_UUID_1);
529 }
530 else {
531 if (uuid.equals(StringPool.BLANK)) {
532 query.append(_FINDER_COLUMN_UUID_UUID_3);
533 }
534 else {
535 query.append(_FINDER_COLUMN_UUID_UUID_2);
536 }
537 }
538
539 if (orderByComparator != null) {
540 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
541 orderByComparator);
542 }
543
544 else {
545 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
546 }
547
548 String sql = query.toString();
549
550 Query q = session.createQuery(sql);
551
552 QueryPos qPos = QueryPos.getInstance(q);
553
554 if (uuid != null) {
555 qPos.add(uuid);
556 }
557
558 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
559 start, end);
560 }
561 catch (Exception e) {
562 throw processException(e);
563 }
564 finally {
565 if (list == null) {
566 list = new ArrayList<PollsChoice>();
567 }
568
569 cacheResult(list);
570
571 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID, finderArgs,
572 list);
573
574 closeSession(session);
575 }
576 }
577
578 return list;
579 }
580
581
594 public PollsChoice findByUuid_First(String uuid,
595 OrderByComparator orderByComparator)
596 throws NoSuchChoiceException, SystemException {
597 List<PollsChoice> list = findByUuid(uuid, 0, 1, orderByComparator);
598
599 if (list.isEmpty()) {
600 StringBundler msg = new StringBundler(4);
601
602 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
603
604 msg.append("uuid=");
605 msg.append(uuid);
606
607 msg.append(StringPool.CLOSE_CURLY_BRACE);
608
609 throw new NoSuchChoiceException(msg.toString());
610 }
611 else {
612 return list.get(0);
613 }
614 }
615
616
629 public PollsChoice findByUuid_Last(String uuid,
630 OrderByComparator orderByComparator)
631 throws NoSuchChoiceException, SystemException {
632 int count = countByUuid(uuid);
633
634 List<PollsChoice> list = findByUuid(uuid, count - 1, count,
635 orderByComparator);
636
637 if (list.isEmpty()) {
638 StringBundler msg = new StringBundler(4);
639
640 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
641
642 msg.append("uuid=");
643 msg.append(uuid);
644
645 msg.append(StringPool.CLOSE_CURLY_BRACE);
646
647 throw new NoSuchChoiceException(msg.toString());
648 }
649 else {
650 return list.get(0);
651 }
652 }
653
654
668 public PollsChoice[] findByUuid_PrevAndNext(long choiceId, String uuid,
669 OrderByComparator orderByComparator)
670 throws NoSuchChoiceException, SystemException {
671 PollsChoice pollsChoice = findByPrimaryKey(choiceId);
672
673 Session session = null;
674
675 try {
676 session = openSession();
677
678 PollsChoice[] array = new PollsChoiceImpl[3];
679
680 array[0] = getByUuid_PrevAndNext(session, pollsChoice, uuid,
681 orderByComparator, true);
682
683 array[1] = pollsChoice;
684
685 array[2] = getByUuid_PrevAndNext(session, pollsChoice, uuid,
686 orderByComparator, false);
687
688 return array;
689 }
690 catch (Exception e) {
691 throw processException(e);
692 }
693 finally {
694 closeSession(session);
695 }
696 }
697
698 protected PollsChoice getByUuid_PrevAndNext(Session session,
699 PollsChoice pollsChoice, String uuid,
700 OrderByComparator orderByComparator, boolean previous) {
701 StringBundler query = null;
702
703 if (orderByComparator != null) {
704 query = new StringBundler(6 +
705 (orderByComparator.getOrderByFields().length * 6));
706 }
707 else {
708 query = new StringBundler(3);
709 }
710
711 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
712
713 if (uuid == null) {
714 query.append(_FINDER_COLUMN_UUID_UUID_1);
715 }
716 else {
717 if (uuid.equals(StringPool.BLANK)) {
718 query.append(_FINDER_COLUMN_UUID_UUID_3);
719 }
720 else {
721 query.append(_FINDER_COLUMN_UUID_UUID_2);
722 }
723 }
724
725 if (orderByComparator != null) {
726 String[] orderByFields = orderByComparator.getOrderByFields();
727
728 if (orderByFields.length > 0) {
729 query.append(WHERE_AND);
730 }
731
732 for (int i = 0; i < orderByFields.length; i++) {
733 query.append(_ORDER_BY_ENTITY_ALIAS);
734 query.append(orderByFields[i]);
735
736 if ((i + 1) < orderByFields.length) {
737 if (orderByComparator.isAscending() ^ previous) {
738 query.append(WHERE_GREATER_THAN_HAS_NEXT);
739 }
740 else {
741 query.append(WHERE_LESSER_THAN_HAS_NEXT);
742 }
743 }
744 else {
745 if (orderByComparator.isAscending() ^ previous) {
746 query.append(WHERE_GREATER_THAN);
747 }
748 else {
749 query.append(WHERE_LESSER_THAN);
750 }
751 }
752 }
753
754 query.append(ORDER_BY_CLAUSE);
755
756 for (int i = 0; i < orderByFields.length; i++) {
757 query.append(_ORDER_BY_ENTITY_ALIAS);
758 query.append(orderByFields[i]);
759
760 if ((i + 1) < orderByFields.length) {
761 if (orderByComparator.isAscending() ^ previous) {
762 query.append(ORDER_BY_ASC_HAS_NEXT);
763 }
764 else {
765 query.append(ORDER_BY_DESC_HAS_NEXT);
766 }
767 }
768 else {
769 if (orderByComparator.isAscending() ^ previous) {
770 query.append(ORDER_BY_ASC);
771 }
772 else {
773 query.append(ORDER_BY_DESC);
774 }
775 }
776 }
777 }
778
779 else {
780 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
781 }
782
783 String sql = query.toString();
784
785 Query q = session.createQuery(sql);
786
787 q.setFirstResult(0);
788 q.setMaxResults(2);
789
790 QueryPos qPos = QueryPos.getInstance(q);
791
792 if (uuid != null) {
793 qPos.add(uuid);
794 }
795
796 if (orderByComparator != null) {
797 Object[] values = orderByComparator.getOrderByValues(pollsChoice);
798
799 for (Object value : values) {
800 qPos.add(value);
801 }
802 }
803
804 List<PollsChoice> list = q.list();
805
806 if (list.size() == 2) {
807 return list.get(1);
808 }
809 else {
810 return null;
811 }
812 }
813
814
821 public List<PollsChoice> findByQuestionId(long questionId)
822 throws SystemException {
823 return findByQuestionId(questionId, QueryUtil.ALL_POS,
824 QueryUtil.ALL_POS, null);
825 }
826
827
840 public List<PollsChoice> findByQuestionId(long questionId, int start,
841 int end) throws SystemException {
842 return findByQuestionId(questionId, start, end, null);
843 }
844
845
859 public List<PollsChoice> findByQuestionId(long questionId, int start,
860 int end, OrderByComparator orderByComparator) throws SystemException {
861 Object[] finderArgs = new Object[] {
862 questionId,
863
864 String.valueOf(start), String.valueOf(end),
865 String.valueOf(orderByComparator)
866 };
867
868 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
869 finderArgs, this);
870
871 if (list == null) {
872 Session session = null;
873
874 try {
875 session = openSession();
876
877 StringBundler query = null;
878
879 if (orderByComparator != null) {
880 query = new StringBundler(3 +
881 (orderByComparator.getOrderByFields().length * 3));
882 }
883 else {
884 query = new StringBundler(3);
885 }
886
887 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
888
889 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
890
891 if (orderByComparator != null) {
892 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
893 orderByComparator);
894 }
895
896 else {
897 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
898 }
899
900 String sql = query.toString();
901
902 Query q = session.createQuery(sql);
903
904 QueryPos qPos = QueryPos.getInstance(q);
905
906 qPos.add(questionId);
907
908 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
909 start, end);
910 }
911 catch (Exception e) {
912 throw processException(e);
913 }
914 finally {
915 if (list == null) {
916 list = new ArrayList<PollsChoice>();
917 }
918
919 cacheResult(list);
920
921 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
922 finderArgs, list);
923
924 closeSession(session);
925 }
926 }
927
928 return list;
929 }
930
931
944 public PollsChoice findByQuestionId_First(long questionId,
945 OrderByComparator orderByComparator)
946 throws NoSuchChoiceException, SystemException {
947 List<PollsChoice> list = findByQuestionId(questionId, 0, 1,
948 orderByComparator);
949
950 if (list.isEmpty()) {
951 StringBundler msg = new StringBundler(4);
952
953 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
954
955 msg.append("questionId=");
956 msg.append(questionId);
957
958 msg.append(StringPool.CLOSE_CURLY_BRACE);
959
960 throw new NoSuchChoiceException(msg.toString());
961 }
962 else {
963 return list.get(0);
964 }
965 }
966
967
980 public PollsChoice findByQuestionId_Last(long questionId,
981 OrderByComparator orderByComparator)
982 throws NoSuchChoiceException, SystemException {
983 int count = countByQuestionId(questionId);
984
985 List<PollsChoice> list = findByQuestionId(questionId, count - 1, count,
986 orderByComparator);
987
988 if (list.isEmpty()) {
989 StringBundler msg = new StringBundler(4);
990
991 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
992
993 msg.append("questionId=");
994 msg.append(questionId);
995
996 msg.append(StringPool.CLOSE_CURLY_BRACE);
997
998 throw new NoSuchChoiceException(msg.toString());
999 }
1000 else {
1001 return list.get(0);
1002 }
1003 }
1004
1005
1019 public PollsChoice[] findByQuestionId_PrevAndNext(long choiceId,
1020 long questionId, OrderByComparator orderByComparator)
1021 throws NoSuchChoiceException, SystemException {
1022 PollsChoice pollsChoice = findByPrimaryKey(choiceId);
1023
1024 Session session = null;
1025
1026 try {
1027 session = openSession();
1028
1029 PollsChoice[] array = new PollsChoiceImpl[3];
1030
1031 array[0] = getByQuestionId_PrevAndNext(session, pollsChoice,
1032 questionId, orderByComparator, true);
1033
1034 array[1] = pollsChoice;
1035
1036 array[2] = getByQuestionId_PrevAndNext(session, pollsChoice,
1037 questionId, orderByComparator, false);
1038
1039 return array;
1040 }
1041 catch (Exception e) {
1042 throw processException(e);
1043 }
1044 finally {
1045 closeSession(session);
1046 }
1047 }
1048
1049 protected PollsChoice getByQuestionId_PrevAndNext(Session session,
1050 PollsChoice pollsChoice, long questionId,
1051 OrderByComparator orderByComparator, boolean previous) {
1052 StringBundler query = null;
1053
1054 if (orderByComparator != null) {
1055 query = new StringBundler(6 +
1056 (orderByComparator.getOrderByFields().length * 6));
1057 }
1058 else {
1059 query = new StringBundler(3);
1060 }
1061
1062 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
1063
1064 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1065
1066 if (orderByComparator != null) {
1067 String[] orderByFields = orderByComparator.getOrderByFields();
1068
1069 if (orderByFields.length > 0) {
1070 query.append(WHERE_AND);
1071 }
1072
1073 for (int i = 0; i < orderByFields.length; i++) {
1074 query.append(_ORDER_BY_ENTITY_ALIAS);
1075 query.append(orderByFields[i]);
1076
1077 if ((i + 1) < orderByFields.length) {
1078 if (orderByComparator.isAscending() ^ previous) {
1079 query.append(WHERE_GREATER_THAN_HAS_NEXT);
1080 }
1081 else {
1082 query.append(WHERE_LESSER_THAN_HAS_NEXT);
1083 }
1084 }
1085 else {
1086 if (orderByComparator.isAscending() ^ previous) {
1087 query.append(WHERE_GREATER_THAN);
1088 }
1089 else {
1090 query.append(WHERE_LESSER_THAN);
1091 }
1092 }
1093 }
1094
1095 query.append(ORDER_BY_CLAUSE);
1096
1097 for (int i = 0; i < orderByFields.length; i++) {
1098 query.append(_ORDER_BY_ENTITY_ALIAS);
1099 query.append(orderByFields[i]);
1100
1101 if ((i + 1) < orderByFields.length) {
1102 if (orderByComparator.isAscending() ^ previous) {
1103 query.append(ORDER_BY_ASC_HAS_NEXT);
1104 }
1105 else {
1106 query.append(ORDER_BY_DESC_HAS_NEXT);
1107 }
1108 }
1109 else {
1110 if (orderByComparator.isAscending() ^ previous) {
1111 query.append(ORDER_BY_ASC);
1112 }
1113 else {
1114 query.append(ORDER_BY_DESC);
1115 }
1116 }
1117 }
1118 }
1119
1120 else {
1121 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
1122 }
1123
1124 String sql = query.toString();
1125
1126 Query q = session.createQuery(sql);
1127
1128 q.setFirstResult(0);
1129 q.setMaxResults(2);
1130
1131 QueryPos qPos = QueryPos.getInstance(q);
1132
1133 qPos.add(questionId);
1134
1135 if (orderByComparator != null) {
1136 Object[] values = orderByComparator.getOrderByValues(pollsChoice);
1137
1138 for (Object value : values) {
1139 qPos.add(value);
1140 }
1141 }
1142
1143 List<PollsChoice> list = q.list();
1144
1145 if (list.size() == 2) {
1146 return list.get(1);
1147 }
1148 else {
1149 return null;
1150 }
1151 }
1152
1153
1162 public PollsChoice findByQ_N(long questionId, String name)
1163 throws NoSuchChoiceException, SystemException {
1164 PollsChoice pollsChoice = fetchByQ_N(questionId, name);
1165
1166 if (pollsChoice == null) {
1167 StringBundler msg = new StringBundler(6);
1168
1169 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1170
1171 msg.append("questionId=");
1172 msg.append(questionId);
1173
1174 msg.append(", name=");
1175 msg.append(name);
1176
1177 msg.append(StringPool.CLOSE_CURLY_BRACE);
1178
1179 if (_log.isWarnEnabled()) {
1180 _log.warn(msg.toString());
1181 }
1182
1183 throw new NoSuchChoiceException(msg.toString());
1184 }
1185
1186 return pollsChoice;
1187 }
1188
1189
1197 public PollsChoice fetchByQ_N(long questionId, String name)
1198 throws SystemException {
1199 return fetchByQ_N(questionId, name, true);
1200 }
1201
1202
1210 public PollsChoice fetchByQ_N(long questionId, String name,
1211 boolean retrieveFromCache) throws SystemException {
1212 Object[] finderArgs = new Object[] { questionId, name };
1213
1214 Object result = null;
1215
1216 if (retrieveFromCache) {
1217 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_N,
1218 finderArgs, this);
1219 }
1220
1221 if (result == null) {
1222 Session session = null;
1223
1224 try {
1225 session = openSession();
1226
1227 StringBundler query = new StringBundler(4);
1228
1229 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
1230
1231 query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
1232
1233 if (name == null) {
1234 query.append(_FINDER_COLUMN_Q_N_NAME_1);
1235 }
1236 else {
1237 if (name.equals(StringPool.BLANK)) {
1238 query.append(_FINDER_COLUMN_Q_N_NAME_3);
1239 }
1240 else {
1241 query.append(_FINDER_COLUMN_Q_N_NAME_2);
1242 }
1243 }
1244
1245 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
1246
1247 String sql = query.toString();
1248
1249 Query q = session.createQuery(sql);
1250
1251 QueryPos qPos = QueryPos.getInstance(q);
1252
1253 qPos.add(questionId);
1254
1255 if (name != null) {
1256 qPos.add(name);
1257 }
1258
1259 List<PollsChoice> list = q.list();
1260
1261 result = list;
1262
1263 PollsChoice pollsChoice = null;
1264
1265 if (list.isEmpty()) {
1266 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1267 finderArgs, list);
1268 }
1269 else {
1270 pollsChoice = list.get(0);
1271
1272 cacheResult(pollsChoice);
1273
1274 if ((pollsChoice.getQuestionId() != questionId) ||
1275 (pollsChoice.getName() == null) ||
1276 !pollsChoice.getName().equals(name)) {
1277 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1278 finderArgs, pollsChoice);
1279 }
1280 }
1281
1282 return pollsChoice;
1283 }
1284 catch (Exception e) {
1285 throw processException(e);
1286 }
1287 finally {
1288 if (result == null) {
1289 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1290 finderArgs, new ArrayList<PollsChoice>());
1291 }
1292
1293 closeSession(session);
1294 }
1295 }
1296 else {
1297 if (result instanceof List<?>) {
1298 return null;
1299 }
1300 else {
1301 return (PollsChoice)result;
1302 }
1303 }
1304 }
1305
1306
1312 public List<PollsChoice> findAll() throws SystemException {
1313 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1314 }
1315
1316
1328 public List<PollsChoice> findAll(int start, int end)
1329 throws SystemException {
1330 return findAll(start, end, null);
1331 }
1332
1333
1346 public List<PollsChoice> findAll(int start, int end,
1347 OrderByComparator orderByComparator) throws SystemException {
1348 Object[] finderArgs = new Object[] {
1349 String.valueOf(start), String.valueOf(end),
1350 String.valueOf(orderByComparator)
1351 };
1352
1353 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1354 finderArgs, this);
1355
1356 if (list == null) {
1357 Session session = null;
1358
1359 try {
1360 session = openSession();
1361
1362 StringBundler query = null;
1363 String sql = null;
1364
1365 if (orderByComparator != null) {
1366 query = new StringBundler(2 +
1367 (orderByComparator.getOrderByFields().length * 3));
1368
1369 query.append(_SQL_SELECT_POLLSCHOICE);
1370
1371 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1372 orderByComparator);
1373
1374 sql = query.toString();
1375 }
1376 else {
1377 sql = _SQL_SELECT_POLLSCHOICE.concat(PollsChoiceModelImpl.ORDER_BY_JPQL);
1378 }
1379
1380 Query q = session.createQuery(sql);
1381
1382 if (orderByComparator == null) {
1383 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1384 start, end, false);
1385
1386 Collections.sort(list);
1387 }
1388 else {
1389 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1390 start, end);
1391 }
1392 }
1393 catch (Exception e) {
1394 throw processException(e);
1395 }
1396 finally {
1397 if (list == null) {
1398 list = new ArrayList<PollsChoice>();
1399 }
1400
1401 cacheResult(list);
1402
1403 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1404
1405 closeSession(session);
1406 }
1407 }
1408
1409 return list;
1410 }
1411
1412
1418 public void removeByUuid(String uuid) throws SystemException {
1419 for (PollsChoice pollsChoice : findByUuid(uuid)) {
1420 remove(pollsChoice);
1421 }
1422 }
1423
1424
1430 public void removeByQuestionId(long questionId) throws SystemException {
1431 for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1432 remove(pollsChoice);
1433 }
1434 }
1435
1436
1443 public void removeByQ_N(long questionId, String name)
1444 throws NoSuchChoiceException, SystemException {
1445 PollsChoice pollsChoice = findByQ_N(questionId, name);
1446
1447 remove(pollsChoice);
1448 }
1449
1450
1455 public void removeAll() throws SystemException {
1456 for (PollsChoice pollsChoice : findAll()) {
1457 remove(pollsChoice);
1458 }
1459 }
1460
1461
1468 public int countByUuid(String uuid) throws SystemException {
1469 Object[] finderArgs = new Object[] { uuid };
1470
1471 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1472 finderArgs, this);
1473
1474 if (count == null) {
1475 Session session = null;
1476
1477 try {
1478 session = openSession();
1479
1480 StringBundler query = new StringBundler(2);
1481
1482 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1483
1484 if (uuid == null) {
1485 query.append(_FINDER_COLUMN_UUID_UUID_1);
1486 }
1487 else {
1488 if (uuid.equals(StringPool.BLANK)) {
1489 query.append(_FINDER_COLUMN_UUID_UUID_3);
1490 }
1491 else {
1492 query.append(_FINDER_COLUMN_UUID_UUID_2);
1493 }
1494 }
1495
1496 String sql = query.toString();
1497
1498 Query q = session.createQuery(sql);
1499
1500 QueryPos qPos = QueryPos.getInstance(q);
1501
1502 if (uuid != null) {
1503 qPos.add(uuid);
1504 }
1505
1506 count = (Long)q.uniqueResult();
1507 }
1508 catch (Exception e) {
1509 throw processException(e);
1510 }
1511 finally {
1512 if (count == null) {
1513 count = Long.valueOf(0);
1514 }
1515
1516 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1517 finderArgs, count);
1518
1519 closeSession(session);
1520 }
1521 }
1522
1523 return count.intValue();
1524 }
1525
1526
1533 public int countByQuestionId(long questionId) throws SystemException {
1534 Object[] finderArgs = new Object[] { questionId };
1535
1536 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1537 finderArgs, this);
1538
1539 if (count == null) {
1540 Session session = null;
1541
1542 try {
1543 session = openSession();
1544
1545 StringBundler query = new StringBundler(2);
1546
1547 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1548
1549 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1550
1551 String sql = query.toString();
1552
1553 Query q = session.createQuery(sql);
1554
1555 QueryPos qPos = QueryPos.getInstance(q);
1556
1557 qPos.add(questionId);
1558
1559 count = (Long)q.uniqueResult();
1560 }
1561 catch (Exception e) {
1562 throw processException(e);
1563 }
1564 finally {
1565 if (count == null) {
1566 count = Long.valueOf(0);
1567 }
1568
1569 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1570 finderArgs, count);
1571
1572 closeSession(session);
1573 }
1574 }
1575
1576 return count.intValue();
1577 }
1578
1579
1587 public int countByQ_N(long questionId, String name)
1588 throws SystemException {
1589 Object[] finderArgs = new Object[] { questionId, name };
1590
1591 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_N,
1592 finderArgs, this);
1593
1594 if (count == null) {
1595 Session session = null;
1596
1597 try {
1598 session = openSession();
1599
1600 StringBundler query = new StringBundler(3);
1601
1602 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1603
1604 query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
1605
1606 if (name == null) {
1607 query.append(_FINDER_COLUMN_Q_N_NAME_1);
1608 }
1609 else {
1610 if (name.equals(StringPool.BLANK)) {
1611 query.append(_FINDER_COLUMN_Q_N_NAME_3);
1612 }
1613 else {
1614 query.append(_FINDER_COLUMN_Q_N_NAME_2);
1615 }
1616 }
1617
1618 String sql = query.toString();
1619
1620 Query q = session.createQuery(sql);
1621
1622 QueryPos qPos = QueryPos.getInstance(q);
1623
1624 qPos.add(questionId);
1625
1626 if (name != null) {
1627 qPos.add(name);
1628 }
1629
1630 count = (Long)q.uniqueResult();
1631 }
1632 catch (Exception e) {
1633 throw processException(e);
1634 }
1635 finally {
1636 if (count == null) {
1637 count = Long.valueOf(0);
1638 }
1639
1640 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_N, finderArgs,
1641 count);
1642
1643 closeSession(session);
1644 }
1645 }
1646
1647 return count.intValue();
1648 }
1649
1650
1656 public int countAll() throws SystemException {
1657 Object[] finderArgs = new Object[0];
1658
1659 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1660 finderArgs, this);
1661
1662 if (count == null) {
1663 Session session = null;
1664
1665 try {
1666 session = openSession();
1667
1668 Query q = session.createQuery(_SQL_COUNT_POLLSCHOICE);
1669
1670 count = (Long)q.uniqueResult();
1671 }
1672 catch (Exception e) {
1673 throw processException(e);
1674 }
1675 finally {
1676 if (count == null) {
1677 count = Long.valueOf(0);
1678 }
1679
1680 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1681 count);
1682
1683 closeSession(session);
1684 }
1685 }
1686
1687 return count.intValue();
1688 }
1689
1690
1693 public void afterPropertiesSet() {
1694 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1695 com.liferay.portal.util.PropsUtil.get(
1696 "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1697
1698 if (listenerClassNames.length > 0) {
1699 try {
1700 List<ModelListener<PollsChoice>> listenersList = new ArrayList<ModelListener<PollsChoice>>();
1701
1702 for (String listenerClassName : listenerClassNames) {
1703 listenersList.add((ModelListener<PollsChoice>)InstanceFactory.newInstance(
1704 listenerClassName));
1705 }
1706
1707 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1708 }
1709 catch (Exception e) {
1710 _log.error(e);
1711 }
1712 }
1713 }
1714
1715 public void destroy() {
1716 EntityCacheUtil.removeCache(PollsChoiceImpl.class.getName());
1717 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1718 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1719 }
1720
1721 @BeanReference(type = PollsChoicePersistence.class)
1722 protected PollsChoicePersistence pollsChoicePersistence;
1723 @BeanReference(type = PollsQuestionPersistence.class)
1724 protected PollsQuestionPersistence pollsQuestionPersistence;
1725 @BeanReference(type = PollsVotePersistence.class)
1726 protected PollsVotePersistence pollsVotePersistence;
1727 @BeanReference(type = ResourcePersistence.class)
1728 protected ResourcePersistence resourcePersistence;
1729 @BeanReference(type = UserPersistence.class)
1730 protected UserPersistence userPersistence;
1731 private static final String _SQL_SELECT_POLLSCHOICE = "SELECT pollsChoice FROM PollsChoice pollsChoice";
1732 private static final String _SQL_SELECT_POLLSCHOICE_WHERE = "SELECT pollsChoice FROM PollsChoice pollsChoice WHERE ";
1733 private static final String _SQL_COUNT_POLLSCHOICE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice";
1734 private static final String _SQL_COUNT_POLLSCHOICE_WHERE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice WHERE ";
1735 private static final String _FINDER_COLUMN_UUID_UUID_1 = "pollsChoice.uuid IS NULL";
1736 private static final String _FINDER_COLUMN_UUID_UUID_2 = "pollsChoice.uuid = ?";
1737 private static final String _FINDER_COLUMN_UUID_UUID_3 = "(pollsChoice.uuid IS NULL OR pollsChoice.uuid = ?)";
1738 private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsChoice.questionId = ?";
1739 private static final String _FINDER_COLUMN_Q_N_QUESTIONID_2 = "pollsChoice.questionId = ? AND ";
1740 private static final String _FINDER_COLUMN_Q_N_NAME_1 = "pollsChoice.name IS NULL";
1741 private static final String _FINDER_COLUMN_Q_N_NAME_2 = "pollsChoice.name = ?";
1742 private static final String _FINDER_COLUMN_Q_N_NAME_3 = "(pollsChoice.name IS NULL OR pollsChoice.name = ?)";
1743 private static final String _ORDER_BY_ENTITY_ALIAS = "pollsChoice.";
1744 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsChoice exists with the primary key ";
1745 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsChoice exists with the key {";
1746 private static Log _log = LogFactoryUtil.getLog(PollsChoicePersistenceImpl.class);
1747 }