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 public void cacheResult(Counter counter) {
088 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
089 CounterImpl.class, counter.getPrimaryKey(), counter);
090
091 counter.resetOriginalValues();
092 }
093
094
099 public void cacheResult(List<Counter> counters) {
100 for (Counter counter : counters) {
101 if (EntityCacheUtil.getResult(
102 CounterModelImpl.ENTITY_CACHE_ENABLED,
103 CounterImpl.class, counter.getPrimaryKey()) == null) {
104 cacheResult(counter);
105 }
106 else {
107 counter.resetOriginalValues();
108 }
109 }
110 }
111
112
119 @Override
120 public void clearCache() {
121 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
122 CacheRegistryUtil.clear(CounterImpl.class.getName());
123 }
124
125 EntityCacheUtil.clearCache(CounterImpl.class.getName());
126
127 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
128 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
129 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
130 }
131
132
139 @Override
140 public void clearCache(Counter counter) {
141 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
142 CounterImpl.class, counter.getPrimaryKey());
143
144 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
145 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
146 }
147
148 @Override
149 public void clearCache(List<Counter> counters) {
150 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
151 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
152
153 for (Counter counter : counters) {
154 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
155 CounterImpl.class, counter.getPrimaryKey());
156 }
157 }
158
159
165 public Counter create(String name) {
166 Counter counter = new CounterImpl();
167
168 counter.setNew(true);
169 counter.setPrimaryKey(name);
170
171 return counter;
172 }
173
174
182 public Counter remove(String name)
183 throws NoSuchCounterException, SystemException {
184 return remove((Serializable)name);
185 }
186
187
195 @Override
196 public Counter remove(Serializable primaryKey)
197 throws NoSuchCounterException, SystemException {
198 Session session = null;
199
200 try {
201 session = openSession();
202
203 Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
204
205 if (counter == null) {
206 if (_log.isWarnEnabled()) {
207 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
208 }
209
210 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
211 primaryKey);
212 }
213
214 return remove(counter);
215 }
216 catch (NoSuchCounterException nsee) {
217 throw nsee;
218 }
219 catch (Exception e) {
220 throw processException(e);
221 }
222 finally {
223 closeSession(session);
224 }
225 }
226
227 @Override
228 protected Counter removeImpl(Counter counter) throws SystemException {
229 counter = toUnwrappedModel(counter);
230
231 Session session = null;
232
233 try {
234 session = openSession();
235
236 if (!session.contains(counter)) {
237 counter = (Counter)session.get(CounterImpl.class,
238 counter.getPrimaryKeyObj());
239 }
240
241 if (counter != null) {
242 session.delete(counter);
243 }
244 }
245 catch (Exception e) {
246 throw processException(e);
247 }
248 finally {
249 closeSession(session);
250 }
251
252 if (counter != null) {
253 clearCache(counter);
254 }
255
256 return counter;
257 }
258
259 @Override
260 public Counter updateImpl(com.liferay.counter.model.Counter counter)
261 throws SystemException {
262 counter = toUnwrappedModel(counter);
263
264 boolean isNew = counter.isNew();
265
266 Session session = null;
267
268 try {
269 session = openSession();
270
271 if (counter.isNew()) {
272 session.save(counter);
273
274 counter.setNew(false);
275 }
276 else {
277 session.merge(counter);
278 }
279 }
280 catch (Exception e) {
281 throw processException(e);
282 }
283 finally {
284 closeSession(session);
285 }
286
287 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
288
289 if (isNew) {
290 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
291 }
292
293 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
294 CounterImpl.class, counter.getPrimaryKey(), counter);
295
296 return counter;
297 }
298
299 protected Counter toUnwrappedModel(Counter counter) {
300 if (counter instanceof CounterImpl) {
301 return counter;
302 }
303
304 CounterImpl counterImpl = new CounterImpl();
305
306 counterImpl.setNew(counter.isNew());
307 counterImpl.setPrimaryKey(counter.getPrimaryKey());
308
309 counterImpl.setName(counter.getName());
310 counterImpl.setCurrentId(counter.getCurrentId());
311
312 return counterImpl;
313 }
314
315
323 @Override
324 public Counter findByPrimaryKey(Serializable primaryKey)
325 throws NoSuchCounterException, SystemException {
326 Counter counter = fetchByPrimaryKey(primaryKey);
327
328 if (counter == null) {
329 if (_log.isWarnEnabled()) {
330 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
331 }
332
333 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
334 primaryKey);
335 }
336
337 return counter;
338 }
339
340
348 public Counter findByPrimaryKey(String name)
349 throws NoSuchCounterException, SystemException {
350 return findByPrimaryKey((Serializable)name);
351 }
352
353
360 @Override
361 public Counter fetchByPrimaryKey(Serializable primaryKey)
362 throws SystemException {
363 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
364 CounterImpl.class, primaryKey);
365
366 if (counter == _nullCounter) {
367 return null;
368 }
369
370 if (counter == null) {
371 Session session = null;
372
373 try {
374 session = openSession();
375
376 counter = (Counter)session.get(CounterImpl.class, primaryKey);
377
378 if (counter != null) {
379 cacheResult(counter);
380 }
381 else {
382 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
383 CounterImpl.class, primaryKey, _nullCounter);
384 }
385 }
386 catch (Exception e) {
387 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
388 CounterImpl.class, primaryKey);
389
390 throw processException(e);
391 }
392 finally {
393 closeSession(session);
394 }
395 }
396
397 return counter;
398 }
399
400
407 public Counter fetchByPrimaryKey(String name) throws SystemException {
408 return fetchByPrimaryKey((Serializable)name);
409 }
410
411
417 public List<Counter> findAll() throws SystemException {
418 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
419 }
420
421
433 public List<Counter> findAll(int start, int end) throws SystemException {
434 return findAll(start, end, null);
435 }
436
437
450 public List<Counter> findAll(int start, int end,
451 OrderByComparator orderByComparator) throws SystemException {
452 boolean pagination = true;
453 FinderPath finderPath = null;
454 Object[] finderArgs = null;
455
456 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
457 (orderByComparator == null)) {
458 pagination = false;
459 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
460 finderArgs = FINDER_ARGS_EMPTY;
461 }
462 else {
463 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
464 finderArgs = new Object[] { start, end, orderByComparator };
465 }
466
467 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
468 finderArgs, this);
469
470 if (list == null) {
471 StringBundler query = null;
472 String sql = null;
473
474 if (orderByComparator != null) {
475 query = new StringBundler(2 +
476 (orderByComparator.getOrderByFields().length * 3));
477
478 query.append(_SQL_SELECT_COUNTER);
479
480 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
481 orderByComparator);
482
483 sql = query.toString();
484 }
485 else {
486 sql = _SQL_SELECT_COUNTER;
487
488 if (pagination) {
489 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
490 }
491 }
492
493 Session session = null;
494
495 try {
496 session = openSession();
497
498 Query q = session.createQuery(sql);
499
500 if (!pagination) {
501 list = (List<Counter>)QueryUtil.list(q, getDialect(),
502 start, end, false);
503
504 Collections.sort(list);
505
506 list = new UnmodifiableList<Counter>(list);
507 }
508 else {
509 list = (List<Counter>)QueryUtil.list(q, getDialect(),
510 start, end);
511 }
512
513 cacheResult(list);
514
515 FinderCacheUtil.putResult(finderPath, finderArgs, list);
516 }
517 catch (Exception e) {
518 FinderCacheUtil.removeResult(finderPath, finderArgs);
519
520 throw processException(e);
521 }
522 finally {
523 closeSession(session);
524 }
525 }
526
527 return list;
528 }
529
530
535 public void removeAll() throws SystemException {
536 for (Counter counter : findAll()) {
537 remove(counter);
538 }
539 }
540
541
547 public int countAll() throws SystemException {
548 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
549 FINDER_ARGS_EMPTY, this);
550
551 if (count == null) {
552 Session session = null;
553
554 try {
555 session = openSession();
556
557 Query q = session.createQuery(_SQL_COUNT_COUNTER);
558
559 count = (Long)q.uniqueResult();
560
561 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
562 FINDER_ARGS_EMPTY, count);
563 }
564 catch (Exception e) {
565 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
566 FINDER_ARGS_EMPTY);
567
568 throw processException(e);
569 }
570 finally {
571 closeSession(session);
572 }
573 }
574
575 return count.intValue();
576 }
577
578
581 public void afterPropertiesSet() {
582 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
583 com.liferay.portal.util.PropsUtil.get(
584 "value.object.listener.com.liferay.counter.model.Counter")));
585
586 if (listenerClassNames.length > 0) {
587 try {
588 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
589
590 for (String listenerClassName : listenerClassNames) {
591 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
592 getClassLoader(), listenerClassName));
593 }
594
595 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
596 }
597 catch (Exception e) {
598 _log.error(e);
599 }
600 }
601 }
602
603 public void destroy() {
604 EntityCacheUtil.removeCache(CounterImpl.class.getName());
605 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
606 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
607 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
608 }
609
610 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
611 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
612 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
613 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
614 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
615 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
616 private static Counter _nullCounter = new CounterImpl() {
617 @Override
618 public Object clone() {
619 return this;
620 }
621
622 @Override
623 public CacheModel<Counter> toCacheModel() {
624 return _nullCounterCacheModel;
625 }
626 };
627
628 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
629 public Counter toEntityModel() {
630 return _nullCounter;
631 }
632 };
633 }