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.NoSuchListTypeException;
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.ListType;
033: import com.liferay.portal.model.ModelListener;
034: import com.liferay.portal.model.impl.ListTypeImpl;
035: import com.liferay.portal.model.impl.ListTypeModelImpl;
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="ListTypePersistenceImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class ListTypePersistenceImpl extends BasePersistence implements
059: ListTypePersistence {
060: public ListType create(int listTypeId) {
061: ListType listType = new ListTypeImpl();
062:
063: listType.setNew(true);
064: listType.setPrimaryKey(listTypeId);
065:
066: return listType;
067: }
068:
069: public ListType remove(int listTypeId)
070: throws NoSuchListTypeException, SystemException {
071: Session session = null;
072:
073: try {
074: session = openSession();
075:
076: ListType listType = (ListType) session.get(
077: ListTypeImpl.class, new Integer(listTypeId));
078:
079: if (listType == null) {
080: if (_log.isWarnEnabled()) {
081: _log
082: .warn("No ListType exists with the primary key "
083: + listTypeId);
084: }
085:
086: throw new NoSuchListTypeException(
087: "No ListType exists with the primary key "
088: + listTypeId);
089: }
090:
091: return remove(listType);
092: } catch (NoSuchListTypeException nsee) {
093: throw nsee;
094: } catch (Exception e) {
095: throw HibernateUtil.processException(e);
096: } finally {
097: closeSession(session);
098: }
099: }
100:
101: public ListType remove(ListType listType) throws SystemException {
102: ModelListener listener = _getListener();
103:
104: if (listener != null) {
105: listener.onBeforeRemove(listType);
106: }
107:
108: listType = removeImpl(listType);
109:
110: if (listener != null) {
111: listener.onAfterRemove(listType);
112: }
113:
114: return listType;
115: }
116:
117: protected ListType removeImpl(ListType listType)
118: throws SystemException {
119: Session session = null;
120:
121: try {
122: session = openSession();
123:
124: session.delete(listType);
125:
126: session.flush();
127:
128: return listType;
129: } catch (Exception e) {
130: throw HibernateUtil.processException(e);
131: } finally {
132: closeSession(session);
133:
134: FinderCache.clearCache(ListType.class.getName());
135: }
136: }
137:
138: public ListType update(ListType listType) throws SystemException {
139: return update(listType, false);
140: }
141:
142: public ListType update(ListType listType, boolean merge)
143: throws SystemException {
144: ModelListener listener = _getListener();
145:
146: boolean isNew = listType.isNew();
147:
148: if (listener != null) {
149: if (isNew) {
150: listener.onBeforeCreate(listType);
151: } else {
152: listener.onBeforeUpdate(listType);
153: }
154: }
155:
156: listType = updateImpl(listType, merge);
157:
158: if (listener != null) {
159: if (isNew) {
160: listener.onAfterCreate(listType);
161: } else {
162: listener.onAfterUpdate(listType);
163: }
164: }
165:
166: return listType;
167: }
168:
169: public ListType updateImpl(
170: com.liferay.portal.model.ListType listType, boolean merge)
171: throws SystemException {
172: Session session = null;
173:
174: try {
175: session = openSession();
176:
177: if (merge) {
178: session.merge(listType);
179: } else {
180: if (listType.isNew()) {
181: session.save(listType);
182: }
183: }
184:
185: session.flush();
186:
187: listType.setNew(false);
188:
189: return listType;
190: } catch (Exception e) {
191: throw HibernateUtil.processException(e);
192: } finally {
193: closeSession(session);
194:
195: FinderCache.clearCache(ListType.class.getName());
196: }
197: }
198:
199: public ListType findByPrimaryKey(int listTypeId)
200: throws NoSuchListTypeException, SystemException {
201: ListType listType = fetchByPrimaryKey(listTypeId);
202:
203: if (listType == null) {
204: if (_log.isWarnEnabled()) {
205: _log.warn("No ListType exists with the primary key "
206: + listTypeId);
207: }
208:
209: throw new NoSuchListTypeException(
210: "No ListType exists with the primary key "
211: + listTypeId);
212: }
213:
214: return listType;
215: }
216:
217: public ListType fetchByPrimaryKey(int listTypeId)
218: throws SystemException {
219: Session session = null;
220:
221: try {
222: session = openSession();
223:
224: return (ListType) session.get(ListTypeImpl.class,
225: new Integer(listTypeId));
226: } catch (Exception e) {
227: throw HibernateUtil.processException(e);
228: } finally {
229: closeSession(session);
230: }
231: }
232:
233: public List findByType(String type) throws SystemException {
234: boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
235: String finderClassName = ListType.class.getName();
236: String finderMethodName = "findByType";
237: String[] finderParams = new String[] { String.class.getName() };
238: Object[] finderArgs = new Object[] { type };
239:
240: Object result = null;
241:
242: if (finderClassNameCacheEnabled) {
243: result = FinderCache.getResult(finderClassName,
244: finderMethodName, finderParams, finderArgs,
245: getSessionFactory());
246: }
247:
248: if (result == null) {
249: Session session = null;
250:
251: try {
252: session = openSession();
253:
254: StringMaker query = new StringMaker();
255:
256: query
257: .append("FROM com.liferay.portal.model.ListType WHERE ");
258:
259: if (type == null) {
260: query.append("type_ IS NULL");
261: } else {
262: query.append("type_ = ?");
263: }
264:
265: query.append(" ");
266:
267: query.append("ORDER BY ");
268:
269: query.append("name ASC");
270:
271: Query q = session.createQuery(query.toString());
272:
273: int queryPos = 0;
274:
275: if (type != null) {
276: q.setString(queryPos++, type);
277: }
278:
279: List list = q.list();
280:
281: FinderCache.putResult(finderClassNameCacheEnabled,
282: finderClassName, finderMethodName,
283: finderParams, finderArgs, list);
284:
285: return list;
286: } catch (Exception e) {
287: throw HibernateUtil.processException(e);
288: } finally {
289: closeSession(session);
290: }
291: } else {
292: return (List) result;
293: }
294: }
295:
296: public List findByType(String type, int begin, int end)
297: throws SystemException {
298: return findByType(type, begin, end, null);
299: }
300:
301: public List findByType(String type, int begin, int end,
302: OrderByComparator obc) throws SystemException {
303: boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
304: String finderClassName = ListType.class.getName();
305: String finderMethodName = "findByType";
306: String[] finderParams = new String[] { String.class.getName(),
307:
308: "java.lang.Integer", "java.lang.Integer",
309: "com.liferay.portal.kernel.util.OrderByComparator" };
310: Object[] finderArgs = new Object[] { type,
311:
312: String.valueOf(begin), String.valueOf(end), String.valueOf(obc) };
313:
314: Object result = null;
315:
316: if (finderClassNameCacheEnabled) {
317: result = FinderCache.getResult(finderClassName,
318: finderMethodName, finderParams, finderArgs,
319: getSessionFactory());
320: }
321:
322: if (result == null) {
323: Session session = null;
324:
325: try {
326: session = openSession();
327:
328: StringMaker query = new StringMaker();
329:
330: query
331: .append("FROM com.liferay.portal.model.ListType WHERE ");
332:
333: if (type == null) {
334: query.append("type_ IS NULL");
335: } else {
336: query.append("type_ = ?");
337: }
338:
339: query.append(" ");
340:
341: if (obc != null) {
342: query.append("ORDER BY ");
343: query.append(obc.getOrderBy());
344: }
345:
346: else {
347: query.append("ORDER BY ");
348:
349: query.append("name ASC");
350: }
351:
352: Query q = session.createQuery(query.toString());
353:
354: int queryPos = 0;
355:
356: if (type != null) {
357: q.setString(queryPos++, type);
358: }
359:
360: List list = QueryUtil.list(q, getDialect(), begin, end);
361:
362: FinderCache.putResult(finderClassNameCacheEnabled,
363: finderClassName, finderMethodName,
364: finderParams, finderArgs, list);
365:
366: return list;
367: } catch (Exception e) {
368: throw HibernateUtil.processException(e);
369: } finally {
370: closeSession(session);
371: }
372: } else {
373: return (List) result;
374: }
375: }
376:
377: public ListType findByType_First(String type, OrderByComparator obc)
378: throws NoSuchListTypeException, SystemException {
379: List list = findByType(type, 0, 1, obc);
380:
381: if (list.size() == 0) {
382: StringMaker msg = new StringMaker();
383:
384: msg.append("No ListType exists with the key {");
385:
386: msg.append("type=" + type);
387:
388: msg.append(StringPool.CLOSE_CURLY_BRACE);
389:
390: throw new NoSuchListTypeException(msg.toString());
391: } else {
392: return (ListType) list.get(0);
393: }
394: }
395:
396: public ListType findByType_Last(String type, OrderByComparator obc)
397: throws NoSuchListTypeException, SystemException {
398: int count = countByType(type);
399:
400: List list = findByType(type, count - 1, count, obc);
401:
402: if (list.size() == 0) {
403: StringMaker msg = new StringMaker();
404:
405: msg.append("No ListType exists with the key {");
406:
407: msg.append("type=" + type);
408:
409: msg.append(StringPool.CLOSE_CURLY_BRACE);
410:
411: throw new NoSuchListTypeException(msg.toString());
412: } else {
413: return (ListType) list.get(0);
414: }
415: }
416:
417: public ListType[] findByType_PrevAndNext(int listTypeId,
418: String type, OrderByComparator obc)
419: throws NoSuchListTypeException, SystemException {
420: ListType listType = findByPrimaryKey(listTypeId);
421:
422: int count = countByType(type);
423:
424: Session session = null;
425:
426: try {
427: session = openSession();
428:
429: StringMaker query = new StringMaker();
430:
431: query
432: .append("FROM com.liferay.portal.model.ListType WHERE ");
433:
434: if (type == null) {
435: query.append("type_ IS NULL");
436: } else {
437: query.append("type_ = ?");
438: }
439:
440: query.append(" ");
441:
442: if (obc != null) {
443: query.append("ORDER BY ");
444: query.append(obc.getOrderBy());
445: }
446:
447: else {
448: query.append("ORDER BY ");
449:
450: query.append("name ASC");
451: }
452:
453: Query q = session.createQuery(query.toString());
454:
455: int queryPos = 0;
456:
457: if (type != null) {
458: q.setString(queryPos++, type);
459: }
460:
461: Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
462: listType);
463:
464: ListType[] array = new ListTypeImpl[3];
465:
466: array[0] = (ListType) objArray[0];
467: array[1] = (ListType) objArray[1];
468: array[2] = (ListType) objArray[2];
469:
470: return array;
471: } catch (Exception e) {
472: throw HibernateUtil.processException(e);
473: } finally {
474: closeSession(session);
475: }
476: }
477:
478: public List findWithDynamicQuery(
479: DynamicQueryInitializer queryInitializer)
480: throws SystemException {
481: Session session = null;
482:
483: try {
484: session = openSession();
485:
486: DynamicQuery query = queryInitializer.initialize(session);
487:
488: return query.list();
489: } catch (Exception e) {
490: throw HibernateUtil.processException(e);
491: } finally {
492: closeSession(session);
493: }
494: }
495:
496: public List findWithDynamicQuery(
497: DynamicQueryInitializer queryInitializer, int begin, int end)
498: throws SystemException {
499: Session session = null;
500:
501: try {
502: session = openSession();
503:
504: DynamicQuery query = queryInitializer.initialize(session);
505:
506: query.setLimit(begin, end);
507:
508: return query.list();
509: } catch (Exception e) {
510: throw HibernateUtil.processException(e);
511: } finally {
512: closeSession(session);
513: }
514: }
515:
516: public List findAll() throws SystemException {
517: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
518: }
519:
520: public List findAll(int begin, int end) throws SystemException {
521: return findAll(begin, end, null);
522: }
523:
524: public List findAll(int begin, int end, OrderByComparator obc)
525: throws SystemException {
526: boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
527: String finderClassName = ListType.class.getName();
528: String finderMethodName = "findAll";
529: String[] finderParams = new String[] { "java.lang.Integer",
530: "java.lang.Integer",
531: "com.liferay.portal.kernel.util.OrderByComparator" };
532: Object[] finderArgs = new Object[] { String.valueOf(begin),
533: String.valueOf(end), String.valueOf(obc) };
534:
535: Object result = null;
536:
537: if (finderClassNameCacheEnabled) {
538: result = FinderCache.getResult(finderClassName,
539: finderMethodName, finderParams, finderArgs,
540: getSessionFactory());
541: }
542:
543: if (result == null) {
544: Session session = null;
545:
546: try {
547: session = openSession();
548:
549: StringMaker query = new StringMaker();
550:
551: query.append("FROM com.liferay.portal.model.ListType ");
552:
553: if (obc != null) {
554: query.append("ORDER BY ");
555: query.append(obc.getOrderBy());
556: }
557:
558: else {
559: query.append("ORDER BY ");
560:
561: query.append("name ASC");
562: }
563:
564: Query q = session.createQuery(query.toString());
565:
566: List list = QueryUtil.list(q, getDialect(), begin, end);
567:
568: if (obc == null) {
569: Collections.sort(list);
570: }
571:
572: FinderCache.putResult(finderClassNameCacheEnabled,
573: finderClassName, finderMethodName,
574: finderParams, finderArgs, list);
575:
576: return list;
577: } catch (Exception e) {
578: throw HibernateUtil.processException(e);
579: } finally {
580: closeSession(session);
581: }
582: } else {
583: return (List) result;
584: }
585: }
586:
587: public void removeByType(String type) throws SystemException {
588: Iterator itr = findByType(type).iterator();
589:
590: while (itr.hasNext()) {
591: ListType listType = (ListType) itr.next();
592:
593: remove(listType);
594: }
595: }
596:
597: public void removeAll() throws SystemException {
598: Iterator itr = findAll().iterator();
599:
600: while (itr.hasNext()) {
601: remove((ListType) itr.next());
602: }
603: }
604:
605: public int countByType(String type) throws SystemException {
606: boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
607: String finderClassName = ListType.class.getName();
608: String finderMethodName = "countByType";
609: String[] finderParams = new String[] { String.class.getName() };
610: Object[] finderArgs = new Object[] { type };
611:
612: Object result = null;
613:
614: if (finderClassNameCacheEnabled) {
615: result = FinderCache.getResult(finderClassName,
616: finderMethodName, finderParams, finderArgs,
617: getSessionFactory());
618: }
619:
620: if (result == null) {
621: Session session = null;
622:
623: try {
624: session = openSession();
625:
626: StringMaker query = new StringMaker();
627:
628: query.append("SELECT COUNT(*) ");
629: query
630: .append("FROM com.liferay.portal.model.ListType WHERE ");
631:
632: if (type == null) {
633: query.append("type_ IS NULL");
634: } else {
635: query.append("type_ = ?");
636: }
637:
638: query.append(" ");
639:
640: Query q = session.createQuery(query.toString());
641:
642: int queryPos = 0;
643:
644: if (type != null) {
645: q.setString(queryPos++, type);
646: }
647:
648: Long count = null;
649:
650: Iterator itr = q.list().iterator();
651:
652: if (itr.hasNext()) {
653: count = (Long) itr.next();
654: }
655:
656: if (count == null) {
657: count = new Long(0);
658: }
659:
660: FinderCache.putResult(finderClassNameCacheEnabled,
661: finderClassName, finderMethodName,
662: finderParams, finderArgs, count);
663:
664: return count.intValue();
665: } catch (Exception e) {
666: throw HibernateUtil.processException(e);
667: } finally {
668: closeSession(session);
669: }
670: } else {
671: return ((Long) result).intValue();
672: }
673: }
674:
675: public int countAll() throws SystemException {
676: boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
677: String finderClassName = ListType.class.getName();
678: String finderMethodName = "countAll";
679: String[] finderParams = new String[] {};
680: Object[] finderArgs = new Object[] {};
681:
682: Object result = null;
683:
684: if (finderClassNameCacheEnabled) {
685: result = FinderCache.getResult(finderClassName,
686: finderMethodName, finderParams, finderArgs,
687: getSessionFactory());
688: }
689:
690: if (result == null) {
691: Session session = null;
692:
693: try {
694: session = openSession();
695:
696: Query q = session
697: .createQuery("SELECT COUNT(*) FROM com.liferay.portal.model.ListType");
698:
699: Long count = null;
700:
701: Iterator itr = q.list().iterator();
702:
703: if (itr.hasNext()) {
704: count = (Long) itr.next();
705: }
706:
707: if (count == null) {
708: count = new Long(0);
709: }
710:
711: FinderCache.putResult(finderClassNameCacheEnabled,
712: finderClassName, finderMethodName,
713: finderParams, finderArgs, count);
714:
715: return count.intValue();
716: } catch (Exception e) {
717: throw HibernateUtil.processException(e);
718: } finally {
719: closeSession(session);
720: }
721: } else {
722: return ((Long) result).intValue();
723: }
724: }
725:
726: protected void initDao() {
727: }
728:
729: private static ModelListener _getListener() {
730: if (Validator.isNotNull(_LISTENER)) {
731: try {
732: return (ModelListener) Class.forName(_LISTENER)
733: .newInstance();
734: } catch (Exception e) {
735: _log.error(e);
736: }
737: }
738:
739: return null;
740: }
741:
742: private static final String _LISTENER = GetterUtil
743: .getString(PropsUtil
744: .get("value.object.listener.com.liferay.portal.model.ListType"));
745: private static Log _log = LogFactory
746: .getLog(ListTypePersistenceImpl.class);
747: }
|