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