001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.seam.lazy;
022    
023    import java.io.Serializable;
024    
025    import org.granite.context.GraniteContext;
026    import org.granite.messaging.amf.io.util.ClassGetter;
027    import org.granite.tide.TidePersistenceManager;
028    import org.hibernate.Session;
029    import org.jboss.seam.Entity;
030    import org.jboss.seam.util.Reflections;
031    
032    /**
033     * Manager responsible for the maintaining a reference for the HibernateContext. 
034     * @author CIngram
035     */
036    public class HibernateContextManager implements TidePersistenceManager {
037            
038            private Session session = null;
039            
040            public HibernateContextManager() {
041                    
042            }
043            
044            public HibernateContextManager(Session session) {
045                    this.session = session;
046            }
047            
048            /**
049             * Attach the passed in entity with the HibernateSession.
050             * @param entity
051             * @return the attached entity object
052             */
053            public Object attachEntity(Object entity, String[] propertyNames) {
054                    Object attachedEntity = null;
055            ClassGetter getter = GraniteContext.getCurrentInstance().getGraniteConfig().getClassGetter();
056            
057                    try { 
058                        attachedEntity = fetchEntity(entity, propertyNames);
059                            
060                            if (propertyNames != null) {
061                        for (int i = 0; i < propertyNames.length; i++) {
062                            Object initializedObj = Reflections.getGetterMethod(attachedEntity.getClass(), propertyNames[i]).invoke(attachedEntity);
063                            
064                            getter.initialize(entity, propertyNames[i], initializedObj);
065                                }
066                            }
067                    } 
068                    catch(Exception e) {
069                            throw new RuntimeException("Unable to attach entity and init collection", e);
070                    }
071                    disconnectSession();
072                    
073                    return attachedEntity;
074            }
075            
076            /**
077             * attaches the entity to the HibernateSession.
078             * @return the attached entity
079             */
080            public Object fetchEntity(Object entity, String[] fetch) {
081                Serializable id = (Serializable)Entity.forClass(entity.getClass()).getIdentifier(entity);
082                if (id == null)
083                    return null;
084                return session.get(entity.getClass(), id);
085            }
086        
087            /**
088             * disconnects from the Hibernate Session.
089             */
090            protected void disconnectSession() {
091                    session.disconnect();
092            }
093    }