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