001
014
015 package com.liferay.counter.service.persistence.impl;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.counter.NoSuchCounterException;
020 import com.liferay.counter.model.Counter;
021 import com.liferay.counter.model.impl.CounterImpl;
022 import com.liferay.counter.model.impl.CounterModelImpl;
023 import com.liferay.counter.service.persistence.CounterPersistence;
024
025 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
026 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
027 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
028 import com.liferay.portal.kernel.dao.orm.FinderPath;
029 import com.liferay.portal.kernel.dao.orm.Query;
030 import com.liferay.portal.kernel.dao.orm.QueryUtil;
031 import com.liferay.portal.kernel.dao.orm.Session;
032 import com.liferay.portal.kernel.log.Log;
033 import com.liferay.portal.kernel.log.LogFactoryUtil;
034 import com.liferay.portal.kernel.util.OrderByComparator;
035 import com.liferay.portal.kernel.util.StringBundler;
036 import com.liferay.portal.kernel.util.StringPool;
037 import com.liferay.portal.model.CacheModel;
038 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
039
040 import java.io.Serializable;
041
042 import java.util.Collections;
043 import java.util.HashMap;
044 import java.util.HashSet;
045 import java.util.Iterator;
046 import java.util.List;
047 import java.util.Map;
048 import java.util.Set;
049
050
062 @ProviderType
063 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
064 implements CounterPersistence {
065
070 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
071 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072 ".List1";
073 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074 ".List2";
075 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
076 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
077 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
078 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
079 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
080 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
081 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
082 CounterModelImpl.FINDER_CACHE_ENABLED, Long.class,
083 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
084
085 public CounterPersistenceImpl() {
086 setModelClass(Counter.class);
087 }
088
089
094 @Override
095 public void cacheResult(Counter counter) {
096 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
097 CounterImpl.class, counter.getPrimaryKey(), counter);
098
099 counter.resetOriginalValues();
100 }
101
102
107 @Override
108 public void cacheResult(List<Counter> counters) {
109 for (Counter counter : counters) {
110 if (EntityCacheUtil.getResult(
111 CounterModelImpl.ENTITY_CACHE_ENABLED,
112 CounterImpl.class, counter.getPrimaryKey()) == null) {
113 cacheResult(counter);
114 }
115 else {
116 counter.resetOriginalValues();
117 }
118 }
119 }
120
121
128 @Override
129 public void clearCache() {
130 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
131 CacheRegistryUtil.clear(CounterImpl.class.getName());
132 }
133
134 EntityCacheUtil.clearCache(CounterImpl.class);
135
136 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
137 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
138 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
139 }
140
141
148 @Override
149 public void clearCache(Counter counter) {
150 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
151 CounterImpl.class, counter.getPrimaryKey());
152
153 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
154 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
155 }
156
157 @Override
158 public void clearCache(List<Counter> counters) {
159 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
160 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
161
162 for (Counter counter : counters) {
163 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
164 CounterImpl.class, counter.getPrimaryKey());
165 }
166 }
167
168
174 @Override
175 public Counter create(String name) {
176 Counter counter = new CounterImpl();
177
178 counter.setNew(true);
179 counter.setPrimaryKey(name);
180
181 return counter;
182 }
183
184
191 @Override
192 public Counter remove(String name) throws NoSuchCounterException {
193 return remove((Serializable)name);
194 }
195
196
203 @Override
204 public Counter remove(Serializable primaryKey)
205 throws NoSuchCounterException {
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) {
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 counter = toUnwrappedModel(counter);
270
271 boolean isNew = counter.isNew();
272
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 if (counter.isNew()) {
279 session.save(counter);
280
281 counter.setNew(false);
282 }
283 else {
284 session.merge(counter);
285 }
286 }
287 catch (Exception e) {
288 throw processException(e);
289 }
290 finally {
291 closeSession(session);
292 }
293
294 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
295
296 if (isNew) {
297 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
298 }
299
300 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
301 CounterImpl.class, counter.getPrimaryKey(), counter, false);
302
303 counter.resetOriginalValues();
304
305 return counter;
306 }
307
308 protected Counter toUnwrappedModel(Counter counter) {
309 if (counter instanceof CounterImpl) {
310 return counter;
311 }
312
313 CounterImpl counterImpl = new CounterImpl();
314
315 counterImpl.setNew(counter.isNew());
316 counterImpl.setPrimaryKey(counter.getPrimaryKey());
317
318 counterImpl.setName(counter.getName());
319 counterImpl.setCurrentId(counter.getCurrentId());
320
321 return counterImpl;
322 }
323
324
331 @Override
332 public Counter findByPrimaryKey(Serializable primaryKey)
333 throws NoSuchCounterException {
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
355 @Override
356 public Counter findByPrimaryKey(String name) throws NoSuchCounterException {
357 return findByPrimaryKey((Serializable)name);
358 }
359
360
366 @Override
367 public Counter fetchByPrimaryKey(Serializable primaryKey) {
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
411 @Override
412 public Counter fetchByPrimaryKey(String name) {
413 return fetchByPrimaryKey((Serializable)name);
414 }
415
416 @Override
417 public Map<Serializable, Counter> fetchByPrimaryKeys(
418 Set<Serializable> primaryKeys) {
419 if (primaryKeys.isEmpty()) {
420 return Collections.emptyMap();
421 }
422
423 Map<Serializable, Counter> map = new HashMap<Serializable, Counter>();
424
425 if (primaryKeys.size() == 1) {
426 Iterator<Serializable> iterator = primaryKeys.iterator();
427
428 Serializable primaryKey = iterator.next();
429
430 Counter counter = fetchByPrimaryKey(primaryKey);
431
432 if (counter != null) {
433 map.put(primaryKey, counter);
434 }
435
436 return map;
437 }
438
439 Set<Serializable> uncachedPrimaryKeys = null;
440
441 for (Serializable primaryKey : primaryKeys) {
442 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
443 CounterImpl.class, primaryKey);
444
445 if (counter == null) {
446 if (uncachedPrimaryKeys == null) {
447 uncachedPrimaryKeys = new HashSet<Serializable>();
448 }
449
450 uncachedPrimaryKeys.add(primaryKey);
451 }
452 else {
453 map.put(primaryKey, counter);
454 }
455 }
456
457 if (uncachedPrimaryKeys == null) {
458 return map;
459 }
460
461 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 4) +
462 1);
463
464 query.append(_SQL_SELECT_COUNTER_WHERE_PKS_IN);
465
466 for (Serializable primaryKey : uncachedPrimaryKeys) {
467 query.append(StringPool.QUOTE);
468 query.append((String)primaryKey);
469 query.append(StringPool.QUOTE);
470
471 query.append(StringPool.COMMA);
472 }
473
474 query.setIndex(query.index() - 1);
475
476 query.append(StringPool.CLOSE_PARENTHESIS);
477
478 String sql = query.toString();
479
480 Session session = null;
481
482 try {
483 session = openSession();
484
485 Query q = session.createQuery(sql);
486
487 for (Counter counter : (List<Counter>)q.list()) {
488 map.put(counter.getPrimaryKeyObj(), counter);
489
490 cacheResult(counter);
491
492 uncachedPrimaryKeys.remove(counter.getPrimaryKeyObj());
493 }
494
495 for (Serializable primaryKey : uncachedPrimaryKeys) {
496 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
497 CounterImpl.class, primaryKey, _nullCounter);
498 }
499 }
500 catch (Exception e) {
501 throw processException(e);
502 }
503 finally {
504 closeSession(session);
505 }
506
507 return map;
508 }
509
510
515 @Override
516 public List<Counter> findAll() {
517 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
518 }
519
520
531 @Override
532 public List<Counter> findAll(int start, int end) {
533 return findAll(start, end, null);
534 }
535
536
548 @Override
549 public List<Counter> findAll(int start, int end,
550 OrderByComparator<Counter> orderByComparator) {
551 boolean pagination = true;
552 FinderPath finderPath = null;
553 Object[] finderArgs = null;
554
555 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
556 (orderByComparator == null)) {
557 pagination = false;
558 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
559 finderArgs = FINDER_ARGS_EMPTY;
560 }
561 else {
562 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
563 finderArgs = new Object[] { start, end, orderByComparator };
564 }
565
566 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
567 finderArgs, this);
568
569 if (list == null) {
570 StringBundler query = null;
571 String sql = null;
572
573 if (orderByComparator != null) {
574 query = new StringBundler(2 +
575 (orderByComparator.getOrderByFields().length * 3));
576
577 query.append(_SQL_SELECT_COUNTER);
578
579 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
580 orderByComparator);
581
582 sql = query.toString();
583 }
584 else {
585 sql = _SQL_SELECT_COUNTER;
586
587 if (pagination) {
588 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
589 }
590 }
591
592 Session session = null;
593
594 try {
595 session = openSession();
596
597 Query q = session.createQuery(sql);
598
599 if (!pagination) {
600 list = (List<Counter>)QueryUtil.list(q, getDialect(),
601 start, end, false);
602
603 Collections.sort(list);
604
605 list = Collections.unmodifiableList(list);
606 }
607 else {
608 list = (List<Counter>)QueryUtil.list(q, getDialect(),
609 start, end);
610 }
611
612 cacheResult(list);
613
614 FinderCacheUtil.putResult(finderPath, finderArgs, list);
615 }
616 catch (Exception e) {
617 FinderCacheUtil.removeResult(finderPath, finderArgs);
618
619 throw processException(e);
620 }
621 finally {
622 closeSession(session);
623 }
624 }
625
626 return list;
627 }
628
629
633 @Override
634 public void removeAll() {
635 for (Counter counter : findAll()) {
636 remove(counter);
637 }
638 }
639
640
645 @Override
646 public int countAll() {
647 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
648 FINDER_ARGS_EMPTY, this);
649
650 if (count == null) {
651 Session session = null;
652
653 try {
654 session = openSession();
655
656 Query q = session.createQuery(_SQL_COUNT_COUNTER);
657
658 count = (Long)q.uniqueResult();
659
660 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
661 FINDER_ARGS_EMPTY, count);
662 }
663 catch (Exception e) {
664 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
665 FINDER_ARGS_EMPTY);
666
667 throw processException(e);
668 }
669 finally {
670 closeSession(session);
671 }
672 }
673
674 return count.intValue();
675 }
676
677
680 public void afterPropertiesSet() {
681 }
682
683 public void destroy() {
684 EntityCacheUtil.removeCache(CounterImpl.class.getName());
685 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
686 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
687 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
688 }
689
690 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
691 private static final String _SQL_SELECT_COUNTER_WHERE_PKS_IN = "SELECT counter FROM Counter counter WHERE name IN (";
692 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
693 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
694 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
695 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
696 private static final Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
697 private static final Counter _nullCounter = new CounterImpl() {
698 @Override
699 public Object clone() {
700 return this;
701 }
702
703 @Override
704 public CacheModel<Counter> toCacheModel() {
705 return _nullCounterCacheModel;
706 }
707 };
708
709 private static final CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
710 @Override
711 public Counter toEntityModel() {
712 return _nullCounter;
713 }
714 };
715 }