001    /*
002     * Copyright (c) 2005-2012 Vincent Vandenschrick. All rights reserved.
003     *
004     *  This file is part of the Jspresso framework.
005     *
006     *  Jspresso is free software: you can redistribute it and/or modify
007     *  it under the terms of the GNU Lesser General Public License as published by
008     *  the Free Software Foundation, either version 3 of the License, or
009     *  (at your option) any later version.
010     *
011     *  Jspresso is distributed in the hope that it will be useful,
012     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     *  GNU Lesser General Public License for more details.
015     *
016     *  You should have received a copy of the GNU Lesser General Public License
017     *  along with Jspresso.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    package org.jspresso.framework.application.backend.action.persistence.hibernate;
020    
021    import java.lang.reflect.InvocationTargetException;
022    import java.util.Map;
023    
024    import org.hibernate.Session;
025    import org.jspresso.framework.application.backend.action.BackendAction;
026    import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
027    import org.jspresso.framework.model.component.IComponent;
028    import org.jspresso.framework.model.entity.IEntity;
029    import org.springframework.orm.hibernate3.HibernateTemplate;
030    
031    /**
032     * This the root abstract class of all hibernate related persistence actions. It
033     * refines the return values of some protected methods (like the controller that
034     * is refined to <code>HibernateBackendController</code>) and adds some new ones
035     * (like the access to the controller's configured &quot;<i>Spring Hibernate
036     * template</i>&quot;. It provides also protected utility methods for various
037     * standard persistence operations on Jspresso managed entities.
038     * 
039     * @version $LastChangedRevision: 5685 $
040     * @author Vincent Vandenschrick
041     */
042    public abstract class AbstractHibernateAction extends BackendAction {
043    
044      /**
045       * Performs necessary cleanings when an entity or component is deleted.
046       * 
047       * @param component
048       *          the deleted entity or component.
049       * @param context
050       *          The action context.
051       * @param dryRun
052       *          set to true to simulate before actually doing it.
053       * @throws IllegalAccessException
054       *           whenever this kind of exception occurs.
055       * @throws InvocationTargetException
056       *           whenever this kind of exception occurs.
057       * @throws NoSuchMethodException
058       *           whenever this kind of exception occurs.
059       */
060      protected void cleanRelationshipsOnDeletion(IComponent component,
061          Map<String, Object> context, boolean dryRun)
062          throws IllegalAccessException, InvocationTargetException,
063          NoSuchMethodException {
064        getController(context).cleanRelationshipsOnDeletion(component, dryRun);
065      }
066    
067      /**
068       * {@inheritDoc}
069       */
070      @Override
071      protected HibernateBackendController getController(Map<String, Object> context) {
072        return (HibernateBackendController) super.getController(context);
073      }
074    
075      /**
076       * Gets the hibernateTemplate.
077       * 
078       * @param context
079       *          the action context.
080       * @return the hibernateTemplate.
081       * @deprecated use {@link #getHibernateSession(Map)} instead.
082       */
083      @Deprecated
084      protected HibernateTemplate getHibernateTemplate(Map<String, Object> context) {
085        return getController(context).getHibernateTemplate();
086      }
087    
088      /**
089       * Gets the hibernateTemplate.
090       * 
091       * @param context
092       *          the action context.
093       * @return the hibernateTemplate.
094       */
095      protected Session getHibernateSession(Map<String, Object> context) {
096        return getController(context).getHibernateSession();
097      }
098    
099      /**
100       * Reloads an entity in hibernate.
101       * 
102       * @param entity
103       *          the entity to reload.
104       * @param context
105       *          the action context.
106       */
107      protected void reloadEntity(IEntity entity, Map<String, Object> context) {
108        getController(context).reload(entity);
109      }
110    }