001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: HibernateManager.java,v $
031: * Revision 1.14 2005/10/14 20:53:03 colinmacleod
032: * Changed remove methods to use the value object rather than the id.
033: *
034: * Revision 1.13 2005/10/14 14:02:32 colinmacleod
035: * Added a check for null objects in filter.
036: *
037: * Revision 1.12 2005/10/11 18:51:38 colinmacleod
038: * Fixed some checkstyle and javadoc issues.
039: *
040: * Revision 1.11 2005/10/03 10:21:14 colinmacleod
041: * Fixed some style and javadoc issues.
042: *
043: * Revision 1.10 2005/10/02 14:08:56 colinmacleod
044: * Added/improved log4j logging.
045: *
046: * Revision 1.9 2005/09/29 13:51:38 colinmacleod
047: * Moved PersistenceListener interfaces to ivata masks.
048: *
049: * Revision 1.8 2005/09/16 13:43:20 colinmacleod
050: * Fixed delete syntax for Hibernate 3.0.5.
051: *
052: * Revision 1.7 2005/09/15 10:23:24 colinmacleod
053: * Upgraded Maven to 1.1 (beta-2).
054: * Upgraded Hibernate to 3.0.5.
055: *
056: * Revision 1.6 2005/09/14 15:20:29 colinmacleod
057: * Removed unused local and class variables.
058: * Added serialVersionUID.
059: *
060: * Revision 1.5 2005/04/29 02:48:15 colinmacleod
061: * Data bugfixes.
062: * Changed primary key back to Integer.
063: *
064: * Revision 1.4 2005/04/10 20:43:09 colinmacleod
065: * Added new themes.
066: * Changed id type to String.
067: * Changed i tag to em and b tag to strong.
068: * Improved PicoContainerFactory with NanoContainer scripts.
069: *
070: * Revision 1.3 2005/04/09 17:19:37 colinmacleod
071: * Changed copyright text to GPL v2 explicitly.
072: *
073: * Revision 1.2 2005/03/16 15:46:44 colinmacleod
074: * Added flush after save.
075: *
076: * Revision 1.1 2005/03/10 19:23:04 colinmacleod
077: * Moved to ivata groupware.
078: *
079: * Revision 1.5 2004/12/31 18:43:17 colinmacleod
080: * Added class checking of key (id) type for ivata masks.
081: *
082: * Revision 1.4 2004/11/12 15:57:10 colinmacleod
083: * Removed dependencies on SSLEXT.
084: * Moved Persistence classes to ivata masks.
085: *
086: * Revision 1.3 2004/09/30 15:15:55 colinmacleod
087: * Split off addressbook elements into security subproject.
088: *
089: * Revision 1.2 2004/08/01 11:55:02 colinmacleod
090: * Added removeAll.
091: *
092: * Revision 1.1 2004/07/13 19:42:44 colinmacleod
093: * Moved project to POJOs from EJBs.
094: * Applied PicoContainer to services layer (replacing session EJBs).
095: * Applied Hibernate to persistence layer (replacing entity EJBs).
096: * -----------------------------------------------------------------------------
097: */
098: package com.ivata.groupware.container.persistence.hibernate;
099:
100: import java.io.Serializable;
101: import java.util.Iterator;
102: import java.util.List;
103: import java.util.Map;
104: import java.util.Set;
105: import java.util.Vector;
106:
107: import javax.servlet.http.HttpSession;
108:
109: import org.apache.log4j.Logger;
110: import org.hibernate.HibernateException;
111: import org.hibernate.ObjectNotFoundException;
112: import org.hibernate.Query;
113: import org.hibernate.Session;
114: import org.hibernate.SessionFactory;
115: import org.hibernate.Transaction;
116:
117: import com.ivata.mask.persistence.FinderException;
118: import com.ivata.mask.persistence.PersistenceException;
119: import com.ivata.mask.persistence.PersistenceFilter;
120: import com.ivata.mask.persistence.PersistenceSession;
121: import com.ivata.mask.persistence.QueryPersistenceManager;
122: import com.ivata.mask.persistence.RightViolationException;
123: import com.ivata.mask.persistence.listener.AddPersistenceListener;
124: import com.ivata.mask.persistence.listener.AmendPersistenceListener;
125: import com.ivata.mask.persistence.listener.RemovePersistenceListener;
126: import com.ivata.mask.util.StringHandling;
127: import com.ivata.mask.valueobject.ValueObject;
128:
129: /**
130: * <p>
131: * This class provides a persistence manager for the whole
132: * <strong>ivata groupware</strong> project. Every time a data object should
133: * be stored to or retrieved from the data store, it happens here.
134: * </p>
135: *
136: * @author Colin MacLeod
137: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
138: * @since ivata groupware 0.10 (Mar 27, 2004)
139: * @version $Revision: 1.14 $
140: */
141: public class HibernateManager implements QueryPersistenceManager,
142: Serializable {
143: /**
144: * Logger for this class.
145: */
146: private static final Logger logger = Logger
147: .getLogger(HibernateManager.class);
148:
149: /**
150: * Serialization version (for <code>Serializable</code> interface).
151: */
152: private static final long serialVersionUID = 1L;
153: /**
154: * Stores all filters - used for limiting the data returned.
155: */
156: private List filters = new Vector();
157: /**
158: * <p>
159: * This object is used to create the queries from strings and arguments.
160: * </p>
161: */
162: private HibernateQueryFactory queryFactory;
163:
164: /**
165: * <p>
166: * Hibernate session factory.
167: * </p>
168: */
169: private SessionFactory sessionFactory;
170:
171: /**
172: * <p>
173: * One and only constructor - called by Pico. Uses constructor arguments
174: * to initialize the whole object.
175: * </p>
176: *
177: * @param sessionFactoryParam Used to create <strong>Hibernate</strong>
178: * sessions!
179: * @param queryFactoryParam This object is used to create the queries from
180: * strings and arguments.
181: * @param interceptorParam
182: */
183: public HibernateManager(final SessionFactory sessionFactoryParam,
184: final HibernateQueryFactory queryFactoryParam) {
185: this .sessionFactory = sessionFactoryParam;
186: this .queryFactory = queryFactoryParam;
187: }
188:
189: /**
190: * {@inheritDoc}
191: *
192: * @param session {@inheritDoc}
193: * @param valueObject {@inheritDoc}
194: * @return {@inheritDoc}
195: * @throws PersistenceException {@inheritDoc}
196: */
197: public ValueObject add(final PersistenceSession session,
198: final ValueObject valueObject) throws PersistenceException {
199: if (logger.isDebugEnabled()) {
200: logger.debug("add(PersistenceSession session = " + session
201: + ", ValueObject valueObject = " + valueObject
202: + ") - start");
203: }
204:
205: HibernateSession hibernateSession = (HibernateSession) session;
206: Session wrappedSession = hibernateSession.getSession();
207: Transaction wrappedTransaction = hibernateSession
208: .getTransaction();
209:
210: try {
211: wrappedSession.save(valueObject);
212: wrappedSession.flush();
213: } catch (Exception e) {
214: logger.error("Exception in add.", e);
215: try {
216: wrappedTransaction.rollback();
217: } catch (HibernateException e2) {
218: logger
219: .error("add(PersistenceSession, ValueObject)",
220: e2);
221: throw new PersistenceException(
222: "Error rolling back a transaction", e2);
223: }
224: throw new PersistenceException(
225: "Error saving a new instance of class '"
226: + valueObject.getClass().getName() + "'", e);
227: }
228:
229: if (logger.isDebugEnabled()) {
230: logger.debug("add - end - return value = " + valueObject);
231: }
232: return valueObject;
233: }
234:
235: /**
236: * {@inheritDoc}
237: *
238: * @param dOClass {@inheritDoc}
239: * @param listener {@inheritDoc}
240: */
241: public void addAddListener(final Class dOClass,
242: final AddPersistenceListener listener) {
243: if (logger.isDebugEnabled()) {
244: logger.debug("addAddListener(Class dOClass = " + dOClass
245: + ", AddPersistenceListener listener = " + listener
246: + ") - start");
247: }
248:
249: HibernateInterceptor.addAddListener(dOClass, listener);
250:
251: if (logger.isDebugEnabled()) {
252: logger
253: .debug("addAddListener(Class, AddPersistenceListener) - end");
254: }
255: }
256:
257: /**
258: * {@inheritDoc}
259: *
260: * @param dOClass {@inheritDoc}
261: * @param listener {@inheritDoc}
262: */
263: public void addAmendListener(final Class dOClass,
264: final AmendPersistenceListener listener) {
265: if (logger.isDebugEnabled()) {
266: logger.debug("addAmendListener(Class dOClass = " + dOClass
267: + ", AmendPersistenceListener listener = "
268: + listener + ") - start");
269: }
270:
271: HibernateInterceptor.addAmendListener(dOClass, listener);
272:
273: if (logger.isDebugEnabled()) {
274: logger.debug("addAmendListener - end");
275: }
276: }
277:
278: /**
279: * {@inheritDoc}
280: *
281: * @param filterParam {@inheritDoc}
282: */
283: public void addFilter(final PersistenceFilter filterParam) {
284: if (logger.isDebugEnabled()) {
285: logger.debug("addFilter(PersistenceFilter filterParam = "
286: + filterParam + ") - start");
287: }
288:
289: filters.add(filterParam);
290:
291: if (logger.isDebugEnabled()) {
292: logger.debug("addFilter(PersistenceFilter) - end");
293: }
294: }
295:
296: /**
297: * {@inheritDoc}
298: *
299: * @param dOClass {@inheritDoc}
300: * @param listener {@inheritDoc}
301: */
302: public void addRemoveListener(final Class dOClass,
303: final RemovePersistenceListener listener) {
304: if (logger.isDebugEnabled()) {
305: logger.debug("addRemoveListener(Class dOClass = " + dOClass
306: + ", RemovePersistenceListener listener = "
307: + listener + ") - start");
308: }
309:
310: HibernateInterceptor.addRemoveListener(dOClass, listener);
311:
312: if (logger.isDebugEnabled()) {
313: logger.debug("addRemoveListener - end");
314: }
315: }
316:
317: /**
318: * {@inheritDoc}
319: *
320: * @param session {@inheritDoc}
321: * @param baseDO {@inheritDoc}
322: * @throws PersistenceException {@inheritDoc}
323: */
324: public void amend(final PersistenceSession session,
325: final ValueObject baseDO) throws PersistenceException {
326: if (logger.isDebugEnabled()) {
327: logger.debug("amend(PersistenceSession session = "
328: + session + ", ValueObject baseDO = " + baseDO
329: + ") - start");
330: }
331:
332: HibernateSession hibernateSession = (HibernateSession) session;
333: Session wrappedSession = hibernateSession.getSession();
334: Transaction wrappedTransaction = hibernateSession
335: .getTransaction();
336:
337: try {
338: wrappedSession.update(baseDO);
339: } catch (Exception e) {
340: logger.error("Exception in amend.", e);
341: try {
342: wrappedTransaction.rollback();
343: } catch (HibernateException e2) {
344: logger.error("amend(PersistenceSession, ValueObject)",
345: e2);
346: throw new PersistenceException(
347: "Error rolling back a transaction", e2);
348: }
349: throw new PersistenceException(
350: "Error updating an instance of class '"
351: + baseDO.getClass().getName()
352: + "' with id '" + baseDO.getIdString()
353: + "'", e);
354: }
355:
356: if (logger.isDebugEnabled()) {
357: logger
358: .debug("amend(PersistenceSession, ValueObject) - end");
359: }
360: }
361:
362: /**
363: * <p>
364: * Internal helper method. This one does all the hard work for retrieving
365: * objects or deleting multiple objects at once.
366: * </p>
367: *
368: * @param session Open, valid persistence session.
369: * @param queryName Name of the query within the query factory.
370: * @param queryArguments Arguments that apply to this query.
371: * @param pageSize If multiple rows should be returned, this indicates the
372: * maximum number to return. Set to <code>null</code> if a single instance
373: * should be returned, or if all instances are needed.
374: * @param pageNumber It is possible to select a subset of all matching rows
375: * by setting this parameter to a number greater than zero. This is used
376: * together with the <code>pageSize</code> argument. Set to
377: * <code>null</code> for a single result, or <code>0</code> to return the
378: * first page of results, or all results (with <code>pageNumber</code>
379: * <code>null</code>.).
380: * @param delete If <code>true</code>, all entries returned by this query
381: * will be deleted.
382: * @return The object which has been retrieved by a query.
383: * @throws PersistenceException If the object cannot be retrieved for any
384: * reason.
385: */
386: private Object execute(final PersistenceSession session,
387: final String queryName, final Object[] queryArguments,
388: final Integer pageSize, final Integer pageNumber,
389: final boolean delete) throws PersistenceException {
390: if (logger.isDebugEnabled()) {
391: logger.debug("execute(PersistenceSession session = "
392: + session + ", String queryName = " + queryName
393: + ", Object[] queryArguments = " + queryArguments
394: + ", Integer pageSize = " + pageSize
395: + ", Integer pageNumber = " + pageNumber
396: + ", boolean delete = " + delete + ") - start");
397: }
398:
399: HibernateSession hibernateSession = (HibernateSession) session;
400: Session wrappedSession = hibernateSession.getSession();
401:
402: Object object = null;
403: HibernateQuery hibernateQuery = (HibernateQuery) queryFactory
404: .generateQuery(queryName, queryArguments);
405: Query q;
406: try {
407: q = wrappedSession.createQuery(hibernateQuery
408: .getQueryString());
409: Map arguments = hibernateQuery.getArguments();
410: Set keys = arguments.keySet();
411: for (Iterator keyIterator = keys.iterator(); keyIterator
412: .hasNext();) {
413: String key = (String) keyIterator.next();
414: Object value = arguments.get(key);
415: q.setParameter(key, value);
416: }
417: } catch (HibernateException e) {
418: logger.error("Exception in execute.", e);
419: throw new PersistenceException("Error retrieving object "
420: + "with query '" + hibernateQuery.getQueryString()
421: + "', " + hibernateQuery.getArguments(), e);
422: }
423:
424: // if delete was specified, or the page number was set, expect multiple
425: // results
426: if (delete || (pageNumber != null)) {
427: List results;
428: try {
429: // if you specified a page size, limit the results
430: if ((pageSize != null) && (pageNumber != null)) {
431: q.setMaxResults(pageSize.intValue());
432: q.setFirstResult(pageSize.intValue()
433: * pageNumber.intValue());
434: }
435: results = q.list();
436: } catch (HibernateException e) {
437: logger
438: .error(
439: "execute - error getting a list of results.",
440: e);
441:
442: throw new PersistenceException(queryName, e);
443: }
444: // if delete was specified, just discard the results
445: if (delete) {
446: results.clear();
447: results = null;
448: } else {
449: results = filter(session, results);
450: object = results;
451: }
452:
453: // otherwise we're trying to retrieve a single instance
454: } else {
455: try {
456: object = q.uniqueResult();
457: if (object instanceof ValueObject) {
458: filter(session, (ValueObject) object);
459: }
460: } catch (HibernateException e) {
461: logger.error("execute - error filtering results", e);
462: throw new PersistenceException(queryName, e);
463: }
464: if (object == null) {
465: throw new FinderException(queryName, queryArguments);
466: }
467: }
468:
469: if (logger.isDebugEnabled()) {
470: logger.debug("execute - end - return value = " + object);
471: }
472: return object;
473: }
474:
475: /**
476: * Calls <code>onView</code> on each of the filters.
477: *
478: * @param session Current, open persistence session for which we are
479: * filtering results. Will be used to check user rights.
480: * @param objects <code>List</code> of <code>valueObject</code> instances
481: * to be filtered for user rights.
482: * @return <code>List</code> of <code>valueObject</code> instances the
483: * current user is allowed to view.
484: */
485: private List filter(final PersistenceSession session,
486: final List objects) {
487: if (logger.isDebugEnabled()) {
488: logger.debug("filter(PersistenceSession session = "
489: + session + ", List objects = " + objects
490: + ") - start");
491: }
492:
493: Iterator filterIterator = filters.iterator();
494: List results = objects;
495: while (filterIterator.hasNext()) {
496: PersistenceFilter filter = (PersistenceFilter) filterIterator
497: .next();
498: results = filter.onView(session, results);
499: }
500:
501: if (logger.isDebugEnabled()) {
502: logger.debug("filter - end - return value = " + results);
503: }
504: return results;
505: }
506:
507: /**
508: * Calls <code>onView</code> on each of the filters.
509: *
510: * @param session Current, open persistence session - used to check user
511: * rights.
512: * @param object The object to be checked against the persistence filters.
513: * @throws RightViolationException If the current user is not allowed to
514: * view this object.
515: */
516: private void filter(final PersistenceSession session,
517: final ValueObject object) throws RightViolationException {
518: if (logger.isDebugEnabled()) {
519: logger.debug("filter(PersistenceSession session = "
520: + session + ", ValueObject object = " + object
521: + ") - start");
522: }
523: if (object == null) {
524: if (logger.isDebugEnabled()) {
525: logger.debug("filter - end - null object encountered.");
526: }
527: return;
528: }
529:
530: Iterator filterIterator = filters.iterator();
531: while (filterIterator.hasNext()) {
532: PersistenceFilter filter = (PersistenceFilter) filterIterator
533: .next();
534: filter.onView(session, object);
535: }
536:
537: if (logger.isDebugEnabled()) {
538: logger
539: .debug("filter(PersistenceSession, ValueObject) - end");
540: }
541: }
542:
543: /**
544: * {@inheritDoc}
545: *
546: * @param session {@inheritDoc}
547: * @param queryName {@inheritDoc}
548: * @param queryArguments {@inheritDoc}
549: * @return {@inheritDoc}
550: * @throws PersistenceException {@inheritDoc}
551: */
552: public List find(final PersistenceSession session,
553: final String queryName, final Object[] queryArguments)
554: throws PersistenceException {
555: if (logger.isDebugEnabled()) {
556: logger.debug("find(PersistenceSession session = " + session
557: + ", String queryName = " + queryName
558: + ", Object[] queryArguments = " + queryArguments
559: + ") - start");
560: }
561:
562: List returnList = find(session, queryName, queryArguments,
563: null, new Integer(0));
564: if (logger.isDebugEnabled()) {
565: logger.debug("find - end - return value = " + returnList);
566: }
567: return returnList;
568: }
569:
570: /**
571: * {@inheritDoc}
572: *
573: * @param session {@inheritDoc}
574: * @param queryName {@inheritDoc}
575: * @param queryArguments {@inheritDoc}
576: * @param pageSize {@inheritDoc}
577: * @param pageNumber {@inheritDoc}
578: * @return {@inheritDoc}
579: * @throws PersistenceException {@inheritDoc}
580: */
581: public List find(final PersistenceSession session,
582: final String queryName, final Object[] queryArguments,
583: final Integer pageSize, final Integer pageNumber)
584: throws PersistenceException {
585: if (logger.isDebugEnabled()) {
586: logger.debug("find(PersistenceSession session = " + session
587: + ", String queryName = " + queryName
588: + ", Object[] queryArguments = " + queryArguments
589: + ", Integer pageSize = " + pageSize
590: + ", Integer pageNumber = " + pageNumber
591: + ") - start");
592: }
593:
594: List returnList = (List) execute(session, queryName,
595: queryArguments, pageSize, pageNumber, false);
596: if (logger.isDebugEnabled()) {
597: logger.debug("find - end - return value = " + returnList);
598: }
599: return returnList;
600: }
601:
602: /**
603: * {@inheritDoc}
604: *
605: * @param session {@inheritDoc}
606: * @param dOClass {@inheritDoc}
607: * @return {@inheritDoc}
608: * @throws PersistenceException {@inheritDoc}
609: */
610: public List findAll(final PersistenceSession session,
611: final Class dOClass) throws PersistenceException {
612: if (logger.isDebugEnabled()) {
613: logger.debug("findAll(PersistenceSession session = "
614: + session + ", Class dOClass = " + dOClass
615: + ") - start");
616: }
617:
618: HibernateSession hibernateSession = (HibernateSession) session;
619: Session wrappedSession = hibernateSession.getSession();
620:
621: List results = null;
622: try {
623: Query query = wrappedSession.createQuery("from "
624: + dOClass.getName());
625: results = query.list();
626: results = filter(session, results);
627: } catch (HibernateException e) {
628: logger.error("Exception in findAll.", e);
629: throw new PersistenceException(
630: "Error retrieving all objects from "
631: + dOClass.getName(), e);
632: }
633:
634: if (logger.isDebugEnabled()) {
635: logger.debug("findAll - end - return value = " + results);
636: }
637: return results;
638: }
639:
640: /**
641: * {@inheritDoc}
642: *
643: * @param session {@inheritDoc}
644: * @param dOClass {@inheritDoc}
645: * @param key {@inheritDoc}
646: * @return {@inheritDoc}
647: * @throws PersistenceException {@inheritDoc}
648: */
649: public ValueObject findByPrimaryKey(
650: final PersistenceSession session, final Class dOClass,
651: final Serializable key) throws PersistenceException {
652: if (logger.isDebugEnabled()) {
653: logger
654: .debug("findByPrimaryKey(PersistenceSession session = "
655: + session
656: + ", Class dOClass = "
657: + dOClass
658: + ", Serializable key = "
659: + key
660: + ") - start");
661: }
662:
663: HibernateSession hibernateSession = (HibernateSession) session;
664: Session wrappedSession = hibernateSession.getSession();
665:
666: if (key == null) {
667: throw new PersistenceException(dOClass.getName()
668: + ": key is null");
669: }
670:
671: ValueObject baseDO = null;
672: try {
673: // TODO: at the moment, everything is indexed as an string - this
674: // probably needs to be abstracted somehow
675: Integer id = StringHandling.integerValue(key.toString());
676: baseDO = (ValueObject) wrappedSession.get(dOClass, id);
677: filter(session, baseDO);
678: } catch (ObjectNotFoundException e) {
679: logger.error("findByPrimaryKey - error calling filter.", e);
680: throw new FinderException(dOClass, key, e);
681: } catch (HibernateException e) {
682: logger.error("findByPrimaryKey - error calling filter.", e);
683: throw new PersistenceException(e);
684: }
685: if (baseDO == null) {
686: throw new FinderException(dOClass, key, null);
687: }
688:
689: if (logger.isDebugEnabled()) {
690: logger.debug("findByPrimaryKey - end - return value = "
691: + baseDO);
692: }
693: return baseDO;
694: }
695:
696: /**
697: * {@inheritDoc}
698: *
699: * @param session {@inheritDoc}
700: * @param queryName {@inheritDoc}
701: * @param queryArguments {@inheritDoc}
702: * @return {@inheritDoc}
703: * @throws PersistenceException {@inheritDoc}
704: */
705: public ValueObject findInstance(final PersistenceSession session,
706: final String queryName, final Object[] queryArguments)
707: throws PersistenceException {
708: if (logger.isDebugEnabled()) {
709: logger.debug("findInstance(PersistenceSession session = "
710: + session + ", String queryName = " + queryName
711: + ", Object[] queryArguments = " + queryArguments
712: + ") - start");
713: }
714:
715: ValueObject returnValueObject = (ValueObject) execute(session,
716: queryName, queryArguments, null, null, false);
717: if (logger.isDebugEnabled()) {
718: logger.debug("findInstance - end - return value = "
719: + returnValueObject);
720: }
721: return returnValueObject;
722: }
723:
724: /**
725: * {@inheritDoc}
726: *
727: * @param session {@inheritDoc}
728: * @param queryName {@inheritDoc}
729: * @param queryArguments {@inheritDoc}
730: * @return {@inheritDoc}
731: * @throws PersistenceException {@inheritDoc}
732: */
733: public Integer findInteger(final PersistenceSession session,
734: final String queryName, final Object[] queryArguments)
735: throws PersistenceException {
736: if (logger.isDebugEnabled()) {
737: logger.debug("findInteger(PersistenceSession session = "
738: + session + ", String queryName = " + queryName
739: + ", Object[] queryArguments = " + queryArguments
740: + ") - start");
741: }
742:
743: Integer returnInteger = (Integer) execute(session, queryName,
744: queryArguments, null, null, false);
745: if (logger.isDebugEnabled()) {
746: logger.debug("findInteger - end - return value = "
747: + returnInteger);
748: }
749: return returnInteger;
750: }
751:
752: /**
753: * {@inheritDoc}
754: *
755: * @param session {@inheritDoc}
756: * @param queryName {@inheritDoc}
757: * @param queryArguments {@inheritDoc}
758: * @return {@inheritDoc}
759: * @throws PersistenceException {@inheritDoc}
760: */
761: public String findString(final PersistenceSession session,
762: final String queryName, final Object[] queryArguments)
763: throws PersistenceException {
764: if (logger.isDebugEnabled()) {
765: logger.debug("findString(PersistenceSession session = "
766: + session + ", String queryName = " + queryName
767: + ", Object[] queryArguments = " + queryArguments
768: + ") - start");
769: }
770:
771: String returnString = (String) execute(session, queryName,
772: queryArguments, null, null, false);
773: if (logger.isDebugEnabled()) {
774: logger.debug("findString - end - return value = "
775: + returnString);
776: }
777: return returnString;
778: }
779:
780: /**
781: * {@inheritDoc}
782: *
783: * @param webSessionParam {@inheritDoc}
784: * @return {@inheritDoc}
785: * @throws PersistenceException {@inheritDoc}
786: */
787: public PersistenceSession openSession(
788: final HttpSession webSessionParam)
789: throws PersistenceException {
790: if (logger.isDebugEnabled()) {
791: logger.debug("openSession(HttpSession webSessionParam = "
792: + webSessionParam + ") - start");
793: }
794:
795: Object securitySession = webSessionParam
796: .getAttribute("securitySession");
797: assert (securitySession != null);
798: PersistenceSession returnPersistenceSession = openSession(securitySession);
799: if (logger.isDebugEnabled()) {
800: logger
801: .debug("openSession(HttpSession) - end - return value = "
802: + returnPersistenceSession);
803: }
804: return returnPersistenceSession;
805: }
806:
807: /**
808: * {@inheritDoc}
809: *
810: * @param systemSession {@inheritDoc}
811: * @return {@inheritDoc}
812: * @throws PersistenceException {@inheritDoc}
813: */
814: public PersistenceSession openSession(final Object systemSession)
815: throws PersistenceException {
816: if (logger.isDebugEnabled()) {
817: logger.debug("openSession(Object systemSession = "
818: + systemSession + ") - start");
819: }
820:
821: try {
822: HibernateInterceptor interceptor = new HibernateInterceptor(
823: this , sessionFactory);
824: Session session = sessionFactory.openSession(interceptor);
825: HibernateSession hibernateSession = new HibernateSession(
826: session, session.beginTransaction(), systemSession);
827: interceptor.setHibernateSession(hibernateSession);
828:
829: if (logger.isDebugEnabled()) {
830: logger
831: .debug("openSession(Object) - end - return value = "
832: + hibernateSession);
833: }
834: return hibernateSession;
835: } catch (HibernateException e) {
836: logger.error("Exception in openSession.", e);
837: throw new PersistenceException(
838: "Error creating a new hibernate session.", e);
839: }
840: }
841:
842: /**
843: * {@inheritDoc}
844: *
845: * @param session {@inheritDoc}
846: * @param dOClass {@inheritDoc}
847: * @param key {@inheritDoc}
848: * @throws PersistenceException {@inheritDoc}
849: */
850: public void remove(final PersistenceSession session,
851: final Class dOClass, final Serializable key)
852: throws PersistenceException {
853: if (logger.isDebugEnabled()) {
854: logger.debug("remove(PersistenceSession session = "
855: + session + ", Class dOClass = " + dOClass
856: + ", Serializable key = " + key + ") - start");
857: }
858: ValueObject valueObject = findByPrimaryKey(session, dOClass,
859: key);
860: assert (valueObject != null);
861: remove(session, valueObject);
862:
863: if (logger.isDebugEnabled()) {
864: logger.debug("remove - end");
865: }
866: }
867:
868: /**
869: * {@inheritDoc}
870: *
871: * @param session {@inheritDoc}
872: * @param valueObject {@inheritDoc}
873: * @throws PersistenceException {@inheritDoc}
874: */
875: public void remove(final PersistenceSession session,
876: final ValueObject valueObject) throws PersistenceException {
877: if (logger.isDebugEnabled()) {
878: logger.debug("remove(PersistenceSession session = "
879: + session + ", ValueObject valueObject = "
880: + valueObject + ") - start");
881: }
882:
883: HibernateSession hibernateSession = (HibernateSession) session;
884: Session wrappedSession = hibernateSession.getSession();
885: Transaction wrappedTransaction = hibernateSession
886: .getTransaction();
887: try {
888: wrappedSession.delete(valueObject);
889: } catch (Exception e) {
890: logger.error("Exception in remove.", e);
891: try {
892: wrappedTransaction.rollback();
893: } catch (HibernateException e2) {
894: logger
895: .error(
896: "remove(PersistenceSession, Class, Serializable)",
897: e2);
898: throw new PersistenceException(
899: "Error rolling back a transaction", e2);
900: }
901: throw new PersistenceException(
902: "Error removing an instance '" + valueObject + "'",
903: e);
904: }
905: if (logger.isDebugEnabled()) {
906: logger
907: .debug("remove(PersistenceSession, ValueObject) - end");
908: }
909: }
910:
911: /**
912: * {@inheritDoc}
913: *
914: * @param session {@inheritDoc}
915: * @param queryName {@inheritDoc}
916: * @param queryArguments {@inheritDoc}
917: * @throws PersistenceException {@inheritDoc}
918: */
919: public void removeAll(final PersistenceSession session,
920: final String queryName, final Object[] queryArguments)
921: throws PersistenceException {
922: if (logger.isDebugEnabled()) {
923: logger.debug("removeAll(PersistenceSession session = "
924: + session + ", String queryName = " + queryName
925: + ", Object[] queryArguments = " + queryArguments
926: + ") - start");
927: }
928:
929: execute(session, queryName, queryArguments, null, null, true);
930:
931: if (logger.isDebugEnabled()) {
932: logger.debug("removeAll - end");
933: }
934: }
935: }
|