001    package railo.runtime.orm.hibernate.event;
002    
003    import org.hibernate.event.PreUpdateEvent;
004    
005    import railo.runtime.Component;
006    import railo.runtime.ComponentPro;
007    import railo.runtime.PageContext;
008    import railo.runtime.component.Property;
009    import railo.runtime.engine.ThreadLocalPageContext;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.exp.PageRuntimeException;
012    import railo.runtime.op.Caster;
013    import railo.runtime.orm.hibernate.HibernateUtil;
014    import railo.runtime.type.Collection;
015    import railo.runtime.type.Collection.Key;
016    import railo.runtime.type.KeyImpl;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.StructImpl;
019    import railo.runtime.type.UDF;
020    import railo.runtime.type.util.ComponentUtil;
021    
022    public abstract class EventListener {
023    
024            private static final long serialVersionUID = -4842481789634140033L;
025            
026            public static final Collection.Key POST_INSERT=KeyImpl.intern("postInsert");
027            public static final Collection.Key POST_UPDATE=KeyImpl.intern("postUpdate");
028            public static final Collection.Key PRE_DELETE=KeyImpl.intern("preDelete");
029            public static final Collection.Key POST_DELETE=KeyImpl.intern("postDelete");
030            public static final Collection.Key PRE_LOAD=KeyImpl.intern("preLoad");
031            public static final Collection.Key POST_LOAD=KeyImpl.intern("postLoad");
032            public static final Collection.Key PRE_UPDATE=KeyImpl.intern("preUpdate");
033            public static final Collection.Key PRE_INSERT=KeyImpl.intern("preInsert");
034            
035    
036            
037        protected ComponentPro component;
038            private boolean allEvents;
039            private Key eventType;
040        
041            public EventListener(Component component, Key eventType, boolean allEvents) {
042                   this.component=ComponentUtil.toComponentPro(component,null); 
043                   this.allEvents=allEvents; 
044                   this.eventType=eventType;
045        }
046            
047            protected boolean preUpdate(PreUpdateEvent event) {
048                    Struct oldData=new StructImpl();
049                    Property[] properties = HibernateUtil.getProperties(component,HibernateUtil.FIELDTYPE_COLUMN,null);
050                    Object[] data = event.getOldState();
051                    
052                    if(data!=null && properties!=null && data.length==properties.length) {
053                            for(int i=0;i<data.length;i++){
054                                    oldData.setEL(KeyImpl.getInstance(properties[i].getName()), data[i]);
055                            }
056                    }
057                    invoke(PRE_UPDATE, event.getEntity(),oldData);
058                    return false;
059            }
060    
061        
062        public Component getCFC() {
063                    return component;
064            }
065        
066    
067        protected void invoke(Collection.Key name, Object obj) {
068            invoke(name, obj,null);
069        }
070        protected void invoke(Collection.Key name, Object obj, Struct data) {
071            if(eventType!=null && !eventType.equals(name)) return;
072            //print.e(name);
073            ComponentPro caller = ComponentUtil.toComponentPro(Caster.toComponent(obj,null),null);
074            ComponentPro c=allEvents?component:caller;
075            if(c==null) return;
076            
077            if(!allEvents &&!caller.getPageSource().equals(component.getPageSource())) return;
078                    invoke(name, c, data, allEvents?obj:null);
079            
080            }
081        
082        public static void invoke(Key name, Component cfc, Struct data, Object arg) {
083            if(cfc==null) return;
084            
085                    try {
086                            PageContext pc = ThreadLocalPageContext.get();
087                            Object[] args;
088                            if(data==null) {
089                                    args=arg!=null?new Object[]{arg}:new Object[]{};
090                            }
091                            else {
092                                    args=arg!=null?new Object[]{arg,data}:new Object[]{data};
093                            }
094                            cfc.call(pc, name, args);
095                    }
096                    catch (PageException pe) {
097                            throw new PageRuntimeException(pe);
098                    }
099            }
100        
101            public static boolean hasEventType(Component cfc, Collection.Key eventType) {
102                    return cfc.get(eventType,null) instanceof UDF;
103            }
104    }