1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchReleaseException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.dao.DynamicQuery;
28 import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.OrderByComparator;
31 import com.liferay.portal.kernel.util.StringMaker;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.ModelListener;
34 import com.liferay.portal.model.Release;
35 import com.liferay.portal.model.impl.ReleaseImpl;
36 import com.liferay.portal.model.impl.ReleaseModelImpl;
37 import com.liferay.portal.spring.hibernate.FinderCache;
38 import com.liferay.portal.spring.hibernate.HibernateUtil;
39 import com.liferay.portal.util.PropsUtil;
40
41 import com.liferay.util.dao.hibernate.QueryUtil;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import org.hibernate.Query;
47 import org.hibernate.Session;
48
49 import java.util.Collections;
50 import java.util.Iterator;
51 import java.util.List;
52
53
59 public class ReleasePersistenceImpl extends BasePersistence
60 implements ReleasePersistence {
61 public Release create(long releaseId) {
62 Release release = new ReleaseImpl();
63
64 release.setNew(true);
65 release.setPrimaryKey(releaseId);
66
67 return release;
68 }
69
70 public Release remove(long releaseId)
71 throws NoSuchReleaseException, SystemException {
72 Session session = null;
73
74 try {
75 session = openSession();
76
77 Release release = (Release)session.get(ReleaseImpl.class,
78 new Long(releaseId));
79
80 if (release == null) {
81 if (_log.isWarnEnabled()) {
82 _log.warn("No Release exists with the primary key " +
83 releaseId);
84 }
85
86 throw new NoSuchReleaseException(
87 "No Release exists with the primary key " + releaseId);
88 }
89
90 return remove(release);
91 }
92 catch (NoSuchReleaseException nsee) {
93 throw nsee;
94 }
95 catch (Exception e) {
96 throw HibernateUtil.processException(e);
97 }
98 finally {
99 closeSession(session);
100 }
101 }
102
103 public Release remove(Release release) throws SystemException {
104 ModelListener listener = _getListener();
105
106 if (listener != null) {
107 listener.onBeforeRemove(release);
108 }
109
110 release = removeImpl(release);
111
112 if (listener != null) {
113 listener.onAfterRemove(release);
114 }
115
116 return release;
117 }
118
119 protected Release removeImpl(Release release) throws SystemException {
120 Session session = null;
121
122 try {
123 session = openSession();
124
125 session.delete(release);
126
127 session.flush();
128
129 return release;
130 }
131 catch (Exception e) {
132 throw HibernateUtil.processException(e);
133 }
134 finally {
135 closeSession(session);
136
137 FinderCache.clearCache(Release.class.getName());
138 }
139 }
140
141 public Release update(Release release) throws SystemException {
142 return update(release, false);
143 }
144
145 public Release update(Release release, boolean merge)
146 throws SystemException {
147 ModelListener listener = _getListener();
148
149 boolean isNew = release.isNew();
150
151 if (listener != null) {
152 if (isNew) {
153 listener.onBeforeCreate(release);
154 }
155 else {
156 listener.onBeforeUpdate(release);
157 }
158 }
159
160 release = updateImpl(release, merge);
161
162 if (listener != null) {
163 if (isNew) {
164 listener.onAfterCreate(release);
165 }
166 else {
167 listener.onAfterUpdate(release);
168 }
169 }
170
171 return release;
172 }
173
174 public Release updateImpl(com.liferay.portal.model.Release release,
175 boolean merge) throws SystemException {
176 Session session = null;
177
178 try {
179 session = openSession();
180
181 if (merge) {
182 session.merge(release);
183 }
184 else {
185 if (release.isNew()) {
186 session.save(release);
187 }
188 }
189
190 session.flush();
191
192 release.setNew(false);
193
194 return release;
195 }
196 catch (Exception e) {
197 throw HibernateUtil.processException(e);
198 }
199 finally {
200 closeSession(session);
201
202 FinderCache.clearCache(Release.class.getName());
203 }
204 }
205
206 public Release findByPrimaryKey(long releaseId)
207 throws NoSuchReleaseException, SystemException {
208 Release release = fetchByPrimaryKey(releaseId);
209
210 if (release == null) {
211 if (_log.isWarnEnabled()) {
212 _log.warn("No Release exists with the primary key " +
213 releaseId);
214 }
215
216 throw new NoSuchReleaseException(
217 "No Release exists with the primary key " + releaseId);
218 }
219
220 return release;
221 }
222
223 public Release fetchByPrimaryKey(long releaseId) throws SystemException {
224 Session session = null;
225
226 try {
227 session = openSession();
228
229 return (Release)session.get(ReleaseImpl.class, new Long(releaseId));
230 }
231 catch (Exception e) {
232 throw HibernateUtil.processException(e);
233 }
234 finally {
235 closeSession(session);
236 }
237 }
238
239 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
240 throws SystemException {
241 Session session = null;
242
243 try {
244 session = openSession();
245
246 DynamicQuery query = queryInitializer.initialize(session);
247
248 return query.list();
249 }
250 catch (Exception e) {
251 throw HibernateUtil.processException(e);
252 }
253 finally {
254 closeSession(session);
255 }
256 }
257
258 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
259 int begin, int end) throws SystemException {
260 Session session = null;
261
262 try {
263 session = openSession();
264
265 DynamicQuery query = queryInitializer.initialize(session);
266
267 query.setLimit(begin, end);
268
269 return query.list();
270 }
271 catch (Exception e) {
272 throw HibernateUtil.processException(e);
273 }
274 finally {
275 closeSession(session);
276 }
277 }
278
279 public List findAll() throws SystemException {
280 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
281 }
282
283 public List findAll(int begin, int end) throws SystemException {
284 return findAll(begin, end, null);
285 }
286
287 public List findAll(int begin, int end, OrderByComparator obc)
288 throws SystemException {
289 boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
290 String finderClassName = Release.class.getName();
291 String finderMethodName = "findAll";
292 String[] finderParams = new String[] {
293 "java.lang.Integer", "java.lang.Integer",
294 "com.liferay.portal.kernel.util.OrderByComparator"
295 };
296 Object[] finderArgs = new Object[] {
297 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
298 };
299
300 Object result = null;
301
302 if (finderClassNameCacheEnabled) {
303 result = FinderCache.getResult(finderClassName, finderMethodName,
304 finderParams, finderArgs, getSessionFactory());
305 }
306
307 if (result == null) {
308 Session session = null;
309
310 try {
311 session = openSession();
312
313 StringMaker query = new StringMaker();
314
315 query.append("FROM com.liferay.portal.model.Release ");
316
317 if (obc != null) {
318 query.append("ORDER BY ");
319 query.append(obc.getOrderBy());
320 }
321
322 Query q = session.createQuery(query.toString());
323
324 List list = QueryUtil.list(q, getDialect(), begin, end);
325
326 if (obc == null) {
327 Collections.sort(list);
328 }
329
330 FinderCache.putResult(finderClassNameCacheEnabled,
331 finderClassName, finderMethodName, finderParams,
332 finderArgs, list);
333
334 return list;
335 }
336 catch (Exception e) {
337 throw HibernateUtil.processException(e);
338 }
339 finally {
340 closeSession(session);
341 }
342 }
343 else {
344 return (List)result;
345 }
346 }
347
348 public void removeAll() throws SystemException {
349 Iterator itr = findAll().iterator();
350
351 while (itr.hasNext()) {
352 remove((Release)itr.next());
353 }
354 }
355
356 public int countAll() throws SystemException {
357 boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
358 String finderClassName = Release.class.getName();
359 String finderMethodName = "countAll";
360 String[] finderParams = new String[] { };
361 Object[] finderArgs = new Object[] { };
362
363 Object result = null;
364
365 if (finderClassNameCacheEnabled) {
366 result = FinderCache.getResult(finderClassName, finderMethodName,
367 finderParams, finderArgs, getSessionFactory());
368 }
369
370 if (result == null) {
371 Session session = null;
372
373 try {
374 session = openSession();
375
376 Query q = session.createQuery(
377 "SELECT COUNT(*) FROM com.liferay.portal.model.Release");
378
379 Long count = null;
380
381 Iterator itr = q.list().iterator();
382
383 if (itr.hasNext()) {
384 count = (Long)itr.next();
385 }
386
387 if (count == null) {
388 count = new Long(0);
389 }
390
391 FinderCache.putResult(finderClassNameCacheEnabled,
392 finderClassName, finderMethodName, finderParams,
393 finderArgs, count);
394
395 return count.intValue();
396 }
397 catch (Exception e) {
398 throw HibernateUtil.processException(e);
399 }
400 finally {
401 closeSession(session);
402 }
403 }
404 else {
405 return ((Long)result).intValue();
406 }
407 }
408
409 protected void initDao() {
410 }
411
412 private static ModelListener _getListener() {
413 if (Validator.isNotNull(_LISTENER)) {
414 try {
415 return (ModelListener)Class.forName(_LISTENER).newInstance();
416 }
417 catch (Exception e) {
418 _log.error(e);
419 }
420 }
421
422 return null;
423 }
424
425 private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
426 "value.object.listener.com.liferay.portal.model.Release"));
427 private static Log _log = LogFactory.getLog(ReleasePersistenceImpl.class);
428 }