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;
020
021import java.io.Serializable;
022import java.util.HashSet;
023import java.util.Iterator;
024import java.util.Set;
025
026import lucee.runtime.Component;
027import lucee.runtime.PageContext;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.orm.hibernate.CommonUtil;
030import lucee.runtime.orm.hibernate.HibernateCaster;
031import lucee.runtime.orm.hibernate.HibernateORMEngine;
032import lucee.runtime.orm.hibernate.HibernateORMSession;
033import lucee.runtime.orm.hibernate.HibernatePageException;
034import lucee.runtime.orm.hibernate.HibernateUtil;
035
036import org.hibernate.mapping.PersistentClass;
037import org.hibernate.tuple.Instantiator;
038
039public class CFCInstantiator implements Instantiator {
040        
041        private String entityName;
042        private Set<String> isInstanceEntityNames = new HashSet<String>();
043        
044        public CFCInstantiator() {
045                this.entityName = null;
046        }
047
048        /**
049         * Constructor of the class
050         * @param mappingInfo
051         */
052        public CFCInstantiator(PersistentClass mappingInfo) {
053                this.entityName = mappingInfo.getEntityName();
054                isInstanceEntityNames.add( entityName );
055                if ( mappingInfo.hasSubclasses() ) {
056                        Iterator<PersistentClass> itr = mappingInfo.getSubclassClosureIterator();
057                        while ( itr.hasNext() ) {
058                                final PersistentClass subclassInfo = itr.next();
059                                isInstanceEntityNames.add( subclassInfo.getEntityName() );
060                        }
061                }
062        }
063
064        @Override
065        public final Object instantiate(Serializable id) {
066                return instantiate();
067        }
068
069        @Override
070        public final Object instantiate() {
071                try {
072                        PageContext pc = CommonUtil.pc();
073                        HibernateORMSession session=HibernateUtil.getORMSession(pc,true);
074                        HibernateORMEngine engine=(HibernateORMEngine) session.getEngine();
075                        Component c = engine.create(pc, session, entityName, true);
076                        CommonUtil.setEntity(c,true);
077                        return c;//new CFCProxy(c);
078                } 
079                catch (PageException pe) {
080                        throw new HibernatePageException(pe);
081                }
082        }
083
084        @Override
085        public final boolean isInstance(Object object) {
086                Component cfc = CommonUtil.toComponent(object,null);
087                if(cfc==null) return false;
088                return isInstanceEntityNames.contains( HibernateCaster.getEntityName(cfc));
089        }
090}