1
14
15 package com.liferay.portlet.ratings.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.DynamicQuery;
21 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23 import com.liferay.portal.kernel.dao.orm.FinderPath;
24 import com.liferay.portal.kernel.dao.orm.Query;
25 import com.liferay.portal.kernel.dao.orm.QueryPos;
26 import com.liferay.portal.kernel.dao.orm.QueryUtil;
27 import com.liferay.portal.kernel.dao.orm.Session;
28 import com.liferay.portal.kernel.exception.SystemException;
29 import com.liferay.portal.kernel.log.Log;
30 import com.liferay.portal.kernel.log.LogFactoryUtil;
31 import com.liferay.portal.kernel.util.GetterUtil;
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.model.ModelListener;
37 import com.liferay.portal.service.persistence.BatchSessionUtil;
38 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
39
40 import com.liferay.portlet.ratings.NoSuchStatsException;
41 import com.liferay.portlet.ratings.model.RatingsStats;
42 import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
43 import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
44
45 import java.io.Serializable;
46
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
50
51
64 public class RatingsStatsPersistenceImpl extends BasePersistenceImpl<RatingsStats>
65 implements RatingsStatsPersistence {
66 public static final String FINDER_CLASS_NAME_ENTITY = RatingsStatsImpl.class.getName();
67 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
68 ".List";
69 public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
70 RatingsStatsModelImpl.FINDER_CACHE_ENABLED,
71 FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
72 new String[] { Long.class.getName(), Long.class.getName() });
73 public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
74 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
75 "countByC_C",
76 new String[] { Long.class.getName(), Long.class.getName() });
77 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
78 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79 "findAll", new String[0]);
80 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
81 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
82 "countAll", new String[0]);
83
84 public void cacheResult(RatingsStats ratingsStats) {
85 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
86 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
87
88 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
89 new Object[] {
90 new Long(ratingsStats.getClassNameId()),
91 new Long(ratingsStats.getClassPK())
92 }, ratingsStats);
93 }
94
95 public void cacheResult(List<RatingsStats> ratingsStatses) {
96 for (RatingsStats ratingsStats : ratingsStatses) {
97 if (EntityCacheUtil.getResult(
98 RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
99 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(),
100 this) == null) {
101 cacheResult(ratingsStats);
102 }
103 }
104 }
105
106 public void clearCache() {
107 CacheRegistry.clear(RatingsStatsImpl.class.getName());
108 EntityCacheUtil.clearCache(RatingsStatsImpl.class.getName());
109 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
110 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
111 }
112
113 public RatingsStats create(long statsId) {
114 RatingsStats ratingsStats = new RatingsStatsImpl();
115
116 ratingsStats.setNew(true);
117 ratingsStats.setPrimaryKey(statsId);
118
119 return ratingsStats;
120 }
121
122 public RatingsStats remove(Serializable primaryKey)
123 throws NoSuchModelException, SystemException {
124 return remove(((Long)primaryKey).longValue());
125 }
126
127 public RatingsStats remove(long statsId)
128 throws NoSuchStatsException, SystemException {
129 Session session = null;
130
131 try {
132 session = openSession();
133
134 RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
135 new Long(statsId));
136
137 if (ratingsStats == null) {
138 if (_log.isWarnEnabled()) {
139 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
140 }
141
142 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
143 statsId);
144 }
145
146 return remove(ratingsStats);
147 }
148 catch (NoSuchStatsException nsee) {
149 throw nsee;
150 }
151 catch (Exception e) {
152 throw processException(e);
153 }
154 finally {
155 closeSession(session);
156 }
157 }
158
159 public RatingsStats remove(RatingsStats ratingsStats)
160 throws SystemException {
161 for (ModelListener<RatingsStats> listener : listeners) {
162 listener.onBeforeRemove(ratingsStats);
163 }
164
165 ratingsStats = removeImpl(ratingsStats);
166
167 for (ModelListener<RatingsStats> listener : listeners) {
168 listener.onAfterRemove(ratingsStats);
169 }
170
171 return ratingsStats;
172 }
173
174 protected RatingsStats removeImpl(RatingsStats ratingsStats)
175 throws SystemException {
176 ratingsStats = toUnwrappedModel(ratingsStats);
177
178 Session session = null;
179
180 try {
181 session = openSession();
182
183 if (ratingsStats.isCachedModel() || BatchSessionUtil.isEnabled()) {
184 Object staleObject = session.get(RatingsStatsImpl.class,
185 ratingsStats.getPrimaryKeyObj());
186
187 if (staleObject != null) {
188 session.evict(staleObject);
189 }
190 }
191
192 session.delete(ratingsStats);
193
194 session.flush();
195 }
196 catch (Exception e) {
197 throw processException(e);
198 }
199 finally {
200 closeSession(session);
201 }
202
203 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
204
205 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
206
207 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
208 new Object[] {
209 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
210 new Long(ratingsStatsModelImpl.getOriginalClassPK())
211 });
212
213 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
214 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
215
216 return ratingsStats;
217 }
218
219 public RatingsStats updateImpl(
220 com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
221 boolean merge) throws SystemException {
222 ratingsStats = toUnwrappedModel(ratingsStats);
223
224 boolean isNew = ratingsStats.isNew();
225
226 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
227
228 Session session = null;
229
230 try {
231 session = openSession();
232
233 BatchSessionUtil.update(session, ratingsStats, merge);
234
235 ratingsStats.setNew(false);
236 }
237 catch (Exception e) {
238 throw processException(e);
239 }
240 finally {
241 closeSession(session);
242 }
243
244 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
245
246 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
247 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
248
249 if (!isNew &&
250 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
251 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
252 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
253 new Object[] {
254 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
255 new Long(ratingsStatsModelImpl.getOriginalClassPK())
256 });
257 }
258
259 if (isNew ||
260 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
261 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
262 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
263 new Object[] {
264 new Long(ratingsStats.getClassNameId()),
265 new Long(ratingsStats.getClassPK())
266 }, ratingsStats);
267 }
268
269 return ratingsStats;
270 }
271
272 protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
273 if (ratingsStats instanceof RatingsStatsImpl) {
274 return ratingsStats;
275 }
276
277 RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
278
279 ratingsStatsImpl.setNew(ratingsStats.isNew());
280 ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
281
282 ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
283 ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
284 ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
285 ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
286 ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
287 ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
288
289 return ratingsStatsImpl;
290 }
291
292 public RatingsStats findByPrimaryKey(Serializable primaryKey)
293 throws NoSuchModelException, SystemException {
294 return findByPrimaryKey(((Long)primaryKey).longValue());
295 }
296
297 public RatingsStats findByPrimaryKey(long statsId)
298 throws NoSuchStatsException, SystemException {
299 RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
300
301 if (ratingsStats == null) {
302 if (_log.isWarnEnabled()) {
303 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
304 }
305
306 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
307 statsId);
308 }
309
310 return ratingsStats;
311 }
312
313 public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
314 throws SystemException {
315 return fetchByPrimaryKey(((Long)primaryKey).longValue());
316 }
317
318 public RatingsStats fetchByPrimaryKey(long statsId)
319 throws SystemException {
320 RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
321 RatingsStatsImpl.class, statsId, this);
322
323 if (ratingsStats == null) {
324 Session session = null;
325
326 try {
327 session = openSession();
328
329 ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
330 new Long(statsId));
331 }
332 catch (Exception e) {
333 throw processException(e);
334 }
335 finally {
336 if (ratingsStats != null) {
337 cacheResult(ratingsStats);
338 }
339
340 closeSession(session);
341 }
342 }
343
344 return ratingsStats;
345 }
346
347 public RatingsStats findByC_C(long classNameId, long classPK)
348 throws NoSuchStatsException, SystemException {
349 RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
350
351 if (ratingsStats == null) {
352 StringBundler msg = new StringBundler(6);
353
354 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
355
356 msg.append("classNameId=");
357 msg.append(classNameId);
358
359 msg.append(", classPK=");
360 msg.append(classPK);
361
362 msg.append(StringPool.CLOSE_CURLY_BRACE);
363
364 if (_log.isWarnEnabled()) {
365 _log.warn(msg.toString());
366 }
367
368 throw new NoSuchStatsException(msg.toString());
369 }
370
371 return ratingsStats;
372 }
373
374 public RatingsStats fetchByC_C(long classNameId, long classPK)
375 throws SystemException {
376 return fetchByC_C(classNameId, classPK, true);
377 }
378
379 public RatingsStats fetchByC_C(long classNameId, long classPK,
380 boolean retrieveFromCache) throws SystemException {
381 Object[] finderArgs = new Object[] {
382 new Long(classNameId), new Long(classPK)
383 };
384
385 Object result = null;
386
387 if (retrieveFromCache) {
388 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
389 finderArgs, this);
390 }
391
392 if (result == null) {
393 Session session = null;
394
395 try {
396 session = openSession();
397
398 StringBundler query = new StringBundler(3);
399
400 query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
401
402 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
403
404 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
405
406 String sql = query.toString();
407
408 Query q = session.createQuery(sql);
409
410 QueryPos qPos = QueryPos.getInstance(q);
411
412 qPos.add(classNameId);
413
414 qPos.add(classPK);
415
416 List<RatingsStats> list = q.list();
417
418 result = list;
419
420 RatingsStats ratingsStats = null;
421
422 if (list.isEmpty()) {
423 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
424 finderArgs, list);
425 }
426 else {
427 ratingsStats = list.get(0);
428
429 cacheResult(ratingsStats);
430
431 if ((ratingsStats.getClassNameId() != classNameId) ||
432 (ratingsStats.getClassPK() != classPK)) {
433 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
434 finderArgs, ratingsStats);
435 }
436 }
437
438 return ratingsStats;
439 }
440 catch (Exception e) {
441 throw processException(e);
442 }
443 finally {
444 if (result == null) {
445 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
446 finderArgs, new ArrayList<RatingsStats>());
447 }
448
449 closeSession(session);
450 }
451 }
452 else {
453 if (result instanceof List<?>) {
454 return null;
455 }
456 else {
457 return (RatingsStats)result;
458 }
459 }
460 }
461
462 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
463 throws SystemException {
464 Session session = null;
465
466 try {
467 session = openSession();
468
469 dynamicQuery.compile(session);
470
471 return dynamicQuery.list();
472 }
473 catch (Exception e) {
474 throw processException(e);
475 }
476 finally {
477 closeSession(session);
478 }
479 }
480
481 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
482 int start, int end) throws SystemException {
483 Session session = null;
484
485 try {
486 session = openSession();
487
488 dynamicQuery.setLimit(start, end);
489
490 dynamicQuery.compile(session);
491
492 return dynamicQuery.list();
493 }
494 catch (Exception e) {
495 throw processException(e);
496 }
497 finally {
498 closeSession(session);
499 }
500 }
501
502 public List<RatingsStats> findAll() throws SystemException {
503 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
504 }
505
506 public List<RatingsStats> findAll(int start, int end)
507 throws SystemException {
508 return findAll(start, end, null);
509 }
510
511 public List<RatingsStats> findAll(int start, int end, OrderByComparator obc)
512 throws SystemException {
513 Object[] finderArgs = new Object[] {
514 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
515 };
516
517 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
518 finderArgs, this);
519
520 if (list == null) {
521 Session session = null;
522
523 try {
524 session = openSession();
525
526 StringBundler query = null;
527 String sql = null;
528
529 if (obc != null) {
530 query = new StringBundler(2 +
531 (obc.getOrderByFields().length * 3));
532
533 query.append(_SQL_SELECT_RATINGSSTATS);
534
535 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
536
537 sql = query.toString();
538 }
539
540 sql = _SQL_SELECT_RATINGSSTATS;
541
542 Query q = session.createQuery(sql);
543
544 if (obc == null) {
545 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
546 start, end, false);
547
548 Collections.sort(list);
549 }
550 else {
551 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
552 start, end);
553 }
554 }
555 catch (Exception e) {
556 throw processException(e);
557 }
558 finally {
559 if (list == null) {
560 list = new ArrayList<RatingsStats>();
561 }
562
563 cacheResult(list);
564
565 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
566
567 closeSession(session);
568 }
569 }
570
571 return list;
572 }
573
574 public void removeByC_C(long classNameId, long classPK)
575 throws NoSuchStatsException, SystemException {
576 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
577
578 remove(ratingsStats);
579 }
580
581 public void removeAll() throws SystemException {
582 for (RatingsStats ratingsStats : findAll()) {
583 remove(ratingsStats);
584 }
585 }
586
587 public int countByC_C(long classNameId, long classPK)
588 throws SystemException {
589 Object[] finderArgs = new Object[] {
590 new Long(classNameId), new Long(classPK)
591 };
592
593 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
594 finderArgs, this);
595
596 if (count == null) {
597 Session session = null;
598
599 try {
600 session = openSession();
601
602 StringBundler query = new StringBundler(3);
603
604 query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
605
606 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
607
608 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
609
610 String sql = query.toString();
611
612 Query q = session.createQuery(sql);
613
614 QueryPos qPos = QueryPos.getInstance(q);
615
616 qPos.add(classNameId);
617
618 qPos.add(classPK);
619
620 count = (Long)q.uniqueResult();
621 }
622 catch (Exception e) {
623 throw processException(e);
624 }
625 finally {
626 if (count == null) {
627 count = Long.valueOf(0);
628 }
629
630 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
631 count);
632
633 closeSession(session);
634 }
635 }
636
637 return count.intValue();
638 }
639
640 public int countAll() throws SystemException {
641 Object[] finderArgs = new Object[0];
642
643 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
644 finderArgs, this);
645
646 if (count == null) {
647 Session session = null;
648
649 try {
650 session = openSession();
651
652 Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
653
654 count = (Long)q.uniqueResult();
655 }
656 catch (Exception e) {
657 throw processException(e);
658 }
659 finally {
660 if (count == null) {
661 count = Long.valueOf(0);
662 }
663
664 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
665 count);
666
667 closeSession(session);
668 }
669 }
670
671 return count.intValue();
672 }
673
674 public void afterPropertiesSet() {
675 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
676 com.liferay.portal.util.PropsUtil.get(
677 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
678
679 if (listenerClassNames.length > 0) {
680 try {
681 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
682
683 for (String listenerClassName : listenerClassNames) {
684 listenersList.add((ModelListener<RatingsStats>)Class.forName(
685 listenerClassName).newInstance());
686 }
687
688 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
689 }
690 catch (Exception e) {
691 _log.error(e);
692 }
693 }
694 }
695
696 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence")
697 protected com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence ratingsEntryPersistence;
698 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence")
699 protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
700 @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
701 protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
702 @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
703 protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
704 private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
705 private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
706 private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
707 private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
708 private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
709 private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
710 private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
711 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
712 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
713 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
714 }