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.accessors;
020
021import java.lang.reflect.Member;
022import java.lang.reflect.Method;
023import java.util.Map;
024
025import lucee.runtime.Component;
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.orm.ORMSession;
029import lucee.runtime.orm.ORMUtil;
030import lucee.runtime.orm.hibernate.CommonUtil;
031import lucee.runtime.orm.hibernate.HibernateCaster;
032import lucee.runtime.orm.hibernate.HibernateORMEngine;
033import lucee.runtime.orm.hibernate.HibernatePageException;
034import lucee.runtime.orm.hibernate.HibernateUtil;
035import lucee.runtime.type.Collection;
036import lucee.runtime.type.Collection.Key;
037
038import org.hibernate.HibernateException;
039import org.hibernate.SessionFactory;
040import org.hibernate.engine.SessionImplementor;
041import org.hibernate.metadata.ClassMetadata;
042import org.hibernate.property.Getter;
043import org.hibernate.type.Type;
044
045public class CFCGetter implements Getter {
046
047        private Key key;
048
049        /**
050         * Constructor of the class
051         * @param key
052         */
053        public CFCGetter(String key){
054                this(CommonUtil.createKey(key));
055        }
056        
057        /**
058         * Constructor of the class
059         * @param engine 
060         * @param key
061         */
062        public CFCGetter( Collection.Key key){
063                this.key=key;
064        }
065        
066        @Override
067        public Object get(Object trg) throws HibernateException {
068                try {
069                        // MUST cache this, perhaps when building xml
070                        PageContext pc = CommonUtil.pc();
071                        ORMSession session = ORMUtil.getSession(pc);
072                        Component cfc = CommonUtil.toComponent(trg);
073                        String dsn = ORMUtil.getDataSourceName(pc, cfc);
074                        String name = HibernateCaster.getEntityName(cfc);
075                        SessionFactory sf=(SessionFactory) session.getRawSessionFactory(dsn);
076                        ClassMetadata metaData = sf.getClassMetadata(name);
077                        Type type = HibernateUtil.getPropertyType(metaData, key.getString());
078
079                        Object rtn = cfc.getComponentScope().get(key,null);
080                        return HibernateCaster.toSQL(type, rtn,null);
081                } 
082                catch (PageException pe) {
083                        throw new HibernatePageException(pe);
084                }
085        }
086        
087
088        public HibernateORMEngine getHibernateORMEngine(){
089                try {
090                        // TODO better impl
091                        return HibernateUtil.getORMEngine(CommonUtil.pc());
092                } 
093                catch (PageException e) {}
094                        
095                return null;
096        }
097        
098
099        @Override
100        public Object getForInsert(Object trg, Map arg1, SessionImplementor arg2)throws HibernateException {
101                return get(trg);// MUST better solution? this is from MapGetter
102        }
103
104        @Override
105        public Member getMember() {
106                return null;
107        }
108
109        @Override
110        public Method getMethod() {
111                return null;
112        }
113
114        public String getMethodName() {
115                return null;// MUST macht es sinn den namen zurueck zu geben?
116        }
117
118        public Class getReturnType() {
119                return Object.class;// MUST more concrete?
120        }
121
122}