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