001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.orm.hibernate.tuplizer.proxy;
020
021import java.io.Serializable;
022import java.lang.reflect.Method;
023import java.util.Set;
024
025import lucee.commons.lang.ExceptionUtil;
026
027import org.hibernate.HibernateException;
028import org.hibernate.engine.SessionImplementor;
029import org.hibernate.mapping.PersistentClass;
030import org.hibernate.proxy.HibernateProxy;
031import org.hibernate.proxy.ProxyFactory;
032import org.hibernate.type.AbstractComponentType;
033
034
035public class CFCHibernateProxyFactory implements ProxyFactory {
036        private String entityName;
037        private String nodeName;
038
039        public void postInstantiate(
040                final String entityName, 
041                final Class persistentClass,
042                final Set interfaces, 
043                final Method getIdentifierMethod,
044                final Method setIdentifierMethod,
045                AbstractComponentType componentIdType) throws HibernateException {
046                int index=entityName.indexOf('.');
047                this.nodeName = entityName;
048                this.entityName = entityName.substring(index+1);
049        }
050
051        public void postInstantiate(PersistentClass pc) {
052                this.nodeName =pc.getNodeName();
053                this.entityName =pc.getEntityName();
054        }
055
056        public HibernateProxy getProxy(final Serializable id,  final SessionImplementor session) {
057                try {
058                        return new CFCHibernateProxy(new CFCLazyInitializer(entityName, id, session));
059                }
060                catch(Throwable t){
061                        ExceptionUtil.rethrowIfNecessary(t);
062                        return new CFCHibernateProxy(new CFCLazyInitializer(nodeName, id, session));
063                }
064        }
065}