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