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 java.io.Serializable; 022 023import lucee.runtime.Component; 024import lucee.runtime.exp.PageException; 025import lucee.runtime.orm.ORMUtil; 026import lucee.runtime.orm.hibernate.CommonUtil; 027import lucee.runtime.orm.hibernate.HibernateCaster; 028import lucee.runtime.type.Collection; 029import lucee.runtime.type.Struct; 030 031import org.hibernate.EmptyInterceptor; 032import org.hibernate.type.Type; 033 034public class InterceptorImpl extends EmptyInterceptor { 035 036 private static final long serialVersionUID = 7992972603422833660L; 037 038 private final AllEventListener listener; 039 private final boolean hasPreInsert; 040 private final boolean hasPreUpdate; 041 042 public InterceptorImpl(AllEventListener listener) { 043 this.listener=listener; 044 if(listener!=null) { 045 Component cfc = listener.getCFC(); 046 hasPreInsert=EventListener.hasEventType(cfc, CommonUtil.PRE_INSERT); 047 hasPreUpdate=EventListener.hasEventType(cfc, CommonUtil.PRE_UPDATE); 048 } 049 else { 050 hasPreInsert=false; 051 hasPreUpdate=false; 052 } 053 } 054 055 @Override 056 public boolean onSave(Object entity, Serializable id, Object[] state, 057 String[] propertyNames, Type[] types) { 058 059 return on(entity, id, state, null, propertyNames, types, CommonUtil.PRE_INSERT, hasPreInsert); 060 //return super.onSave(entity, id, state, propertyNames, types); 061 } 062 063 @Override 064 public boolean onFlushDirty(Object entity, Serializable id, 065 Object[] currentState, Object[] previousState, 066 String[] propertyNames, Type[] types) { 067 return on(entity, id, currentState, toStruct(propertyNames, previousState), propertyNames, types, CommonUtil.PRE_UPDATE, hasPreUpdate); 068 //return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types); 069 } 070 071 072 private boolean on(Object entity, Serializable id, 073 Object[] state, Struct data, 074 String[] propertyNames, Type[] types, Collection.Key eventType, boolean hasMethod) { 075 076 Component cfc = CommonUtil.toComponent(entity,null); 077 if(cfc!=null && EventListener.hasEventType(cfc, eventType)){ 078 EventListener.invoke(eventType, cfc, data, null); 079 } 080 if(hasMethod) 081 EventListener.invoke(eventType, listener.getCFC(), data, entity); 082 083 084 boolean rtn=false; 085 String prop; 086 Object before,current; 087 /* jira2049 088 ORMSession session = null; 089 try { 090 session=ORMUtil.getSession(ThreadLocalPageContext.get()); 091 } catch (PageException pe) {}*/ 092 093 for(int i = 0; i < propertyNames.length; i++) { 094 prop = propertyNames[i]; 095 before = state[i]; 096 current = ORMUtil.getPropertyValue(/* jira2049 session,*/cfc, prop,null); 097 098 if(before != current && (current == null || !CommonUtil.equalsComplexEL(before, current))) { 099 try { 100 state[i] = HibernateCaster.toSQL(types[i], current,null); 101 } catch (PageException e) { 102 state[i] = current; 103 } 104 rtn = true; 105 } 106 } 107 return rtn; 108 } 109 110 111 112 113 114 115 private static Struct toStruct(String propertyNames[], Object state[]) { 116 Struct sct = CommonUtil.createStruct(); 117 if(state!=null && propertyNames!=null){ 118 for(int i = 0; i < propertyNames.length; i++) { 119 sct.setEL(CommonUtil.createKey(propertyNames[i]), state[i]); 120 } 121 } 122 return sct; 123 } 124}