    View Javadoc
    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit.dao;
    7
    8   import com.infodesire.infobit.security.SecurityProvider;
    9   import com.infodesire.infobit.hibernate.SessionProvider;
    10  import com.infodesire.infobit.InfobitException;
    11
    12  import net.sf.hibernate.Session;
    13  import net.sf.hibernate.Query;
    14  import net.sf.hibernate.HibernateException;
    15
    16  import org.apache.commons.logging.Log;
    17  import org.apache.commons.logging.LogFactory;
    18
    19  import java.io.Serializable;
    20
    21  import java.util.Map;
    22  import java.util.List;
    23  import java.util.Iterator;
    24  /***
    25   * implements base DAO capabilities. - low level persistence and security
    26   * checking derived classes shall do transaction management
    27   *
    28   * @author    konstantin
    29   * @created   August 8, 2003
    30   * @version   $Revision: 1.7 $
    31   */
    32  public class BaseDAO {
    33      private static Log _log = LogFactory.getLog(BaseDAO.class);
    34
    35      private SecurityProvider _securityProvider;
    36
    37      private SessionProvider _sessionProvider;
    38
    39
    40      /***
    41       * Constructor for the InfobitDAO object
    42       *
    43       * @param sec   Description of Parameter
    44       * @param sess  Description of Parameter
    45       */
    46      public BaseDAO(SecurityProvider sec, SessionProvider sess) {
    47          setSessionProvider(sess);
    48          setSecurityProvider(sec);
    49      }
    50
    51
    52      /***
    53       * Gets the SecurityProvider attribute of the InfobitDAO object
    54       *
    55       * @return   The SecurityProvider value
    56       */
    57      public SecurityProvider getSecurityProvider() {
    58          return _securityProvider;
    59      }
    60
    61
    62      /***
    63       * Gets the SessionProvider attribute of the InfobitDAO object
    64       *
    65       * @return   The SessionProvider value
    66       */
    67      public SessionProvider getSessionProvider() {
    68          return _sessionProvider;
    69      }
    70
    71
    72      /***
    73       * Sets the SecurityProvider attribute of the InfobitDAO object
    74       *
    75       * @param securityProvider  The new SecurityProvider value
    76       */
    77      public void setSecurityProvider(SecurityProvider securityProvider) {
    78          _securityProvider = securityProvider;
    79      }
    80
    81
    82      /***
    83       * Sets the SessionProvider attribute of the InfobitDAO object
    84       *
    85       * @param sessionProvider  The new SessionProvider value
    86       */
    87      public void setSessionProvider(SessionProvider sessionProvider) {
    88          _sessionProvider = sessionProvider;
    89      }
    90
    91
    92      /***
    93       * perform query somebody already rigged up for us
    94       *
    95       * @param query          Description of Parameter
    96       * @return               Description of the Returned Value
    97       * @exception Exception  Description of Exception
    98       */
    99      public List performQuery(String query) throws Exception {
    100         List retval;
    101         Session sess = getSessionProvider().getSession();
    102         try {
    103             retval = sess.find(query);
    104         } catch (Exception ex) {
    105             _log.error("exception while performing query  ,  session killed", ex);
    106             getSessionProvider().rollback();
    107             getSessionProvider().returnCloseSession(sess);
    108             throw ex;
    109         }
    110         getSessionProvider().returnSession(sess);
    111         return retval;
    112     }
    113
    114
    115     /***
    116      * perform hibernate query.
    117      *
    118      * @param name           name of the query
    119      * @param parameters     map of parameters
    120      * @return               result list
    121      * @exception Exception  thrown by hibernate
    122      */
    123     public List performQuery(String name, Map parameters) throws Exception {
    124         Session sess = null;
    125         List retval = null;
    126         Query query = null;
    127         String param = null;
    128
    129         sess = getSessionProvider().getSession();
    130         try {
    131             //tr = sess.beginTransaction();
    132             query = sess.getNamedQuery(name);
    133             for (Iterator iter = parameters.keySet().iterator(); iter.hasNext(); ) {
    134                 param = (String) iter.next();
    135                 query.setParameter(param, parameters.get(param));
    136             }
    137             retval = query.list();
    138         } catch (Exception ex) {
    139             _log.error("exception while performing query  ,  session killed", ex);
    140             getSessionProvider().rollback();
    141             getSessionProvider().returnCloseSession(sess);
    142             throw ex;
    143         }
    144         getSessionProvider().returnSession(sess);
    145         return retval;
    146     }
    147
    148
    149     /***
    150      * load persistent entity
    151      *
    152      * @param clazz          Description of Parameter
    153      * @param id             Description of Parameter
    154      * @return               Description of the Returned Value
    155      * @exception Exception  Description of Exception
    156      */
    157     public Object doLoad(Class clazz, Serializable id) throws Exception {
    158         Session sess = getSessionProvider().getSession();
    159         Object retval = null;
    160         try {
    161             retval = sess.load(clazz, id);
    162         } catch (Exception ex) {
    163             _log.error("exception while updating object , transaction was rolled back. session killed", ex);
    164             getSessionProvider().returnCloseSession(sess);
    165             throw ex;
    166         }
    167
    168         getSessionProvider().returnSession(sess);
    169         return retval;
    170     }
    171
    172
    173     /***
    174      * perform transactionally safe object creation
    175      *
    176      * @param obj            Description of Parameter
    177      * @return               Description of the Returned Value
    178      * @exception Exception  Description of Exception
    179      */
    180     Serializable doCreate(Object obj) throws Exception {
    181         Session sess = null;
    182         Object retval;
    183         Serializable id;
    184         sess = getSessionProvider().getSession();
    185         try {
    186             id = sess.save(obj);
    187
    188         } catch (Exception ex) {
    189             _log.error("exception while creating object , transaction was rolled back. session killed", ex);
    190             getSessionProvider().rollback();
    191             getSessionProvider().returnCloseSession(sess);
    192             throw ex;
    193         }
    194         //sess.evict(obj);
    195         getSessionProvider().returnSession(sess);
    196         return id;
    197     }
    198
    199
    200     /***
    201      * update state of persistent object
    202      *
    203      * @param obj            object to be updated
    204      * @exception Exception  thrown on behaf of hibernate
    205      */
    206     void doUpdate(Object obj) throws Exception {
    207         Session sess = null;
    208         Object retval;
    209         sess = getSessionProvider().getSession();
    210         try {
    211             sess.update(obj);
    212         } catch (Exception ex) {
    213             _log.error("exception while updating object , transaction was rolled back. session killed", ex);
    214             getSessionProvider().rollback();
    215             getSessionProvider().returnCloseSession(sess);
    216             throw ex;
    217         }
    218
    219         getSessionProvider().returnSession(sess);
    220     }
    221
    222
    223     /***
    224      * delete object from datastore
    225      *
    226      * @param obj            object to be removed
    227      * @exception Exception  thrown on behalf of hibernate
    228      */
    229     void doRemove(Object obj) throws Exception {
    230         Session sess = getSessionProvider().getSession();
    231         try {
    232             if (!sess.contains(obj)) {
    233                 Object o = sess.load(obj.getClass(), ((BaseEntityImpl) obj).getId());
    234                 sess.delete(o);
    235             }
    236             else {
    237                 sess.delete(obj);
    238             }
    239         } catch (Exception ex) {
    240             _log.error("exception while updating object , transaction was rolled back. session killed", ex);
    241             getSessionProvider().rollback();
    242             getSessionProvider().returnCloseSession(sess);
    243             throw ex;
    244         }
    245
    246         getSessionProvider().returnSession(sess);
    247
    248     }
    249 }

This page was automatically generated by Maven