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_WITHOUT_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_WITH_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
158 public Counter create(String name) {
159 Counter counter = new CounterImpl();
160
161 counter.setNew(true);
162 counter.setPrimaryKey(name);
163
164 return counter;
165 }
166
167
175 @Override
176 public Counter remove(Serializable primaryKey)
177 throws NoSuchModelException, SystemException {
178 return remove((String)primaryKey);
179 }
180
181
189 public Counter remove(String name)
190 throws NoSuchCounterException, SystemException {
191 Session session = null;
192
193 try {
194 session = openSession();
195
196 Counter counter = (Counter)session.get(CounterImpl.class, name);
197
198 if (counter == null) {
199 if (_log.isWarnEnabled()) {
200 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
201 }
202
203 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
204 name);
205 }
206
207 return counterPersistence.remove(counter);
208 }
209 catch (NoSuchCounterException nsee) {
210 throw nsee;
211 }
212 catch (Exception e) {
213 throw processException(e);
214 }
215 finally {
216 closeSession(session);
217 }
218 }
219
220
227 @Override
228 public Counter remove(Counter counter) throws SystemException {
229 return super.remove(counter);
230 }
231
232 @Override
233 protected Counter removeImpl(Counter counter) throws SystemException {
234 counter = toUnwrappedModel(counter);
235
236 Session session = null;
237
238 try {
239 session = openSession();
240
241 BatchSessionUtil.delete(session, counter);
242 }
243 catch (Exception e) {
244 throw processException(e);
245 }
246 finally {
247 closeSession(session);
248 }
249
250 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
251 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
252
253 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
254 CounterImpl.class, counter.getPrimaryKey());
255
256 return counter;
257 }
258
259 @Override
260 public Counter updateImpl(com.liferay.counter.model.Counter counter,
261 boolean merge) throws SystemException {
262 counter = toUnwrappedModel(counter);
263
264 Session session = null;
265
266 try {
267 session = openSession();
268
269 BatchSessionUtil.update(session, counter, merge);
270
271 counter.setNew(false);
272 }
273 catch (Exception e) {
274 throw processException(e);
275 }
276 finally {
277 closeSession(session);
278 }
279
280 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
281
282 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
283 CounterImpl.class, counter.getPrimaryKey(), counter);
284
285 return counter;
286 }
287
288 protected Counter toUnwrappedModel(Counter counter) {
289 if (counter instanceof CounterImpl) {
290 return counter;
291 }
292
293 CounterImpl counterImpl = new CounterImpl();
294
295 counterImpl.setNew(counter.isNew());
296 counterImpl.setPrimaryKey(counter.getPrimaryKey());
297
298 counterImpl.setName(counter.getName());
299 counterImpl.setCurrentId(counter.getCurrentId());
300
301 return counterImpl;
302 }
303
304
312 @Override
313 public Counter findByPrimaryKey(Serializable primaryKey)
314 throws NoSuchModelException, SystemException {
315 return findByPrimaryKey((String)primaryKey);
316 }
317
318
326 public Counter findByPrimaryKey(String name)
327 throws NoSuchCounterException, SystemException {
328 Counter counter = fetchByPrimaryKey(name);
329
330 if (counter == null) {
331 if (_log.isWarnEnabled()) {
332 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
333 }
334
335 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
336 name);
337 }
338
339 return counter;
340 }
341
342
349 @Override
350 public Counter fetchByPrimaryKey(Serializable primaryKey)
351 throws SystemException {
352 return fetchByPrimaryKey((String)primaryKey);
353 }
354
355
362 public Counter fetchByPrimaryKey(String name) throws SystemException {
363 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
364 CounterImpl.class, name);
365
366 if (counter == _nullCounter) {
367 return null;
368 }
369
370 if (counter == null) {
371 Session session = null;
372
373 boolean hasException = false;
374
375 try {
376 session = openSession();
377
378 counter = (Counter)session.get(CounterImpl.class, name);
379 }
380 catch (Exception e) {
381 hasException = true;
382
383 throw processException(e);
384 }
385 finally {
386 if (counter != null) {
387 cacheResult(counter);
388 }
389 else if (!hasException) {
390 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
391 CounterImpl.class, name, _nullCounter);
392 }
393
394 closeSession(session);
395 }
396 }
397
398 return counter;
399 }
400
401
407 public List<Counter> findAll() throws SystemException {
408 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
409 }
410
411
423 public List<Counter> findAll(int start, int end) throws SystemException {
424 return findAll(start, end, null);
425 }
426
427
440 public List<Counter> findAll(int start, int end,
441 OrderByComparator orderByComparator) throws SystemException {
442 FinderPath finderPath = null;
443 Object[] finderArgs = new Object[] { start, end, orderByComparator };
444
445 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
446 (orderByComparator == null)) {
447 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
448 finderArgs = FINDER_ARGS_EMPTY;
449 }
450 else {
451 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
452 finderArgs = new Object[] { start, end, orderByComparator };
453 }
454
455 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
456 finderArgs, this);
457
458 if (list == null) {
459 StringBundler query = null;
460 String sql = null;
461
462 if (orderByComparator != null) {
463 query = new StringBundler(2 +
464 (orderByComparator.getOrderByFields().length * 3));
465
466 query.append(_SQL_SELECT_COUNTER);
467
468 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
469 orderByComparator);
470
471 sql = query.toString();
472 }
473 else {
474 sql = _SQL_SELECT_COUNTER;
475 }
476
477 Session session = null;
478
479 try {
480 session = openSession();
481
482 Query q = session.createQuery(sql);
483
484 if (orderByComparator == null) {
485 list = (List<Counter>)QueryUtil.list(q, getDialect(),
486 start, end, false);
487
488 Collections.sort(list);
489 }
490 else {
491 list = (List<Counter>)QueryUtil.list(q, getDialect(),
492 start, end);
493 }
494 }
495 catch (Exception e) {
496 throw processException(e);
497 }
498 finally {
499 if (list == null) {
500 FinderCacheUtil.removeResult(finderPath, finderArgs);
501 }
502 else {
503 cacheResult(list);
504
505 FinderCacheUtil.putResult(finderPath, finderArgs, list);
506 }
507
508 closeSession(session);
509 }
510 }
511
512 return list;
513 }
514
515
520 public void removeAll() throws SystemException {
521 for (Counter counter : findAll()) {
522 counterPersistence.remove(counter);
523 }
524 }
525
526
532 public int countAll() throws SystemException {
533 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
534 FINDER_ARGS_EMPTY, this);
535
536 if (count == null) {
537 Session session = null;
538
539 try {
540 session = openSession();
541
542 Query q = session.createQuery(_SQL_COUNT_COUNTER);
543
544 count = (Long)q.uniqueResult();
545 }
546 catch (Exception e) {
547 throw processException(e);
548 }
549 finally {
550 if (count == null) {
551 count = Long.valueOf(0);
552 }
553
554 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
555 FINDER_ARGS_EMPTY, count);
556
557 closeSession(session);
558 }
559 }
560
561 return count.intValue();
562 }
563
564
567 public void afterPropertiesSet() {
568 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
569 com.liferay.portal.util.PropsUtil.get(
570 "value.object.listener.com.liferay.counter.model.Counter")));
571
572 if (listenerClassNames.length > 0) {
573 try {
574 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
575
576 for (String listenerClassName : listenerClassNames) {
577 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
578 listenerClassName));
579 }
580
581 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
582 }
583 catch (Exception e) {
584 _log.error(e);
585 }
586 }
587 }
588
589 public void destroy() {
590 EntityCacheUtil.removeCache(CounterImpl.class.getName());
591 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
592 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
593 }
594
595 @BeanReference(type = CounterPersistence.class)
596 protected CounterPersistence counterPersistence;
597 @BeanReference(type = ResourcePersistence.class)
598 protected ResourcePersistence resourcePersistence;
599 @BeanReference(type = UserPersistence.class)
600 protected UserPersistence userPersistence;
601 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
602 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
603 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
604 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
605 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
606 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
607 private static Counter _nullCounter = new CounterImpl() {
608 @Override
609 public Object clone() {
610 return this;
611 }
612
613 @Override
614 public CacheModel<Counter> toCacheModel() {
615 return _nullCounterCacheModel;
616 }
617 };
618
619 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
620 public Counter toEntityModel() {
621 return _nullCounter;
622 }
623 };
624 }