001    /*
002     * $Id: KatEntityInterceptor.java,v 1.1 2004/12/13 02:24:46 mitch Exp $
003     * 
004     * Copyright (c) 2004, FullSpan Software (www.fullspan.com)
005     *
006     * Licensed under the BSD License
007     * OSI Certified Open Source Software (www.opensource.org)
008     *
009     * You may not use this file except in compliance with the License.  You should
010     * have received a copy of the License with this distribution, or you can find
011     * it at: http://www.fullspan.com/shared/license.html.
012     *
013     * NO WARRANTY - USE AT YOUR OWN RISK.  All software and other materials
014     * distributed under the License are provided on an "AS IS" BASIS, WITHOUT
015     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     */
017    package com.fullspan.kat.db.dao;
018    
019    import java.io.Serializable;
020    import java.util.*;
021    
022    import net.sf.hibernate.CallbackException;
023    import net.sf.hibernate.Interceptor;
024    import net.sf.hibernate.type.Type;
025    
026    import com.fullspan.kat.domain.entity.KatEntity;
027    
028    /**
029     * 
030     * @author Mitch Stuart
031     */
032    public class KatEntityInterceptor implements Interceptor
033    {
034       /**
035        * @see net.sf.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
036        */
037       public boolean onLoad(Object entity, Serializable id, Object[] state,
038          String[] propertyNames, Type[] types)
039          throws CallbackException
040       {
041          // We have not modified the state
042          return false;
043       }
044    
045       /**
046        * @see net.sf.hibernate.Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
047        */
048       public boolean onFlushDirty(Object entity, Serializable id,
049          Object[] currentState, Object[] previousState, String[] propertyNames,
050          Type[] types)
051          throws CallbackException
052       {
053          if (!(entity instanceof KatEntity))
054          {
055             return false;
056          }
057          
058          int propIndex = findPropIndex(propertyNames,
059             KatEntity.PROPNAME_UPDATE_TIME);
060          
061          if (propIndex >= 0)
062          {
063             currentState[propIndex] = new Date();
064             return true;
065          }
066          
067          return false;
068       }
069    
070       /**
071        * @see net.sf.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
072        */
073       public boolean onSave(Object entity, Serializable id, Object[] state,
074          String[] propertyNames, Type[] types)
075          throws CallbackException
076       {
077          if (!(entity instanceof KatEntity))
078          {
079             return false;
080          }
081          
082          int createTimeIndex = findPropIndex(propertyNames,
083                KatEntity.PROPNAME_CREATE_TIME);
084          int updateTimeIndex = findPropIndex(propertyNames,
085             KatEntity.PROPNAME_UPDATE_TIME);
086          
087          if (createTimeIndex > 0 || updateTimeIndex > 0)
088          {
089             Date datestamp = new Date();
090             
091             if (createTimeIndex > 0)
092             {
093                state[createTimeIndex] = datestamp;
094             }
095             
096             if (updateTimeIndex > 0)
097             {
098                state[updateTimeIndex] = datestamp;
099             }
100             
101             return true;
102          }
103          
104          return false;
105       }
106    
107       /**
108        * @see net.sf.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
109        */
110       public void onDelete(Object entity, Serializable id, Object[] state,
111          String[] propertyNames, Type[] types)
112          throws CallbackException
113       {
114          // Do nothing
115       }
116    
117       /**
118        * @see net.sf.hibernate.Interceptor#preFlush(java.util.Iterator)
119        */
120       public void preFlush(Iterator entities)
121          throws CallbackException
122       {
123          // Do nothing
124       }
125    
126       /**
127        * @see net.sf.hibernate.Interceptor#postFlush(java.util.Iterator)
128        */
129       public void postFlush(Iterator entities)
130          throws CallbackException   
131       {
132          // Do nothing
133       }
134    
135       /**
136        * @see net.sf.hibernate.Interceptor#isUnsaved(java.lang.Object)
137        */
138       public Boolean isUnsaved(Object entity)
139       {
140          // Use the default behavior to determine if the object is unsaved
141          return null;
142       }
143    
144       /**
145        * @see net.sf.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
146        */
147       public int[] findDirty(Object entity, Serializable id,
148          Object[] currentState, Object[] previousState, String[] propertyNames,
149          Type[] types)
150       {
151          // Use the default dirty-checking behavior
152          return null;
153       }
154    
155       /**
156        * @see net.sf.hibernate.Interceptor#instantiate(java.lang.Class, java.io.Serializable)
157        */
158       public Object instantiate(Class clazz, Serializable id)
159          throws CallbackException
160       {
161          // Use the default behavior to instantiate
162          return null;
163       }
164       
165       protected int findPropIndex(String[] propNames, String propName)
166       {
167          for (int i = 0; i < propNames.length; i++)
168          {
169             if (propNames[i].equals(propName))
170             {
171                return i;
172             }
173          }
174          
175          return -1;
176       }
177    
178    }