001    package railo.runtime.orm.hibernate.tuplizer;
002    
003    import java.io.Serializable;
004    import java.util.HashMap;
005    
006    import org.hibernate.EntityMode;
007    import org.hibernate.EntityNameResolver;
008    import org.hibernate.HibernateException;
009    import org.hibernate.engine.SessionFactoryImplementor;
010    import org.hibernate.engine.SessionImplementor;
011    import org.hibernate.mapping.PersistentClass;
012    import org.hibernate.mapping.Property;
013    import org.hibernate.property.Getter;
014    import org.hibernate.property.PropertyAccessor;
015    import org.hibernate.property.Setter;
016    import org.hibernate.proxy.ProxyFactory;
017    import org.hibernate.tuple.Instantiator;
018    import org.hibernate.tuple.entity.AbstractEntityTuplizer;
019    import org.hibernate.tuple.entity.EntityMetamodel;
020    
021    import railo.runtime.ComponentPro;
022    import railo.runtime.ComponentScope;
023    import railo.runtime.op.Caster;
024    import railo.runtime.orm.hibernate.tuplizer.accessors.CFCAccessor;
025    import railo.runtime.orm.hibernate.tuplizer.proxy.CFCProxyFactory;
026    import railo.runtime.type.KeyImpl;
027    import railo.runtime.type.cfc.ComponentAccess;
028    import railo.runtime.type.util.ComponentUtil;
029    
030    public class AbstractEntityTuplizerImpl extends AbstractEntityTuplizer {
031    
032            private static CFCAccessor accessor=new CFCAccessor();
033            
034            public AbstractEntityTuplizerImpl(EntityMetamodel entityMetamodel, PersistentClass persistentClass) {
035                    super(entityMetamodel, persistentClass);
036            }
037    
038            /**
039             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#getIdentifier(java.lang.Object, org.hibernate.engine.SessionImplementor)
040             */
041            public Serializable getIdentifier(Object entity, SessionImplementor arg1) {
042                    return toIdentifier(super.getIdentifier(entity, arg1));
043            }
044            
045            /**
046             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#getIdentifier(java.lang.Object)
047             */
048            public Serializable getIdentifier(Object entity) throws HibernateException {
049                    return toIdentifier(super.getIdentifier(entity));
050            }
051    
052            private Serializable toIdentifier(Serializable id) {
053                    if(id instanceof ComponentPro) {
054                            HashMap<String, String> map = new HashMap<String, String>();
055                            ComponentPro cfc=(ComponentPro) id;
056                            ComponentScope scope = cfc.getComponentScope();
057                            railo.runtime.component.Property[] props = ComponentUtil.getIDProperties(cfc, true);
058                            String name,value;
059                            for(int i=0;i<props.length;i++){
060                                    name=props[i].getName();
061                                    value=Caster.toString(scope.get(KeyImpl.init(name),null),null);
062                                    map.put(name, value);
063                            }
064                            return map;
065                    }
066                    return id;
067            }
068    
069    
070            /**
071             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#buildInstantiator(org.hibernate.mapping.PersistentClass)
072             */
073            protected Instantiator buildInstantiator(PersistentClass persistentClass) {
074                    return new CFCInstantiator(persistentClass);
075            }
076            
077            /**
078             * return accessors 
079             * @param mappedProperty
080             * @return
081             */
082            private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
083                    if ( mappedProperty.isBackRef() ) {
084                            PropertyAccessor ac = mappedProperty.getPropertyAccessor(null);
085                            if(ac!=null) return ac;
086                    }
087                    return accessor;
088            }
089    
090            
091            /**
092             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#buildPropertyGetter(org.hibernate.mapping.Property, org.hibernate.mapping.PersistentClass)
093             */
094            protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity) {
095                    return buildPropertyAccessor(mappedProperty).getGetter( null, mappedProperty.getName() );
096            }
097    
098            
099            /**
100             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#buildPropertySetter(org.hibernate.mapping.Property, org.hibernate.mapping.PersistentClass)
101             */
102            protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
103                    return buildPropertyAccessor(mappedProperty).getSetter( null, mappedProperty.getName() );
104            }
105            
106            /**
107             * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#buildProxyFactory(org.hibernate.mapping.PersistentClass, org.hibernate.property.Getter, org.hibernate.property.Setter)
108             */
109            protected ProxyFactory buildProxyFactory(PersistentClass pc, Getter arg1,Setter arg2) {
110                    CFCProxyFactory pf = new CFCProxyFactory();
111                    pf.postInstantiate(pc);
112                    
113                    return pf;
114            }
115    
116            /**
117             * @see org.hibernate.tuple.entity.EntityTuplizer#determineConcreteSubclassEntityName(java.lang.Object, org.hibernate.engine.SessionFactoryImplementor)
118             */
119            public String determineConcreteSubclassEntityName(Object entityInstance, SessionFactoryImplementor factory) {
120                    return CFCEntityNameResolver.INSTANCE.resolveEntityName(entityInstance);
121            }
122    
123            /**
124             * @see org.hibernate.tuple.entity.EntityTuplizer#getEntityNameResolvers()
125             */
126            public EntityNameResolver[] getEntityNameResolvers() {
127                    return new EntityNameResolver[] { CFCEntityNameResolver.INSTANCE };
128            }
129    
130            /**
131             * @see org.hibernate.tuple.entity.EntityTuplizer#getConcreteProxyClass()
132             */
133            public Class getConcreteProxyClass() {
134                    return ComponentAccess.class;// ????
135            }
136    
137            /**
138             * @see org.hibernate.tuple.Tuplizer#getMappedClass()
139             */
140            public Class getMappedClass() {
141                    return ComponentAccess.class; // ????
142            }
143    
144            public EntityMode getEntityMode() {
145                    return EntityMode.MAP;
146            }
147    
148            /**
149             * @see org.hibernate.tuple.entity.EntityTuplizer#isInstrumented()
150             */
151            public boolean isInstrumented() {
152                    return false;
153            }
154    
155    }