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