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