1
14
15 package com.liferay.portlet.ratings.service.persistence;
16
17 import com.liferay.portal.NoSuchModelException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.kernel.annotation.BeanReference;
20 import com.liferay.portal.kernel.cache.CacheRegistry;
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.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 protected RatingsStats removeImpl(RatingsStats ratingsStats)
173 throws SystemException {
174 ratingsStats = toUnwrappedModel(ratingsStats);
175
176 Session session = null;
177
178 try {
179 session = openSession();
180
181 BatchSessionUtil.delete(session, ratingsStats);
182 }
183 catch (Exception e) {
184 throw processException(e);
185 }
186 finally {
187 closeSession(session);
188 }
189
190 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
191
192 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
193
194 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
195 new Object[] {
196 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
197 new Long(ratingsStatsModelImpl.getOriginalClassPK())
198 });
199
200 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
201 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
202
203 return ratingsStats;
204 }
205
206
209 public RatingsStats update(RatingsStats ratingsStats)
210 throws SystemException {
211 if (_log.isWarnEnabled()) {
212 _log.warn(
213 "Using the deprecated update(RatingsStats ratingsStats) method. Use update(RatingsStats ratingsStats, boolean merge) instead.");
214 }
215
216 return update(ratingsStats, false);
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[] { classNameId, classPK };
382
383 Object result = null;
384
385 if (retrieveFromCache) {
386 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
387 finderArgs, this);
388 }
389
390 if (result == null) {
391 StringBundler query = new StringBundler(3);
392
393 query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
394
395 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
396
397 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
398
399 String sql = query.toString();
400
401 Session session = null;
402
403 try {
404 session = openSession();
405
406 Query q = session.createQuery(sql);
407
408 QueryPos qPos = QueryPos.getInstance(q);
409
410 qPos.add(classNameId);
411
412 qPos.add(classPK);
413
414 List<RatingsStats> list = q.list();
415
416 result = list;
417
418 RatingsStats ratingsStats = null;
419
420 if (list.isEmpty()) {
421 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
422 finderArgs, list);
423 }
424 else {
425 ratingsStats = list.get(0);
426
427 cacheResult(ratingsStats);
428
429 if ((ratingsStats.getClassNameId() != classNameId) ||
430 (ratingsStats.getClassPK() != classPK)) {
431 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
432 finderArgs, ratingsStats);
433 }
434 }
435
436 return ratingsStats;
437 }
438 catch (Exception e) {
439 throw processException(e);
440 }
441 finally {
442 if (result == null) {
443 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
444 finderArgs, new ArrayList<RatingsStats>());
445 }
446
447 closeSession(session);
448 }
449 }
450 else {
451 if (result instanceof List<?>) {
452 return null;
453 }
454 else {
455 return (RatingsStats)result;
456 }
457 }
458 }
459
460 public List<RatingsStats> findAll() throws SystemException {
461 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
462 }
463
464 public List<RatingsStats> findAll(int start, int end)
465 throws SystemException {
466 return findAll(start, end, null);
467 }
468
469 public List<RatingsStats> findAll(int start, int end,
470 OrderByComparator orderByComparator) throws SystemException {
471 Object[] finderArgs = new Object[] {
472 String.valueOf(start), String.valueOf(end),
473 String.valueOf(orderByComparator)
474 };
475
476 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
477 finderArgs, this);
478
479 if (list == null) {
480 StringBundler query = null;
481 String sql = null;
482
483 if (orderByComparator != null) {
484 query = new StringBundler(2 +
485 (orderByComparator.getOrderByFields().length * 3));
486
487 query.append(_SQL_SELECT_RATINGSSTATS);
488
489 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
490 orderByComparator);
491
492 sql = query.toString();
493 }
494 else {
495 sql = _SQL_SELECT_RATINGSSTATS;
496 }
497
498 Session session = null;
499
500 try {
501 session = openSession();
502
503 Query q = session.createQuery(sql);
504
505 if (orderByComparator == null) {
506 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
507 start, end, false);
508
509 Collections.sort(list);
510 }
511 else {
512 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
513 start, end);
514 }
515 }
516 catch (Exception e) {
517 throw processException(e);
518 }
519 finally {
520 if (list == null) {
521 list = new ArrayList<RatingsStats>();
522 }
523
524 cacheResult(list);
525
526 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
527
528 closeSession(session);
529 }
530 }
531
532 return list;
533 }
534
535 public void removeByC_C(long classNameId, long classPK)
536 throws NoSuchStatsException, SystemException {
537 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
538
539 remove(ratingsStats);
540 }
541
542 public void removeAll() throws SystemException {
543 for (RatingsStats ratingsStats : findAll()) {
544 remove(ratingsStats);
545 }
546 }
547
548 public int countByC_C(long classNameId, long classPK)
549 throws SystemException {
550 Object[] finderArgs = new Object[] { classNameId, classPK };
551
552 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
553 finderArgs, this);
554
555 if (count == null) {
556 StringBundler query = new StringBundler(3);
557
558 query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
559
560 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
561
562 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
563
564 String sql = query.toString();
565
566 Session session = null;
567
568 try {
569 session = openSession();
570
571 Query q = session.createQuery(sql);
572
573 QueryPos qPos = QueryPos.getInstance(q);
574
575 qPos.add(classNameId);
576
577 qPos.add(classPK);
578
579 count = (Long)q.uniqueResult();
580 }
581 catch (Exception e) {
582 throw processException(e);
583 }
584 finally {
585 if (count == null) {
586 count = Long.valueOf(0);
587 }
588
589 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
590 count);
591
592 closeSession(session);
593 }
594 }
595
596 return count.intValue();
597 }
598
599 public int countAll() throws SystemException {
600 Object[] finderArgs = new Object[0];
601
602 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
603 finderArgs, this);
604
605 if (count == null) {
606 Session session = null;
607
608 try {
609 session = openSession();
610
611 Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
612
613 count = (Long)q.uniqueResult();
614 }
615 catch (Exception e) {
616 throw processException(e);
617 }
618 finally {
619 if (count == null) {
620 count = Long.valueOf(0);
621 }
622
623 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
624 count);
625
626 closeSession(session);
627 }
628 }
629
630 return count.intValue();
631 }
632
633 public void afterPropertiesSet() {
634 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
635 com.liferay.portal.util.PropsUtil.get(
636 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
637
638 if (listenerClassNames.length > 0) {
639 try {
640 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
641
642 for (String listenerClassName : listenerClassNames) {
643 listenersList.add((ModelListener<RatingsStats>)InstanceFactory.newInstance(
644 listenerClassName));
645 }
646
647 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
648 }
649 catch (Exception e) {
650 _log.error(e);
651 }
652 }
653 }
654
655 public void destroy() {
656 EntityCacheUtil.removeCache(RatingsStatsImpl.class.getName());
657 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
658 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
659 }
660
661 @BeanReference(type = RatingsEntryPersistence.class)
662 protected RatingsEntryPersistence ratingsEntryPersistence;
663 @BeanReference(type = RatingsStatsPersistence.class)
664 protected RatingsStatsPersistence ratingsStatsPersistence;
665 @BeanReference(type = ResourcePersistence.class)
666 protected ResourcePersistence resourcePersistence;
667 @BeanReference(type = UserPersistence.class)
668 protected UserPersistence userPersistence;
669 private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
670 private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
671 private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
672 private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
673 private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
674 private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
675 private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
676 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
677 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
678 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
679 }