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