001
014
015 package com.liferay.counter.service.persistence;
016
017 import com.liferay.counter.NoSuchCounterException;
018 import com.liferay.counter.model.Counter;
019 import com.liferay.counter.model.impl.CounterImpl;
020 import com.liferay.counter.model.impl.CounterModelImpl;
021
022 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
023 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
024 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
025 import com.liferay.portal.kernel.dao.orm.FinderPath;
026 import com.liferay.portal.kernel.dao.orm.Query;
027 import com.liferay.portal.kernel.dao.orm.QueryUtil;
028 import com.liferay.portal.kernel.dao.orm.Session;
029 import com.liferay.portal.kernel.exception.SystemException;
030 import com.liferay.portal.kernel.log.Log;
031 import com.liferay.portal.kernel.log.LogFactoryUtil;
032 import com.liferay.portal.kernel.util.GetterUtil;
033 import com.liferay.portal.kernel.util.InstanceFactory;
034 import com.liferay.portal.kernel.util.OrderByComparator;
035 import com.liferay.portal.kernel.util.StringBundler;
036 import com.liferay.portal.kernel.util.StringUtil;
037 import com.liferay.portal.kernel.util.UnmodifiableList;
038 import com.liferay.portal.model.CacheModel;
039 import com.liferay.portal.model.ModelListener;
040 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041
042 import java.io.Serializable;
043
044 import java.util.ArrayList;
045 import java.util.Collections;
046 import java.util.List;
047
048
060 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
061 implements CounterPersistence {
062
067 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
068 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
069 ".List1";
070 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071 ".List2";
072 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
073 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
074 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
075 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
076 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
077 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
078 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
079 CounterModelImpl.FINDER_CACHE_ENABLED, Long.class,
080 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
081
082
087 @Override
088 public void cacheResult(Counter counter) {
089 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
090 CounterImpl.class, counter.getPrimaryKey(), counter);
091
092 counter.resetOriginalValues();
093 }
094
095
100 @Override
101 public void cacheResult(List<Counter> counters) {
102 for (Counter counter : counters) {
103 if (EntityCacheUtil.getResult(
104 CounterModelImpl.ENTITY_CACHE_ENABLED,
105 CounterImpl.class, counter.getPrimaryKey()) == null) {
106 cacheResult(counter);
107 }
108 else {
109 counter.resetOriginalValues();
110 }
111 }
112 }
113
114
121 @Override
122 public void clearCache() {
123 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
124 CacheRegistryUtil.clear(CounterImpl.class.getName());
125 }
126
127 EntityCacheUtil.clearCache(CounterImpl.class.getName());
128
129 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
130 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
131 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
132 }
133
134
141 @Override
142 public void clearCache(Counter counter) {
143 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
144 CounterImpl.class, counter.getPrimaryKey());
145
146 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
147 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
148 }
149
150 @Override
151 public void clearCache(List<Counter> counters) {
152 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
153 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
154
155 for (Counter counter : counters) {
156 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
157 CounterImpl.class, counter.getPrimaryKey());
158 }
159 }
160
161
167 @Override
168 public Counter create(String name) {
169 Counter counter = new CounterImpl();
170
171 counter.setNew(true);
172 counter.setPrimaryKey(name);
173
174 return counter;
175 }
176
177
185 @Override
186 public Counter remove(String name)
187 throws NoSuchCounterException, SystemException {
188 return remove((Serializable)name);
189 }
190
191
199 @Override
200 public Counter remove(Serializable primaryKey)
201 throws NoSuchCounterException, SystemException {
202 Session session = null;
203
204 try {
205 session = openSession();
206
207 Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
208
209 if (counter == null) {
210 if (_log.isWarnEnabled()) {
211 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
212 }
213
214 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
215 primaryKey);
216 }
217
218 return remove(counter);
219 }
220 catch (NoSuchCounterException nsee) {
221 throw nsee;
222 }
223 catch (Exception e) {
224 throw processException(e);
225 }
226 finally {
227 closeSession(session);
228 }
229 }
230
231 @Override
232 protected Counter removeImpl(Counter counter) throws SystemException {
233 counter = toUnwrappedModel(counter);
234
235 Session session = null;
236
237 try {
238 session = openSession();
239
240 if (!session.contains(counter)) {
241 counter = (Counter)session.get(CounterImpl.class,
242 counter.getPrimaryKeyObj());
243 }
244
245 if (counter != null) {
246 session.delete(counter);
247 }
248 }
249 catch (Exception e) {
250 throw processException(e);
251 }
252 finally {
253 closeSession(session);
254 }
255
256 if (counter != null) {
257 clearCache(counter);
258 }
259
260 return counter;
261 }
262
263 @Override
264 public Counter updateImpl(com.liferay.counter.model.Counter counter)
265 throws SystemException {
266 counter = toUnwrappedModel(counter);
267
268 boolean isNew = counter.isNew();
269
270 Session session = null;
271
272 try {
273 session = openSession();
274
275 if (counter.isNew()) {
276 session.save(counter);
277
278 counter.setNew(false);
279 }
280 else {
281 session.merge(counter);
282 }
283 }
284 catch (Exception e) {
285 throw processException(e);
286 }
287 finally {
288 closeSession(session);
289 }
290
291 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
292
293 if (isNew) {
294 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
295 }
296
297 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
298 CounterImpl.class, counter.getPrimaryKey(), counter);
299
300 return counter;
301 }
302
303 protected Counter toUnwrappedModel(Counter counter) {
304 if (counter instanceof CounterImpl) {
305 return counter;
306 }
307
308 CounterImpl counterImpl = new CounterImpl();
309
310 counterImpl.setNew(counter.isNew());
311 counterImpl.setPrimaryKey(counter.getPrimaryKey());
312
313 counterImpl.setName(counter.getName());
314 counterImpl.setCurrentId(counter.getCurrentId());
315
316 return counterImpl;
317 }
318
319
327 @Override
328 public Counter findByPrimaryKey(Serializable primaryKey)
329 throws NoSuchCounterException, SystemException {
330 Counter counter = fetchByPrimaryKey(primaryKey);
331
332 if (counter == null) {
333 if (_log.isWarnEnabled()) {
334 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
335 }
336
337 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
338 primaryKey);
339 }
340
341 return counter;
342 }
343
344
352 @Override
353 public Counter findByPrimaryKey(String name)
354 throws NoSuchCounterException, SystemException {
355 return findByPrimaryKey((Serializable)name);
356 }
357
358
365 @Override
366 public Counter fetchByPrimaryKey(Serializable primaryKey)
367 throws SystemException {
368 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
369 CounterImpl.class, primaryKey);
370
371 if (counter == _nullCounter) {
372 return null;
373 }
374
375 if (counter == null) {
376 Session session = null;
377
378 try {
379 session = openSession();
380
381 counter = (Counter)session.get(CounterImpl.class, primaryKey);
382
383 if (counter != null) {
384 cacheResult(counter);
385 }
386 else {
387 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
388 CounterImpl.class, primaryKey, _nullCounter);
389 }
390 }
391 catch (Exception e) {
392 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
393 CounterImpl.class, primaryKey);
394
395 throw processException(e);
396 }
397 finally {
398 closeSession(session);
399 }
400 }
401
402 return counter;
403 }
404
405
412 @Override
413 public Counter fetchByPrimaryKey(String name) throws SystemException {
414 return fetchByPrimaryKey((Serializable)name);
415 }
416
417
423 @Override
424 public List<Counter> findAll() throws SystemException {
425 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
426 }
427
428
440 @Override
441 public List<Counter> findAll(int start, int end) throws SystemException {
442 return findAll(start, end, null);
443 }
444
445
458 @Override
459 public List<Counter> findAll(int start, int end,
460 OrderByComparator orderByComparator) throws SystemException {
461 boolean pagination = true;
462 FinderPath finderPath = null;
463 Object[] finderArgs = null;
464
465 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
466 (orderByComparator == null)) {
467 pagination = false;
468 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
469 finderArgs = FINDER_ARGS_EMPTY;
470 }
471 else {
472 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
473 finderArgs = new Object[] { start, end, orderByComparator };
474 }
475
476 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
477 finderArgs, this);
478
479 if (list == null) {
480 StringBundler query = null;
481 String sql = null;
482
483 if (orderByComparator != null) {
484 query = new StringBundler(2 +
485 (orderByComparator.getOrderByFields().length * 3));
486
487 query.append(_SQL_SELECT_COUNTER);
488
489 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
490 orderByComparator);
491
492 sql = query.toString();
493 }
494 else {
495 sql = _SQL_SELECT_COUNTER;
496
497 if (pagination) {
498 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
499 }
500 }
501
502 Session session = null;
503
504 try {
505 session = openSession();
506
507 Query q = session.createQuery(sql);
508
509 if (!pagination) {
510 list = (List<Counter>)QueryUtil.list(q, getDialect(),
511 start, end, false);
512
513 Collections.sort(list);
514
515 list = new UnmodifiableList<Counter>(list);
516 }
517 else {
518 list = (List<Counter>)QueryUtil.list(q, getDialect(),
519 start, end);
520 }
521
522 cacheResult(list);
523
524 FinderCacheUtil.putResult(finderPath, finderArgs, list);
525 }
526 catch (Exception e) {
527 FinderCacheUtil.removeResult(finderPath, finderArgs);
528
529 throw processException(e);
530 }
531 finally {
532 closeSession(session);
533 }
534 }
535
536 return list;
537 }
538
539
544 @Override
545 public void removeAll() throws SystemException {
546 for (Counter counter : findAll()) {
547 remove(counter);
548 }
549 }
550
551
557 @Override
558 public int countAll() throws SystemException {
559 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
560 FINDER_ARGS_EMPTY, this);
561
562 if (count == null) {
563 Session session = null;
564
565 try {
566 session = openSession();
567
568 Query q = session.createQuery(_SQL_COUNT_COUNTER);
569
570 count = (Long)q.uniqueResult();
571
572 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
573 FINDER_ARGS_EMPTY, count);
574 }
575 catch (Exception e) {
576 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
577 FINDER_ARGS_EMPTY);
578
579 throw processException(e);
580 }
581 finally {
582 closeSession(session);
583 }
584 }
585
586 return count.intValue();
587 }
588
589
592 public void afterPropertiesSet() {
593 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
594 com.liferay.portal.util.PropsUtil.get(
595 "value.object.listener.com.liferay.counter.model.Counter")));
596
597 if (listenerClassNames.length > 0) {
598 try {
599 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
600
601 for (String listenerClassName : listenerClassNames) {
602 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
603 getClassLoader(), listenerClassName));
604 }
605
606 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
607 }
608 catch (Exception e) {
609 _log.error(e);
610 }
611 }
612 }
613
614 public void destroy() {
615 EntityCacheUtil.removeCache(CounterImpl.class.getName());
616 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
617 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
618 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
619 }
620
621 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
622 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
623 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
624 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
625 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
626 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
627 private static Counter _nullCounter = new CounterImpl() {
628 @Override
629 public Object clone() {
630 return this;
631 }
632
633 @Override
634 public CacheModel<Counter> toCacheModel() {
635 return _nullCounterCacheModel;
636 }
637 };
638
639 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
640 @Override
641 public Counter toEntityModel() {
642 return _nullCounter;
643 }
644 };
645 }