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.NoSuchClassNameException;
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.StringPool;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.ClassName;
033: import com.liferay.portal.model.ModelListener;
034: import com.liferay.portal.model.impl.ClassNameImpl;
035: import com.liferay.portal.model.impl.ClassNameModelImpl;
036: import com.liferay.portal.spring.hibernate.FinderCache;
037: import com.liferay.portal.spring.hibernate.HibernateUtil;
038: import com.liferay.portal.util.PropsUtil;
039:
040: import com.liferay.util.dao.hibernate.QueryUtil;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: import org.hibernate.Query;
046: import org.hibernate.Session;
047:
048: import java.util.Collections;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: /**
053: * <a href="ClassNamePersistenceImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class ClassNamePersistenceImpl extends BasePersistence implements
059: ClassNamePersistence {
060: public ClassName create(long classNameId) {
061: ClassName className = new ClassNameImpl();
062:
063: className.setNew(true);
064: className.setPrimaryKey(classNameId);
065:
066: return className;
067: }
068:
069: public ClassName remove(long classNameId)
070: throws NoSuchClassNameException, SystemException {
071: Session session = null;
072:
073: try {
074: session = openSession();
075:
076: ClassName className = (ClassName) session.get(
077: ClassNameImpl.class, new Long(classNameId));
078:
079: if (className == null) {
080: if (_log.isWarnEnabled()) {
081: _log
082: .warn("No ClassName exists with the primary key "
083: + classNameId);
084: }
085:
086: throw new NoSuchClassNameException(
087: "No ClassName exists with the primary key "
088: + classNameId);
089: }
090:
091: return remove(className);
092: } catch (NoSuchClassNameException nsee) {
093: throw nsee;
094: } catch (Exception e) {
095: throw HibernateUtil.processException(e);
096: } finally {
097: closeSession(session);
098: }
099: }
100:
101: public ClassName remove(ClassName className) throws SystemException {
102: ModelListener listener = _getListener();
103:
104: if (listener != null) {
105: listener.onBeforeRemove(className);
106: }
107:
108: className = removeImpl(className);
109:
110: if (listener != null) {
111: listener.onAfterRemove(className);
112: }
113:
114: return className;
115: }
116:
117: protected ClassName removeImpl(ClassName className)
118: throws SystemException {
119: Session session = null;
120:
121: try {
122: session = openSession();
123:
124: session.delete(className);
125:
126: session.flush();
127:
128: return className;
129: } catch (Exception e) {
130: throw HibernateUtil.processException(e);
131: } finally {
132: closeSession(session);
133:
134: FinderCache.clearCache(ClassName.class.getName());
135: }
136: }
137:
138: public ClassName update(ClassName className) throws SystemException {
139: return update(className, false);
140: }
141:
142: public ClassName update(ClassName className, boolean merge)
143: throws SystemException {
144: ModelListener listener = _getListener();
145:
146: boolean isNew = className.isNew();
147:
148: if (listener != null) {
149: if (isNew) {
150: listener.onBeforeCreate(className);
151: } else {
152: listener.onBeforeUpdate(className);
153: }
154: }
155:
156: className = updateImpl(className, merge);
157:
158: if (listener != null) {
159: if (isNew) {
160: listener.onAfterCreate(className);
161: } else {
162: listener.onAfterUpdate(className);
163: }
164: }
165:
166: return className;
167: }
168:
169: public ClassName updateImpl(
170: com.liferay.portal.model.ClassName className, boolean merge)
171: throws SystemException {
172: Session session = null;
173:
174: try {
175: session = openSession();
176:
177: if (merge) {
178: session.merge(className);
179: } else {
180: if (className.isNew()) {
181: session.save(className);
182: }
183: }
184:
185: session.flush();
186:
187: className.setNew(false);
188:
189: return className;
190: } catch (Exception e) {
191: throw HibernateUtil.processException(e);
192: } finally {
193: closeSession(session);
194:
195: FinderCache.clearCache(ClassName.class.getName());
196: }
197: }
198:
199: public ClassName findByPrimaryKey(long classNameId)
200: throws NoSuchClassNameException, SystemException {
201: ClassName className = fetchByPrimaryKey(classNameId);
202:
203: if (className == null) {
204: if (_log.isWarnEnabled()) {
205: _log.warn("No ClassName exists with the primary key "
206: + classNameId);
207: }
208:
209: throw new NoSuchClassNameException(
210: "No ClassName exists with the primary key "
211: + classNameId);
212: }
213:
214: return className;
215: }
216:
217: public ClassName fetchByPrimaryKey(long classNameId)
218: throws SystemException {
219: Session session = null;
220:
221: try {
222: session = openSession();
223:
224: return (ClassName) session.get(ClassNameImpl.class,
225: new Long(classNameId));
226: } catch (Exception e) {
227: throw HibernateUtil.processException(e);
228: } finally {
229: closeSession(session);
230: }
231: }
232:
233: public ClassName findByValue(String value)
234: throws NoSuchClassNameException, SystemException {
235: ClassName className = fetchByValue(value);
236:
237: if (className == null) {
238: StringMaker msg = new StringMaker();
239:
240: msg.append("No ClassName exists with the key {");
241:
242: msg.append("value=" + value);
243:
244: msg.append(StringPool.CLOSE_CURLY_BRACE);
245:
246: if (_log.isWarnEnabled()) {
247: _log.warn(msg.toString());
248: }
249:
250: throw new NoSuchClassNameException(msg.toString());
251: }
252:
253: return className;
254: }
255:
256: public ClassName fetchByValue(String value) throws SystemException {
257: boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
258: String finderClassName = ClassName.class.getName();
259: String finderMethodName = "fetchByValue";
260: String[] finderParams = new String[] { String.class.getName() };
261: Object[] finderArgs = new Object[] { value };
262:
263: Object result = null;
264:
265: if (finderClassNameCacheEnabled) {
266: result = FinderCache.getResult(finderClassName,
267: finderMethodName, finderParams, finderArgs,
268: getSessionFactory());
269: }
270:
271: if (result == null) {
272: Session session = null;
273:
274: try {
275: session = openSession();
276:
277: StringMaker query = new StringMaker();
278:
279: query
280: .append("FROM com.liferay.portal.model.ClassName WHERE ");
281:
282: if (value == null) {
283: query.append("value IS NULL");
284: } else {
285: query.append("value = ?");
286: }
287:
288: query.append(" ");
289:
290: Query q = session.createQuery(query.toString());
291:
292: int queryPos = 0;
293:
294: if (value != null) {
295: q.setString(queryPos++, value);
296: }
297:
298: List list = q.list();
299:
300: FinderCache.putResult(finderClassNameCacheEnabled,
301: finderClassName, finderMethodName,
302: finderParams, finderArgs, list);
303:
304: if (list.size() == 0) {
305: return null;
306: } else {
307: return (ClassName) list.get(0);
308: }
309: } catch (Exception e) {
310: throw HibernateUtil.processException(e);
311: } finally {
312: closeSession(session);
313: }
314: } else {
315: List list = (List) result;
316:
317: if (list.size() == 0) {
318: return null;
319: } else {
320: return (ClassName) list.get(0);
321: }
322: }
323: }
324:
325: public List findWithDynamicQuery(
326: DynamicQueryInitializer queryInitializer)
327: throws SystemException {
328: Session session = null;
329:
330: try {
331: session = openSession();
332:
333: DynamicQuery query = queryInitializer.initialize(session);
334:
335: return query.list();
336: } catch (Exception e) {
337: throw HibernateUtil.processException(e);
338: } finally {
339: closeSession(session);
340: }
341: }
342:
343: public List findWithDynamicQuery(
344: DynamicQueryInitializer queryInitializer, int begin, int end)
345: throws SystemException {
346: Session session = null;
347:
348: try {
349: session = openSession();
350:
351: DynamicQuery query = queryInitializer.initialize(session);
352:
353: query.setLimit(begin, end);
354:
355: return query.list();
356: } catch (Exception e) {
357: throw HibernateUtil.processException(e);
358: } finally {
359: closeSession(session);
360: }
361: }
362:
363: public List findAll() throws SystemException {
364: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
365: }
366:
367: public List findAll(int begin, int end) throws SystemException {
368: return findAll(begin, end, null);
369: }
370:
371: public List findAll(int begin, int end, OrderByComparator obc)
372: throws SystemException {
373: boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
374: String finderClassName = ClassName.class.getName();
375: String finderMethodName = "findAll";
376: String[] finderParams = new String[] { "java.lang.Integer",
377: "java.lang.Integer",
378: "com.liferay.portal.kernel.util.OrderByComparator" };
379: Object[] finderArgs = new Object[] { String.valueOf(begin),
380: String.valueOf(end), String.valueOf(obc) };
381:
382: Object result = null;
383:
384: if (finderClassNameCacheEnabled) {
385: result = FinderCache.getResult(finderClassName,
386: finderMethodName, finderParams, finderArgs,
387: getSessionFactory());
388: }
389:
390: if (result == null) {
391: Session session = null;
392:
393: try {
394: session = openSession();
395:
396: StringMaker query = new StringMaker();
397:
398: query
399: .append("FROM com.liferay.portal.model.ClassName ");
400:
401: if (obc != null) {
402: query.append("ORDER BY ");
403: query.append(obc.getOrderBy());
404: }
405:
406: Query q = session.createQuery(query.toString());
407:
408: List list = QueryUtil.list(q, getDialect(), begin, end);
409:
410: if (obc == null) {
411: Collections.sort(list);
412: }
413:
414: FinderCache.putResult(finderClassNameCacheEnabled,
415: finderClassName, finderMethodName,
416: finderParams, finderArgs, list);
417:
418: return list;
419: } catch (Exception e) {
420: throw HibernateUtil.processException(e);
421: } finally {
422: closeSession(session);
423: }
424: } else {
425: return (List) result;
426: }
427: }
428:
429: public void removeByValue(String value)
430: throws NoSuchClassNameException, SystemException {
431: ClassName className = findByValue(value);
432:
433: remove(className);
434: }
435:
436: public void removeAll() throws SystemException {
437: Iterator itr = findAll().iterator();
438:
439: while (itr.hasNext()) {
440: remove((ClassName) itr.next());
441: }
442: }
443:
444: public int countByValue(String value) throws SystemException {
445: boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
446: String finderClassName = ClassName.class.getName();
447: String finderMethodName = "countByValue";
448: String[] finderParams = new String[] { String.class.getName() };
449: Object[] finderArgs = new Object[] { value };
450:
451: Object result = null;
452:
453: if (finderClassNameCacheEnabled) {
454: result = FinderCache.getResult(finderClassName,
455: finderMethodName, finderParams, finderArgs,
456: getSessionFactory());
457: }
458:
459: if (result == null) {
460: Session session = null;
461:
462: try {
463: session = openSession();
464:
465: StringMaker query = new StringMaker();
466:
467: query.append("SELECT COUNT(*) ");
468: query
469: .append("FROM com.liferay.portal.model.ClassName WHERE ");
470:
471: if (value == null) {
472: query.append("value IS NULL");
473: } else {
474: query.append("value = ?");
475: }
476:
477: query.append(" ");
478:
479: Query q = session.createQuery(query.toString());
480:
481: int queryPos = 0;
482:
483: if (value != null) {
484: q.setString(queryPos++, value);
485: }
486:
487: Long count = null;
488:
489: Iterator itr = q.list().iterator();
490:
491: if (itr.hasNext()) {
492: count = (Long) itr.next();
493: }
494:
495: if (count == null) {
496: count = new Long(0);
497: }
498:
499: FinderCache.putResult(finderClassNameCacheEnabled,
500: finderClassName, finderMethodName,
501: finderParams, finderArgs, count);
502:
503: return count.intValue();
504: } catch (Exception e) {
505: throw HibernateUtil.processException(e);
506: } finally {
507: closeSession(session);
508: }
509: } else {
510: return ((Long) result).intValue();
511: }
512: }
513:
514: public int countAll() throws SystemException {
515: boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
516: String finderClassName = ClassName.class.getName();
517: String finderMethodName = "countAll";
518: String[] finderParams = new String[] {};
519: Object[] finderArgs = new Object[] {};
520:
521: Object result = null;
522:
523: if (finderClassNameCacheEnabled) {
524: result = FinderCache.getResult(finderClassName,
525: finderMethodName, finderParams, finderArgs,
526: getSessionFactory());
527: }
528:
529: if (result == null) {
530: Session session = null;
531:
532: try {
533: session = openSession();
534:
535: Query q = session
536: .createQuery("SELECT COUNT(*) FROM com.liferay.portal.model.ClassName");
537:
538: Long count = null;
539:
540: Iterator itr = q.list().iterator();
541:
542: if (itr.hasNext()) {
543: count = (Long) itr.next();
544: }
545:
546: if (count == null) {
547: count = new Long(0);
548: }
549:
550: FinderCache.putResult(finderClassNameCacheEnabled,
551: finderClassName, finderMethodName,
552: finderParams, finderArgs, count);
553:
554: return count.intValue();
555: } catch (Exception e) {
556: throw HibernateUtil.processException(e);
557: } finally {
558: closeSession(session);
559: }
560: } else {
561: return ((Long) result).intValue();
562: }
563: }
564:
565: protected void initDao() {
566: }
567:
568: private static ModelListener _getListener() {
569: if (Validator.isNotNull(_LISTENER)) {
570: try {
571: return (ModelListener) Class.forName(_LISTENER)
572: .newInstance();
573: } catch (Exception e) {
574: _log.error(e);
575: }
576: }
577:
578: return null;
579: }
580:
581: private static final String _LISTENER = GetterUtil
582: .getString(PropsUtil
583: .get("value.object.listener.com.liferay.portal.model.ClassName"));
584: private static Log _log = LogFactory
585: .getLog(ClassNamePersistenceImpl.class);
586: }
|