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.portlet.tags.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.dao.DynamicQuery;
025: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.OrderByComparator;
028: import com.liferay.portal.kernel.util.StringMaker;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.ModelListener;
031: import com.liferay.portal.service.persistence.BasePersistence;
032: import com.liferay.portal.spring.hibernate.FinderCache;
033: import com.liferay.portal.spring.hibernate.HibernateUtil;
034: import com.liferay.portal.util.PropsUtil;
035:
036: import com.liferay.portlet.tags.NoSuchSourceException;
037: import com.liferay.portlet.tags.model.TagsSource;
038: import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
039: import com.liferay.portlet.tags.model.impl.TagsSourceModelImpl;
040:
041: import com.liferay.util.dao.hibernate.QueryUtil;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: import org.hibernate.Query;
047: import org.hibernate.Session;
048:
049: import java.util.Collections;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: /**
054: * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class TagsSourcePersistenceImpl extends BasePersistence
060: implements TagsSourcePersistence {
061: public TagsSource create(long sourceId) {
062: TagsSource tagsSource = new TagsSourceImpl();
063:
064: tagsSource.setNew(true);
065: tagsSource.setPrimaryKey(sourceId);
066:
067: return tagsSource;
068: }
069:
070: public TagsSource remove(long sourceId)
071: throws NoSuchSourceException, SystemException {
072: Session session = null;
073:
074: try {
075: session = openSession();
076:
077: TagsSource tagsSource = (TagsSource) session.get(
078: TagsSourceImpl.class, new Long(sourceId));
079:
080: if (tagsSource == null) {
081: if (_log.isWarnEnabled()) {
082: _log
083: .warn("No TagsSource exists with the primary key "
084: + sourceId);
085: }
086:
087: throw new NoSuchSourceException(
088: "No TagsSource exists with the primary key "
089: + sourceId);
090: }
091:
092: return remove(tagsSource);
093: } catch (NoSuchSourceException nsee) {
094: throw nsee;
095: } catch (Exception e) {
096: throw HibernateUtil.processException(e);
097: } finally {
098: closeSession(session);
099: }
100: }
101:
102: public TagsSource remove(TagsSource tagsSource)
103: throws SystemException {
104: ModelListener listener = _getListener();
105:
106: if (listener != null) {
107: listener.onBeforeRemove(tagsSource);
108: }
109:
110: tagsSource = removeImpl(tagsSource);
111:
112: if (listener != null) {
113: listener.onAfterRemove(tagsSource);
114: }
115:
116: return tagsSource;
117: }
118:
119: protected TagsSource removeImpl(TagsSource tagsSource)
120: throws SystemException {
121: Session session = null;
122:
123: try {
124: session = openSession();
125:
126: session.delete(tagsSource);
127:
128: session.flush();
129:
130: return tagsSource;
131: } catch (Exception e) {
132: throw HibernateUtil.processException(e);
133: } finally {
134: closeSession(session);
135:
136: FinderCache.clearCache(TagsSource.class.getName());
137: }
138: }
139:
140: public TagsSource update(TagsSource tagsSource)
141: throws SystemException {
142: return update(tagsSource, false);
143: }
144:
145: public TagsSource update(TagsSource tagsSource, boolean merge)
146: throws SystemException {
147: ModelListener listener = _getListener();
148:
149: boolean isNew = tagsSource.isNew();
150:
151: if (listener != null) {
152: if (isNew) {
153: listener.onBeforeCreate(tagsSource);
154: } else {
155: listener.onBeforeUpdate(tagsSource);
156: }
157: }
158:
159: tagsSource = updateImpl(tagsSource, merge);
160:
161: if (listener != null) {
162: if (isNew) {
163: listener.onAfterCreate(tagsSource);
164: } else {
165: listener.onAfterUpdate(tagsSource);
166: }
167: }
168:
169: return tagsSource;
170: }
171:
172: public TagsSource updateImpl(
173: com.liferay.portlet.tags.model.TagsSource tagsSource,
174: boolean merge) throws SystemException {
175: Session session = null;
176:
177: try {
178: session = openSession();
179:
180: if (merge) {
181: session.merge(tagsSource);
182: } else {
183: if (tagsSource.isNew()) {
184: session.save(tagsSource);
185: }
186: }
187:
188: session.flush();
189:
190: tagsSource.setNew(false);
191:
192: return tagsSource;
193: } catch (Exception e) {
194: throw HibernateUtil.processException(e);
195: } finally {
196: closeSession(session);
197:
198: FinderCache.clearCache(TagsSource.class.getName());
199: }
200: }
201:
202: public TagsSource findByPrimaryKey(long sourceId)
203: throws NoSuchSourceException, SystemException {
204: TagsSource tagsSource = fetchByPrimaryKey(sourceId);
205:
206: if (tagsSource == null) {
207: if (_log.isWarnEnabled()) {
208: _log.warn("No TagsSource exists with the primary key "
209: + sourceId);
210: }
211:
212: throw new NoSuchSourceException(
213: "No TagsSource exists with the primary key "
214: + sourceId);
215: }
216:
217: return tagsSource;
218: }
219:
220: public TagsSource fetchByPrimaryKey(long sourceId)
221: throws SystemException {
222: Session session = null;
223:
224: try {
225: session = openSession();
226:
227: return (TagsSource) session.get(TagsSourceImpl.class,
228: new Long(sourceId));
229: } catch (Exception e) {
230: throw HibernateUtil.processException(e);
231: } finally {
232: closeSession(session);
233: }
234: }
235:
236: public List findWithDynamicQuery(
237: DynamicQueryInitializer queryInitializer)
238: throws SystemException {
239: Session session = null;
240:
241: try {
242: session = openSession();
243:
244: DynamicQuery query = queryInitializer.initialize(session);
245:
246: return query.list();
247: } catch (Exception e) {
248: throw HibernateUtil.processException(e);
249: } finally {
250: closeSession(session);
251: }
252: }
253:
254: public List findWithDynamicQuery(
255: DynamicQueryInitializer queryInitializer, int begin, int end)
256: throws SystemException {
257: Session session = null;
258:
259: try {
260: session = openSession();
261:
262: DynamicQuery query = queryInitializer.initialize(session);
263:
264: query.setLimit(begin, end);
265:
266: return query.list();
267: } catch (Exception e) {
268: throw HibernateUtil.processException(e);
269: } finally {
270: closeSession(session);
271: }
272: }
273:
274: public List findAll() throws SystemException {
275: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
276: }
277:
278: public List findAll(int begin, int end) throws SystemException {
279: return findAll(begin, end, null);
280: }
281:
282: public List findAll(int begin, int end, OrderByComparator obc)
283: throws SystemException {
284: boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
285: String finderClassName = TagsSource.class.getName();
286: String finderMethodName = "findAll";
287: String[] finderParams = new String[] { "java.lang.Integer",
288: "java.lang.Integer",
289: "com.liferay.portal.kernel.util.OrderByComparator" };
290: Object[] finderArgs = new Object[] { String.valueOf(begin),
291: String.valueOf(end), String.valueOf(obc) };
292:
293: Object result = null;
294:
295: if (finderClassNameCacheEnabled) {
296: result = FinderCache.getResult(finderClassName,
297: finderMethodName, finderParams, finderArgs,
298: getSessionFactory());
299: }
300:
301: if (result == null) {
302: Session session = null;
303:
304: try {
305: session = openSession();
306:
307: StringMaker query = new StringMaker();
308:
309: query
310: .append("FROM com.liferay.portlet.tags.model.TagsSource ");
311:
312: if (obc != null) {
313: query.append("ORDER BY ");
314: query.append(obc.getOrderBy());
315: }
316:
317: Query q = session.createQuery(query.toString());
318:
319: List list = QueryUtil.list(q, getDialect(), begin, end);
320:
321: if (obc == null) {
322: Collections.sort(list);
323: }
324:
325: FinderCache.putResult(finderClassNameCacheEnabled,
326: finderClassName, finderMethodName,
327: finderParams, finderArgs, list);
328:
329: return list;
330: } catch (Exception e) {
331: throw HibernateUtil.processException(e);
332: } finally {
333: closeSession(session);
334: }
335: } else {
336: return (List) result;
337: }
338: }
339:
340: public void removeAll() throws SystemException {
341: Iterator itr = findAll().iterator();
342:
343: while (itr.hasNext()) {
344: remove((TagsSource) itr.next());
345: }
346: }
347:
348: public int countAll() throws SystemException {
349: boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
350: String finderClassName = TagsSource.class.getName();
351: String finderMethodName = "countAll";
352: String[] finderParams = new String[] {};
353: Object[] finderArgs = new Object[] {};
354:
355: Object result = null;
356:
357: if (finderClassNameCacheEnabled) {
358: result = FinderCache.getResult(finderClassName,
359: finderMethodName, finderParams, finderArgs,
360: getSessionFactory());
361: }
362:
363: if (result == null) {
364: Session session = null;
365:
366: try {
367: session = openSession();
368:
369: Query q = session
370: .createQuery("SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
371:
372: Long count = null;
373:
374: Iterator itr = q.list().iterator();
375:
376: if (itr.hasNext()) {
377: count = (Long) itr.next();
378: }
379:
380: if (count == null) {
381: count = new Long(0);
382: }
383:
384: FinderCache.putResult(finderClassNameCacheEnabled,
385: finderClassName, finderMethodName,
386: finderParams, finderArgs, count);
387:
388: return count.intValue();
389: } catch (Exception e) {
390: throw HibernateUtil.processException(e);
391: } finally {
392: closeSession(session);
393: }
394: } else {
395: return ((Long) result).intValue();
396: }
397: }
398:
399: protected void initDao() {
400: }
401:
402: private static ModelListener _getListener() {
403: if (Validator.isNotNull(_LISTENER)) {
404: try {
405: return (ModelListener) Class.forName(_LISTENER)
406: .newInstance();
407: } catch (Exception e) {
408: _log.error(e);
409: }
410: }
411:
412: return null;
413: }
414:
415: private static final String _LISTENER = GetterUtil
416: .getString(PropsUtil
417: .get("value.object.listener.com.liferay.portlet.tags.model.TagsSource"));
418: private static Log _log = LogFactory
419: .getLog(TagsSourcePersistenceImpl.class);
420: }
|