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 BatchSessionUtil.delete(session, counter);
206 }
207 catch (Exception e) {
208 throw processException(e);
209 }
210 finally {
211 closeSession(session);
212 }
213
214 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
215
216 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
217 CounterImpl.class, counter.getPrimaryKey());
218
219 return counter;
220 }
221
222 public Counter updateImpl(com.liferay.counter.model.Counter counter,
223 boolean merge) throws SystemException {
224 counter = toUnwrappedModel(counter);
225
226 Session session = null;
227
228 try {
229 session = openSession();
230
231 BatchSessionUtil.update(session, counter, merge);
232
233 counter.setNew(false);
234 }
235 catch (Exception e) {
236 throw processException(e);
237 }
238 finally {
239 closeSession(session);
240 }
241
242 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
243
244 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
245 CounterImpl.class, counter.getPrimaryKey(), counter);
246
247 return counter;
248 }
249
250 protected Counter toUnwrappedModel(Counter counter) {
251 if (counter instanceof CounterImpl) {
252 return counter;
253 }
254
255 CounterImpl counterImpl = new CounterImpl();
256
257 counterImpl.setNew(counter.isNew());
258 counterImpl.setPrimaryKey(counter.getPrimaryKey());
259
260 counterImpl.setName(counter.getName());
261 counterImpl.setCurrentId(counter.getCurrentId());
262
263 return counterImpl;
264 }
265
266
274 public Counter findByPrimaryKey(Serializable primaryKey)
275 throws NoSuchModelException, SystemException {
276 return findByPrimaryKey((String)primaryKey);
277 }
278
279
287 public Counter findByPrimaryKey(String name)
288 throws NoSuchCounterException, SystemException {
289 Counter counter = fetchByPrimaryKey(name);
290
291 if (counter == null) {
292 if (_log.isWarnEnabled()) {
293 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
294 }
295
296 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
297 name);
298 }
299
300 return counter;
301 }
302
303
310 public Counter fetchByPrimaryKey(Serializable primaryKey)
311 throws SystemException {
312 return fetchByPrimaryKey((String)primaryKey);
313 }
314
315
322 public Counter fetchByPrimaryKey(String name) throws SystemException {
323 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
324 CounterImpl.class, name, this);
325
326 if (counter == null) {
327 Session session = null;
328
329 try {
330 session = openSession();
331
332 counter = (Counter)session.get(CounterImpl.class, name);
333 }
334 catch (Exception e) {
335 throw processException(e);
336 }
337 finally {
338 if (counter != null) {
339 cacheResult(counter);
340 }
341
342 closeSession(session);
343 }
344 }
345
346 return counter;
347 }
348
349
355 public List<Counter> findAll() throws SystemException {
356 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
357 }
358
359
371 public List<Counter> findAll(int start, int end) throws SystemException {
372 return findAll(start, end, null);
373 }
374
375
388 public List<Counter> findAll(int start, int end,
389 OrderByComparator orderByComparator) throws SystemException {
390 Object[] finderArgs = new Object[] {
391 String.valueOf(start), String.valueOf(end),
392 String.valueOf(orderByComparator)
393 };
394
395 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
396 finderArgs, this);
397
398 if (list == null) {
399 Session session = null;
400
401 try {
402 session = openSession();
403
404 StringBundler query = null;
405 String sql = null;
406
407 if (orderByComparator != null) {
408 query = new StringBundler(2 +
409 (orderByComparator.getOrderByFields().length * 3));
410
411 query.append(_SQL_SELECT_COUNTER);
412
413 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
414 orderByComparator);
415
416 sql = query.toString();
417 }
418 else {
419 sql = _SQL_SELECT_COUNTER;
420 }
421
422 Query q = session.createQuery(sql);
423
424 if (orderByComparator == null) {
425 list = (List<Counter>)QueryUtil.list(q, getDialect(),
426 start, end, false);
427
428 Collections.sort(list);
429 }
430 else {
431 list = (List<Counter>)QueryUtil.list(q, getDialect(),
432 start, end);
433 }
434 }
435 catch (Exception e) {
436 throw processException(e);
437 }
438 finally {
439 if (list == null) {
440 list = new ArrayList<Counter>();
441 }
442
443 cacheResult(list);
444
445 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
446
447 closeSession(session);
448 }
449 }
450
451 return list;
452 }
453
454
459 public void removeAll() throws SystemException {
460 for (Counter counter : findAll()) {
461 remove(counter);
462 }
463 }
464
465
471 public int countAll() throws SystemException {
472 Object[] finderArgs = new Object[0];
473
474 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
475 finderArgs, this);
476
477 if (count == null) {
478 Session session = null;
479
480 try {
481 session = openSession();
482
483 Query q = session.createQuery(_SQL_COUNT_COUNTER);
484
485 count = (Long)q.uniqueResult();
486 }
487 catch (Exception e) {
488 throw processException(e);
489 }
490 finally {
491 if (count == null) {
492 count = Long.valueOf(0);
493 }
494
495 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
496 count);
497
498 closeSession(session);
499 }
500 }
501
502 return count.intValue();
503 }
504
505
508 public void afterPropertiesSet() {
509 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
510 com.liferay.portal.util.PropsUtil.get(
511 "value.object.listener.com.liferay.counter.model.Counter")));
512
513 if (listenerClassNames.length > 0) {
514 try {
515 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
516
517 for (String listenerClassName : listenerClassNames) {
518 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
519 listenerClassName));
520 }
521
522 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
523 }
524 catch (Exception e) {
525 _log.error(e);
526 }
527 }
528 }
529
530 public void destroy() {
531 EntityCacheUtil.removeCache(CounterImpl.class.getName());
532 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
533 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
534 }
535
536 @BeanReference(type = CounterPersistence.class)
537 protected CounterPersistence counterPersistence;
538 @BeanReference(type = ResourcePersistence.class)
539 protected ResourcePersistence resourcePersistence;
540 @BeanReference(type = UserPersistence.class)
541 protected UserPersistence userPersistence;
542 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
543 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
544 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
545 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
546 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
547 }