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