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 public CounterPersistenceImpl() {
083 setModelClass(Counter.class);
084 }
085
086
091 @Override
092 public void cacheResult(Counter counter) {
093 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
094 CounterImpl.class, counter.getPrimaryKey(), counter);
095
096 counter.resetOriginalValues();
097 }
098
099
104 @Override
105 public void cacheResult(List<Counter> counters) {
106 for (Counter counter : counters) {
107 if (EntityCacheUtil.getResult(
108 CounterModelImpl.ENTITY_CACHE_ENABLED,
109 CounterImpl.class, counter.getPrimaryKey()) == null) {
110 cacheResult(counter);
111 }
112 else {
113 counter.resetOriginalValues();
114 }
115 }
116 }
117
118
125 @Override
126 public void clearCache() {
127 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
128 CacheRegistryUtil.clear(CounterImpl.class.getName());
129 }
130
131 EntityCacheUtil.clearCache(CounterImpl.class.getName());
132
133 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
135 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
136 }
137
138
145 @Override
146 public void clearCache(Counter counter) {
147 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
148 CounterImpl.class, counter.getPrimaryKey());
149
150 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
151 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
152 }
153
154 @Override
155 public void clearCache(List<Counter> counters) {
156 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
157 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
158
159 for (Counter counter : counters) {
160 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
161 CounterImpl.class, counter.getPrimaryKey());
162 }
163 }
164
165
171 @Override
172 public Counter create(String name) {
173 Counter counter = new CounterImpl();
174
175 counter.setNew(true);
176 counter.setPrimaryKey(name);
177
178 return counter;
179 }
180
181
189 @Override
190 public Counter remove(String name)
191 throws NoSuchCounterException, SystemException {
192 return remove((Serializable)name);
193 }
194
195
203 @Override
204 public Counter remove(Serializable primaryKey)
205 throws NoSuchCounterException, SystemException {
206 Session session = null;
207
208 try {
209 session = openSession();
210
211 Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
212
213 if (counter == null) {
214 if (_log.isWarnEnabled()) {
215 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
216 }
217
218 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
219 primaryKey);
220 }
221
222 return remove(counter);
223 }
224 catch (NoSuchCounterException nsee) {
225 throw nsee;
226 }
227 catch (Exception e) {
228 throw processException(e);
229 }
230 finally {
231 closeSession(session);
232 }
233 }
234
235 @Override
236 protected Counter removeImpl(Counter counter) throws SystemException {
237 counter = toUnwrappedModel(counter);
238
239 Session session = null;
240
241 try {
242 session = openSession();
243
244 if (!session.contains(counter)) {
245 counter = (Counter)session.get(CounterImpl.class,
246 counter.getPrimaryKeyObj());
247 }
248
249 if (counter != null) {
250 session.delete(counter);
251 }
252 }
253 catch (Exception e) {
254 throw processException(e);
255 }
256 finally {
257 closeSession(session);
258 }
259
260 if (counter != null) {
261 clearCache(counter);
262 }
263
264 return counter;
265 }
266
267 @Override
268 public Counter updateImpl(com.liferay.counter.model.Counter counter)
269 throws SystemException {
270 counter = toUnwrappedModel(counter);
271
272 boolean isNew = counter.isNew();
273
274 Session session = null;
275
276 try {
277 session = openSession();
278
279 if (counter.isNew()) {
280 session.save(counter);
281
282 counter.setNew(false);
283 }
284 else {
285 session.merge(counter);
286 }
287 }
288 catch (Exception e) {
289 throw processException(e);
290 }
291 finally {
292 closeSession(session);
293 }
294
295 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
296
297 if (isNew) {
298 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
299 }
300
301 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
302 CounterImpl.class, counter.getPrimaryKey(), counter);
303
304 return counter;
305 }
306
307 protected Counter toUnwrappedModel(Counter counter) {
308 if (counter instanceof CounterImpl) {
309 return counter;
310 }
311
312 CounterImpl counterImpl = new CounterImpl();
313
314 counterImpl.setNew(counter.isNew());
315 counterImpl.setPrimaryKey(counter.getPrimaryKey());
316
317 counterImpl.setName(counter.getName());
318 counterImpl.setCurrentId(counter.getCurrentId());
319
320 return counterImpl;
321 }
322
323
331 @Override
332 public Counter findByPrimaryKey(Serializable primaryKey)
333 throws NoSuchCounterException, SystemException {
334 Counter counter = fetchByPrimaryKey(primaryKey);
335
336 if (counter == null) {
337 if (_log.isWarnEnabled()) {
338 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
339 }
340
341 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
342 primaryKey);
343 }
344
345 return counter;
346 }
347
348
356 @Override
357 public Counter findByPrimaryKey(String name)
358 throws NoSuchCounterException, SystemException {
359 return findByPrimaryKey((Serializable)name);
360 }
361
362
369 @Override
370 public Counter fetchByPrimaryKey(Serializable primaryKey)
371 throws SystemException {
372 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
373 CounterImpl.class, primaryKey);
374
375 if (counter == _nullCounter) {
376 return null;
377 }
378
379 if (counter == null) {
380 Session session = null;
381
382 try {
383 session = openSession();
384
385 counter = (Counter)session.get(CounterImpl.class, primaryKey);
386
387 if (counter != null) {
388 cacheResult(counter);
389 }
390 else {
391 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
392 CounterImpl.class, primaryKey, _nullCounter);
393 }
394 }
395 catch (Exception e) {
396 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
397 CounterImpl.class, primaryKey);
398
399 throw processException(e);
400 }
401 finally {
402 closeSession(session);
403 }
404 }
405
406 return counter;
407 }
408
409
416 @Override
417 public Counter fetchByPrimaryKey(String name) throws SystemException {
418 return fetchByPrimaryKey((Serializable)name);
419 }
420
421
427 @Override
428 public List<Counter> findAll() throws SystemException {
429 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
430 }
431
432
444 @Override
445 public List<Counter> findAll(int start, int end) throws SystemException {
446 return findAll(start, end, null);
447 }
448
449
462 @Override
463 public List<Counter> findAll(int start, int end,
464 OrderByComparator orderByComparator) throws SystemException {
465 boolean pagination = true;
466 FinderPath finderPath = null;
467 Object[] finderArgs = null;
468
469 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
470 (orderByComparator == null)) {
471 pagination = false;
472 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
473 finderArgs = FINDER_ARGS_EMPTY;
474 }
475 else {
476 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
477 finderArgs = new Object[] { start, end, orderByComparator };
478 }
479
480 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
481 finderArgs, this);
482
483 if (list == null) {
484 StringBundler query = null;
485 String sql = null;
486
487 if (orderByComparator != null) {
488 query = new StringBundler(2 +
489 (orderByComparator.getOrderByFields().length * 3));
490
491 query.append(_SQL_SELECT_COUNTER);
492
493 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
494 orderByComparator);
495
496 sql = query.toString();
497 }
498 else {
499 sql = _SQL_SELECT_COUNTER;
500
501 if (pagination) {
502 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
503 }
504 }
505
506 Session session = null;
507
508 try {
509 session = openSession();
510
511 Query q = session.createQuery(sql);
512
513 if (!pagination) {
514 list = (List<Counter>)QueryUtil.list(q, getDialect(),
515 start, end, false);
516
517 Collections.sort(list);
518
519 list = new UnmodifiableList<Counter>(list);
520 }
521 else {
522 list = (List<Counter>)QueryUtil.list(q, getDialect(),
523 start, end);
524 }
525
526 cacheResult(list);
527
528 FinderCacheUtil.putResult(finderPath, finderArgs, list);
529 }
530 catch (Exception e) {
531 FinderCacheUtil.removeResult(finderPath, finderArgs);
532
533 throw processException(e);
534 }
535 finally {
536 closeSession(session);
537 }
538 }
539
540 return list;
541 }
542
543
548 @Override
549 public void removeAll() throws SystemException {
550 for (Counter counter : findAll()) {
551 remove(counter);
552 }
553 }
554
555
561 @Override
562 public int countAll() throws SystemException {
563 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
564 FINDER_ARGS_EMPTY, this);
565
566 if (count == null) {
567 Session session = null;
568
569 try {
570 session = openSession();
571
572 Query q = session.createQuery(_SQL_COUNT_COUNTER);
573
574 count = (Long)q.uniqueResult();
575
576 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
577 FINDER_ARGS_EMPTY, count);
578 }
579 catch (Exception e) {
580 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
581 FINDER_ARGS_EMPTY);
582
583 throw processException(e);
584 }
585 finally {
586 closeSession(session);
587 }
588 }
589
590 return count.intValue();
591 }
592
593
596 public void afterPropertiesSet() {
597 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
598 com.liferay.portal.util.PropsUtil.get(
599 "value.object.listener.com.liferay.counter.model.Counter")));
600
601 if (listenerClassNames.length > 0) {
602 try {
603 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
604
605 for (String listenerClassName : listenerClassNames) {
606 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
607 getClassLoader(), listenerClassName));
608 }
609
610 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
611 }
612 catch (Exception e) {
613 _log.error(e);
614 }
615 }
616 }
617
618 public void destroy() {
619 EntityCacheUtil.removeCache(CounterImpl.class.getName());
620 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
621 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
622 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
623 }
624
625 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
626 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
627 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
628 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
629 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
630 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
631 private static Counter _nullCounter = new CounterImpl() {
632 @Override
633 public Object clone() {
634 return this;
635 }
636
637 @Override
638 public CacheModel<Counter> toCacheModel() {
639 return _nullCounterCacheModel;
640 }
641 };
642
643 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
644 @Override
645 public Counter toEntityModel() {
646 return _nullCounter;
647 }
648 };
649 }