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