001
014
015 package com.liferay.counter.service.persistence.impl;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.counter.exception.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.EntityCache;
026 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
027 import com.liferay.portal.kernel.dao.orm.FinderCache;
028 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
029 import com.liferay.portal.kernel.dao.orm.FinderPath;
030 import com.liferay.portal.kernel.dao.orm.Query;
031 import com.liferay.portal.kernel.dao.orm.QueryUtil;
032 import com.liferay.portal.kernel.dao.orm.Session;
033 import com.liferay.portal.kernel.log.Log;
034 import com.liferay.portal.kernel.log.LogFactoryUtil;
035 import com.liferay.portal.kernel.util.OrderByComparator;
036 import com.liferay.portal.kernel.util.StringBundler;
037 import com.liferay.portal.kernel.util.StringPool;
038 import com.liferay.portal.model.CacheModel;
039 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
040
041 import java.io.Serializable;
042
043 import java.util.Collections;
044 import java.util.HashMap;
045 import java.util.HashSet;
046 import java.util.Iterator;
047 import java.util.List;
048 import java.util.Map;
049 import java.util.Set;
050
051
063 @ProviderType
064 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
065 implements CounterPersistence {
066
071 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
072 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073 ".List1";
074 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075 ".List2";
076 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
077 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
078 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
080 CounterModelImpl.FINDER_CACHE_ENABLED, CounterImpl.class,
081 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
082 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
083 CounterModelImpl.FINDER_CACHE_ENABLED, Long.class,
084 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
085
086 public CounterPersistenceImpl() {
087 setModelClass(Counter.class);
088 }
089
090
095 @Override
096 public void cacheResult(Counter counter) {
097 entityCache.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
098 CounterImpl.class, counter.getPrimaryKey(), counter);
099
100 counter.resetOriginalValues();
101 }
102
103
108 @Override
109 public void cacheResult(List<Counter> counters) {
110 for (Counter counter : counters) {
111 if (entityCache.getResult(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 entityCache.clearCache(CounterImpl.class);
131
132 finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
133 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
134 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
135 }
136
137
144 @Override
145 public void clearCache(Counter counter) {
146 entityCache.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
147 CounterImpl.class, counter.getPrimaryKey());
148
149 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
150 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
151 }
152
153 @Override
154 public void clearCache(List<Counter> counters) {
155 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
156 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
157
158 for (Counter counter : counters) {
159 entityCache.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
160 CounterImpl.class, counter.getPrimaryKey());
161 }
162 }
163
164
170 @Override
171 public Counter create(String name) {
172 Counter counter = new CounterImpl();
173
174 counter.setNew(true);
175 counter.setPrimaryKey(name);
176
177 return counter;
178 }
179
180
187 @Override
188 public Counter remove(String name) throws NoSuchCounterException {
189 return remove((Serializable)name);
190 }
191
192
199 @Override
200 public Counter remove(Serializable primaryKey)
201 throws NoSuchCounterException {
202 Session session = null;
203
204 try {
205 session = openSession();
206
207 Counter counter = (Counter)session.get(CounterImpl.class, primaryKey);
208
209 if (counter == null) {
210 if (_log.isWarnEnabled()) {
211 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
212 }
213
214 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
215 primaryKey);
216 }
217
218 return remove(counter);
219 }
220 catch (NoSuchCounterException nsee) {
221 throw nsee;
222 }
223 catch (Exception e) {
224 throw processException(e);
225 }
226 finally {
227 closeSession(session);
228 }
229 }
230
231 @Override
232 protected Counter removeImpl(Counter counter) {
233 counter = toUnwrappedModel(counter);
234
235 Session session = null;
236
237 try {
238 session = openSession();
239
240 if (!session.contains(counter)) {
241 counter = (Counter)session.get(CounterImpl.class,
242 counter.getPrimaryKeyObj());
243 }
244
245 if (counter != null) {
246 session.delete(counter);
247 }
248 }
249 catch (Exception e) {
250 throw processException(e);
251 }
252 finally {
253 closeSession(session);
254 }
255
256 if (counter != null) {
257 clearCache(counter);
258 }
259
260 return counter;
261 }
262
263 @Override
264 public Counter updateImpl(Counter counter) {
265 counter = toUnwrappedModel(counter);
266
267 boolean isNew = counter.isNew();
268
269 Session session = null;
270
271 try {
272 session = openSession();
273
274 if (counter.isNew()) {
275 session.save(counter);
276
277 counter.setNew(false);
278 }
279 else {
280 counter = (Counter)session.merge(counter);
281 }
282 }
283 catch (Exception e) {
284 throw processException(e);
285 }
286 finally {
287 closeSession(session);
288 }
289
290 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
291
292 if (isNew) {
293 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
294 }
295
296 entityCache.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
297 CounterImpl.class, counter.getPrimaryKey(), counter, false);
298
299 counter.resetOriginalValues();
300
301 return counter;
302 }
303
304 protected Counter toUnwrappedModel(Counter counter) {
305 if (counter instanceof CounterImpl) {
306 return counter;
307 }
308
309 CounterImpl counterImpl = new CounterImpl();
310
311 counterImpl.setNew(counter.isNew());
312 counterImpl.setPrimaryKey(counter.getPrimaryKey());
313
314 counterImpl.setName(counter.getName());
315 counterImpl.setCurrentId(counter.getCurrentId());
316
317 return counterImpl;
318 }
319
320
327 @Override
328 public Counter findByPrimaryKey(Serializable primaryKey)
329 throws NoSuchCounterException {
330 Counter counter = fetchByPrimaryKey(primaryKey);
331
332 if (counter == null) {
333 if (_log.isWarnEnabled()) {
334 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
335 }
336
337 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
338 primaryKey);
339 }
340
341 return counter;
342 }
343
344
351 @Override
352 public Counter findByPrimaryKey(String name) throws NoSuchCounterException {
353 return findByPrimaryKey((Serializable)name);
354 }
355
356
362 @Override
363 public Counter fetchByPrimaryKey(Serializable primaryKey) {
364 Counter counter = (Counter)entityCache.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
365 CounterImpl.class, primaryKey);
366
367 if (counter == _nullCounter) {
368 return null;
369 }
370
371 if (counter == null) {
372 Session session = null;
373
374 try {
375 session = openSession();
376
377 counter = (Counter)session.get(CounterImpl.class, primaryKey);
378
379 if (counter != null) {
380 cacheResult(counter);
381 }
382 else {
383 entityCache.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
384 CounterImpl.class, primaryKey, _nullCounter);
385 }
386 }
387 catch (Exception e) {
388 entityCache.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
389 CounterImpl.class, primaryKey);
390
391 throw processException(e);
392 }
393 finally {
394 closeSession(session);
395 }
396 }
397
398 return counter;
399 }
400
401
407 @Override
408 public Counter fetchByPrimaryKey(String name) {
409 return fetchByPrimaryKey((Serializable)name);
410 }
411
412 @Override
413 public Map<Serializable, Counter> fetchByPrimaryKeys(
414 Set<Serializable> primaryKeys) {
415 if (primaryKeys.isEmpty()) {
416 return Collections.emptyMap();
417 }
418
419 Map<Serializable, Counter> map = new HashMap<Serializable, Counter>();
420
421 if (primaryKeys.size() == 1) {
422 Iterator<Serializable> iterator = primaryKeys.iterator();
423
424 Serializable primaryKey = iterator.next();
425
426 Counter counter = fetchByPrimaryKey(primaryKey);
427
428 if (counter != null) {
429 map.put(primaryKey, counter);
430 }
431
432 return map;
433 }
434
435 Set<Serializable> uncachedPrimaryKeys = null;
436
437 for (Serializable primaryKey : primaryKeys) {
438 Counter counter = (Counter)entityCache.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
439 CounterImpl.class, primaryKey);
440
441 if (counter == null) {
442 if (uncachedPrimaryKeys == null) {
443 uncachedPrimaryKeys = new HashSet<Serializable>();
444 }
445
446 uncachedPrimaryKeys.add(primaryKey);
447 }
448 else {
449 map.put(primaryKey, counter);
450 }
451 }
452
453 if (uncachedPrimaryKeys == null) {
454 return map;
455 }
456
457 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 4) +
458 1);
459
460 query.append(_SQL_SELECT_COUNTER_WHERE_PKS_IN);
461
462 for (Serializable primaryKey : uncachedPrimaryKeys) {
463 query.append(StringPool.APOSTROPHE);
464 query.append((String)primaryKey);
465 query.append(StringPool.APOSTROPHE);
466
467 query.append(StringPool.COMMA);
468 }
469
470 query.setIndex(query.index() - 1);
471
472 query.append(StringPool.CLOSE_PARENTHESIS);
473
474 String sql = query.toString();
475
476 Session session = null;
477
478 try {
479 session = openSession();
480
481 Query q = session.createQuery(sql);
482
483 for (Counter counter : (List<Counter>)q.list()) {
484 map.put(counter.getPrimaryKeyObj(), counter);
485
486 cacheResult(counter);
487
488 uncachedPrimaryKeys.remove(counter.getPrimaryKeyObj());
489 }
490
491 for (Serializable primaryKey : uncachedPrimaryKeys) {
492 entityCache.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
493 CounterImpl.class, primaryKey, _nullCounter);
494 }
495 }
496 catch (Exception e) {
497 throw processException(e);
498 }
499 finally {
500 closeSession(session);
501 }
502
503 return map;
504 }
505
506
511 @Override
512 public List<Counter> findAll() {
513 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
514 }
515
516
527 @Override
528 public List<Counter> findAll(int start, int end) {
529 return findAll(start, end, null);
530 }
531
532
544 @Override
545 public List<Counter> findAll(int start, int end,
546 OrderByComparator<Counter> orderByComparator) {
547 return findAll(start, end, orderByComparator, true);
548 }
549
550
563 @Override
564 public List<Counter> findAll(int start, int end,
565 OrderByComparator<Counter> orderByComparator, boolean retrieveFromCache) {
566 boolean pagination = true;
567 FinderPath finderPath = null;
568 Object[] finderArgs = null;
569
570 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
571 (orderByComparator == null)) {
572 pagination = false;
573 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
574 finderArgs = FINDER_ARGS_EMPTY;
575 }
576 else {
577 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
578 finderArgs = new Object[] { start, end, orderByComparator };
579 }
580
581 List<Counter> list = null;
582
583 if (retrieveFromCache) {
584 list = (List<Counter>)finderCache.getResult(finderPath, finderArgs,
585 this);
586 }
587
588 if (list == null) {
589 StringBundler query = null;
590 String sql = null;
591
592 if (orderByComparator != null) {
593 query = new StringBundler(2 +
594 (orderByComparator.getOrderByFields().length * 3));
595
596 query.append(_SQL_SELECT_COUNTER);
597
598 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
599 orderByComparator);
600
601 sql = query.toString();
602 }
603 else {
604 sql = _SQL_SELECT_COUNTER;
605
606 if (pagination) {
607 sql = sql.concat(CounterModelImpl.ORDER_BY_JPQL);
608 }
609 }
610
611 Session session = null;
612
613 try {
614 session = openSession();
615
616 Query q = session.createQuery(sql);
617
618 if (!pagination) {
619 list = (List<Counter>)QueryUtil.list(q, getDialect(),
620 start, end, false);
621
622 Collections.sort(list);
623
624 list = Collections.unmodifiableList(list);
625 }
626 else {
627 list = (List<Counter>)QueryUtil.list(q, getDialect(),
628 start, end);
629 }
630
631 cacheResult(list);
632
633 finderCache.putResult(finderPath, finderArgs, list);
634 }
635 catch (Exception e) {
636 finderCache.removeResult(finderPath, finderArgs);
637
638 throw processException(e);
639 }
640 finally {
641 closeSession(session);
642 }
643 }
644
645 return list;
646 }
647
648
652 @Override
653 public void removeAll() {
654 for (Counter counter : findAll()) {
655 remove(counter);
656 }
657 }
658
659
664 @Override
665 public int countAll() {
666 Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
667 FINDER_ARGS_EMPTY, this);
668
669 if (count == null) {
670 Session session = null;
671
672 try {
673 session = openSession();
674
675 Query q = session.createQuery(_SQL_COUNT_COUNTER);
676
677 count = (Long)q.uniqueResult();
678
679 finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
680 count);
681 }
682 catch (Exception e) {
683 finderCache.removeResult(FINDER_PATH_COUNT_ALL,
684 FINDER_ARGS_EMPTY);
685
686 throw processException(e);
687 }
688 finally {
689 closeSession(session);
690 }
691 }
692
693 return count.intValue();
694 }
695
696 @Override
697 protected Map<String, Integer> getTableColumnsMap() {
698 return CounterModelImpl.TABLE_COLUMNS_MAP;
699 }
700
701
704 public void afterPropertiesSet() {
705 }
706
707 public void destroy() {
708 entityCache.removeCache(CounterImpl.class.getName());
709 finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
710 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
711 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
712 }
713
714 protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
715 protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
716 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
717 private static final String _SQL_SELECT_COUNTER_WHERE_PKS_IN = "SELECT counter FROM Counter counter WHERE name IN (";
718 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
719 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
720 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
721 private static final Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
722 private static final Counter _nullCounter = new CounterImpl() {
723 @Override
724 public Object clone() {
725 return this;
726 }
727
728 @Override
729 public CacheModel<Counter> toCacheModel() {
730 return _nullCounterCacheModel;
731 }
732 };
733
734 private static final CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
735 @Override
736 public Counter toEntityModel() {
737 return _nullCounter;
738 }
739 };
740 }