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