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