001    package net.sf.persism;
002    
003    import java.lang.reflect.InvocationTargetException;
004    import java.util.Collection;
005    
006    /**
007     * Data objects can optionally inherit from this class. Persism uses information from this class to detect which
008     * properties are modified in your data objects and only includes those columns in the SQL UPDATE statements.
009     *
010     * @author Dan Howard
011     * @since 9/15/11 7:14 AM
012     */
013    public abstract class PersistableObject implements Persistable {
014    
015        Persistable originalValue = null;
016    
017        public final void saveReadState() throws PersismException {
018            try {
019                Collection<PropertyInfo> properties = MetaData.getPropertyInfo(getClass());
020                originalValue = getClass().newInstance();
021                for (PropertyInfo propertyInfo : properties) {
022    
023                    // It's possible to have a read-only property in a class. We just ignore those
024                    if (propertyInfo.setter != null) {
025                        propertyInfo.setter.invoke(originalValue, propertyInfo.getter.invoke(this));
026                    }
027                }
028            } catch (IllegalAccessException e) {
029                throw new PersismException(e);
030            } catch (IllegalArgumentException e) {
031                throw new PersismException(e);
032            } catch (InstantiationException e) {
033                throw new PersismException(e);
034            } catch (InvocationTargetException e) {
035                throw new PersismException(e);
036            }
037        }
038    
039        public final Persistable getOriginalValue() {
040            return originalValue;
041        }
042    }