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.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
223 public RatingsStats update(RatingsStats ratingsStats)
224 throws SystemException {
225 if (_log.isWarnEnabled()) {
226 _log.warn(
227 "Using the deprecated update(RatingsStats ratingsStats) method. Use update(RatingsStats ratingsStats, boolean merge) instead.");
228 }
229
230 return update(ratingsStats, false);
231 }
232
233 public RatingsStats updateImpl(
234 com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
235 boolean merge) throws SystemException {
236 ratingsStats = toUnwrappedModel(ratingsStats);
237
238 boolean isNew = ratingsStats.isNew();
239
240 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
241
242 Session session = null;
243
244 try {
245 session = openSession();
246
247 BatchSessionUtil.update(session, ratingsStats, merge);
248
249 ratingsStats.setNew(false);
250 }
251 catch (Exception e) {
252 throw processException(e);
253 }
254 finally {
255 closeSession(session);
256 }
257
258 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
259
260 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
261 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
262
263 if (!isNew &&
264 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
265 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
266 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
267 new Object[] {
268 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
269 new Long(ratingsStatsModelImpl.getOriginalClassPK())
270 });
271 }
272
273 if (isNew ||
274 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
275 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
276 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
277 new Object[] {
278 new Long(ratingsStats.getClassNameId()),
279 new Long(ratingsStats.getClassPK())
280 }, ratingsStats);
281 }
282
283 return ratingsStats;
284 }
285
286 protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
287 if (ratingsStats instanceof RatingsStatsImpl) {
288 return ratingsStats;
289 }
290
291 RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
292
293 ratingsStatsImpl.setNew(ratingsStats.isNew());
294 ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
295
296 ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
297 ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
298 ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
299 ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
300 ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
301 ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
302
303 return ratingsStatsImpl;
304 }
305
306 public RatingsStats findByPrimaryKey(Serializable primaryKey)
307 throws NoSuchModelException, SystemException {
308 return findByPrimaryKey(((Long)primaryKey).longValue());
309 }
310
311 public RatingsStats findByPrimaryKey(long statsId)
312 throws NoSuchStatsException, SystemException {
313 RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
314
315 if (ratingsStats == null) {
316 if (_log.isWarnEnabled()) {
317 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
318 }
319
320 throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
321 statsId);
322 }
323
324 return ratingsStats;
325 }
326
327 public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
328 throws SystemException {
329 return fetchByPrimaryKey(((Long)primaryKey).longValue());
330 }
331
332 public RatingsStats fetchByPrimaryKey(long statsId)
333 throws SystemException {
334 RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
335 RatingsStatsImpl.class, statsId, this);
336
337 if (ratingsStats == null) {
338 Session session = null;
339
340 try {
341 session = openSession();
342
343 ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
344 new Long(statsId));
345 }
346 catch (Exception e) {
347 throw processException(e);
348 }
349 finally {
350 if (ratingsStats != null) {
351 cacheResult(ratingsStats);
352 }
353
354 closeSession(session);
355 }
356 }
357
358 return ratingsStats;
359 }
360
361 public RatingsStats findByC_C(long classNameId, long classPK)
362 throws NoSuchStatsException, SystemException {
363 RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
364
365 if (ratingsStats == null) {
366 StringBundler msg = new StringBundler(6);
367
368 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
369
370 msg.append("classNameId=");
371 msg.append(classNameId);
372
373 msg.append(", classPK=");
374 msg.append(classPK);
375
376 msg.append(StringPool.CLOSE_CURLY_BRACE);
377
378 if (_log.isWarnEnabled()) {
379 _log.warn(msg.toString());
380 }
381
382 throw new NoSuchStatsException(msg.toString());
383 }
384
385 return ratingsStats;
386 }
387
388 public RatingsStats fetchByC_C(long classNameId, long classPK)
389 throws SystemException {
390 return fetchByC_C(classNameId, classPK, true);
391 }
392
393 public RatingsStats fetchByC_C(long classNameId, long classPK,
394 boolean retrieveFromCache) throws SystemException {
395 Object[] finderArgs = new Object[] {
396 new Long(classNameId), new Long(classPK)
397 };
398
399 Object result = null;
400
401 if (retrieveFromCache) {
402 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
403 finderArgs, this);
404 }
405
406 if (result == null) {
407 Session session = null;
408
409 try {
410 session = openSession();
411
412 StringBundler query = new StringBundler(3);
413
414 query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
415
416 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
417
418 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
419
420 String sql = query.toString();
421
422 Query q = session.createQuery(sql);
423
424 QueryPos qPos = QueryPos.getInstance(q);
425
426 qPos.add(classNameId);
427
428 qPos.add(classPK);
429
430 List<RatingsStats> list = q.list();
431
432 result = list;
433
434 RatingsStats ratingsStats = null;
435
436 if (list.isEmpty()) {
437 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
438 finderArgs, list);
439 }
440 else {
441 ratingsStats = list.get(0);
442
443 cacheResult(ratingsStats);
444
445 if ((ratingsStats.getClassNameId() != classNameId) ||
446 (ratingsStats.getClassPK() != classPK)) {
447 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
448 finderArgs, ratingsStats);
449 }
450 }
451
452 return ratingsStats;
453 }
454 catch (Exception e) {
455 throw processException(e);
456 }
457 finally {
458 if (result == null) {
459 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
460 finderArgs, new ArrayList<RatingsStats>());
461 }
462
463 closeSession(session);
464 }
465 }
466 else {
467 if (result instanceof List<?>) {
468 return null;
469 }
470 else {
471 return (RatingsStats)result;
472 }
473 }
474 }
475
476 public List<RatingsStats> findAll() throws SystemException {
477 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
478 }
479
480 public List<RatingsStats> findAll(int start, int end)
481 throws SystemException {
482 return findAll(start, end, null);
483 }
484
485 public List<RatingsStats> findAll(int start, int end,
486 OrderByComparator orderByComparator) throws SystemException {
487 Object[] finderArgs = new Object[] {
488 String.valueOf(start), String.valueOf(end),
489 String.valueOf(orderByComparator)
490 };
491
492 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
493 finderArgs, this);
494
495 if (list == null) {
496 Session session = null;
497
498 try {
499 session = openSession();
500
501 StringBundler query = null;
502 String sql = null;
503
504 if (orderByComparator != null) {
505 query = new StringBundler(2 +
506 (orderByComparator.getOrderByFields().length * 3));
507
508 query.append(_SQL_SELECT_RATINGSSTATS);
509
510 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
511 orderByComparator);
512
513 sql = query.toString();
514 }
515
516 sql = _SQL_SELECT_RATINGSSTATS;
517
518 Query q = session.createQuery(sql);
519
520 if (orderByComparator == null) {
521 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
522 start, end, false);
523
524 Collections.sort(list);
525 }
526 else {
527 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
528 start, end);
529 }
530 }
531 catch (Exception e) {
532 throw processException(e);
533 }
534 finally {
535 if (list == null) {
536 list = new ArrayList<RatingsStats>();
537 }
538
539 cacheResult(list);
540
541 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
542
543 closeSession(session);
544 }
545 }
546
547 return list;
548 }
549
550 public void removeByC_C(long classNameId, long classPK)
551 throws NoSuchStatsException, SystemException {
552 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
553
554 remove(ratingsStats);
555 }
556
557 public void removeAll() throws SystemException {
558 for (RatingsStats ratingsStats : findAll()) {
559 remove(ratingsStats);
560 }
561 }
562
563 public int countByC_C(long classNameId, long classPK)
564 throws SystemException {
565 Object[] finderArgs = new Object[] {
566 new Long(classNameId), new Long(classPK)
567 };
568
569 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
570 finderArgs, this);
571
572 if (count == null) {
573 Session session = null;
574
575 try {
576 session = openSession();
577
578 StringBundler query = new StringBundler(3);
579
580 query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
581
582 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
583
584 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
585
586 String sql = query.toString();
587
588 Query q = session.createQuery(sql);
589
590 QueryPos qPos = QueryPos.getInstance(q);
591
592 qPos.add(classNameId);
593
594 qPos.add(classPK);
595
596 count = (Long)q.uniqueResult();
597 }
598 catch (Exception e) {
599 throw processException(e);
600 }
601 finally {
602 if (count == null) {
603 count = Long.valueOf(0);
604 }
605
606 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
607 count);
608
609 closeSession(session);
610 }
611 }
612
613 return count.intValue();
614 }
615
616 public int countAll() throws SystemException {
617 Object[] finderArgs = new Object[0];
618
619 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
620 finderArgs, this);
621
622 if (count == null) {
623 Session session = null;
624
625 try {
626 session = openSession();
627
628 Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
629
630 count = (Long)q.uniqueResult();
631 }
632 catch (Exception e) {
633 throw processException(e);
634 }
635 finally {
636 if (count == null) {
637 count = Long.valueOf(0);
638 }
639
640 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
641 count);
642
643 closeSession(session);
644 }
645 }
646
647 return count.intValue();
648 }
649
650 public void afterPropertiesSet() {
651 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
652 com.liferay.portal.util.PropsUtil.get(
653 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
654
655 if (listenerClassNames.length > 0) {
656 try {
657 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
658
659 for (String listenerClassName : listenerClassNames) {
660 listenersList.add((ModelListener<RatingsStats>)Class.forName(
661 listenerClassName).newInstance());
662 }
663
664 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
665 }
666 catch (Exception e) {
667 _log.error(e);
668 }
669 }
670 }
671
672 @BeanReference(type = RatingsEntryPersistence.class)
673 protected RatingsEntryPersistence ratingsEntryPersistence;
674 @BeanReference(type = RatingsStatsPersistence.class)
675 protected RatingsStatsPersistence ratingsStatsPersistence;
676 @BeanReference(type = ResourcePersistence.class)
677 protected ResourcePersistence resourcePersistence;
678 @BeanReference(type = UserPersistence.class)
679 protected UserPersistence userPersistence;
680 private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
681 private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
682 private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
683 private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
684 private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
685 private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
686 private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
687 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
688 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
689 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
690 }