1
22
23 package com.liferay.portlet.polls.service.persistence;
24
25 import com.liferay.portal.NoSuchModelException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.annotation.BeanReference;
28 import com.liferay.portal.kernel.cache.CacheRegistry;
29 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32 import com.liferay.portal.kernel.dao.orm.FinderPath;
33 import com.liferay.portal.kernel.dao.orm.Query;
34 import com.liferay.portal.kernel.dao.orm.QueryPos;
35 import com.liferay.portal.kernel.dao.orm.QueryUtil;
36 import com.liferay.portal.kernel.dao.orm.Session;
37 import com.liferay.portal.kernel.log.Log;
38 import com.liferay.portal.kernel.log.LogFactoryUtil;
39 import com.liferay.portal.kernel.util.GetterUtil;
40 import com.liferay.portal.kernel.util.OrderByComparator;
41 import com.liferay.portal.kernel.util.StringBundler;
42 import com.liferay.portal.kernel.util.StringPool;
43 import com.liferay.portal.kernel.util.StringUtil;
44 import com.liferay.portal.kernel.util.Validator;
45 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
46 import com.liferay.portal.model.ModelListener;
47 import com.liferay.portal.service.persistence.BatchSessionUtil;
48 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
49
50 import com.liferay.portlet.polls.NoSuchChoiceException;
51 import com.liferay.portlet.polls.model.PollsChoice;
52 import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
53 import com.liferay.portlet.polls.model.impl.PollsChoiceModelImpl;
54
55 import java.io.Serializable;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.List;
60
61
74 public class PollsChoicePersistenceImpl extends BasePersistenceImpl<PollsChoice>
75 implements PollsChoicePersistence {
76 public static final String FINDER_CLASS_NAME_ENTITY = PollsChoiceImpl.class.getName();
77 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
78 ".List";
79 public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
80 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81 "findByUuid", new String[] { String.class.getName() });
82 public static final FinderPath FINDER_PATH_FIND_BY_OBC_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
83 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
84 "findByUuid",
85 new String[] {
86 String.class.getName(),
87
88 "java.lang.Integer", "java.lang.Integer",
89 "com.liferay.portal.kernel.util.OrderByComparator"
90 });
91 public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
92 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
93 "countByUuid", new String[] { String.class.getName() });
94 public static final FinderPath FINDER_PATH_FIND_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
95 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
96 "findByQuestionId", new String[] { Long.class.getName() });
97 public static final FinderPath FINDER_PATH_FIND_BY_OBC_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
98 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
99 "findByQuestionId",
100 new String[] {
101 Long.class.getName(),
102
103 "java.lang.Integer", "java.lang.Integer",
104 "com.liferay.portal.kernel.util.OrderByComparator"
105 });
106 public static final FinderPath FINDER_PATH_COUNT_BY_QUESTIONID = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
107 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
108 "countByQuestionId", new String[] { Long.class.getName() });
109 public static final FinderPath FINDER_PATH_FETCH_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
110 PollsChoiceModelImpl.FINDER_CACHE_ENABLED,
111 FINDER_CLASS_NAME_ENTITY, "fetchByQ_N",
112 new String[] { Long.class.getName(), String.class.getName() });
113 public static final FinderPath FINDER_PATH_COUNT_BY_Q_N = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
114 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
115 "countByQ_N",
116 new String[] { Long.class.getName(), String.class.getName() });
117 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
118 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
119 "findAll", new String[0]);
120 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
121 PollsChoiceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
122 "countAll", new String[0]);
123
124 public void cacheResult(PollsChoice pollsChoice) {
125 EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
126 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
127
128 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
129 new Object[] {
130 new Long(pollsChoice.getQuestionId()),
131
132 pollsChoice.getName()
133 }, pollsChoice);
134 }
135
136 public void cacheResult(List<PollsChoice> pollsChoices) {
137 for (PollsChoice pollsChoice : pollsChoices) {
138 if (EntityCacheUtil.getResult(
139 PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
140 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), this) == null) {
141 cacheResult(pollsChoice);
142 }
143 }
144 }
145
146 public void clearCache() {
147 CacheRegistry.clear(PollsChoiceImpl.class.getName());
148 EntityCacheUtil.clearCache(PollsChoiceImpl.class.getName());
149 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
150 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
151 }
152
153 public PollsChoice create(long choiceId) {
154 PollsChoice pollsChoice = new PollsChoiceImpl();
155
156 pollsChoice.setNew(true);
157 pollsChoice.setPrimaryKey(choiceId);
158
159 String uuid = PortalUUIDUtil.generate();
160
161 pollsChoice.setUuid(uuid);
162
163 return pollsChoice;
164 }
165
166 public PollsChoice remove(Serializable primaryKey)
167 throws NoSuchModelException, SystemException {
168 return remove(((Long)primaryKey).longValue());
169 }
170
171 public PollsChoice remove(long choiceId)
172 throws NoSuchChoiceException, SystemException {
173 Session session = null;
174
175 try {
176 session = openSession();
177
178 PollsChoice pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
179 new Long(choiceId));
180
181 if (pollsChoice == null) {
182 if (_log.isWarnEnabled()) {
183 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
184 }
185
186 throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
187 choiceId);
188 }
189
190 return remove(pollsChoice);
191 }
192 catch (NoSuchChoiceException nsee) {
193 throw nsee;
194 }
195 catch (Exception e) {
196 throw processException(e);
197 }
198 finally {
199 closeSession(session);
200 }
201 }
202
203 public PollsChoice remove(PollsChoice pollsChoice)
204 throws SystemException {
205 for (ModelListener<PollsChoice> listener : listeners) {
206 listener.onBeforeRemove(pollsChoice);
207 }
208
209 pollsChoice = removeImpl(pollsChoice);
210
211 for (ModelListener<PollsChoice> listener : listeners) {
212 listener.onAfterRemove(pollsChoice);
213 }
214
215 return pollsChoice;
216 }
217
218 protected PollsChoice removeImpl(PollsChoice pollsChoice)
219 throws SystemException {
220 pollsChoice = toUnwrappedModel(pollsChoice);
221
222 Session session = null;
223
224 try {
225 session = openSession();
226
227 if (pollsChoice.isCachedModel() || BatchSessionUtil.isEnabled()) {
228 Object staleObject = session.get(PollsChoiceImpl.class,
229 pollsChoice.getPrimaryKeyObj());
230
231 if (staleObject != null) {
232 session.evict(staleObject);
233 }
234 }
235
236 session.delete(pollsChoice);
237
238 session.flush();
239 }
240 catch (Exception e) {
241 throw processException(e);
242 }
243 finally {
244 closeSession(session);
245 }
246
247 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
248
249 PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
250
251 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
252 new Object[] {
253 new Long(pollsChoiceModelImpl.getOriginalQuestionId()),
254
255 pollsChoiceModelImpl.getOriginalName()
256 });
257
258 EntityCacheUtil.removeResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
259 PollsChoiceImpl.class, pollsChoice.getPrimaryKey());
260
261 return pollsChoice;
262 }
263
264
267 public PollsChoice update(PollsChoice pollsChoice)
268 throws SystemException {
269 if (_log.isWarnEnabled()) {
270 _log.warn(
271 "Using the deprecated update(PollsChoice pollsChoice) method. Use update(PollsChoice pollsChoice, boolean merge) instead.");
272 }
273
274 return update(pollsChoice, false);
275 }
276
277 public PollsChoice updateImpl(
278 com.liferay.portlet.polls.model.PollsChoice pollsChoice, boolean merge)
279 throws SystemException {
280 pollsChoice = toUnwrappedModel(pollsChoice);
281
282 boolean isNew = pollsChoice.isNew();
283
284 PollsChoiceModelImpl pollsChoiceModelImpl = (PollsChoiceModelImpl)pollsChoice;
285
286 if (Validator.isNull(pollsChoice.getUuid())) {
287 String uuid = PortalUUIDUtil.generate();
288
289 pollsChoice.setUuid(uuid);
290 }
291
292 Session session = null;
293
294 try {
295 session = openSession();
296
297 BatchSessionUtil.update(session, pollsChoice, merge);
298
299 pollsChoice.setNew(false);
300 }
301 catch (Exception e) {
302 throw processException(e);
303 }
304 finally {
305 closeSession(session);
306 }
307
308 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
309
310 EntityCacheUtil.putResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
311 PollsChoiceImpl.class, pollsChoice.getPrimaryKey(), pollsChoice);
312
313 if (!isNew &&
314 ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
315 !Validator.equals(pollsChoice.getName(),
316 pollsChoiceModelImpl.getOriginalName()))) {
317 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_Q_N,
318 new Object[] {
319 new Long(pollsChoiceModelImpl.getOriginalQuestionId()),
320
321 pollsChoiceModelImpl.getOriginalName()
322 });
323 }
324
325 if (isNew ||
326 ((pollsChoice.getQuestionId() != pollsChoiceModelImpl.getOriginalQuestionId()) ||
327 !Validator.equals(pollsChoice.getName(),
328 pollsChoiceModelImpl.getOriginalName()))) {
329 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
330 new Object[] {
331 new Long(pollsChoice.getQuestionId()),
332
333 pollsChoice.getName()
334 }, pollsChoice);
335 }
336
337 return pollsChoice;
338 }
339
340 protected PollsChoice toUnwrappedModel(PollsChoice pollsChoice) {
341 if (pollsChoice instanceof PollsChoiceImpl) {
342 return pollsChoice;
343 }
344
345 PollsChoiceImpl pollsChoiceImpl = new PollsChoiceImpl();
346
347 pollsChoiceImpl.setNew(pollsChoice.isNew());
348 pollsChoiceImpl.setPrimaryKey(pollsChoice.getPrimaryKey());
349
350 pollsChoiceImpl.setUuid(pollsChoice.getUuid());
351 pollsChoiceImpl.setChoiceId(pollsChoice.getChoiceId());
352 pollsChoiceImpl.setQuestionId(pollsChoice.getQuestionId());
353 pollsChoiceImpl.setName(pollsChoice.getName());
354 pollsChoiceImpl.setDescription(pollsChoice.getDescription());
355
356 return pollsChoiceImpl;
357 }
358
359 public PollsChoice findByPrimaryKey(Serializable primaryKey)
360 throws NoSuchModelException, SystemException {
361 return findByPrimaryKey(((Long)primaryKey).longValue());
362 }
363
364 public PollsChoice findByPrimaryKey(long choiceId)
365 throws NoSuchChoiceException, SystemException {
366 PollsChoice pollsChoice = fetchByPrimaryKey(choiceId);
367
368 if (pollsChoice == null) {
369 if (_log.isWarnEnabled()) {
370 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + choiceId);
371 }
372
373 throw new NoSuchChoiceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
374 choiceId);
375 }
376
377 return pollsChoice;
378 }
379
380 public PollsChoice fetchByPrimaryKey(Serializable primaryKey)
381 throws SystemException {
382 return fetchByPrimaryKey(((Long)primaryKey).longValue());
383 }
384
385 public PollsChoice fetchByPrimaryKey(long choiceId)
386 throws SystemException {
387 PollsChoice pollsChoice = (PollsChoice)EntityCacheUtil.getResult(PollsChoiceModelImpl.ENTITY_CACHE_ENABLED,
388 PollsChoiceImpl.class, choiceId, this);
389
390 if (pollsChoice == null) {
391 Session session = null;
392
393 try {
394 session = openSession();
395
396 pollsChoice = (PollsChoice)session.get(PollsChoiceImpl.class,
397 new Long(choiceId));
398 }
399 catch (Exception e) {
400 throw processException(e);
401 }
402 finally {
403 if (pollsChoice != null) {
404 cacheResult(pollsChoice);
405 }
406
407 closeSession(session);
408 }
409 }
410
411 return pollsChoice;
412 }
413
414 public List<PollsChoice> findByUuid(String uuid) throws SystemException {
415 Object[] finderArgs = new Object[] { uuid };
416
417 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
418 finderArgs, this);
419
420 if (list == null) {
421 Session session = null;
422
423 try {
424 session = openSession();
425
426 StringBundler query = new StringBundler(3);
427
428 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
429
430 if (uuid == null) {
431 query.append(_FINDER_COLUMN_UUID_UUID_1);
432 }
433 else {
434 if (uuid.equals(StringPool.BLANK)) {
435 query.append(_FINDER_COLUMN_UUID_UUID_3);
436 }
437 else {
438 query.append(_FINDER_COLUMN_UUID_UUID_2);
439 }
440 }
441
442 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
443
444 String sql = query.toString();
445
446 Query q = session.createQuery(sql);
447
448 QueryPos qPos = QueryPos.getInstance(q);
449
450 if (uuid != null) {
451 qPos.add(uuid);
452 }
453
454 list = q.list();
455 }
456 catch (Exception e) {
457 throw processException(e);
458 }
459 finally {
460 if (list == null) {
461 list = new ArrayList<PollsChoice>();
462 }
463
464 cacheResult(list);
465
466 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID, finderArgs,
467 list);
468
469 closeSession(session);
470 }
471 }
472
473 return list;
474 }
475
476 public List<PollsChoice> findByUuid(String uuid, int start, int end)
477 throws SystemException {
478 return findByUuid(uuid, start, end, null);
479 }
480
481 public List<PollsChoice> findByUuid(String uuid, int start, int end,
482 OrderByComparator obc) throws SystemException {
483 Object[] finderArgs = new Object[] {
484 uuid,
485
486 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
487 };
488
489 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_UUID,
490 finderArgs, this);
491
492 if (list == null) {
493 Session session = null;
494
495 try {
496 session = openSession();
497
498 StringBundler query = null;
499
500 if (obc != null) {
501 query = new StringBundler(3 +
502 (obc.getOrderByFields().length * 3));
503 }
504 else {
505 query = new StringBundler(3);
506 }
507
508 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
509
510 if (uuid == null) {
511 query.append(_FINDER_COLUMN_UUID_UUID_1);
512 }
513 else {
514 if (uuid.equals(StringPool.BLANK)) {
515 query.append(_FINDER_COLUMN_UUID_UUID_3);
516 }
517 else {
518 query.append(_FINDER_COLUMN_UUID_UUID_2);
519 }
520 }
521
522 if (obc != null) {
523 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
524 }
525
526 else {
527 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
528 }
529
530 String sql = query.toString();
531
532 Query q = session.createQuery(sql);
533
534 QueryPos qPos = QueryPos.getInstance(q);
535
536 if (uuid != null) {
537 qPos.add(uuid);
538 }
539
540 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
541 start, end);
542 }
543 catch (Exception e) {
544 throw processException(e);
545 }
546 finally {
547 if (list == null) {
548 list = new ArrayList<PollsChoice>();
549 }
550
551 cacheResult(list);
552
553 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_UUID,
554 finderArgs, list);
555
556 closeSession(session);
557 }
558 }
559
560 return list;
561 }
562
563 public PollsChoice findByUuid_First(String uuid, OrderByComparator obc)
564 throws NoSuchChoiceException, SystemException {
565 List<PollsChoice> list = findByUuid(uuid, 0, 1, obc);
566
567 if (list.isEmpty()) {
568 StringBundler msg = new StringBundler(4);
569
570 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
571
572 msg.append("uuid=");
573 msg.append(uuid);
574
575 msg.append(StringPool.CLOSE_CURLY_BRACE);
576
577 throw new NoSuchChoiceException(msg.toString());
578 }
579 else {
580 return list.get(0);
581 }
582 }
583
584 public PollsChoice findByUuid_Last(String uuid, OrderByComparator obc)
585 throws NoSuchChoiceException, SystemException {
586 int count = countByUuid(uuid);
587
588 List<PollsChoice> list = findByUuid(uuid, count - 1, count, obc);
589
590 if (list.isEmpty()) {
591 StringBundler msg = new StringBundler(4);
592
593 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
594
595 msg.append("uuid=");
596 msg.append(uuid);
597
598 msg.append(StringPool.CLOSE_CURLY_BRACE);
599
600 throw new NoSuchChoiceException(msg.toString());
601 }
602 else {
603 return list.get(0);
604 }
605 }
606
607 public PollsChoice[] findByUuid_PrevAndNext(long choiceId, String uuid,
608 OrderByComparator obc) throws NoSuchChoiceException, SystemException {
609 PollsChoice pollsChoice = findByPrimaryKey(choiceId);
610
611 int count = countByUuid(uuid);
612
613 Session session = null;
614
615 try {
616 session = openSession();
617
618 StringBundler query = null;
619
620 if (obc != null) {
621 query = new StringBundler(3 +
622 (obc.getOrderByFields().length * 3));
623 }
624 else {
625 query = new StringBundler(3);
626 }
627
628 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
629
630 if (uuid == null) {
631 query.append(_FINDER_COLUMN_UUID_UUID_1);
632 }
633 else {
634 if (uuid.equals(StringPool.BLANK)) {
635 query.append(_FINDER_COLUMN_UUID_UUID_3);
636 }
637 else {
638 query.append(_FINDER_COLUMN_UUID_UUID_2);
639 }
640 }
641
642 if (obc != null) {
643 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
644 }
645
646 else {
647 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
648 }
649
650 String sql = query.toString();
651
652 Query q = session.createQuery(sql);
653
654 QueryPos qPos = QueryPos.getInstance(q);
655
656 if (uuid != null) {
657 qPos.add(uuid);
658 }
659
660 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
661 pollsChoice);
662
663 PollsChoice[] array = new PollsChoiceImpl[3];
664
665 array[0] = (PollsChoice)objArray[0];
666 array[1] = (PollsChoice)objArray[1];
667 array[2] = (PollsChoice)objArray[2];
668
669 return array;
670 }
671 catch (Exception e) {
672 throw processException(e);
673 }
674 finally {
675 closeSession(session);
676 }
677 }
678
679 public List<PollsChoice> findByQuestionId(long questionId)
680 throws SystemException {
681 Object[] finderArgs = new Object[] { new Long(questionId) };
682
683 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_QUESTIONID,
684 finderArgs, this);
685
686 if (list == null) {
687 Session session = null;
688
689 try {
690 session = openSession();
691
692 StringBundler query = new StringBundler(3);
693
694 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
695
696 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
697
698 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
699
700 String sql = query.toString();
701
702 Query q = session.createQuery(sql);
703
704 QueryPos qPos = QueryPos.getInstance(q);
705
706 qPos.add(questionId);
707
708 list = q.list();
709 }
710 catch (Exception e) {
711 throw processException(e);
712 }
713 finally {
714 if (list == null) {
715 list = new ArrayList<PollsChoice>();
716 }
717
718 cacheResult(list);
719
720 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_QUESTIONID,
721 finderArgs, list);
722
723 closeSession(session);
724 }
725 }
726
727 return list;
728 }
729
730 public List<PollsChoice> findByQuestionId(long questionId, int start,
731 int end) throws SystemException {
732 return findByQuestionId(questionId, start, end, null);
733 }
734
735 public List<PollsChoice> findByQuestionId(long questionId, int start,
736 int end, OrderByComparator obc) throws SystemException {
737 Object[] finderArgs = new Object[] {
738 new Long(questionId),
739
740 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
741 };
742
743 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
744 finderArgs, this);
745
746 if (list == null) {
747 Session session = null;
748
749 try {
750 session = openSession();
751
752 StringBundler query = null;
753
754 if (obc != null) {
755 query = new StringBundler(3 +
756 (obc.getOrderByFields().length * 3));
757 }
758 else {
759 query = new StringBundler(3);
760 }
761
762 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
763
764 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
765
766 if (obc != null) {
767 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
768 }
769
770 else {
771 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
772 }
773
774 String sql = query.toString();
775
776 Query q = session.createQuery(sql);
777
778 QueryPos qPos = QueryPos.getInstance(q);
779
780 qPos.add(questionId);
781
782 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
783 start, end);
784 }
785 catch (Exception e) {
786 throw processException(e);
787 }
788 finally {
789 if (list == null) {
790 list = new ArrayList<PollsChoice>();
791 }
792
793 cacheResult(list);
794
795 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_QUESTIONID,
796 finderArgs, list);
797
798 closeSession(session);
799 }
800 }
801
802 return list;
803 }
804
805 public PollsChoice findByQuestionId_First(long questionId,
806 OrderByComparator obc) throws NoSuchChoiceException, SystemException {
807 List<PollsChoice> list = findByQuestionId(questionId, 0, 1, obc);
808
809 if (list.isEmpty()) {
810 StringBundler msg = new StringBundler(4);
811
812 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
813
814 msg.append("questionId=");
815 msg.append(questionId);
816
817 msg.append(StringPool.CLOSE_CURLY_BRACE);
818
819 throw new NoSuchChoiceException(msg.toString());
820 }
821 else {
822 return list.get(0);
823 }
824 }
825
826 public PollsChoice findByQuestionId_Last(long questionId,
827 OrderByComparator obc) throws NoSuchChoiceException, SystemException {
828 int count = countByQuestionId(questionId);
829
830 List<PollsChoice> list = findByQuestionId(questionId, count - 1, count,
831 obc);
832
833 if (list.isEmpty()) {
834 StringBundler msg = new StringBundler(4);
835
836 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
837
838 msg.append("questionId=");
839 msg.append(questionId);
840
841 msg.append(StringPool.CLOSE_CURLY_BRACE);
842
843 throw new NoSuchChoiceException(msg.toString());
844 }
845 else {
846 return list.get(0);
847 }
848 }
849
850 public PollsChoice[] findByQuestionId_PrevAndNext(long choiceId,
851 long questionId, OrderByComparator obc)
852 throws NoSuchChoiceException, SystemException {
853 PollsChoice pollsChoice = findByPrimaryKey(choiceId);
854
855 int count = countByQuestionId(questionId);
856
857 Session session = null;
858
859 try {
860 session = openSession();
861
862 StringBundler query = null;
863
864 if (obc != null) {
865 query = new StringBundler(3 +
866 (obc.getOrderByFields().length * 3));
867 }
868 else {
869 query = new StringBundler(3);
870 }
871
872 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
873
874 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
875
876 if (obc != null) {
877 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
878 }
879
880 else {
881 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
882 }
883
884 String sql = query.toString();
885
886 Query q = session.createQuery(sql);
887
888 QueryPos qPos = QueryPos.getInstance(q);
889
890 qPos.add(questionId);
891
892 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
893 pollsChoice);
894
895 PollsChoice[] array = new PollsChoiceImpl[3];
896
897 array[0] = (PollsChoice)objArray[0];
898 array[1] = (PollsChoice)objArray[1];
899 array[2] = (PollsChoice)objArray[2];
900
901 return array;
902 }
903 catch (Exception e) {
904 throw processException(e);
905 }
906 finally {
907 closeSession(session);
908 }
909 }
910
911 public PollsChoice findByQ_N(long questionId, String name)
912 throws NoSuchChoiceException, SystemException {
913 PollsChoice pollsChoice = fetchByQ_N(questionId, name);
914
915 if (pollsChoice == null) {
916 StringBundler msg = new StringBundler(6);
917
918 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
919
920 msg.append("questionId=");
921 msg.append(questionId);
922
923 msg.append(", name=");
924 msg.append(name);
925
926 msg.append(StringPool.CLOSE_CURLY_BRACE);
927
928 if (_log.isWarnEnabled()) {
929 _log.warn(msg.toString());
930 }
931
932 throw new NoSuchChoiceException(msg.toString());
933 }
934
935 return pollsChoice;
936 }
937
938 public PollsChoice fetchByQ_N(long questionId, String name)
939 throws SystemException {
940 return fetchByQ_N(questionId, name, true);
941 }
942
943 public PollsChoice fetchByQ_N(long questionId, String name,
944 boolean retrieveFromCache) throws SystemException {
945 Object[] finderArgs = new Object[] { new Long(questionId), name };
946
947 Object result = null;
948
949 if (retrieveFromCache) {
950 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_Q_N,
951 finderArgs, this);
952 }
953
954 if (result == null) {
955 Session session = null;
956
957 try {
958 session = openSession();
959
960 StringBundler query = new StringBundler(4);
961
962 query.append(_SQL_SELECT_POLLSCHOICE_WHERE);
963
964 query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
965
966 if (name == null) {
967 query.append(_FINDER_COLUMN_Q_N_NAME_1);
968 }
969 else {
970 if (name.equals(StringPool.BLANK)) {
971 query.append(_FINDER_COLUMN_Q_N_NAME_3);
972 }
973 else {
974 query.append(_FINDER_COLUMN_Q_N_NAME_2);
975 }
976 }
977
978 query.append(PollsChoiceModelImpl.ORDER_BY_JPQL);
979
980 String sql = query.toString();
981
982 Query q = session.createQuery(sql);
983
984 QueryPos qPos = QueryPos.getInstance(q);
985
986 qPos.add(questionId);
987
988 if (name != null) {
989 qPos.add(name);
990 }
991
992 List<PollsChoice> list = q.list();
993
994 result = list;
995
996 PollsChoice pollsChoice = null;
997
998 if (list.isEmpty()) {
999 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1000 finderArgs, list);
1001 }
1002 else {
1003 pollsChoice = list.get(0);
1004
1005 cacheResult(pollsChoice);
1006
1007 if ((pollsChoice.getQuestionId() != questionId) ||
1008 (pollsChoice.getName() == null) ||
1009 !pollsChoice.getName().equals(name)) {
1010 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1011 finderArgs, pollsChoice);
1012 }
1013 }
1014
1015 return pollsChoice;
1016 }
1017 catch (Exception e) {
1018 throw processException(e);
1019 }
1020 finally {
1021 if (result == null) {
1022 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_Q_N,
1023 finderArgs, new ArrayList<PollsChoice>());
1024 }
1025
1026 closeSession(session);
1027 }
1028 }
1029 else {
1030 if (result instanceof List<?>) {
1031 return null;
1032 }
1033 else {
1034 return (PollsChoice)result;
1035 }
1036 }
1037 }
1038
1039 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1040 throws SystemException {
1041 Session session = null;
1042
1043 try {
1044 session = openSession();
1045
1046 dynamicQuery.compile(session);
1047
1048 return dynamicQuery.list();
1049 }
1050 catch (Exception e) {
1051 throw processException(e);
1052 }
1053 finally {
1054 closeSession(session);
1055 }
1056 }
1057
1058 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1059 int start, int end) throws SystemException {
1060 Session session = null;
1061
1062 try {
1063 session = openSession();
1064
1065 dynamicQuery.setLimit(start, end);
1066
1067 dynamicQuery.compile(session);
1068
1069 return dynamicQuery.list();
1070 }
1071 catch (Exception e) {
1072 throw processException(e);
1073 }
1074 finally {
1075 closeSession(session);
1076 }
1077 }
1078
1079 public List<PollsChoice> findAll() throws SystemException {
1080 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1081 }
1082
1083 public List<PollsChoice> findAll(int start, int end)
1084 throws SystemException {
1085 return findAll(start, end, null);
1086 }
1087
1088 public List<PollsChoice> findAll(int start, int end, OrderByComparator obc)
1089 throws SystemException {
1090 Object[] finderArgs = new Object[] {
1091 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1092 };
1093
1094 List<PollsChoice> list = (List<PollsChoice>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1095 finderArgs, this);
1096
1097 if (list == null) {
1098 Session session = null;
1099
1100 try {
1101 session = openSession();
1102
1103 StringBundler query = null;
1104 String sql = null;
1105
1106 if (obc != null) {
1107 query = new StringBundler(2 +
1108 (obc.getOrderByFields().length * 3));
1109
1110 query.append(_SQL_SELECT_POLLSCHOICE);
1111
1112 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1113
1114 sql = query.toString();
1115 }
1116
1117 else {
1118 sql = _SQL_SELECT_POLLSCHOICE.concat(PollsChoiceModelImpl.ORDER_BY_JPQL);
1119 }
1120
1121 Query q = session.createQuery(sql);
1122
1123 if (obc == null) {
1124 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1125 start, end, false);
1126
1127 Collections.sort(list);
1128 }
1129 else {
1130 list = (List<PollsChoice>)QueryUtil.list(q, getDialect(),
1131 start, end);
1132 }
1133 }
1134 catch (Exception e) {
1135 throw processException(e);
1136 }
1137 finally {
1138 if (list == null) {
1139 list = new ArrayList<PollsChoice>();
1140 }
1141
1142 cacheResult(list);
1143
1144 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1145
1146 closeSession(session);
1147 }
1148 }
1149
1150 return list;
1151 }
1152
1153 public void removeByUuid(String uuid) throws SystemException {
1154 for (PollsChoice pollsChoice : findByUuid(uuid)) {
1155 remove(pollsChoice);
1156 }
1157 }
1158
1159 public void removeByQuestionId(long questionId) throws SystemException {
1160 for (PollsChoice pollsChoice : findByQuestionId(questionId)) {
1161 remove(pollsChoice);
1162 }
1163 }
1164
1165 public void removeByQ_N(long questionId, String name)
1166 throws NoSuchChoiceException, SystemException {
1167 PollsChoice pollsChoice = findByQ_N(questionId, name);
1168
1169 remove(pollsChoice);
1170 }
1171
1172 public void removeAll() throws SystemException {
1173 for (PollsChoice pollsChoice : findAll()) {
1174 remove(pollsChoice);
1175 }
1176 }
1177
1178 public int countByUuid(String uuid) throws SystemException {
1179 Object[] finderArgs = new Object[] { uuid };
1180
1181 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1182 finderArgs, this);
1183
1184 if (count == null) {
1185 Session session = null;
1186
1187 try {
1188 session = openSession();
1189
1190 StringBundler query = new StringBundler(2);
1191
1192 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1193
1194 if (uuid == null) {
1195 query.append(_FINDER_COLUMN_UUID_UUID_1);
1196 }
1197 else {
1198 if (uuid.equals(StringPool.BLANK)) {
1199 query.append(_FINDER_COLUMN_UUID_UUID_3);
1200 }
1201 else {
1202 query.append(_FINDER_COLUMN_UUID_UUID_2);
1203 }
1204 }
1205
1206 String sql = query.toString();
1207
1208 Query q = session.createQuery(sql);
1209
1210 QueryPos qPos = QueryPos.getInstance(q);
1211
1212 if (uuid != null) {
1213 qPos.add(uuid);
1214 }
1215
1216 count = (Long)q.uniqueResult();
1217 }
1218 catch (Exception e) {
1219 throw processException(e);
1220 }
1221 finally {
1222 if (count == null) {
1223 count = Long.valueOf(0);
1224 }
1225
1226 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1227 finderArgs, count);
1228
1229 closeSession(session);
1230 }
1231 }
1232
1233 return count.intValue();
1234 }
1235
1236 public int countByQuestionId(long questionId) throws SystemException {
1237 Object[] finderArgs = new Object[] { new Long(questionId) };
1238
1239 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1240 finderArgs, this);
1241
1242 if (count == null) {
1243 Session session = null;
1244
1245 try {
1246 session = openSession();
1247
1248 StringBundler query = new StringBundler(2);
1249
1250 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1251
1252 query.append(_FINDER_COLUMN_QUESTIONID_QUESTIONID_2);
1253
1254 String sql = query.toString();
1255
1256 Query q = session.createQuery(sql);
1257
1258 QueryPos qPos = QueryPos.getInstance(q);
1259
1260 qPos.add(questionId);
1261
1262 count = (Long)q.uniqueResult();
1263 }
1264 catch (Exception e) {
1265 throw processException(e);
1266 }
1267 finally {
1268 if (count == null) {
1269 count = Long.valueOf(0);
1270 }
1271
1272 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_QUESTIONID,
1273 finderArgs, count);
1274
1275 closeSession(session);
1276 }
1277 }
1278
1279 return count.intValue();
1280 }
1281
1282 public int countByQ_N(long questionId, String name)
1283 throws SystemException {
1284 Object[] finderArgs = new Object[] { new Long(questionId), name };
1285
1286 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_Q_N,
1287 finderArgs, this);
1288
1289 if (count == null) {
1290 Session session = null;
1291
1292 try {
1293 session = openSession();
1294
1295 StringBundler query = new StringBundler(3);
1296
1297 query.append(_SQL_COUNT_POLLSCHOICE_WHERE);
1298
1299 query.append(_FINDER_COLUMN_Q_N_QUESTIONID_2);
1300
1301 if (name == null) {
1302 query.append(_FINDER_COLUMN_Q_N_NAME_1);
1303 }
1304 else {
1305 if (name.equals(StringPool.BLANK)) {
1306 query.append(_FINDER_COLUMN_Q_N_NAME_3);
1307 }
1308 else {
1309 query.append(_FINDER_COLUMN_Q_N_NAME_2);
1310 }
1311 }
1312
1313 String sql = query.toString();
1314
1315 Query q = session.createQuery(sql);
1316
1317 QueryPos qPos = QueryPos.getInstance(q);
1318
1319 qPos.add(questionId);
1320
1321 if (name != null) {
1322 qPos.add(name);
1323 }
1324
1325 count = (Long)q.uniqueResult();
1326 }
1327 catch (Exception e) {
1328 throw processException(e);
1329 }
1330 finally {
1331 if (count == null) {
1332 count = Long.valueOf(0);
1333 }
1334
1335 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_Q_N, finderArgs,
1336 count);
1337
1338 closeSession(session);
1339 }
1340 }
1341
1342 return count.intValue();
1343 }
1344
1345 public int countAll() throws SystemException {
1346 Object[] finderArgs = new Object[0];
1347
1348 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1349 finderArgs, this);
1350
1351 if (count == null) {
1352 Session session = null;
1353
1354 try {
1355 session = openSession();
1356
1357 Query q = session.createQuery(_SQL_COUNT_POLLSCHOICE);
1358
1359 count = (Long)q.uniqueResult();
1360 }
1361 catch (Exception e) {
1362 throw processException(e);
1363 }
1364 finally {
1365 if (count == null) {
1366 count = Long.valueOf(0);
1367 }
1368
1369 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1370 count);
1371
1372 closeSession(session);
1373 }
1374 }
1375
1376 return count.intValue();
1377 }
1378
1379 public void afterPropertiesSet() {
1380 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1381 com.liferay.portal.util.PropsUtil.get(
1382 "value.object.listener.com.liferay.portlet.polls.model.PollsChoice")));
1383
1384 if (listenerClassNames.length > 0) {
1385 try {
1386 List<ModelListener<PollsChoice>> listenersList = new ArrayList<ModelListener<PollsChoice>>();
1387
1388 for (String listenerClassName : listenerClassNames) {
1389 listenersList.add((ModelListener<PollsChoice>)Class.forName(
1390 listenerClassName).newInstance());
1391 }
1392
1393 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1394 }
1395 catch (Exception e) {
1396 _log.error(e);
1397 }
1398 }
1399 }
1400
1401 @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsChoicePersistence")
1402 protected com.liferay.portlet.polls.service.persistence.PollsChoicePersistence pollsChoicePersistence;
1403 @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence")
1404 protected com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence pollsQuestionPersistence;
1405 @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsVotePersistence")
1406 protected com.liferay.portlet.polls.service.persistence.PollsVotePersistence pollsVotePersistence;
1407 @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1408 protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1409 @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1410 protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1411 private static final String _SQL_SELECT_POLLSCHOICE = "SELECT pollsChoice FROM PollsChoice pollsChoice";
1412 private static final String _SQL_SELECT_POLLSCHOICE_WHERE = "SELECT pollsChoice FROM PollsChoice pollsChoice WHERE ";
1413 private static final String _SQL_COUNT_POLLSCHOICE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice";
1414 private static final String _SQL_COUNT_POLLSCHOICE_WHERE = "SELECT COUNT(pollsChoice) FROM PollsChoice pollsChoice WHERE ";
1415 private static final String _FINDER_COLUMN_UUID_UUID_1 = "pollsChoice.uuid IS NULL";
1416 private static final String _FINDER_COLUMN_UUID_UUID_2 = "pollsChoice.uuid = ?";
1417 private static final String _FINDER_COLUMN_UUID_UUID_3 = "(pollsChoice.uuid IS NULL OR pollsChoice.uuid = ?)";
1418 private static final String _FINDER_COLUMN_QUESTIONID_QUESTIONID_2 = "pollsChoice.questionId = ?";
1419 private static final String _FINDER_COLUMN_Q_N_QUESTIONID_2 = "pollsChoice.questionId = ? AND ";
1420 private static final String _FINDER_COLUMN_Q_N_NAME_1 = "pollsChoice.name IS NULL";
1421 private static final String _FINDER_COLUMN_Q_N_NAME_2 = "pollsChoice.name = ?";
1422 private static final String _FINDER_COLUMN_Q_N_NAME_3 = "(pollsChoice.name IS NULL OR pollsChoice.name = ?)";
1423 private static final String _ORDER_BY_ENTITY_ALIAS = "pollsChoice.";
1424 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PollsChoice exists with the primary key ";
1425 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PollsChoice exists with the key {";
1426 private static Log _log = LogFactoryUtil.getLog(PollsChoicePersistenceImpl.class);
1427}