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 @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 Session session = null;
260
261 try {
262 session = openSession();
263
264 BatchSessionUtil.update(session, counter, merge);
265
266 counter.setNew(false);
267 }
268 catch (Exception e) {
269 throw processException(e);
270 }
271 finally {
272 closeSession(session);
273 }
274
275 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
276
277 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
278 CounterImpl.class, counter.getPrimaryKey(), counter);
279
280 return counter;
281 }
282
283 protected Counter toUnwrappedModel(Counter counter) {
284 if (counter instanceof CounterImpl) {
285 return counter;
286 }
287
288 CounterImpl counterImpl = new CounterImpl();
289
290 counterImpl.setNew(counter.isNew());
291 counterImpl.setPrimaryKey(counter.getPrimaryKey());
292
293 counterImpl.setName(counter.getName());
294 counterImpl.setCurrentId(counter.getCurrentId());
295
296 return counterImpl;
297 }
298
299
307 @Override
308 public Counter findByPrimaryKey(Serializable primaryKey)
309 throws NoSuchModelException, SystemException {
310 return findByPrimaryKey((String)primaryKey);
311 }
312
313
321 public Counter findByPrimaryKey(String name)
322 throws NoSuchCounterException, SystemException {
323 Counter counter = fetchByPrimaryKey(name);
324
325 if (counter == null) {
326 if (_log.isWarnEnabled()) {
327 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
328 }
329
330 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
331 name);
332 }
333
334 return counter;
335 }
336
337
344 @Override
345 public Counter fetchByPrimaryKey(Serializable primaryKey)
346 throws SystemException {
347 return fetchByPrimaryKey((String)primaryKey);
348 }
349
350
357 public Counter fetchByPrimaryKey(String name) throws SystemException {
358 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
359 CounterImpl.class, name);
360
361 if (counter == _nullCounter) {
362 return null;
363 }
364
365 if (counter == null) {
366 Session session = null;
367
368 boolean hasException = false;
369
370 try {
371 session = openSession();
372
373 counter = (Counter)session.get(CounterImpl.class, name);
374 }
375 catch (Exception e) {
376 hasException = true;
377
378 throw processException(e);
379 }
380 finally {
381 if (counter != null) {
382 cacheResult(counter);
383 }
384 else if (!hasException) {
385 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
386 CounterImpl.class, name, _nullCounter);
387 }
388
389 closeSession(session);
390 }
391 }
392
393 return counter;
394 }
395
396
402 public List<Counter> findAll() throws SystemException {
403 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
404 }
405
406
418 public List<Counter> findAll(int start, int end) throws SystemException {
419 return findAll(start, end, null);
420 }
421
422
435 public List<Counter> findAll(int start, int end,
436 OrderByComparator orderByComparator) throws SystemException {
437 FinderPath finderPath = null;
438 Object[] finderArgs = new Object[] { start, end, orderByComparator };
439
440 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
441 (orderByComparator == null)) {
442 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
443 finderArgs = FINDER_ARGS_EMPTY;
444 }
445 else {
446 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
447 finderArgs = new Object[] { start, end, orderByComparator };
448 }
449
450 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(finderPath,
451 finderArgs, this);
452
453 if (list == null) {
454 StringBundler query = null;
455 String sql = null;
456
457 if (orderByComparator != null) {
458 query = new StringBundler(2 +
459 (orderByComparator.getOrderByFields().length * 3));
460
461 query.append(_SQL_SELECT_COUNTER);
462
463 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
464 orderByComparator);
465
466 sql = query.toString();
467 }
468 else {
469 sql = _SQL_SELECT_COUNTER;
470 }
471
472 Session session = null;
473
474 try {
475 session = openSession();
476
477 Query q = session.createQuery(sql);
478
479 if (orderByComparator == null) {
480 list = (List<Counter>)QueryUtil.list(q, getDialect(),
481 start, end, false);
482
483 Collections.sort(list);
484 }
485 else {
486 list = (List<Counter>)QueryUtil.list(q, getDialect(),
487 start, end);
488 }
489 }
490 catch (Exception e) {
491 throw processException(e);
492 }
493 finally {
494 if (list == null) {
495 FinderCacheUtil.removeResult(finderPath, finderArgs);
496 }
497 else {
498 cacheResult(list);
499
500 FinderCacheUtil.putResult(finderPath, finderArgs, list);
501 }
502
503 closeSession(session);
504 }
505 }
506
507 return list;
508 }
509
510
515 public void removeAll() throws SystemException {
516 for (Counter counter : findAll()) {
517 remove(counter);
518 }
519 }
520
521
527 public int countAll() throws SystemException {
528 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
529 FINDER_ARGS_EMPTY, this);
530
531 if (count == null) {
532 Session session = null;
533
534 try {
535 session = openSession();
536
537 Query q = session.createQuery(_SQL_COUNT_COUNTER);
538
539 count = (Long)q.uniqueResult();
540 }
541 catch (Exception e) {
542 throw processException(e);
543 }
544 finally {
545 if (count == null) {
546 count = Long.valueOf(0);
547 }
548
549 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
550 FINDER_ARGS_EMPTY, count);
551
552 closeSession(session);
553 }
554 }
555
556 return count.intValue();
557 }
558
559
562 public void afterPropertiesSet() {
563 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
564 com.liferay.portal.util.PropsUtil.get(
565 "value.object.listener.com.liferay.counter.model.Counter")));
566
567 if (listenerClassNames.length > 0) {
568 try {
569 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
570
571 for (String listenerClassName : listenerClassNames) {
572 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
573 listenerClassName));
574 }
575
576 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
577 }
578 catch (Exception e) {
579 _log.error(e);
580 }
581 }
582 }
583
584 public void destroy() {
585 EntityCacheUtil.removeCache(CounterImpl.class.getName());
586 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
587 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
588 }
589
590 @BeanReference(type = CounterPersistence.class)
591 protected CounterPersistence counterPersistence;
592 @BeanReference(type = ResourcePersistence.class)
593 protected ResourcePersistence resourcePersistence;
594 @BeanReference(type = UserPersistence.class)
595 protected UserPersistence userPersistence;
596 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
597 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
598 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
599 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
600 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
601 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
602 private static Counter _nullCounter = new CounterImpl() {
603 @Override
604 public Object clone() {
605 return this;
606 }
607
608 @Override
609 public CacheModel<Counter> toCacheModel() {
610 return _nullCounterCacheModel;
611 }
612 };
613
614 private static CacheModel<Counter> _nullCounterCacheModel = new CacheModel<Counter>() {
615 public Counter toEntityModel() {
616 return _nullCounter;
617 }
618 };
619 }