001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.orm.hibernate.event;
020
021import lucee.runtime.Component;
022import lucee.runtime.PageContext;
023import lucee.runtime.component.Property;
024import lucee.runtime.engine.ThreadLocalPageContext;
025import lucee.runtime.exp.PageException;
026import lucee.runtime.exp.PageRuntimeException;
027import lucee.runtime.orm.hibernate.CommonUtil;
028import lucee.runtime.orm.hibernate.HibernateUtil;
029import lucee.runtime.type.Collection;
030import lucee.runtime.type.Collection.Key;
031import lucee.runtime.type.Struct;
032import lucee.runtime.type.UDF;
033
034import org.hibernate.event.PreUpdateEvent;
035
036public abstract class EventListener {
037
038        private static final long serialVersionUID = -4842481789634140033L;
039        
040        
041
042        
043    protected Component component;
044
045    private boolean allEvents;
046        private Key eventType;
047    
048        public EventListener(Component component, Key eventType, boolean allEvents) {
049               this.component=component; 
050               this.allEvents=allEvents; 
051               this.eventType=eventType;
052    }
053        
054        protected boolean preUpdate(PreUpdateEvent event) {
055                Struct oldData=CommonUtil.createStruct();
056                Property[] properties = HibernateUtil.getProperties(component,HibernateUtil.FIELDTYPE_COLUMN,null);
057                Object[] data = event.getOldState();
058                
059                if(data!=null && properties!=null && data.length==properties.length) {
060                        for(int i=0;i<data.length;i++){
061                                oldData.setEL(CommonUtil.createKey(properties[i].getName()), data[i]);
062                        }
063                }
064                invoke(CommonUtil.PRE_UPDATE, event.getEntity(),oldData);
065                return false;
066        }
067
068    
069    public Component getCFC() {
070                return component;
071        }
072    
073
074    protected void invoke(Collection.Key name, Object obj) {
075        invoke(name, obj,null);
076    }
077    protected void invoke(Collection.Key name, Object obj, Struct data) {
078        if(eventType!=null && !eventType.equals(name)) return;
079        //print.e(name);
080        Component caller = CommonUtil.toComponent(obj,null);
081        Component c=allEvents?component:caller;
082        if(c==null) return;
083        
084        if(!allEvents &&!caller.getPageSource().equals(component.getPageSource())) return;
085                invoke(name, c, data, allEvents?obj:null);
086        
087        }
088    
089    public static void invoke(Key name, Component cfc, Struct data, Object arg) {
090        if(cfc==null) return;
091        
092                try {
093                        PageContext pc = ThreadLocalPageContext.get();
094                        Object[] args;
095                        if(data==null) {
096                                args=arg!=null?new Object[]{arg}:new Object[]{};
097                        }
098                        else {
099                                args=arg!=null?new Object[]{arg,data}:new Object[]{data};
100                        }
101                        cfc.call(pc, name, args);
102                }
103                catch (PageException pe) {
104                        throw new PageRuntimeException(pe);
105                }
106        }
107    
108        public static boolean hasEventType(Component cfc, Collection.Key eventType) {
109                return cfc.get(eventType,null) instanceof UDF;
110        }
111}