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.annotation.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.ModelListener;
040 import com.liferay.portal.service.persistence.BatchSessionUtil;
041 import com.liferay.portal.service.persistence.ResourcePersistence;
042 import com.liferay.portal.service.persistence.UserPersistence;
043 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044
045 import java.io.Serializable;
046
047 import java.util.ArrayList;
048 import java.util.Collections;
049 import java.util.List;
050
051
067 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
068 implements CounterPersistence {
069 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
070 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
071 ".List";
072 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
073 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
074 "findAll", new String[0]);
075 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
076 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
077 "countAll", new String[0]);
078
079
084 public void cacheResult(Counter counter) {
085 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
086 CounterImpl.class, counter.getPrimaryKey(), counter);
087 }
088
089
094 public void cacheResult(List<Counter> counters) {
095 for (Counter counter : counters) {
096 if (EntityCacheUtil.getResult(
097 CounterModelImpl.ENTITY_CACHE_ENABLED,
098 CounterImpl.class, counter.getPrimaryKey(), this) == null) {
099 cacheResult(counter);
100 }
101 }
102 }
103
104
111 public void clearCache() {
112 CacheRegistryUtil.clear(CounterImpl.class.getName());
113 EntityCacheUtil.clearCache(CounterImpl.class.getName());
114 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
115 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
116 }
117
118
125 public void clearCache(Counter counter) {
126 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
127 CounterImpl.class, counter.getPrimaryKey());
128 }
129
130
136 public Counter create(String name) {
137 Counter counter = new CounterImpl();
138
139 counter.setNew(true);
140 counter.setPrimaryKey(name);
141
142 return counter;
143 }
144
145
153 public Counter remove(Serializable primaryKey)
154 throws NoSuchModelException, SystemException {
155 return remove((String)primaryKey);
156 }
157
158
166 public Counter remove(String name)
167 throws NoSuchCounterException, SystemException {
168 Session session = null;
169
170 try {
171 session = openSession();
172
173 Counter counter = (Counter)session.get(CounterImpl.class, name);
174
175 if (counter == null) {
176 if (_log.isWarnEnabled()) {
177 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
178 }
179
180 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
181 name);
182 }
183
184 return remove(counter);
185 }
186 catch (NoSuchCounterException nsee) {
187 throw nsee;
188 }
189 catch (Exception e) {
190 throw processException(e);
191 }
192 finally {
193 closeSession(session);
194 }
195 }
196
197 protected Counter removeImpl(Counter counter) throws SystemException {
198 counter = toUnwrappedModel(counter);
199
200 Session session = null;
201
202 try {
203 session = openSession();
204
205 if (counter.isCachedModel() || BatchSessionUtil.isEnabled()) {
206 Object staleObject = session.get(CounterImpl.class,
207 counter.getPrimaryKeyObj());
208
209 if (staleObject != null) {
210 session.evict(staleObject);
211 }
212 }
213
214 session.delete(counter);
215
216 session.flush();
217 }
218 catch (Exception e) {
219 throw processException(e);
220 }
221 finally {
222 closeSession(session);
223 }
224
225 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226
227 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
228 CounterImpl.class, counter.getPrimaryKey());
229
230 return counter;
231 }
232
233 public Counter updateImpl(com.liferay.counter.model.Counter counter,
234 boolean merge) throws SystemException {
235 counter = toUnwrappedModel(counter);
236
237 Session session = null;
238
239 try {
240 session = openSession();
241
242 BatchSessionUtil.update(session, counter, merge);
243
244 counter.setNew(false);
245 }
246 catch (Exception e) {
247 throw processException(e);
248 }
249 finally {
250 closeSession(session);
251 }
252
253 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
254
255 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
256 CounterImpl.class, counter.getPrimaryKey(), counter);
257
258 return counter;
259 }
260
261 protected Counter toUnwrappedModel(Counter counter) {
262 if (counter instanceof CounterImpl) {
263 return counter;
264 }
265
266 CounterImpl counterImpl = new CounterImpl();
267
268 counterImpl.setNew(counter.isNew());
269 counterImpl.setPrimaryKey(counter.getPrimaryKey());
270
271 counterImpl.setName(counter.getName());
272 counterImpl.setCurrentId(counter.getCurrentId());
273
274 return counterImpl;
275 }
276
277
285 public Counter findByPrimaryKey(Serializable primaryKey)
286 throws NoSuchModelException, SystemException {
287 return findByPrimaryKey((String)primaryKey);
288 }
289
290
298 public Counter findByPrimaryKey(String name)
299 throws NoSuchCounterException, SystemException {
300 Counter counter = fetchByPrimaryKey(name);
301
302 if (counter == null) {
303 if (_log.isWarnEnabled()) {
304 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
305 }
306
307 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
308 name);
309 }
310
311 return counter;
312 }
313
314
321 public Counter fetchByPrimaryKey(Serializable primaryKey)
322 throws SystemException {
323 return fetchByPrimaryKey((String)primaryKey);
324 }
325
326
333 public Counter fetchByPrimaryKey(String name) throws SystemException {
334 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
335 CounterImpl.class, name, this);
336
337 if (counter == null) {
338 Session session = null;
339
340 try {
341 session = openSession();
342
343 counter = (Counter)session.get(CounterImpl.class, name);
344 }
345 catch (Exception e) {
346 throw processException(e);
347 }
348 finally {
349 if (counter != null) {
350 cacheResult(counter);
351 }
352
353 closeSession(session);
354 }
355 }
356
357 return counter;
358 }
359
360
366 public List<Counter> findAll() throws SystemException {
367 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
368 }
369
370
382 public List<Counter> findAll(int start, int end) throws SystemException {
383 return findAll(start, end, null);
384 }
385
386
399 public List<Counter> findAll(int start, int end,
400 OrderByComparator orderByComparator) throws SystemException {
401 Object[] finderArgs = new Object[] {
402 String.valueOf(start), String.valueOf(end),
403 String.valueOf(orderByComparator)
404 };
405
406 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
407 finderArgs, this);
408
409 if (list == null) {
410 Session session = null;
411
412 try {
413 session = openSession();
414
415 StringBundler query = null;
416 String sql = null;
417
418 if (orderByComparator != null) {
419 query = new StringBundler(2 +
420 (orderByComparator.getOrderByFields().length * 3));
421
422 query.append(_SQL_SELECT_COUNTER);
423
424 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
425 orderByComparator);
426
427 sql = query.toString();
428 }
429 else {
430 sql = _SQL_SELECT_COUNTER;
431 }
432
433 Query q = session.createQuery(sql);
434
435 if (orderByComparator == null) {
436 list = (List<Counter>)QueryUtil.list(q, getDialect(),
437 start, end, false);
438
439 Collections.sort(list);
440 }
441 else {
442 list = (List<Counter>)QueryUtil.list(q, getDialect(),
443 start, end);
444 }
445 }
446 catch (Exception e) {
447 throw processException(e);
448 }
449 finally {
450 if (list == null) {
451 list = new ArrayList<Counter>();
452 }
453
454 cacheResult(list);
455
456 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
457
458 closeSession(session);
459 }
460 }
461
462 return list;
463 }
464
465
470 public void removeAll() throws SystemException {
471 for (Counter counter : findAll()) {
472 remove(counter);
473 }
474 }
475
476
482 public int countAll() throws SystemException {
483 Object[] finderArgs = new Object[0];
484
485 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
486 finderArgs, this);
487
488 if (count == null) {
489 Session session = null;
490
491 try {
492 session = openSession();
493
494 Query q = session.createQuery(_SQL_COUNT_COUNTER);
495
496 count = (Long)q.uniqueResult();
497 }
498 catch (Exception e) {
499 throw processException(e);
500 }
501 finally {
502 if (count == null) {
503 count = Long.valueOf(0);
504 }
505
506 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
507 count);
508
509 closeSession(session);
510 }
511 }
512
513 return count.intValue();
514 }
515
516
519 public void afterPropertiesSet() {
520 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
521 com.liferay.portal.util.PropsUtil.get(
522 "value.object.listener.com.liferay.counter.model.Counter")));
523
524 if (listenerClassNames.length > 0) {
525 try {
526 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
527
528 for (String listenerClassName : listenerClassNames) {
529 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
530 listenerClassName));
531 }
532
533 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
534 }
535 catch (Exception e) {
536 _log.error(e);
537 }
538 }
539 }
540
541 @BeanReference(type = CounterPersistence.class)
542 protected CounterPersistence counterPersistence;
543 @BeanReference(type = ResourcePersistence.class)
544 protected ResourcePersistence resourcePersistence;
545 @BeanReference(type = UserPersistence.class)
546 protected UserPersistence userPersistence;
547 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
548 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
549 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
550 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
551 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
552 }