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