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