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