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