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