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