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 "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
414
415 query.append("classNameId = ?");
416
417 query.append(" AND ");
418
419 query.append("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 "FROM com.liferay.portlet.ratings.model.RatingsStats ");
545
546 if (obc != null) {
547 query.append("ORDER BY ");
548 query.append(obc.getOrderBy());
549 }
550
551 Query q = session.createQuery(query.toString());
552
553 if (obc == null) {
554 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
555 start, end, false);
556
557 Collections.sort(list);
558 }
559 else {
560 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
561 start, end);
562 }
563 }
564 catch (Exception e) {
565 throw processException(e);
566 }
567 finally {
568 if (list == null) {
569 list = new ArrayList<RatingsStats>();
570 }
571
572 cacheResult(list);
573
574 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
575
576 closeSession(session);
577 }
578 }
579
580 return list;
581 }
582
583 public void removeByC_C(long classNameId, long classPK)
584 throws NoSuchStatsException, SystemException {
585 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
586
587 remove(ratingsStats);
588 }
589
590 public void removeAll() throws SystemException {
591 for (RatingsStats ratingsStats : findAll()) {
592 remove(ratingsStats);
593 }
594 }
595
596 public int countByC_C(long classNameId, long classPK)
597 throws SystemException {
598 Object[] finderArgs = new Object[] {
599 new Long(classNameId), new Long(classPK)
600 };
601
602 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
603 finderArgs, this);
604
605 if (count == null) {
606 Session session = null;
607
608 try {
609 session = openSession();
610
611 StringBuilder query = new StringBuilder();
612
613 query.append("SELECT COUNT(*) ");
614 query.append(
615 "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
616
617 query.append("classNameId = ?");
618
619 query.append(" AND ");
620
621 query.append("classPK = ?");
622
623 query.append(" ");
624
625 Query q = session.createQuery(query.toString());
626
627 QueryPos qPos = QueryPos.getInstance(q);
628
629 qPos.add(classNameId);
630
631 qPos.add(classPK);
632
633 count = (Long)q.uniqueResult();
634 }
635 catch (Exception e) {
636 throw processException(e);
637 }
638 finally {
639 if (count == null) {
640 count = Long.valueOf(0);
641 }
642
643 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
644 count);
645
646 closeSession(session);
647 }
648 }
649
650 return count.intValue();
651 }
652
653 public int countAll() throws SystemException {
654 Object[] finderArgs = new Object[0];
655
656 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
657 finderArgs, this);
658
659 if (count == null) {
660 Session session = null;
661
662 try {
663 session = openSession();
664
665 Query q = session.createQuery(
666 "SELECT COUNT(*) FROM com.liferay.portlet.ratings.model.RatingsStats");
667
668 count = (Long)q.uniqueResult();
669 }
670 catch (Exception e) {
671 throw processException(e);
672 }
673 finally {
674 if (count == null) {
675 count = Long.valueOf(0);
676 }
677
678 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
679 count);
680
681 closeSession(session);
682 }
683 }
684
685 return count.intValue();
686 }
687
688 public void afterPropertiesSet() {
689 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
690 com.liferay.portal.util.PropsUtil.get(
691 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
692
693 if (listenerClassNames.length > 0) {
694 try {
695 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
696
697 for (String listenerClassName : listenerClassNames) {
698 listenersList.add((ModelListener<RatingsStats>)Class.forName(
699 listenerClassName).newInstance());
700 }
701
702 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
703 }
704 catch (Exception e) {
705 _log.error(e);
706 }
707 }
708 }
709
710 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence.impl")
711 protected com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence ratingsEntryPersistence;
712 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
713 protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
714 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
715 }