View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23   
24  package net.smartlab.web.history;
25  
26  import java.io.Serializable;
27  import java.sql.Timestamp;
28  import java.util.Iterator;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.hibernate.EntityMode;
33  import org.hibernate.HibernateException;
34  import org.hibernate.MappingException;
35  import org.hibernate.Transaction;
36  import org.hibernate.property.PropertyAccessor;
37  import org.hibernate.property.PropertyAccessorFactory;
38  import org.hibernate.property.Setter;
39  import org.hibernate.type.Type;
40  
41  /**
42   * TODO documentation
43   * 
44   * @author rlogiacco
45   */
46  public class Interceptor implements org.hibernate.Interceptor {
47  
48  	/**
49  	 * Logger for this class
50  	 */
51  	protected final Log logger = LogFactory.getLog(Interceptor.class);
52  
53  	/**
54  	 * @see org.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
55  	 * TODO implement
56  	 */
57  	public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, org.hibernate.type.Type[] arg4) throws org.hibernate.CallbackException {
58  		// TODO Auto-generated method stub
59  		return false;
60  	}
61  
62  	/**
63  	 * @see org.hibernate.Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
64  	 * TODO implement
65  	 */
66  	public boolean onFlushDirty(Object entity, Serializable key, Object[] current, Object[] previous, String[] properties, Type[] types) throws org.hibernate.CallbackException {
67  		if (logger.isDebugEnabled()) {
68  			logger.debug("onFlushDirty(entity = " + entity + ", key = " + key + ", current = " + current + ", previous = " + previous
69  					+ ", properties = " + properties + ", types = " + types + ") - start");
70  		}
71  		boolean modified = false;
72  		if (entity instanceof HistoricizableBusinessObject) {
73  			// instantiate historized object
74  			HistorizedBusinessObject historized = ((HistoricizableBusinessObject)entity).getHistorized();
75  			historized.setLastModified(new Timestamp(System.currentTimeMillis()));
76  			try {
77  				// put values into historized object
78  				PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(null);
79  				for (int i = 0; i < properties.length; i++) {
80  					if (!current[i].equals(previous[i])) {
81  						modified = true;
82  						Setter setter = accessor.getSetter(historized.getClass(), properties[i]);
83  						// FIXME
84  						//setter.set(historized, current[i]);
85  					} else if (!(historized instanceof DifferenciallyHistorizedBusinessObject)) {
86  						Setter setter = accessor.getSetter(historized.getClass(), properties[i]);
87  						// FIXME
88  						//setter.set(historized, current[i]);
89  					}
90  				}
91  				if (modified) {
92  					//TODO save the historized object
93  				}
94  			} catch (MappingException me) {
95  				logger.error("onFlushDirty(entity = " + entity + ", key = " + key + ", current = " + current + ", previous = " + previous
96  						+ ", properties = " + properties + ", types = " + types + ") - error", me);
97  
98  			} catch (HibernateException he) {
99  				logger.error("onFlushDirty(entity = " + entity + ", key = " + key + ", current = " + current + ", previous = " + previous
100 						+ ", properties = " + properties + ", types = " + types + ") - error", he);
101 			}
102 		}
103 		return modified;
104 	}
105 
106 	/**
107 	 * @see org.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
108 	 * TODO implement
109 	 */
110 	public boolean onSave(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, org.hibernate.type.Type[] arg4) throws org.hibernate.CallbackException {
111 		// TODO Auto-generated method stub
112 		return false;
113 	}
114 
115 	/**
116 	 * @see org.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
117 	 * TODO implement
118 	 */
119 	public void onDelete(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, org.hibernate.type.Type[] arg4) throws org.hibernate.CallbackException {
120 		// TODO Auto-generated method stub
121 		
122 	}
123 
124 	/**
125 	 * @see org.hibernate.Interceptor#onCollectionRecreate(java.lang.Object, java.io.Serializable)
126 	 * TODO implement
127 	 */
128 	public void onCollectionRecreate(Object arg0, Serializable arg1) throws org.hibernate.CallbackException {
129 		// TODO Auto-generated method stub
130 		
131 	}
132 
133 	/**
134 	 * @see org.hibernate.Interceptor#onCollectionRemove(java.lang.Object, java.io.Serializable)
135 	 * TODO implement
136 	 */
137 	public void onCollectionRemove(Object arg0, Serializable arg1) throws org.hibernate.CallbackException {
138 		// TODO Auto-generated method stub
139 		
140 	}
141 
142 	/**
143 	 * @see org.hibernate.Interceptor#onCollectionUpdate(java.lang.Object, java.io.Serializable)
144 	 * TODO implement
145 	 */
146 	public void onCollectionUpdate(Object arg0, Serializable arg1) throws org.hibernate.CallbackException {
147 		// TODO Auto-generated method stub
148 		
149 	}
150 
151 	/**
152 	 * @see org.hibernate.Interceptor#preFlush(java.util.Iterator)
153 	 * TODO implement
154 	 */
155 	public void preFlush(Iterator arg0) throws org.hibernate.CallbackException {
156 		// TODO Auto-generated method stub
157 		
158 	}
159 
160 	/**
161 	 * @see org.hibernate.Interceptor#postFlush(java.util.Iterator)
162 	 * TODO implement
163 	 */
164 	public void postFlush(Iterator arg0) throws org.hibernate.CallbackException {
165 		// TODO Auto-generated method stub
166 		
167 	}
168 
169 	/**
170 	 * @see org.hibernate.Interceptor#isTransient(java.lang.Object)
171 	 * TODO implement
172 	 */
173 	public Boolean isTransient(Object arg0) {
174 		// TODO Auto-generated method stub
175 		return null;
176 	}
177 
178 	/**
179 	 * @see org.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
180 	 * TODO implement
181 	 */
182 	public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, org.hibernate.type.Type[] arg5) {
183 		// TODO Auto-generated method stub
184 		return null;
185 	}
186 
187 	/**
188 	 * @see org.hibernate.Interceptor#instantiate(java.lang.String, org.hibernate.EntityMode, java.io.Serializable)
189 	 * TODO implement
190 	 */
191 	public Object instantiate(String arg0, EntityMode arg1, Serializable arg2) throws org.hibernate.CallbackException {
192 		// TODO Auto-generated method stub
193 		return null;
194 	}
195 
196 	/**
197 	 * @see org.hibernate.Interceptor#getEntityName(java.lang.Object)
198 	 * TODO implement
199 	 */
200 	public String getEntityName(Object arg0) throws org.hibernate.CallbackException {
201 		// TODO Auto-generated method stub
202 		return null;
203 	}
204 
205 	/**
206 	 * @see org.hibernate.Interceptor#getEntity(java.lang.String, java.io.Serializable)
207 	 * TODO implement
208 	 */
209 	public Object getEntity(String arg0, Serializable arg1) throws org.hibernate.CallbackException {
210 		// TODO Auto-generated method stub
211 		return null;
212 	}
213 
214 	/**
215 	 * @see org.hibernate.Interceptor#afterTransactionBegin(org.hibernate.Transaction)
216 	 * TODO implement
217 	 */
218 	public void afterTransactionBegin(Transaction arg0) {
219 		// TODO Auto-generated method stub
220 		
221 	}
222 
223 	/**
224 	 * @see org.hibernate.Interceptor#beforeTransactionCompletion(org.hibernate.Transaction)
225 	 * TODO implement
226 	 */
227 	public void beforeTransactionCompletion(Transaction arg0) {
228 		// TODO Auto-generated method stub
229 		
230 	}
231 
232 	/**
233 	 * @see org.hibernate.Interceptor#afterTransactionCompletion(org.hibernate.Transaction)
234 	 * TODO implement
235 	 */
236 	public void afterTransactionCompletion(Transaction arg0) {
237 		// TODO Auto-generated method stub
238 		
239 	}
240 
241 	/**
242 	 * @see org.hibernate.Interceptor#onPrepareStatement(java.lang.String)
243 	 * TODO implement
244 	 */
245 	public String onPrepareStatement(String arg0) {
246 		// TODO Auto-generated method stub
247 		return null;
248 	}
249 }