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 StringBundler query = null;
400 String sql = null;
401
402 if (orderByComparator != null) {
403 query = new StringBundler(2 +
404 (orderByComparator.getOrderByFields().length * 3));
405
406 query.append(_SQL_SELECT_COUNTER);
407
408 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
409 orderByComparator);
410
411 sql = query.toString();
412 }
413 else {
414 sql = _SQL_SELECT_COUNTER;
415 }
416
417 Session session = null;
418
419 try {
420 session = openSession();
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 FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
441 finderArgs);
442 }
443 else {
444 cacheResult(list);
445
446 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
447 list);
448 }
449
450 closeSession(session);
451 }
452 }
453
454 return list;
455 }
456
457
462 public void removeAll() throws SystemException {
463 for (Counter counter : findAll()) {
464 remove(counter);
465 }
466 }
467
468
474 public int countAll() throws SystemException {
475 Object[] finderArgs = new Object[0];
476
477 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
478 finderArgs, this);
479
480 if (count == null) {
481 Session session = null;
482
483 try {
484 session = openSession();
485
486 Query q = session.createQuery(_SQL_COUNT_COUNTER);
487
488 count = (Long)q.uniqueResult();
489 }
490 catch (Exception e) {
491 throw processException(e);
492 }
493 finally {
494 if (count == null) {
495 count = Long.valueOf(0);
496 }
497
498 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
499 count);
500
501 closeSession(session);
502 }
503 }
504
505 return count.intValue();
506 }
507
508
511 public void afterPropertiesSet() {
512 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
513 com.liferay.portal.util.PropsUtil.get(
514 "value.object.listener.com.liferay.counter.model.Counter")));
515
516 if (listenerClassNames.length > 0) {
517 try {
518 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
519
520 for (String listenerClassName : listenerClassNames) {
521 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
522 listenerClassName));
523 }
524
525 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
526 }
527 catch (Exception e) {
528 _log.error(e);
529 }
530 }
531 }
532
533 public void destroy() {
534 EntityCacheUtil.removeCache(CounterImpl.class.getName());
535 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
536 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
537 }
538
539 @BeanReference(type = CounterPersistence.class)
540 protected CounterPersistence counterPersistence;
541 @BeanReference(type = ResourcePersistence.class)
542 protected ResourcePersistence resourcePersistence;
543 @BeanReference(type = UserPersistence.class)
544 protected UserPersistence userPersistence;
545 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
546 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
547 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
548 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
549 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
550 }