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