001    package railo.runtime.orm.hibernate.tuplizer.accessors;
002    
003    import java.lang.reflect.Member;
004    import java.lang.reflect.Method;
005    import java.util.Map;
006    
007    import org.hibernate.HibernateException;
008    import org.hibernate.engine.SessionImplementor;
009    import org.hibernate.metadata.ClassMetadata;
010    import org.hibernate.property.Getter;
011    import org.hibernate.type.Type;
012    
013    import railo.runtime.Component;
014    import railo.runtime.PageContext;
015    import railo.runtime.config.ConfigImpl;
016    import railo.runtime.engine.ThreadLocalPageContext;
017    import railo.runtime.exp.PageException;
018    import railo.runtime.op.Caster;
019    import railo.runtime.orm.hibernate.HibernateCaster;
020    import railo.runtime.orm.hibernate.HibernateORMEngine;
021    import railo.runtime.orm.hibernate.HibernateRuntimeException;
022    import railo.runtime.orm.hibernate.HibernateUtil;
023    import railo.runtime.type.Collection;
024    import railo.runtime.type.Collection.Key;
025    import railo.runtime.type.KeyImpl;
026    import railo.runtime.type.util.ComponentUtil;
027    
028    public class CFCGetter implements Getter {
029    
030            private Key key;
031    
032            /**
033             * Constructor of the class
034             * @param key
035             */
036            public CFCGetter(String key){
037                    this(KeyImpl.getInstance(key));
038            }
039            
040            /**
041             * Constructor of the class
042             * @param engine 
043             * @param key
044             */
045            public CFCGetter( Collection.Key key){
046                    this.key=key;
047            }
048            
049            /**
050             * @see org.hibernate.property.Getter#get(java.lang.Object)
051             */
052            public Object get(Object trg) throws HibernateException {
053                    try {
054                            // MUST cache this, perhaps when building xml
055                            HibernateORMEngine engine = getHibernateORMEngine();
056                            PageContext pc = ThreadLocalPageContext.get();
057                            Component cfc = Caster.toComponent(trg);
058                            String name = HibernateCaster.getEntityName(cfc);
059                            ClassMetadata metaData = engine.getSessionFactory(pc).getClassMetadata(name);
060                            Type type = HibernateUtil.getPropertyType(metaData, key.getString());
061                            
062                            Object rtn = ComponentUtil.toComponentPro(cfc).getComponentScope().get(key,null);
063                            return HibernateCaster.toSQL(engine, type, rtn,null);
064                    } 
065                    catch (PageException e) {
066                            throw new HibernateRuntimeException(e);
067                    }
068            }
069            
070    
071            public HibernateORMEngine getHibernateORMEngine(){
072                    try {
073                            // TODO better impl
074                            PageContext pc = ThreadLocalPageContext.get();
075                            ConfigImpl config=(ConfigImpl) pc.getConfig();
076                            return (HibernateORMEngine) config.getORMEngine(pc);
077                    } 
078                    catch (PageException e) {}
079                            
080                    return null;
081            }
082            
083    
084            /**
085             * @see org.hibernate.property.Getter#getForInsert(java.lang.Object, java.util.Map, org.hibernate.engine.SessionImplementor)
086             */
087            public Object getForInsert(Object trg, Map arg1, SessionImplementor arg2)throws HibernateException {
088                    return get(trg);// MUST better solution? this is from MapGetter
089            }
090    
091            /**
092             * @see org.hibernate.property.Getter#getMember()
093             */
094            public Member getMember() {
095                    return null;
096            }
097    
098            /**
099             * @see org.hibernate.property.Getter#getMethod()
100             */
101            public Method getMethod() {
102                    return null;
103            }
104    
105            public String getMethodName() {
106                    return null;// MUST macht es sinn den namen zur�ck zu geben?
107            }
108    
109            public Class getReturnType() {
110                    return Object.class;// MUST more concrete?
111            }
112    
113    }