| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.store.event; |
| 20 | |
| 21 | import hu.netmind.beankeeper.model.ClassInfo; |
| 22 | import hu.netmind.beankeeper.model.ClassTracker; |
| 23 | import hu.netmind.beankeeper.common.StoreException; |
| 24 | import hu.netmind.beankeeper.parser.TimeControl; |
| 25 | import hu.netmind.beankeeper.object.ObjectTracker; |
| 26 | import hu.netmind.beankeeper.type.TypeHandlerTracker; |
| 27 | import hu.netmind.beankeeper.query.QueryService; |
| 28 | import java.util.Map; |
| 29 | import java.util.Arrays; |
| 30 | import org.apache.log4j.Logger; |
| 31 | |
| 32 | /** |
| 33 | * This event is generated if an object's attributes changed. |
| 34 | * @author Brautigam Robert |
| 35 | * @version CVS Revision: $Revision$ |
| 36 | */ |
| 37 | public class ModifyObjectEvent extends ObjectEvent |
| 38 | { |
| 39 | private static Logger logger = Logger.getLogger(ModifyObjectEvent.class); |
| 40 | |
| 41 | private TimeControl timeControl = null; |
| 42 | private Object originalObject = null; |
| 43 | private Map originalAttributes; |
| 44 | |
| 45 | private ClassTracker classTracker = null; |
| 46 | private ObjectTracker objectTracker = null; |
| 47 | private TypeHandlerTracker typeHandlerTracker = null; |
| 48 | private QueryService queryService = null; |
| 49 | |
| 50 | public ModifyObjectEvent(ClassTracker classTracker, ObjectTracker objectTracker, |
| 51 | TypeHandlerTracker typeHandlerTracker, QueryService queryService, |
| 52 | Map originalAttributes, TimeControl timeControl, Object object) |
| 53 | { |
| 54 | super(object); |
| 55 | this.originalAttributes=originalAttributes; |
| 56 | this.classTracker=classTracker; |
| 57 | this.objectTracker=objectTracker; |
| 58 | this.timeControl=new TimeControl(timeControl); |
| 59 | this.queryService=queryService; |
| 60 | this.typeHandlerTracker=typeHandlerTracker; |
| 61 | // Set the time control to the previous instant, because |
| 62 | // in the given instant the object is already modified! |
| 63 | this.timeControl.setSerial(new Long(this.timeControl.getSerial().longValue()-1)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Determine whether an original attribute was equals to the given |
| 68 | * value. If the value is not a primitive type, it is considered |
| 69 | * the old value if they have the same identity. This method is |
| 70 | * inexpensive unlike <code>getOriginalObject()</code>, so use |
| 71 | * this if possible. |
| 72 | * @param attributeName The attribute to check, case insensitive. |
| 73 | * @param value The value. |
| 74 | * @throws StoreException If container types are tried to be compared. |
| 75 | */ |
| 76 | public boolean isOriginalValue(String attributeName, Object value) |
| 77 | { |
| 78 | ClassInfo info = classTracker.getClassInfo(getObject().getClass(),getObject()); |
| 79 | Object originalValue = originalAttributes.get(attributeName.toLowerCase()); |
| 80 | // Check nulls |
| 81 | if ( (originalValue==null) && (value==null) ) |
| 82 | return true; |
| 83 | if ( ((originalValue!=null) && (value==null)) || |
| 84 | ((originalValue==null) && (value!=null)) ) |
| 85 | return false; |
| 86 | // Check handled types |
| 87 | Class type = info.getAttributeType(attributeName); |
| 88 | if ( typeHandlerTracker.isHandled(type) ) |
| 89 | throw new StoreException("container types can not be compared against their original values, try getOriginalObject() for that"); |
| 90 | // Check byte array |
| 91 | if ( originalValue instanceof byte[] ) |
| 92 | { |
| 93 | boolean result = Arrays.equals((byte[]) originalValue, (byte[]) value); |
| 94 | return result; |
| 95 | } |
| 96 | // Check custom objects, in which case the originalValue is a persistence_id, |
| 97 | // and the 'value' is an object. |
| 98 | if ( classTracker.getType(value.getClass()) == ClassTracker.ClassType.TYPE_OBJECT ) |
| 99 | return objectTracker.getIdentifier(value).equals(originalValue); |
| 100 | // Fallback to equals() |
| 101 | return value.equals(originalValue); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the original object before the change occured. Note, that |
| 106 | * this method selects the object from database, so this should |
| 107 | * be considered an expensive operation. Use with caution. |
| 108 | */ |
| 109 | public Object getOriginalObject() |
| 110 | { |
| 111 | if ( originalObject == null ) |
| 112 | { |
| 113 | logger.debug("selecting original object at "+timeControl); |
| 114 | originalObject = queryService.findSingle("find obj("+getObject().getClass().getName()+") where obj = ? at ?", |
| 115 | new Object[] { getObject(), timeControl }); |
| 116 | } |
| 117 | return originalObject; |
| 118 | |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |