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.bean.BeanReference;
024 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
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.exception.SystemException;
032 import com.liferay.portal.kernel.log.Log;
033 import com.liferay.portal.kernel.log.LogFactoryUtil;
034 import com.liferay.portal.kernel.util.GetterUtil;
035 import com.liferay.portal.kernel.util.InstanceFactory;
036 import com.liferay.portal.kernel.util.OrderByComparator;
037 import com.liferay.portal.kernel.util.StringBundler;
038 import com.liferay.portal.kernel.util.StringUtil;
039 import com.liferay.portal.model.CacheModel;
040 import com.liferay.portal.model.ModelListener;
041 import com.liferay.portal.service.persistence.BatchSessionUtil;
042 import com.liferay.portal.service.persistence.ResourcePersistence;
043 import com.liferay.portal.service.persistence.UserPersistence;
044 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
045
046 import java.io.Serializable;
047
048 import java.util.ArrayList;
049 import java.util.Collections;
050 import java.util.List;
051
052
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
091 public void cacheResult(Counter counter) {
092 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
093 CounterImpl.class, counter.getPrimaryKey(), counter);
094
095 counter.resetOriginalValues();
096 }
097
098
103 public void cacheResult(List<Counter> counters) {
104 for (Counter counter : counters) {
105 if (EntityCacheUtil.getResult(
106 CounterModelImpl.ENTITY_CACHE_ENABLED,
107 CounterImpl.class, counter.getPrimaryKey()) == null) {
108 cacheResult(counter);
109 }
110 else {
111 counter.resetOriginalValues();
112 }
113 }
114 }
115
116
123 @Override
124 public void clearCache() {
125 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
126 CacheRegistryUtil.clear(CounterImpl.class.getName());
127 }
128
129 EntityCacheUtil.clearCache(CounterImpl.class.getName());
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 public Counter create(String name) {
170 Counter counter = new CounterImpl();
171
172 counter.setNew(true);
173 counter.setPrimaryKey(name);
174
175 return counter;
176 }
177
178
186 public Counter remove(String name)
187 throws NoSuchCounterException, SystemException {
188 return remove((Serializable)name);
189 }
190
191
199 @Override
200 public Counter remove(Serializable primaryKey)
201 throws NoSuchCounterException, SystemException {
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) throws SystemException {
233 counter = toUnwrappedModel(counter);
234
235 Session session = null;
236
237 try {
238 session = openSession();
239
240 BatchSessionUtil.delete(session, counter);
241 }
242 catch (Exception e) {
243 throw processException(e);
244 }
245 finally {
246 closeSession(session);
247 }
248
249 clearCache(counter);
250
251 return counter;
252 }
253
254 @Override
255 public Counter updateImpl(com.liferay.counter.model.Counter counter,
256 boolean merge) throws SystemException {
257 counter = toUnwrappedModel(counter);
258
259 boolean isNew = counter.isNew();
260
261 Session session = null;
262
263 try {
264 session = openSession();
265
266 BatchSessionUtil.update(session, counter, merge);
267
268 counter.setNew(false);
269 }
270 catch (Exception e) {
271 throw processException(e);
272 }
273 finally {
274 closeSession(session);
275 }
276
277 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
278
279 if (isNew) {
280 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
281 }
282
283 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
284 CounterImpl.class, counter.getPrimaryKey(), counter);
285
286 return counter;
287 }
288
289 protected Counter toUnwrappedModel(Counter counter) {
290 if (counter instanceof CounterImpl) {
291 return counter;
292 }
293
294 CounterImpl counterImpl = new CounterImpl();
295
296 counterImpl.setNew(counter.isNew());
297 counterImpl.setPrimaryKey(counter.getPrimaryKey());
298
299 counterImpl.setName(counter.getName());
300 counterImpl.setCurrentId(counter.getCurrentId());
301
302 return counterImpl;
303 }
304
305
313 @Override
314 public Counter findByPrimaryKey(Serializable primaryKey)
315 throws NoSuchModelException, SystemException {
316 return findByPrimaryKey((String)primaryKey);
317 }
318
319
327 public Counter findByPrimaryKey(String name)
328 throws NoSuchCounterException, SystemException {
329 Counter counter = fetchByPrimaryKey(name);
330
331 if (counter == null) {
332 if (_log.isWarnEnabled()) {
333 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
334 }
335
336 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
337 name);
338 }
339
340 return counter;
341 }
342
343
350 @Override
351 public Counter fetchByPrimaryKey(Serializable primaryKey)
352 throws SystemException {
353 return fetchByPrimaryKey((String)primaryKey);
354 }
355
356
363 public Counter fetchByPrimaryKey(String name) throws SystemException {
364 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
365 CounterImpl.class, name);
366
367 if (counter == _nullCounter) {
368 return null;
369 }
370
371 if (counter == null) {
372 Session session = null;
373
374 boolean hasException = false;
375
376 try {
377 session = openSession();
378
379 counter = (Counter)session.get(CounterImpl.class, name);
380 }
381 catch (Exception e) {
382 hasException = true;
383
384 throw processException(e);
385 }
386 finally {
387 if (counter != null) {
388 cacheResult(counter);
389 }
390 else if (!hasException) {
391 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
392 CounterImpl.class, name, _nullCounter);
393 }
394
395 closeSession(session);
396 }
397 }
398
399 return counter;
400 }
401
402
408 public List<Counter> findAll() throws SystemException {
409 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
410 }
411
412
424 public List<Counter> findAll(int start, int end) throws SystemException {
425 return findAll(start, end, null);
426 }
427
428
441 public List<Counter> findAll(int start, int end,
442 OrderByComparator orderByComparator) throws SystemException {
443 FinderPath finderPath = null;
444 Object[] finderArgs = new Object[] { start, end, orderByComparator };
445
446 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
447 (orderByComparator == null)) {
448 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
449 finderArgs = FINDER_ARGS_EMPTY;
450 }
451 else {
452 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
453 finderArgs = new Object[] { start, end, orderByComparator };
454 }
455
456 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
457 finderArgs, this);
458
459 if (list == null) {
460 StringBundler query = null;
461 String sql = null;
462
463 if (orderByComparator != null) {
464 query = new StringBundler(2 +
465 (orderByComparator.getOrderByFields().length * 3));
466
467 query.append(_SQL_SELECT_COUNTER);
468
469 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
470 orderByComparator);
471
472 sql = query.toString();
473 }
474 else {
475 sql = _SQL_SELECT_COUNTER;
476 }
477
478 Session session = null;
479
480 try {
481 session = openSession();
482
483 Query q = session.createQuery(sql);
484
485 if (orderByComparator == null) {
486 list = (List<Counter>)QueryUtil.list(q, getDialect(),
487 start, end, false);
488
489 Collections.sort(list);
490 }
491 else {
492 list = (List<Counter>)QueryUtil.list(q, getDialect(),
493 start, end);
494 }
495 }
496 catch (Exception e) {
497 throw processException(e);
498 }
499 finally {
500 if (list == null) {
501 FinderCacheUtil.removeResult(finderPath, finderArgs);
502 }
503 else {
504 cacheResult(list);
505
506 FinderCacheUtil.putResult(finderPath, finderArgs, list);
507 }
508
509 closeSession(session);
510 }
511 }
512
513 return list;
514 }
515
516
521 public void removeAll() throws SystemException {
522 for (Counter counter : findAll()) {
523 remove(counter);
524 }
525 }
526
527
533 public int countAll() throws SystemException {
534 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
535 FINDER_ARGS_EMPTY, this);
536
537 if (count == null) {
538 Session session = null;
539
540 try {
541 session = openSession();
542
543 Query q = session.createQuery(_SQL_COUNT_COUNTER);
544
545 count = (Long)q.uniqueResult();
546 }
547 catch (Exception e) {
548 throw processException(e);
549 }
550 finally {
551 if (count == null) {
552 count = Long.valueOf(0);
553 }
554
555 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
556 FINDER_ARGS_EMPTY, count);
557
558 closeSession(session);
559 }
560 }
561
562 return count.intValue();
563 }
564
565
568 public void afterPropertiesSet() {
569 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
570 com.liferay.portal.util.PropsUtil.get(
571 "value.object.listener.com.liferay.counter.model.Counter")));
572
573 if (listenerClassNames.length > 0) {
574 try {
575 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
576
577 for (String listenerClassName : listenerClassNames) {
578 Class<?> clazz = getClass();
579
580 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
581 clazz.getClassLoader(), listenerClassName));
582 }
583
584 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
585 }
586 catch (Exception e) {
587 _log.error(e);
588 }
589 }
590 }
591
592 public void destroy() {
593 EntityCacheUtil.removeCache(CounterImpl.class.getName());
594 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
595 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
596 }
597
598 @BeanReference(type = CounterPersistence.class)
599 protected CounterPersistence counterPersistence;
600 @BeanReference(type = ResourcePersistence.class)
601 protected ResourcePersistence resourcePersistence;
602 @BeanReference(type = UserPersistence.class)
603 protected UserPersistence userPersistence;
604 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
605 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
606 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
607 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
608 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
609 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
610 private static Counter _nullCounter = new CounterImpl() {
611 @Override
612 public Object clone() {
613 return this;
614 }
615
616 @Override
617 public CacheModel<Counter> toCacheModel() {
618 return _nullCounterCacheModel;
619 }
620 };
621
622 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
623 public Counter toEntityModel() {
624 return _nullCounter;
625 }
626 };
627 }