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.dao.orm.EntityCacheUtil;
026 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
027 import com.liferay.portal.kernel.dao.orm.FinderPath;
028 import com.liferay.portal.kernel.dao.orm.Query;
029 import com.liferay.portal.kernel.dao.orm.QueryUtil;
030 import com.liferay.portal.kernel.dao.orm.Session;
031 import com.liferay.portal.kernel.log.Log;
032 import com.liferay.portal.kernel.log.LogFactoryUtil;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.model.CacheModel;
037 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
038
039 import java.io.Serializable;
040
041 import java.util.Collections;
042 import java.util.HashMap;
043 import java.util.HashSet;
044 import java.util.Iterator;
045 import java.util.List;
046 import java.util.Map;
047 import java.util.Set;
048
049
061 @ProviderType
062 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
063 implements CounterPersistence {
064
069 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
070 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071 ".List1";
072 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073 ".List2";
074 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
075 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
076 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
078 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
079 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
080 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
081 CounterModelImpl.FINDER_CACHE_ENABLED, Long.class,
082 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
083
084 public CounterPersistenceImpl() {
085 setModelClass(Counter.class);
086 }
087
088
093 @Override
094 public void cacheResult(Counter counter) {
095 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
096 CounterImpl.class, counter.getPrimaryKey(), counter);
097
098 counter.resetOriginalValues();
099 }
100
101
106 @Override
107 public void cacheResult(List<Counter> counters) {
108 for (Counter counter : counters) {
109 if (EntityCacheUtil.getResult(
110 CounterModelImpl.ENTITY_CACHE_ENABLED,
111 CounterImpl.class, counter.getPrimaryKey()) == null) {
112 cacheResult(counter);
113 }
114 else {
115 counter.resetOriginalValues();
116 }
117 }
118 }
119
120
127 @Override
128 public void clearCache() {
129 EntityCacheUtil.clearCache(CounterImpl.class);
130
131 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
133 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
134 }
135
136
143 @Override
144 public void clearCache(Counter counter) {
145 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
146 CounterImpl.class, counter.getPrimaryKey());
147
148 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
149 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
150 }
151
152 @Override
153 public void clearCache(List<Counter> counters) {
154 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
155 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
156
157 for (Counter counter : counters) {
158 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
159 CounterImpl.class, counter.getPrimaryKey());
160 }
161 }
162
163
169 @Override
170 public Counter create(String name) {
171 Counter counter = new CounterImpl();
172
173 counter.setNew(true);
174 counter.setPrimaryKey(name);
175
176 return counter;
177 }
178
179
186 @Override
187 public Counter remove(String name) throws NoSuchCounterException {
188 return remove((Serializable)name);
189 }
190
191
198 @Override
199 public Counter remove(Serializable primaryKey)
200 throws NoSuchCounterException {
201 Session session = null;
202
203 try {
204 session = openSession();
205
206 Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
207
208 if (counter == null) {
209 if (_log.isWarnEnabled()) {
210 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
211 }
212
213 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
214 primaryKey);
215 }
216
217 return remove(counter);
218 }
219 catch (NoSuchCounterException nsee) {
220 throw nsee;
221 }
222 catch (Exception e) {
223 throw processException(e);
224 }
225 finally {
226 closeSession(session);
227 }
228 }
229
230 @Override
231 protected Counter removeImpl(Counter counter) {
232 counter = toUnwrappedModel(counter);
233
234 Session session = null;
235
236 try {
237 session = openSession();
238
239 if (!session.contains(counter)) {
240 counter = (Counter)session.get(CounterImpl.class,
241 counter.getPrimaryKeyObj());
242 }
243
244 if (counter != null) {
245 session.delete(counter);
246 }
247 }
248 catch (Exception e) {
249 throw processException(e);
250 }
251 finally {
252 closeSession(session);
253 }
254
255 if (counter != null) {
256 clearCache(counter);
257 }
258
259 return counter;
260 }
261
262 @Override
263 public Counter updateImpl(Counter counter) {
264 counter = toUnwrappedModel(counter);
265
266 boolean isNew = counter.isNew();
267
268 Session session = null;
269
270 try {
271 session = openSession();
272
273 if (counter.isNew()) {
274 session.save(counter);
275
276 counter.setNew(false);
277 }
278 else {
279 session.merge(counter);
280 }
281 }
282 catch (Exception e) {
283 throw processException(e);
284 }
285 finally {
286 closeSession(session);
287 }
288
289 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
290
291 if (isNew) {
292 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
293 }
294
295 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
296 CounterImpl.class, counter.getPrimaryKey(), counter, false);
297
298 counter.resetOriginalValues();
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
326 @Override
327 public Counter findByPrimaryKey(Serializable primaryKey)
328 throws NoSuchCounterException {
329 Counter counter = fetchByPrimaryKey(primaryKey);
330
331 if (counter == null) {
332 if (_log.isWarnEnabled()) {
333 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
334 }
335
336 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
337 primaryKey);
338 }
339
340 return counter;
341 }
342
343
350 @Override
351 public Counter findByPrimaryKey(String name) throws NoSuchCounterException {
352 return findByPrimaryKey((Serializable)name);
353 }
354
355
361 @Override
362 public Counter fetchByPrimaryKey(Serializable primaryKey) {
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
406 @Override
407 public Counter fetchByPrimaryKey(String name) {
408 return fetchByPrimaryKey((Serializable)name);
409 }
410
411 @Override
412 public Map<Serializable, Counter> fetchByPrimaryKeys(
413 Set<Serializable> primaryKeys) {
414 if (primaryKeys.isEmpty()) {
415 return Collections.emptyMap();
416 }
417
418 Map<Serializable, Counter> map = new HashMap<Serializable, Counter>();
419
420 if (primaryKeys.size() == 1) {
421 Iterator<Serializable> iterator = primaryKeys.iterator();
422
423 Serializable primaryKey = iterator.next();
424
425 Counter counter = fetchByPrimaryKey(primaryKey);
426
427 if (counter != null) {
428 map.put(primaryKey, counter);
429 }
430
431 return map;
432 }
433
434 Set<Serializable> uncachedPrimaryKeys = null;
435
436 for (Serializable primaryKey : primaryKeys) {
437 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
438 CounterImpl.class, primaryKey);
439
440 if (counter == null) {
441 if (uncachedPrimaryKeys == null) {
442 uncachedPrimaryKeys = new HashSet<Serializable>();
443 }
444
445 uncachedPrimaryKeys.add(primaryKey);
446 }
447 else {
448 map.put(primaryKey, counter);
449 }
450 }
451
452 if (uncachedPrimaryKeys == null) {
453 return map;
454 }
455
456 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 4) +
457 1);
458
459 query.append(_SQL_SELECT_COUNTER_WHERE_PKS_IN);
460
461 for (Serializable primaryKey : uncachedPrimaryKeys) {
462 query.append(StringPool.QUOTE);
463 query.append((String)primaryKey);
464 query.append(StringPool.QUOTE);
465
466 query.append(StringPool.COMMA);
467 }
468
469 query.setIndex(query.index() - 1);
470
471 query.append(StringPool.CLOSE_PARENTHESIS);
472
473 String sql = query.toString();
474
475 Session session = null;
476
477 try {
478 session = openSession();
479
480 Query q = session.createQuery(sql);
481
482 for (Counter counter : (List<Counter>)q.list()) {
483 map.put(counter.getPrimaryKeyObj(), counter);
484
485 cacheResult(counter);
486
487 uncachedPrimaryKeys.remove(counter.getPrimaryKeyObj());
488 }
489
490 for (Serializable primaryKey : uncachedPrimaryKeys) {
491 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
492 CounterImpl.class, primaryKey, _nullCounter);
493 }
494 }
495 catch (Exception e) {
496 throw processException(e);
497 }
498 finally {
499 closeSession(session);
500 }
501
502 return map;
503 }
504
505
510 @Override
511 public List<Counter> findAll() {
512 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
513 }
514
515
526 @Override
527 public List<Counter> findAll(int start, int end) {
528 return findAll(start, end, null);
529 }
530
531
543 @Override
544 public List<Counter> findAll(int start, int end,
545 OrderByComparator<Counter> orderByComparator) {
546 boolean pagination = true;
547 FinderPath finderPath = null;
548 Object[] finderArgs = null;
549
550 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
551 (orderByComparator == null)) {
552 pagination = false;
553 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
554 finderArgs = FINDER_ARGS_EMPTY;
555 }
556 else {
557 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
558 finderArgs = new Object[] { start, end, orderByComparator };
559 }
560
561 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
562 finderArgs, this);
563
564 if (list == null) {
565 StringBundler query = null;
566 String sql = null;
567
568 if (orderByComparator != null) {
569 query = new StringBundler(2 +
570 (orderByComparator.getOrderByFields().length * 3));
571
572 query.append(_SQL_SELECT_COUNTER);
573
574 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
575 orderByComparator);
576
577 sql = query.toString();
578 }
579 else {
580 sql = _SQL_SELECT_COUNTER;
581
582 if (pagination) {
583 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
584 }
585 }
586
587 Session session = null;
588
589 try {
590 session = openSession();
591
592 Query q = session.createQuery(sql);
593
594 if (!pagination) {
595 list = (List<Counter>)QueryUtil.list(q, getDialect(),
596 start, end, false);
597
598 Collections.sort(list);
599
600 list = Collections.unmodifiableList(list);
601 }
602 else {
603 list = (List<Counter>)QueryUtil.list(q, getDialect(),
604 start, end);
605 }
606
607 cacheResult(list);
608
609 FinderCacheUtil.putResult(finderPath, finderArgs, list);
610 }
611 catch (Exception e) {
612 FinderCacheUtil.removeResult(finderPath, finderArgs);
613
614 throw processException(e);
615 }
616 finally {
617 closeSession(session);
618 }
619 }
620
621 return list;
622 }
623
624
628 @Override
629 public void removeAll() {
630 for (Counter counter : findAll()) {
631 remove(counter);
632 }
633 }
634
635
640 @Override
641 public int countAll() {
642 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
643 FINDER_ARGS_EMPTY, this);
644
645 if (count == null) {
646 Session session = null;
647
648 try {
649 session = openSession();
650
651 Query q = session.createQuery(_SQL_COUNT_COUNTER);
652
653 count = (Long)q.uniqueResult();
654
655 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
656 FINDER_ARGS_EMPTY, count);
657 }
658 catch (Exception e) {
659 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
660 FINDER_ARGS_EMPTY);
661
662 throw processException(e);
663 }
664 finally {
665 closeSession(session);
666 }
667 }
668
669 return count.intValue();
670 }
671
672
675 public void afterPropertiesSet() {
676 }
677
678 public void destroy() {
679 EntityCacheUtil.removeCache(CounterImpl.class.getName());
680 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
681 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
682 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
683 }
684
685 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
686 private static final String _SQL_SELECT_COUNTER_WHERE_PKS_IN = "SELECT counter FROM Counter counter WHERE name IN (";
687 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
688 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
689 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
690 private static final Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
691 private static final Counter _nullCounter = new CounterImpl() {
692 @Override
693 public Object clone() {
694 return this;
695 }
696
697 @Override
698 public CacheModel<Counter> toCacheModel() {
699 return _nullCounterCacheModel;
700 }
701 };
702
703 private static final CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
704 @Override
705 public Counter toEntityModel() {
706 return _nullCounter;
707 }
708 };
709 }