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