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 BatchSessionUtil.delete(session, ratingsStats);
231 }
232 catch (Exception e) {
233 throw processException(e);
234 }
235 finally {
236 closeSession(session);
237 }
238
239 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
240
241 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
242
243 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
244 new Object[] {
245 new Long(ratingsStatsModelImpl.getClassNameId()),
246 new Long(ratingsStatsModelImpl.getClassPK())
247 });
248
249 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
250 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
251
252 return ratingsStats;
253 }
254
255 public RatingsStats updateImpl(
256 com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
257 boolean merge) throws SystemException {
258 ratingsStats = toUnwrappedModel(ratingsStats);
259
260 boolean isNew = ratingsStats.isNew();
261
262 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
263
264 Session session = null;
265
266 try {
267 session = openSession();
268
269 BatchSessionUtil.update(session, ratingsStats, merge);
270
271 ratingsStats.setNew(false);
272 }
273 catch (Exception e) {
274 throw processException(e);
275 }
276 finally {
277 closeSession(session);
278 }
279
280 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
281
282 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
283 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
284
285 if (!isNew &&
286 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
287 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
288 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
289 new Object[] {
290 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
291 new Long(ratingsStatsModelImpl.getOriginalClassPK())
292 });
293 }
294
295 if (isNew ||
296 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
297 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
298 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
299 new Object[] {
300 new Long(ratingsStats.getClassNameId()),
301 new Long(ratingsStats.getClassPK())
302 }, ratingsStats);
303 }
304
305 return ratingsStats;
306 }
307
308 protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
309 if (ratingsStats instanceof RatingsStatsImpl) {
310 return ratingsStats;
311 }
312
313 RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
314
315 ratingsStatsImpl.setNew(ratingsStats.isNew());
316 ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
317
318 ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
319 ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
320 ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
321 ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
322 ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
323 ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
324
325 return ratingsStatsImpl;
326 }
327
328
336 public RatingsStats findByPrimaryKey(Serializable primaryKey)
337 throws NoSuchModelException, SystemException {
338 return findByPrimaryKey(((Long)primaryKey).longValue());
339 }
340
341
349 public RatingsStats findByPrimaryKey(long statsId)
350 throws NoSuchStatsException, SystemException {
351 RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
352
353 if (ratingsStats == null) {
354 if (_log.isWarnEnabled()) {
355 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
356 }
357
358 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
359 statsId);
360 }
361
362 return ratingsStats;
363 }
364
365
372 public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
373 throws SystemException {
374 return fetchByPrimaryKey(((Long)primaryKey).longValue());
375 }
376
377
384 public RatingsStats fetchByPrimaryKey(long statsId)
385 throws SystemException {
386 RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
387 RatingsStatsImpl.class, statsId, this);
388
389 if (ratingsStats == null) {
390 Session session = null;
391
392 try {
393 session = openSession();
394
395 ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
396 new Long(statsId));
397 }
398 catch (Exception e) {
399 throw processException(e);
400 }
401 finally {
402 if (ratingsStats != null) {
403 cacheResult(ratingsStats);
404 }
405
406 closeSession(session);
407 }
408 }
409
410 return ratingsStats;
411 }
412
413
422 public RatingsStats findByC_C(long classNameId, long classPK)
423 throws NoSuchStatsException, SystemException {
424 RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
425
426 if (ratingsStats == null) {
427 StringBundler msg = new StringBundler(6);
428
429 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
430
431 msg.append("classNameId=");
432 msg.append(classNameId);
433
434 msg.append(", classPK=");
435 msg.append(classPK);
436
437 msg.append(StringPool.CLOSE_CURLY_BRACE);
438
439 if (_log.isWarnEnabled()) {
440 _log.warn(msg.toString());
441 }
442
443 throw new NoSuchStatsException(msg.toString());
444 }
445
446 return ratingsStats;
447 }
448
449
457 public RatingsStats fetchByC_C(long classNameId, long classPK)
458 throws SystemException {
459 return fetchByC_C(classNameId, classPK, true);
460 }
461
462
470 public RatingsStats fetchByC_C(long classNameId, long classPK,
471 boolean retrieveFromCache) throws SystemException {
472 Object[] finderArgs = new Object[] { classNameId, classPK };
473
474 Object result = null;
475
476 if (retrieveFromCache) {
477 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
478 finderArgs, this);
479 }
480
481 if (result == null) {
482 StringBundler query = new StringBundler(3);
483
484 query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
485
486 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
487
488 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
489
490 String sql = query.toString();
491
492 Session session = null;
493
494 try {
495 session = openSession();
496
497 Query q = session.createQuery(sql);
498
499 QueryPos qPos = QueryPos.getInstance(q);
500
501 qPos.add(classNameId);
502
503 qPos.add(classPK);
504
505 List<RatingsStats> list = q.list();
506
507 result = list;
508
509 RatingsStats ratingsStats = null;
510
511 if (list.isEmpty()) {
512 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
513 finderArgs, list);
514 }
515 else {
516 ratingsStats = list.get(0);
517
518 cacheResult(ratingsStats);
519
520 if ((ratingsStats.getClassNameId() != classNameId) ||
521 (ratingsStats.getClassPK() != classPK)) {
522 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
523 finderArgs, ratingsStats);
524 }
525 }
526
527 return ratingsStats;
528 }
529 catch (Exception e) {
530 throw processException(e);
531 }
532 finally {
533 if (result == null) {
534 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
535 finderArgs);
536 }
537
538 closeSession(session);
539 }
540 }
541 else {
542 if (result instanceof List<?>) {
543 return null;
544 }
545 else {
546 return (RatingsStats)result;
547 }
548 }
549 }
550
551
557 public List<RatingsStats> findAll() throws SystemException {
558 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
559 }
560
561
573 public List<RatingsStats> findAll(int start, int end)
574 throws SystemException {
575 return findAll(start, end, null);
576 }
577
578
591 public List<RatingsStats> findAll(int start, int end,
592 OrderByComparator orderByComparator) throws SystemException {
593 Object[] finderArgs = new Object[] {
594 String.valueOf(start), String.valueOf(end),
595 String.valueOf(orderByComparator)
596 };
597
598 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
599 finderArgs, this);
600
601 if (list == null) {
602 StringBundler query = null;
603 String sql = null;
604
605 if (orderByComparator != null) {
606 query = new StringBundler(2 +
607 (orderByComparator.getOrderByFields().length * 3));
608
609 query.append(_SQL_SELECT_RATINGSSTATS);
610
611 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
612 orderByComparator);
613
614 sql = query.toString();
615 }
616 else {
617 sql = _SQL_SELECT_RATINGSSTATS;
618 }
619
620 Session session = null;
621
622 try {
623 session = openSession();
624
625 Query q = session.createQuery(sql);
626
627 if (orderByComparator == null) {
628 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
629 start, end, false);
630
631 Collections.sort(list);
632 }
633 else {
634 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
635 start, end);
636 }
637 }
638 catch (Exception e) {
639 throw processException(e);
640 }
641 finally {
642 if (list == null) {
643 FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
644 finderArgs);
645 }
646 else {
647 cacheResult(list);
648
649 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
650 list);
651 }
652
653 closeSession(session);
654 }
655 }
656
657 return list;
658 }
659
660
667 public void removeByC_C(long classNameId, long classPK)
668 throws NoSuchStatsException, SystemException {
669 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
670
671 remove(ratingsStats);
672 }
673
674
679 public void removeAll() throws SystemException {
680 for (RatingsStats ratingsStats : findAll()) {
681 remove(ratingsStats);
682 }
683 }
684
685
693 public int countByC_C(long classNameId, long classPK)
694 throws SystemException {
695 Object[] finderArgs = new Object[] { classNameId, classPK };
696
697 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
698 finderArgs, this);
699
700 if (count == null) {
701 StringBundler query = new StringBundler(3);
702
703 query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
704
705 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
706
707 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
708
709 String sql = query.toString();
710
711 Session session = null;
712
713 try {
714 session = openSession();
715
716 Query q = session.createQuery(sql);
717
718 QueryPos qPos = QueryPos.getInstance(q);
719
720 qPos.add(classNameId);
721
722 qPos.add(classPK);
723
724 count = (Long)q.uniqueResult();
725 }
726 catch (Exception e) {
727 throw processException(e);
728 }
729 finally {
730 if (count == null) {
731 count = Long.valueOf(0);
732 }
733
734 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
735 count);
736
737 closeSession(session);
738 }
739 }
740
741 return count.intValue();
742 }
743
744
750 public int countAll() throws SystemException {
751 Object[] finderArgs = new Object[0];
752
753 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
754 finderArgs, this);
755
756 if (count == null) {
757 Session session = null;
758
759 try {
760 session = openSession();
761
762 Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
763
764 count = (Long)q.uniqueResult();
765 }
766 catch (Exception e) {
767 throw processException(e);
768 }
769 finally {
770 if (count == null) {
771 count = Long.valueOf(0);
772 }
773
774 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
775 count);
776
777 closeSession(session);
778 }
779 }
780
781 return count.intValue();
782 }
783
784
787 public void afterPropertiesSet() {
788 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
789 com.liferay.portal.util.PropsUtil.get(
790 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
791
792 if (listenerClassNames.length > 0) {
793 try {
794 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
795
796 for (String listenerClassName : listenerClassNames) {
797 listenersList.add((ModelListener<RatingsStats>)InstanceFactory.newInstance(
798 listenerClassName));
799 }
800
801 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
802 }
803 catch (Exception e) {
804 _log.error(e);
805 }
806 }
807 }
808
809 public void destroy() {
810 EntityCacheUtil.removeCache(RatingsStatsImpl.class.getName());
811 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
812 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
813 }
814
815 @BeanReference(type = RatingsEntryPersistence.class)
816 protected RatingsEntryPersistence ratingsEntryPersistence;
817 @BeanReference(type = RatingsStatsPersistence.class)
818 protected RatingsStatsPersistence ratingsStatsPersistence;
819 @BeanReference(type = ResourcePersistence.class)
820 protected ResourcePersistence resourcePersistence;
821 @BeanReference(type = UserPersistence.class)
822 protected UserPersistence userPersistence;
823 private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
824 private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
825 private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
826 private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
827 private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
828 private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
829 private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
830 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
831 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
832 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
833 }