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
057 public class CounterPersistenceImpl extends BasePersistenceImpl<Counter>
058 implements CounterPersistence {
059 public static final String FINDER_CLASS_NAME_ENTITY = CounterImpl.class.getName();
060 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
061 ".List";
062 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
063 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
064 "findAll", new String[0]);
065 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CounterModelImpl.ENTITY_CACHE_ENABLED,
066 CounterModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
067 "countAll", new String[0]);
068
069 public void cacheResult(Counter counter) {
070 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
071 CounterImpl.class, counter.getPrimaryKey(), counter);
072 }
073
074 public void cacheResult(List<Counter> counters) {
075 for (Counter counter : counters) {
076 if (EntityCacheUtil.getResult(
077 CounterModelImpl.ENTITY_CACHE_ENABLED,
078 CounterImpl.class, counter.getPrimaryKey(), this) == null) {
079 cacheResult(counter);
080 }
081 }
082 }
083
084 public void clearCache() {
085 CacheRegistryUtil.clear(CounterImpl.class.getName());
086 EntityCacheUtil.clearCache(CounterImpl.class.getName());
087 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
088 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
089 }
090
091 public void clearCache(Counter counter) {
092 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
093 CounterImpl.class, counter.getPrimaryKey());
094 }
095
096 public Counter create(String name) {
097 Counter counter = new CounterImpl();
098
099 counter.setNew(true);
100 counter.setPrimaryKey(name);
101
102 return counter;
103 }
104
105 public Counter remove(Serializable primaryKey)
106 throws NoSuchModelException, SystemException {
107 return remove((String)primaryKey);
108 }
109
110 public Counter remove(String name)
111 throws NoSuchCounterException, SystemException {
112 Session session = null;
113
114 try {
115 session = openSession();
116
117 Counter counter = (Counter)session.get(CounterImpl.class, name);
118
119 if (counter == null) {
120 if (_log.isWarnEnabled()) {
121 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
122 }
123
124 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
125 name);
126 }
127
128 return remove(counter);
129 }
130 catch (NoSuchCounterException nsee) {
131 throw nsee;
132 }
133 catch (Exception e) {
134 throw processException(e);
135 }
136 finally {
137 closeSession(session);
138 }
139 }
140
141 protected Counter removeImpl(Counter counter) throws SystemException {
142 counter = toUnwrappedModel(counter);
143
144 Session session = null;
145
146 try {
147 session = openSession();
148
149 if (counter.isCachedModel() || BatchSessionUtil.isEnabled()) {
150 Object staleObject = session.get(CounterImpl.class,
151 counter.getPrimaryKeyObj());
152
153 if (staleObject != null) {
154 session.evict(staleObject);
155 }
156 }
157
158 session.delete(counter);
159
160 session.flush();
161 }
162 catch (Exception e) {
163 throw processException(e);
164 }
165 finally {
166 closeSession(session);
167 }
168
169 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
170
171 EntityCacheUtil.removeResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
172 CounterImpl.class, counter.getPrimaryKey());
173
174 return counter;
175 }
176
177 public Counter updateImpl(com.liferay.counter.model.Counter counter,
178 boolean merge) throws SystemException {
179 counter = toUnwrappedModel(counter);
180
181 Session session = null;
182
183 try {
184 session = openSession();
185
186 BatchSessionUtil.update(session, counter, merge);
187
188 counter.setNew(false);
189 }
190 catch (Exception e) {
191 throw processException(e);
192 }
193 finally {
194 closeSession(session);
195 }
196
197 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
198
199 EntityCacheUtil.putResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
200 CounterImpl.class, counter.getPrimaryKey(), counter);
201
202 return counter;
203 }
204
205 protected Counter toUnwrappedModel(Counter counter) {
206 if (counter instanceof CounterImpl) {
207 return counter;
208 }
209
210 CounterImpl counterImpl = new CounterImpl();
211
212 counterImpl.setNew(counter.isNew());
213 counterImpl.setPrimaryKey(counter.getPrimaryKey());
214
215 counterImpl.setName(counter.getName());
216 counterImpl.setCurrentId(counter.getCurrentId());
217
218 return counterImpl;
219 }
220
221 public Counter findByPrimaryKey(Serializable primaryKey)
222 throws NoSuchModelException, SystemException {
223 return findByPrimaryKey((String)primaryKey);
224 }
225
226 public Counter findByPrimaryKey(String name)
227 throws NoSuchCounterException, SystemException {
228 Counter counter = fetchByPrimaryKey(name);
229
230 if (counter == null) {
231 if (_log.isWarnEnabled()) {
232 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + name);
233 }
234
235 throw new NoSuchCounterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
236 name);
237 }
238
239 return counter;
240 }
241
242 public Counter fetchByPrimaryKey(Serializable primaryKey)
243 throws SystemException {
244 return fetchByPrimaryKey((String)primaryKey);
245 }
246
247 public Counter fetchByPrimaryKey(String name) throws SystemException {
248 Counter counter = (Counter)EntityCacheUtil.getResult(CounterModelImpl.ENTITY_CACHE_ENABLED,
249 CounterImpl.class, name, this);
250
251 if (counter == null) {
252 Session session = null;
253
254 try {
255 session = openSession();
256
257 counter = (Counter)session.get(CounterImpl.class, name);
258 }
259 catch (Exception e) {
260 throw processException(e);
261 }
262 finally {
263 if (counter != null) {
264 cacheResult(counter);
265 }
266
267 closeSession(session);
268 }
269 }
270
271 return counter;
272 }
273
274 public List<Counter> findAll() throws SystemException {
275 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
276 }
277
278 public List<Counter> findAll(int start, int end) throws SystemException {
279 return findAll(start, end, null);
280 }
281
282 public List<Counter> findAll(int start, int end,
283 OrderByComparator orderByComparator) throws SystemException {
284 Object[] finderArgs = new Object[] {
285 String.valueOf(start), String.valueOf(end),
286 String.valueOf(orderByComparator)
287 };
288
289 List<Counter> list = (List<Counter>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
290 finderArgs, this);
291
292 if (list == null) {
293 Session session = null;
294
295 try {
296 session = openSession();
297
298 StringBundler query = null;
299 String sql = null;
300
301 if (orderByComparator != null) {
302 query = new StringBundler(2 +
303 (orderByComparator.getOrderByFields().length * 3));
304
305 query.append(_SQL_SELECT_COUNTER);
306
307 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
308 orderByComparator);
309
310 sql = query.toString();
311 }
312 else {
313 sql = _SQL_SELECT_COUNTER;
314 }
315
316 Query q = session.createQuery(sql);
317
318 if (orderByComparator == null) {
319 list = (List<Counter>)QueryUtil.list(q, getDialect(),
320 start, end, false);
321
322 Collections.sort(list);
323 }
324 else {
325 list = (List<Counter>)QueryUtil.list(q, getDialect(),
326 start, end);
327 }
328 }
329 catch (Exception e) {
330 throw processException(e);
331 }
332 finally {
333 if (list == null) {
334 list = new ArrayList<Counter>();
335 }
336
337 cacheResult(list);
338
339 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
340
341 closeSession(session);
342 }
343 }
344
345 return list;
346 }
347
348 public void removeAll() throws SystemException {
349 for (Counter counter : findAll()) {
350 remove(counter);
351 }
352 }
353
354 public int countAll() throws SystemException {
355 Object[] finderArgs = new Object[0];
356
357 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
358 finderArgs, this);
359
360 if (count == null) {
361 Session session = null;
362
363 try {
364 session = openSession();
365
366 Query q = session.createQuery(_SQL_COUNT_COUNTER);
367
368 count = (Long)q.uniqueResult();
369 }
370 catch (Exception e) {
371 throw processException(e);
372 }
373 finally {
374 if (count == null) {
375 count = Long.valueOf(0);
376 }
377
378 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
379 count);
380
381 closeSession(session);
382 }
383 }
384
385 return count.intValue();
386 }
387
388 public void afterPropertiesSet() {
389 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
390 com.liferay.portal.util.PropsUtil.get(
391 "value.object.listener.com.liferay.counter.model.Counter")));
392
393 if (listenerClassNames.length > 0) {
394 try {
395 List<ModelListener<Counter>> listenersList = new ArrayList<ModelListener<Counter>>();
396
397 for (String listenerClassName : listenerClassNames) {
398 listenersList.add((ModelListener<Counter>)InstanceFactory.newInstance(
399 listenerClassName));
400 }
401
402 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
403 }
404 catch (Exception e) {
405 _log.error(e);
406 }
407 }
408 }
409
410 @BeanReference(type = CounterPersistence.class)
411 protected CounterPersistence counterPersistence;
412 @BeanReference(type = ResourcePersistence.class)
413 protected ResourcePersistence resourcePersistence;
414 @BeanReference(type = UserPersistence.class)
415 protected UserPersistence userPersistence;
416 private static final String _SQL_SELECT_COUNTER = "SELECT counter FROM Counter counter";
417 private static final String _SQL_COUNT_COUNTER = "SELECT COUNT(counter) FROM Counter counter";
418 private static final String _ORDER_BY_ENTITY_ALIAS = "counter.";
419 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Counter exists with the primary key ";
420 private static Log _log = LogFactoryUtil.getLog(CounterPersistenceImpl.class);
421 }