| 1 | /** |
| 2 | * Copyright (C) 2008 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.type.impl; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Set; |
| 24 | import java.util.List; |
| 25 | import hu.netmind.beankeeper.parser.*; |
| 26 | import hu.netmind.beankeeper.type.TypeHandler; |
| 27 | import hu.netmind.beankeeper.model.*; |
| 28 | import hu.netmind.beankeeper.transaction.Transaction; |
| 29 | |
| 30 | /** |
| 31 | * This is a type handler for those simple types, which can store their |
| 32 | * content in a primitive type, but need some kind of wrapping and |
| 33 | * unwrapping. Extend this class to provide type handlers for simple |
| 34 | * types which translate to one primitive attribute. |
| 35 | * @author Brautigam Robert |
| 36 | * @version Revision: $Revision$ |
| 37 | */ |
| 38 | public abstract class SimpleTypeHandler implements TypeHandler |
| 39 | { |
| 40 | /** |
| 41 | * This method is called to create the table backing a type like |
| 42 | * this. Because we store the type's value in the object itself, |
| 43 | * we don't need any additional tables, so this is empty. |
| 44 | */ |
| 45 | public void ensureTableExists(ClassInfo parentInfo, String attributeName, boolean create) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the attribute types that are representing this type |
| 51 | * in the original object. This type will be primitive type |
| 52 | * which this type translates to. |
| 53 | */ |
| 54 | public Map getAttributeTypes(String attributeName) |
| 55 | { |
| 56 | HashMap attributeTypes = new HashMap(); |
| 57 | attributeTypes.put(attributeName,getPrimitiveType()); |
| 58 | return attributeTypes; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Implement this method to return the primitive type this handled |
| 63 | * type translates to. |
| 64 | */ |
| 65 | public abstract Class getPrimitiveType(); |
| 66 | |
| 67 | /** |
| 68 | * Determine whether the two values differ. Because these are simple |
| 69 | * primitive values, we simply use the <code>equals()</code> method. |
| 70 | */ |
| 71 | public boolean hasChanged(ClassInfo info, Object obj, String attributeName, Map dbAttributes, |
| 72 | Long serial) |
| 73 | { |
| 74 | Object objectValue = info.getAttributeValue(obj,attributeName); |
| 75 | Object dbValue = dbAttributes.get(attributeName); |
| 76 | return ! (((objectValue==null) && (dbValue==null)) || |
| 77 | ((objectValue!=null) && (objectValue.equals(dbValue)))); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the umarshalled instance. |
| 82 | * @param classInfo The info of the parent object. |
| 83 | * @param obj The object itself. |
| 84 | * @param attributeName The name of the attribute in question. |
| 85 | * @param marshalledValues All the values of the parent object. |
| 86 | * @param timeControl The time control in which the parent was selected. |
| 87 | */ |
| 88 | public Object unmarshallType(ClassInfo classInfo, Object obj, |
| 89 | String attributeName, Map marshalledValues, TimeControl timeControl) |
| 90 | { |
| 91 | Object primitive = marshalledValues.get(attributeName); |
| 92 | return unmarshallType(primitive); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Implement this method to create an instance of this handled type |
| 97 | * from the primitive representation. |
| 98 | */ |
| 99 | public abstract Object unmarshallType(Object primitiveValue); |
| 100 | |
| 101 | /** |
| 102 | * Save the changes occured, this only has to marshall the object into |
| 103 | * the primitive form. |
| 104 | * @param classInfo The class info of the parent object. |
| 105 | * @param current The parent object. |
| 106 | * @param attributeName The name of the attribute about to save. |
| 107 | * @param transaction The transaction the saves must occur in. |
| 108 | * @param currentSerial The serial id the current saved are in. |
| 109 | * @param newValue The new value. |
| 110 | * @param waitingObjects List of object on which this operation depends on. |
| 111 | * This is writable. |
| 112 | * @param events The events the type save generated. |
| 113 | * @param changedAttributes The attributes need to be updated in the |
| 114 | * database. |
| 115 | * @param dbAttributes The database attributes. |
| 116 | * @param updatedAttributes The attributes that need to be updated |
| 117 | * in the object tracker. |
| 118 | * @return The new object of the attribute. |
| 119 | */ |
| 120 | public Object save(ClassInfo classInfo, Object current, String attributeName, |
| 121 | Transaction transaction, Long currentSerial, |
| 122 | Object newValue, Set waitingObjects, Set saveTables, Set removeTables, |
| 123 | List events, Map changedAttributes, Map dbAttributes) |
| 124 | { |
| 125 | changedAttributes.put(attributeName,marshallType(newValue)); |
| 126 | return newValue; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Implement this method to translate an instance of this handled type |
| 131 | * into a primitive representation. |
| 132 | */ |
| 133 | public abstract Object marshallType(Object obj); |
| 134 | |
| 135 | /** |
| 136 | * This method is empty, not used. |
| 137 | */ |
| 138 | public void postSave(Object value) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Create the approriate symbol entry when parsing a query. Because |
| 144 | * primitive types do not need a symbol entry, this method simply |
| 145 | * returns null. |
| 146 | */ |
| 147 | public WhereResolver.SymbolTableEntry getSymbolEntry(AttributeSpecifier spec, |
| 148 | WhereResolver.SymbolTableEntry previousEntry, ClassInfo previousInfo, |
| 149 | ReferenceTerm previousTerm) |
| 150 | throws ParserException |
| 151 | { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Determine the next class info after the given specifier. This is |
| 157 | * never invoked, because this handler does not produce a symbol entry. |
| 158 | */ |
| 159 | public ClassInfo getSymbolInfo(WhereResolver.SymbolTableEntry entry, |
| 160 | AttributeSpecifier spec) |
| 161 | throws ParserException |
| 162 | { |
| 163 | throw new ParserException(ParserException.ABORT,"simple typehandler handles this attribute, so it's a primitive type, but it was dereferenced"); |
| 164 | } |
| 165 | } |