001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.persistence;
022:
023: import com.liferay.portal.NoSuchReleaseException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.dao.DynamicQuery;
026: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.OrderByComparator;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.ModelListener;
032: import com.liferay.portal.model.Release;
033: import com.liferay.portal.model.impl.ReleaseImpl;
034: import com.liferay.portal.model.impl.ReleaseModelImpl;
035: import com.liferay.portal.spring.hibernate.FinderCache;
036: import com.liferay.portal.spring.hibernate.HibernateUtil;
037: import com.liferay.portal.util.PropsUtil;
038:
039: import com.liferay.util.dao.hibernate.QueryUtil;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import org.hibernate.Query;
045: import org.hibernate.Session;
046:
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: /**
052: * <a href="ReleasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Brian Wing Shun Chan
055: *
056: */
057: public class ReleasePersistenceImpl extends BasePersistence implements
058: ReleasePersistence {
059: public Release create(long releaseId) {
060: Release release = new ReleaseImpl();
061:
062: release.setNew(true);
063: release.setPrimaryKey(releaseId);
064:
065: return release;
066: }
067:
068: public Release remove(long releaseId)
069: throws NoSuchReleaseException, SystemException {
070: Session session = null;
071:
072: try {
073: session = openSession();
074:
075: Release release = (Release) session.get(ReleaseImpl.class,
076: new Long(releaseId));
077:
078: if (release == null) {
079: if (_log.isWarnEnabled()) {
080: _log.warn("No Release exists with the primary key "
081: + releaseId);
082: }
083:
084: throw new NoSuchReleaseException(
085: "No Release exists with the primary key "
086: + releaseId);
087: }
088:
089: return remove(release);
090: } catch (NoSuchReleaseException nsee) {
091: throw nsee;
092: } catch (Exception e) {
093: throw HibernateUtil.processException(e);
094: } finally {
095: closeSession(session);
096: }
097: }
098:
099: public Release remove(Release release) throws SystemException {
100: ModelListener listener = _getListener();
101:
102: if (listener != null) {
103: listener.onBeforeRemove(release);
104: }
105:
106: release = removeImpl(release);
107:
108: if (listener != null) {
109: listener.onAfterRemove(release);
110: }
111:
112: return release;
113: }
114:
115: protected Release removeImpl(Release release)
116: throws SystemException {
117: Session session = null;
118:
119: try {
120: session = openSession();
121:
122: session.delete(release);
123:
124: session.flush();
125:
126: return release;
127: } catch (Exception e) {
128: throw HibernateUtil.processException(e);
129: } finally {
130: closeSession(session);
131:
132: FinderCache.clearCache(Release.class.getName());
133: }
134: }
135:
136: public Release update(Release release) throws SystemException {
137: return update(release, false);
138: }
139:
140: public Release update(Release release, boolean merge)
141: throws SystemException {
142: ModelListener listener = _getListener();
143:
144: boolean isNew = release.isNew();
145:
146: if (listener != null) {
147: if (isNew) {
148: listener.onBeforeCreate(release);
149: } else {
150: listener.onBeforeUpdate(release);
151: }
152: }
153:
154: release = updateImpl(release, merge);
155:
156: if (listener != null) {
157: if (isNew) {
158: listener.onAfterCreate(release);
159: } else {
160: listener.onAfterUpdate(release);
161: }
162: }
163:
164: return release;
165: }
166:
167: public Release updateImpl(com.liferay.portal.model.Release release,
168: boolean merge) throws SystemException {
169: Session session = null;
170:
171: try {
172: session = openSession();
173:
174: if (merge) {
175: session.merge(release);
176: } else {
177: if (release.isNew()) {
178: session.save(release);
179: }
180: }
181:
182: session.flush();
183:
184: release.setNew(false);
185:
186: return release;
187: } catch (Exception e) {
188: throw HibernateUtil.processException(e);
189: } finally {
190: closeSession(session);
191:
192: FinderCache.clearCache(Release.class.getName());
193: }
194: }
195:
196: public Release findByPrimaryKey(long releaseId)
197: throws NoSuchReleaseException, SystemException {
198: Release release = fetchByPrimaryKey(releaseId);
199:
200: if (release == null) {
201: if (_log.isWarnEnabled()) {
202: _log.warn("No Release exists with the primary key "
203: + releaseId);
204: }
205:
206: throw new NoSuchReleaseException(
207: "No Release exists with the primary key "
208: + releaseId);
209: }
210:
211: return release;
212: }
213:
214: public Release fetchByPrimaryKey(long releaseId)
215: throws SystemException {
216: Session session = null;
217:
218: try {
219: session = openSession();
220:
221: return (Release) session.get(ReleaseImpl.class, new Long(
222: releaseId));
223: } catch (Exception e) {
224: throw HibernateUtil.processException(e);
225: } finally {
226: closeSession(session);
227: }
228: }
229:
230: public List findWithDynamicQuery(
231: DynamicQueryInitializer queryInitializer)
232: throws SystemException {
233: Session session = null;
234:
235: try {
236: session = openSession();
237:
238: DynamicQuery query = queryInitializer.initialize(session);
239:
240: return query.list();
241: } catch (Exception e) {
242: throw HibernateUtil.processException(e);
243: } finally {
244: closeSession(session);
245: }
246: }
247:
248: public List findWithDynamicQuery(
249: DynamicQueryInitializer queryInitializer, int begin, int end)
250: throws SystemException {
251: Session session = null;
252:
253: try {
254: session = openSession();
255:
256: DynamicQuery query = queryInitializer.initialize(session);
257:
258: query.setLimit(begin, end);
259:
260: return query.list();
261: } catch (Exception e) {
262: throw HibernateUtil.processException(e);
263: } finally {
264: closeSession(session);
265: }
266: }
267:
268: public List findAll() throws SystemException {
269: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
270: }
271:
272: public List findAll(int begin, int end) throws SystemException {
273: return findAll(begin, end, null);
274: }
275:
276: public List findAll(int begin, int end, OrderByComparator obc)
277: throws SystemException {
278: boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
279: String finderClassName = Release.class.getName();
280: String finderMethodName = "findAll";
281: String[] finderParams = new String[] { "java.lang.Integer",
282: "java.lang.Integer",
283: "com.liferay.portal.kernel.util.OrderByComparator" };
284: Object[] finderArgs = new Object[] { String.valueOf(begin),
285: String.valueOf(end), String.valueOf(obc) };
286:
287: Object result = null;
288:
289: if (finderClassNameCacheEnabled) {
290: result = FinderCache.getResult(finderClassName,
291: finderMethodName, finderParams, finderArgs,
292: getSessionFactory());
293: }
294:
295: if (result == null) {
296: Session session = null;
297:
298: try {
299: session = openSession();
300:
301: StringMaker query = new StringMaker();
302:
303: query.append("FROM com.liferay.portal.model.Release ");
304:
305: if (obc != null) {
306: query.append("ORDER BY ");
307: query.append(obc.getOrderBy());
308: }
309:
310: Query q = session.createQuery(query.toString());
311:
312: List list = QueryUtil.list(q, getDialect(), begin, end);
313:
314: if (obc == null) {
315: Collections.sort(list);
316: }
317:
318: FinderCache.putResult(finderClassNameCacheEnabled,
319: finderClassName, finderMethodName,
320: finderParams, finderArgs, list);
321:
322: return list;
323: } catch (Exception e) {
324: throw HibernateUtil.processException(e);
325: } finally {
326: closeSession(session);
327: }
328: } else {
329: return (List) result;
330: }
331: }
332:
333: public void removeAll() throws SystemException {
334: Iterator itr = findAll().iterator();
335:
336: while (itr.hasNext()) {
337: remove((Release) itr.next());
338: }
339: }
340:
341: public int countAll() throws SystemException {
342: boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
343: String finderClassName = Release.class.getName();
344: String finderMethodName = "countAll";
345: String[] finderParams = new String[] {};
346: Object[] finderArgs = new Object[] {};
347:
348: Object result = null;
349:
350: if (finderClassNameCacheEnabled) {
351: result = FinderCache.getResult(finderClassName,
352: finderMethodName, finderParams, finderArgs,
353: getSessionFactory());
354: }
355:
356: if (result == null) {
357: Session session = null;
358:
359: try {
360: session = openSession();
361:
362: Query q = session
363: .createQuery("SELECT COUNT(*) FROM com.liferay.portal.model.Release");
364:
365: Long count = null;
366:
367: Iterator itr = q.list().iterator();
368:
369: if (itr.hasNext()) {
370: count = (Long) itr.next();
371: }
372:
373: if (count == null) {
374: count = new Long(0);
375: }
376:
377: FinderCache.putResult(finderClassNameCacheEnabled,
378: finderClassName, finderMethodName,
379: finderParams, finderArgs, count);
380:
381: return count.intValue();
382: } catch (Exception e) {
383: throw HibernateUtil.processException(e);
384: } finally {
385: closeSession(session);
386: }
387: } else {
388: return ((Long) result).intValue();
389: }
390: }
391:
392: protected void initDao() {
393: }
394:
395: private static ModelListener _getListener() {
396: if (Validator.isNotNull(_LISTENER)) {
397: try {
398: return (ModelListener) Class.forName(_LISTENER)
399: .newInstance();
400: } catch (Exception e) {
401: _log.error(e);
402: }
403: }
404:
405: return null;
406: }
407:
408: private static final String _LISTENER = GetterUtil
409: .getString(PropsUtil
410: .get("value.object.listener.com.liferay.portal.model.Release"));
411: private static Log _log = LogFactory
412: .getLog(ReleasePersistenceImpl.class);
413: }
|