001
014
015 package com.liferay.portlet.ratings.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.kernel.annotation.BeanReference;
019 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderPath;
023 import com.liferay.portal.kernel.dao.orm.Query;
024 import com.liferay.portal.kernel.dao.orm.QueryPos;
025 import com.liferay.portal.kernel.dao.orm.QueryUtil;
026 import com.liferay.portal.kernel.dao.orm.Session;
027 import com.liferay.portal.kernel.exception.SystemException;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.InstanceFactory;
032 import com.liferay.portal.kernel.util.OrderByComparator;
033 import com.liferay.portal.kernel.util.StringBundler;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.StringUtil;
036 import com.liferay.portal.model.ModelListener;
037 import com.liferay.portal.service.persistence.BatchSessionUtil;
038 import com.liferay.portal.service.persistence.ResourcePersistence;
039 import com.liferay.portal.service.persistence.UserPersistence;
040 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041
042 import com.liferay.portlet.ratings.NoSuchStatsException;
043 import com.liferay.portlet.ratings.model.RatingsStats;
044 import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
045 import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
046
047 import java.io.Serializable;
048
049 import java.util.ArrayList;
050 import java.util.Collections;
051 import java.util.List;
052
053
069 public class RatingsStatsPersistenceImpl extends BasePersistenceImpl<RatingsStats>
070 implements RatingsStatsPersistence {
071 public static final String FINDER_CLASS_NAME_ENTITY = RatingsStatsImpl.class.getName();
072 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
073 ".List";
074 public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
075 RatingsStatsModelImpl.FINDER_CACHE_ENABLED,
076 FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
077 new String[] { Long.class.getName(), Long.class.getName() });
078 public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
079 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
080 "countByC_C",
081 new String[] { Long.class.getName(), Long.class.getName() });
082 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
083 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084 "findAll", new String[0]);
085 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
086 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087 "countAll", new String[0]);
088
089
094 public void cacheResult(RatingsStats ratingsStats) {
095 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
096 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
097
098 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
099 new Object[] {
100 new Long(ratingsStats.getClassNameId()),
101 new Long(ratingsStats.getClassPK())
102 }, ratingsStats);
103 }
104
105
110 public void cacheResult(List<RatingsStats> ratingsStatses) {
111 for (RatingsStats ratingsStats : ratingsStatses) {
112 if (EntityCacheUtil.getResult(
113 RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
114 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(),
115 this) == null) {
116 cacheResult(ratingsStats);
117 }
118 }
119 }
120
121
128 public void clearCache() {
129 CacheRegistryUtil.clear(RatingsStatsImpl.class.getName());
130 EntityCacheUtil.clearCache(RatingsStatsImpl.class.getName());
131 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
133 }
134
135
142 public void clearCache(RatingsStats ratingsStats) {
143 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
144 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
145
146 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
147 new Object[] {
148 new Long(ratingsStats.getClassNameId()),
149 new Long(ratingsStats.getClassPK())
150 });
151 }
152
153
159 public RatingsStats create(long statsId) {
160 RatingsStats ratingsStats = new RatingsStatsImpl();
161
162 ratingsStats.setNew(true);
163 ratingsStats.setPrimaryKey(statsId);
164
165 return ratingsStats;
166 }
167
168
176 public RatingsStats remove(Serializable primaryKey)
177 throws NoSuchModelException, SystemException {
178 return remove(((Long)primaryKey).longValue());
179 }
180
181
189 public RatingsStats remove(long statsId)
190 throws NoSuchStatsException, SystemException {
191 Session session = null;
192
193 try {
194 session = openSession();
195
196 RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
197 new Long(statsId));
198
199 if (ratingsStats == null) {
200 if (_log.isWarnEnabled()) {
201 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
202 }
203
204 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
205 statsId);
206 }
207
208 return remove(ratingsStats);
209 }
210 catch (NoSuchStatsException nsee) {
211 throw nsee;
212 }
213 catch (Exception e) {
214 throw processException(e);
215 }
216 finally {
217 closeSession(session);
218 }
219 }
220
221 protected RatingsStats removeImpl(RatingsStats ratingsStats)
222 throws SystemException {
223 ratingsStats = toUnwrappedModel(ratingsStats);
224
225 Session session = null;
226
227 try {
228 session = openSession();
229
230 if (ratingsStats.isCachedModel() || BatchSessionUtil.isEnabled()) {
231 Object staleObject = session.get(RatingsStatsImpl.class,
232 ratingsStats.getPrimaryKeyObj());
233
234 if (staleObject != null) {
235 session.evict(staleObject);
236 }
237 }
238
239 session.delete(ratingsStats);
240
241 session.flush();
242 }
243 catch (Exception e) {
244 throw processException(e);
245 }
246 finally {
247 closeSession(session);
248 }
249
250 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
251
252 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
253
254 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
255 new Object[] {
256 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
257 new Long(ratingsStatsModelImpl.getOriginalClassPK())
258 });
259
260 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
261 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
262
263 return ratingsStats;
264 }
265
266 public RatingsStats updateImpl(
267 com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
268 boolean merge) throws SystemException {
269 ratingsStats = toUnwrappedModel(ratingsStats);
270
271 boolean isNew = ratingsStats.isNew();
272
273 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
274
275 Session session = null;
276
277 try {
278 session = openSession();
279
280 BatchSessionUtil.update(session, ratingsStats, merge);
281
282 ratingsStats.setNew(false);
283 }
284 catch (Exception e) {
285 throw processException(e);
286 }
287 finally {
288 closeSession(session);
289 }
290
291 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
292
293 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
294 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
295
296 if (!isNew &&
297 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
298 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
299 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
300 new Object[] {
301 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
302 new Long(ratingsStatsModelImpl.getOriginalClassPK())
303 });
304 }
305
306 if (isNew ||
307 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
308 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
309 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
310 new Object[] {
311 new Long(ratingsStats.getClassNameId()),
312 new Long(ratingsStats.getClassPK())
313 }, ratingsStats);
314 }
315
316 return ratingsStats;
317 }
318
319 protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
320 if (ratingsStats instanceof RatingsStatsImpl) {
321 return ratingsStats;
322 }
323
324 RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
325
326 ratingsStatsImpl.setNew(ratingsStats.isNew());
327 ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
328
329 ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
330 ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
331 ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
332 ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
333 ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
334 ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
335
336 return ratingsStatsImpl;
337 }
338
339
347 public RatingsStats findByPrimaryKey(Serializable primaryKey)
348 throws NoSuchModelException, SystemException {
349 return findByPrimaryKey(((Long)primaryKey).longValue());
350 }
351
352
360 public RatingsStats findByPrimaryKey(long statsId)
361 throws NoSuchStatsException, SystemException {
362 RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
363
364 if (ratingsStats == null) {
365 if (_log.isWarnEnabled()) {
366 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
367 }
368
369 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
370 statsId);
371 }
372
373 return ratingsStats;
374 }
375
376
383 public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
384 throws SystemException {
385 return fetchByPrimaryKey(((Long)primaryKey).longValue());
386 }
387
388
395 public RatingsStats fetchByPrimaryKey(long statsId)
396 throws SystemException {
397 RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
398 RatingsStatsImpl.class, statsId, this);
399
400 if (ratingsStats == null) {
401 Session session = null;
402
403 try {
404 session = openSession();
405
406 ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
407 new Long(statsId));
408 }
409 catch (Exception e) {
410 throw processException(e);
411 }
412 finally {
413 if (ratingsStats != null) {
414 cacheResult(ratingsStats);
415 }
416
417 closeSession(session);
418 }
419 }
420
421 return ratingsStats;
422 }
423
424
433 public RatingsStats findByC_C(long classNameId, long classPK)
434 throws NoSuchStatsException, SystemException {
435 RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
436
437 if (ratingsStats == null) {
438 StringBundler msg = new StringBundler(6);
439
440 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
441
442 msg.append("classNameId=");
443 msg.append(classNameId);
444
445 msg.append(", classPK=");
446 msg.append(classPK);
447
448 msg.append(StringPool.CLOSE_CURLY_BRACE);
449
450 if (_log.isWarnEnabled()) {
451 _log.warn(msg.toString());
452 }
453
454 throw new NoSuchStatsException(msg.toString());
455 }
456
457 return ratingsStats;
458 }
459
460
468 public RatingsStats fetchByC_C(long classNameId, long classPK)
469 throws SystemException {
470 return fetchByC_C(classNameId, classPK, true);
471 }
472
473
481 public RatingsStats fetchByC_C(long classNameId, long classPK,
482 boolean retrieveFromCache) throws SystemException {
483 Object[] finderArgs = new Object[] { classNameId, classPK };
484
485 Object result = null;
486
487 if (retrieveFromCache) {
488 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
489 finderArgs, this);
490 }
491
492 if (result == null) {
493 Session session = null;
494
495 try {
496 session = openSession();
497
498 StringBundler query = new StringBundler(3);
499
500 query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
501
502 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
503
504 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
505
506 String sql = query.toString();
507
508 Query q = session.createQuery(sql);
509
510 QueryPos qPos = QueryPos.getInstance(q);
511
512 qPos.add(classNameId);
513
514 qPos.add(classPK);
515
516 List<RatingsStats> list = q.list();
517
518 result = list;
519
520 RatingsStats ratingsStats = null;
521
522 if (list.isEmpty()) {
523 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
524 finderArgs, list);
525 }
526 else {
527 ratingsStats = list.get(0);
528
529 cacheResult(ratingsStats);
530
531 if ((ratingsStats.getClassNameId() != classNameId) ||
532 (ratingsStats.getClassPK() != classPK)) {
533 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
534 finderArgs, ratingsStats);
535 }
536 }
537
538 return ratingsStats;
539 }
540 catch (Exception e) {
541 throw processException(e);
542 }
543 finally {
544 if (result == null) {
545 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
546 finderArgs, new ArrayList<RatingsStats>());
547 }
548
549 closeSession(session);
550 }
551 }
552 else {
553 if (result instanceof List<?>) {
554 return null;
555 }
556 else {
557 return (RatingsStats)result;
558 }
559 }
560 }
561
562
568 public List<RatingsStats> findAll() throws SystemException {
569 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
570 }
571
572
584 public List<RatingsStats> findAll(int start, int end)
585 throws SystemException {
586 return findAll(start, end, null);
587 }
588
589
602 public List<RatingsStats> findAll(int start, int end,
603 OrderByComparator orderByComparator) throws SystemException {
604 Object[] finderArgs = new Object[] {
605 String.valueOf(start), String.valueOf(end),
606 String.valueOf(orderByComparator)
607 };
608
609 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
610 finderArgs, this);
611
612 if (list == null) {
613 Session session = null;
614
615 try {
616 session = openSession();
617
618 StringBundler query = null;
619 String sql = null;
620
621 if (orderByComparator != null) {
622 query = new StringBundler(2 +
623 (orderByComparator.getOrderByFields().length * 3));
624
625 query.append(_SQL_SELECT_RATINGSSTATS);
626
627 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
628 orderByComparator);
629
630 sql = query.toString();
631 }
632 else {
633 sql = _SQL_SELECT_RATINGSSTATS;
634 }
635
636 Query q = session.createQuery(sql);
637
638 if (orderByComparator == null) {
639 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
640 start, end, false);
641
642 Collections.sort(list);
643 }
644 else {
645 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
646 start, end);
647 }
648 }
649 catch (Exception e) {
650 throw processException(e);
651 }
652 finally {
653 if (list == null) {
654 list = new ArrayList<RatingsStats>();
655 }
656
657 cacheResult(list);
658
659 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
660
661 closeSession(session);
662 }
663 }
664
665 return list;
666 }
667
668
675 public void removeByC_C(long classNameId, long classPK)
676 throws NoSuchStatsException, SystemException {
677 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
678
679 remove(ratingsStats);
680 }
681
682
687 public void removeAll() throws SystemException {
688 for (RatingsStats ratingsStats : findAll()) {
689 remove(ratingsStats);
690 }
691 }
692
693
701 public int countByC_C(long classNameId, long classPK)
702 throws SystemException {
703 Object[] finderArgs = new Object[] { classNameId, classPK };
704
705 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
706 finderArgs, this);
707
708 if (count == null) {
709 Session session = null;
710
711 try {
712 session = openSession();
713
714 StringBundler query = new StringBundler(3);
715
716 query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
717
718 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
719
720 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
721
722 String sql = query.toString();
723
724 Query q = session.createQuery(sql);
725
726 QueryPos qPos = QueryPos.getInstance(q);
727
728 qPos.add(classNameId);
729
730 qPos.add(classPK);
731
732 count = (Long)q.uniqueResult();
733 }
734 catch (Exception e) {
735 throw processException(e);
736 }
737 finally {
738 if (count == null) {
739 count = Long.valueOf(0);
740 }
741
742 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
743 count);
744
745 closeSession(session);
746 }
747 }
748
749 return count.intValue();
750 }
751
752
758 public int countAll() throws SystemException {
759 Object[] finderArgs = new Object[0];
760
761 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
762 finderArgs, this);
763
764 if (count == null) {
765 Session session = null;
766
767 try {
768 session = openSession();
769
770 Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
771
772 count = (Long)q.uniqueResult();
773 }
774 catch (Exception e) {
775 throw processException(e);
776 }
777 finally {
778 if (count == null) {
779 count = Long.valueOf(0);
780 }
781
782 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
783 count);
784
785 closeSession(session);
786 }
787 }
788
789 return count.intValue();
790 }
791
792
795 public void afterPropertiesSet() {
796 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
797 com.liferay.portal.util.PropsUtil.get(
798 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
799
800 if (listenerClassNames.length > 0) {
801 try {
802 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
803
804 for (String listenerClassName : listenerClassNames) {
805 listenersList.add((ModelListener<RatingsStats>)InstanceFactory.newInstance(
806 listenerClassName));
807 }
808
809 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
810 }
811 catch (Exception e) {
812 _log.error(e);
813 }
814 }
815 }
816
817 @BeanReference(type = RatingsEntryPersistence.class)
818 protected RatingsEntryPersistence ratingsEntryPersistence;
819 @BeanReference(type = RatingsStatsPersistence.class)
820 protected RatingsStatsPersistence ratingsStatsPersistence;
821 @BeanReference(type = ResourcePersistence.class)
822 protected ResourcePersistence resourcePersistence;
823 @BeanReference(type = UserPersistence.class)
824 protected UserPersistence userPersistence;
825 private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
826 private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
827 private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
828 private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
829 private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
830 private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
831 private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
832 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
833 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
834 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
835 }