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.hibernate;
022    
023    import java.io.Serializable;
024    
025    import javax.persistence.Entity;
026    
027    import org.granite.logging.Logger;
028    import org.granite.messaging.amf.io.util.DefaultClassGetter;
029    import org.granite.util.TypeUtil;
030    import org.hibernate.Hibernate;
031    import org.hibernate.proxy.HibernateProxy;
032    import org.hibernate.proxy.LazyInitializer;
033    
034    /**
035     * @author Franck WOLFF
036     */
037    public class HibernateClassGetter extends DefaultClassGetter {
038    
039        private final static Logger log = Logger.getLogger(HibernateClassGetter.class);
040    
041        @Override
042        public Class<?> getClass(Object o) {
043    
044            if (o instanceof HibernateProxy) {
045                HibernateProxy proxy = (HibernateProxy)o;
046                LazyInitializer initializer = proxy.getHibernateLazyInitializer();
047                
048                String className = (
049                    initializer.isUninitialized() ?
050                    initializer.getEntityName() :
051                    initializer.getImplementation().getClass().getName()
052                ); 
053                
054                if (className != null && className.length() > 0) {
055                    try {
056                        return TypeUtil.forName(className);
057                    } catch (Exception e) {
058                        log.warn(e, "Could not get class with initializer: %s for: %s", initializer.getClass().getName(), className);
059                    }
060                }
061                // fallback...
062                return initializer.getPersistentClass();
063            }
064    
065            return super.getClass(o);
066        }
067        
068        @Override
069        public boolean isEntity(Object o) {
070            return o.getClass().isAnnotationPresent(Entity.class);    
071        }
072    
073        @Override
074        public Serializable getIdentifier(Object o) {
075            if (o instanceof HibernateProxy)
076                    return ((HibernateProxy)o).getHibernateLazyInitializer().getIdentifier();
077            return null;
078        }
079        
080        @Override
081        public boolean isInitialized(Object owner, String propertyName, Object propertyValue) {
082            return Hibernate.isInitialized(propertyValue);
083        }
084        
085        @Override
086        public void initialize(Object owner, String propertyName, Object propertyValue) {
087            Hibernate.initialize(propertyValue);
088        }
089    }