1
14
15 package com.liferay.counter.service.persistence;
16
17 import com.liferay.counter.NoSuchCounterException;
18 import com.liferay.counter.model.Counter;
19 import com.liferay.counter.model.impl.CounterImpl;
20 import com.liferay.counter.model.impl.CounterModelImpl;
21
22 import com.liferay.portal.NoSuchModelException;
23 import com.liferay.portal.kernel.annotation.BeanReference;
24 import com.liferay.portal.kernel.cache.CacheRegistry;
25 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
26 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
27 import com.liferay.portal.kernel.dao.orm.FinderPath;
28 import com.liferay.portal.kernel.dao.orm.Query;
29 import com.liferay.portal.kernel.dao.orm.QueryUtil;
30 import com.liferay.portal.kernel.dao.orm.Session;
31 import com.liferay.portal.kernel.exception.SystemException;
32 import com.liferay.portal.kernel.log.Log;
33 import com.liferay.portal.kernel.log.LogFactoryUtil;
34 import com.liferay.portal.kernel.util.GetterUtil;
35 import com.liferay.portal.kernel.util.InstanceFactory;
36 import com.liferay.portal.kernel.util.OrderByComparator;
37 import com.liferay.portal.kernel.util.StringBundler;
38 import com.liferay.portal.kernel.util.StringUtil;
39 import com.liferay.portal.model.ModelListener;
40 import com.liferay.portal.service.persistence.BatchSessionUtil;
41 import com.liferay.portal.service.persistence.ResourcePersistence;
42 import com.liferay.portal.service.persistence.UserPersistence;
43 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44
45 import java.io.Serializable;
46
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
50
51
64 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
65 implements CounterPersistence {
66 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
67 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
68 ".List";
69 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
70 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
71 "findAll", new String[0]);
72 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
73 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
74 "countAll", new String[0]);
75
76 public void cacheResult(Counter counter) {
77 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
78 CounterImpl.class, counter.getPrimaryKey(), counter);
79 }
80
81 public void cacheResult(List<Counter> counters) {
82 for (Counter counter : counters) {
83 if (EntityCacheUtil.getResult(
84 CounterModelImpl.ENTITY_CACHE_ENABLED,
85 CounterImpl.class, counter.getPrimaryKey(), this) == null) {
86 cacheResult(counter);
87 }
88 }
89 }
90
91 public void clearCache() {
92 CacheRegistry.clear(CounterImpl.class.getName());
93 EntityCacheUtil.clearCache(CounterImpl.class.getName());
94 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
95 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
96 }
97
98 public void clearCache(Counter counter) {
99 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
100 CounterImpl.class, counter.getPrimaryKey());
101 }
102
103 public Counter create(String name) {
104 Counter counter = new CounterImpl();
105
106 counter.setNew(true);
107 counter.setPrimaryKey(name);
108
109 return counter;
110 }
111
112 public Counter remove(Serializable primaryKey)
113 throws NoSuchModelException, SystemException {
114 return remove((String)primaryKey);
115 }
116
117 public Counter remove(String name)
118 throws NoSuchCounterException, SystemException {
119 Session session = null;
120
121 try {
122 session = openSession();
123
124 Counter counter = (Counter)session.get(CounterImpl.class, name);
125
126 if (counter == null) {
127 if (_log.isWarnEnabled()) {
128 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
129 }
130
131 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
132 name);
133 }
134
135 return remove(counter);
136 }
137 catch (NoSuchCounterException nsee) {
138 throw nsee;
139 }
140 catch (Exception e) {
141 throw processException(e);
142 }
143 finally {
144 closeSession(session);
145 }
146 }
147
148 public Counter remove(Counter counter) throws SystemException {
149 for (ModelListener<Counter> listener : listeners) {
150 listener.onBeforeRemove(counter);
151 }
152
153 counter = removeImpl(counter);
154
155 for (ModelListener<Counter> listener : listeners) {
156 listener.onAfterRemove(counter);
157 }
158
159 return counter;
160 }
161
162 protected Counter removeImpl(Counter counter) throws SystemException {
163 counter = toUnwrappedModel(counter);
164
165 Session session = null;
166
167 try {
168 session = openSession();
169
170 if (counter.isCachedModel() || BatchSessionUtil.isEnabled()) {
171 Object staleObject = session.get(CounterImpl.class,
172 counter.getPrimaryKeyObj());
173
174 if (staleObject != null) {
175 session.evict(staleObject);
176 }
177 }
178
179 session.delete(counter);
180
181 session.flush();
182 }
183 catch (Exception e) {
184 throw processException(e);
185 }
186 finally {
187 closeSession(session);
188 }
189
190 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
191
192 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
193 CounterImpl.class, counter.getPrimaryKey());
194
195 return counter;
196 }
197
198 public Counter updateImpl(com.liferay.counter.model.Counter counter,
199 boolean merge) throws SystemException {
200 counter = toUnwrappedModel(counter);
201
202 Session session = null;
203
204 try {
205 session = openSession();
206
207 BatchSessionUtil.update(session, counter, merge);
208
209 counter.setNew(false);
210 }
211 catch (Exception e) {
212 throw processException(e);
213 }
214 finally {
215 closeSession(session);
216 }
217
218 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
219
220 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
221 CounterImpl.class, counter.getPrimaryKey(), counter);
222
223 return counter;
224 }
225
226 protected Counter toUnwrappedModel(Counter counter) {
227 if (counter instanceof CounterImpl) {
228 return counter;
229 }
230
231 CounterImpl counterImpl = new CounterImpl();
232
233 counterImpl.setNew(counter.isNew());
234 counterImpl.setPrimaryKey(counter.getPrimaryKey());
235
236 counterImpl.setName(counter.getName());
237 counterImpl.setCurrentId(counter.getCurrentId());
238
239 return counterImpl;
240 }
241
242 public Counter findByPrimaryKey(Serializable primaryKey)
243 throws NoSuchModelException, SystemException {
244 return findByPrimaryKey((String)primaryKey);
245 }
246
247 public Counter findByPrimaryKey(String name)
248 throws NoSuchCounterException, SystemException {
249 Counter counter = fetchByPrimaryKey(name);
250
251 if (counter == null) {
252 if (_log.isWarnEnabled()) {
253 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
254 }
255
256 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
257 name);
258 }
259
260 return counter;
261 }
262
263 public Counter fetchByPrimaryKey(Serializable primaryKey)
264 throws SystemException {
265 return fetchByPrimaryKey((String)primaryKey);
266 }
267
268 public Counter fetchByPrimaryKey(String name) throws SystemException {
269 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
270 CounterImpl.class, name, this);
271
272 if (counter == null) {
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 counter = (Counter)session.get(CounterImpl.class, name);
279 }
280 catch (Exception e) {
281 throw processException(e);
282 }
283 finally {
284 if (counter != null) {
285 cacheResult(counter);
286 }
287
288 closeSession(session);
289 }
290 }
291
292 return counter;
293 }
294
295 public List<Counter> findAll() throws SystemException {
296 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
297 }
298
299 public List<Counter> findAll(int start, int end) throws SystemException {
300 return findAll(start, end, null);
301 }
302
303 public List<Counter> findAll(int start, int end,
304 OrderByComparator orderByComparator) throws SystemException {
305 Object[] finderArgs = new Object[] {
306 String.valueOf(start), String.valueOf(end),
307 String.valueOf(orderByComparator)
308 };
309
310 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
311 finderArgs, this);
312
313 if (list == null) {
314 Session session = null;
315
316 try {
317 session = openSession();
318
319 StringBundler query = null;
320 String sql = null;
321
322 if (orderByComparator != null) {
323 query = new StringBundler(2 +
324 (orderByComparator.getOrderByFields().length * 3));
325
326 query.append(_SQL_SELECT_COUNTER);
327
328 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
329 orderByComparator);
330
331 sql = query.toString();
332 }
333
334 sql = _SQL_SELECT_COUNTER;
335
336 Query q = session.createQuery(sql);
337
338 if (orderByComparator == null) {
339 list = (List<Counter>)QueryUtil.list(q, getDialect(),
340 start, end, false);
341
342 Collections.sort(list);
343 }
344 else {
345 list = (List<Counter>)QueryUtil.list(q, getDialect(),
346 start, end);
347 }
348 }
349 catch (Exception e) {
350 throw processException(e);
351 }
352 finally {
353 if (list == null) {
354 list = new ArrayList<Counter>();
355 }
356
357 cacheResult(list);
358
359 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
360
361 closeSession(session);
362 }
363 }
364
365 return list;
366 }
367
368 public void removeAll() throws SystemException {
369 for (Counter counter : findAll()) {
370 remove(counter);
371 }
372 }
373
374 public int countAll() throws SystemException {
375 Object[] finderArgs = new Object[0];
376
377 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
378 finderArgs, this);
379
380 if (count == null) {
381 Session session = null;
382
383 try {
384 session = openSession();
385
386 Query q = session.createQuery(_SQL_COUNT_COUNTER);
387
388 count = (Long)q.uniqueResult();
389 }
390 catch (Exception e) {
391 throw processException(e);
392 }
393 finally {
394 if (count == null) {
395 count = Long.valueOf(0);
396 }
397
398 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
399 count);
400
401 closeSession(session);
402 }
403 }
404
405 return count.intValue();
406 }
407
408 public void afterPropertiesSet() {
409 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
410 com.liferay.portal.util.PropsUtil.get(
411 "value.object.listener.com.liferay.counter.model.Counter")));
412
413 if (listenerClassNames.length > 0) {
414 try {
415 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
416
417 for (String listenerClassName : listenerClassNames) {
418 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
419 listenerClassName));
420 }
421
422 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
423 }
424 catch (Exception e) {
425 _log.error(e);
426 }
427 }
428 }
429
430 @BeanReference(type = CounterPersistence.class)
431 protected CounterPersistence counterPersistence;
432 @BeanReference(type = ResourcePersistence.class)
433 protected ResourcePersistence resourcePersistence;
434 @BeanReference(type = UserPersistence.class)
435 protected UserPersistence userPersistence;
436 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
437 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
438 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
439 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
440 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
441 }