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