001    package railo.runtime.orm.hibernate.event;
002    
003    import java.io.Serializable;
004    
005    import org.hibernate.EmptyInterceptor;
006    import org.hibernate.type.Type;
007    
008    import railo.runtime.Component;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.op.Operator;
012    import railo.runtime.orm.ORMUtil;
013    import railo.runtime.orm.hibernate.HibernateCaster;
014    import railo.runtime.type.Collection;
015    import railo.runtime.type.KeyImpl;
016    import railo.runtime.type.Struct;
017    import railo.runtime.type.StructImpl;
018    
019    public class InterceptorImpl extends EmptyInterceptor {
020    
021            private static final long serialVersionUID = 7992972603422833660L;
022    
023            private final AllEventListener listener;
024            private final boolean hasPreInsert;
025            private final boolean hasPreUpdate;
026    
027            public InterceptorImpl(AllEventListener listener) {
028                    this.listener=listener;
029                    if(listener!=null) {
030                            Component cfc = listener.getCFC();
031                            hasPreInsert=EventListener.hasEventType(cfc, EventListener.PRE_INSERT);
032                            hasPreUpdate=EventListener.hasEventType(cfc, EventListener.PRE_UPDATE);
033                    }
034                    else {
035                            hasPreInsert=false;
036                            hasPreUpdate=false;
037                    }
038            }
039    
040            /**
041             * @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
042             */
043            public boolean onSave(Object entity, Serializable id, Object[] state,
044                            String[] propertyNames, Type[] types) {
045                    
046                    return on(entity, id, state, null, propertyNames, types, EventListener.PRE_INSERT, hasPreInsert);
047                    //return super.onSave(entity, id, state, propertyNames, types);
048            }
049    
050            /**
051             * @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
052             */
053            public boolean onFlushDirty(Object entity, Serializable id,
054                            Object[] currentState, Object[] previousState,
055                            String[] propertyNames, Type[] types) {
056                    return on(entity, id, currentState, toStruct(propertyNames, previousState), propertyNames, types, EventListener.PRE_UPDATE, hasPreUpdate);
057                    //return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
058            }
059            
060    
061            private boolean on(Object entity, Serializable id,
062                            Object[] state, Struct data,
063                            String[] propertyNames, Type[] types, Collection.Key eventType, boolean hasMethod) {
064                    
065                    Component cfc = Caster.toComponent(entity,null);
066                    if(cfc!=null && EventListener.hasEventType(cfc, eventType)){
067                            EventListener.invoke(eventType, cfc, data, null);
068                    }
069                    if(hasMethod) 
070                            EventListener.invoke(eventType, listener.getCFC(), data, entity);
071                    
072                    
073                    boolean rtn=false;
074                    String prop;
075                    Object before,current;
076            for(int i = 0; i < propertyNames.length; i++)        {
077                prop = propertyNames[i];
078                before = state[i];
079                current = ORMUtil.getPropertyValue(cfc, prop,null);
080                
081                if(before != current && (current == null || !Operator.equalsEL(before, current, false, true))) {
082                    try {
083                                            state[i] = HibernateCaster.toSQL(null, types[i], current,null);
084                                    } catch (PageException e) {
085                                            state[i] = current;
086                                    }
087                                    rtn = true;
088                }
089            }
090            return rtn;     
091            }
092            
093        
094            
095            
096            
097    
098        private static Struct toStruct(String propertyNames[], Object state[])  {
099            Struct sct = new StructImpl();
100            if(state!=null && propertyNames!=null){
101                    for(int i = 0; i < propertyNames.length; i++) {
102                            sct.setEL(KeyImpl.init(propertyNames[i]), state[i]);
103                    }
104            }
105            return sct;
106        }
107    }