| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.mapping; |
| 20 | |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.Method; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import sf.qof.exception.ValidationException; |
| 28 | |
| 29 | |
| 30 | public class MappingFactory { |
| 31 | |
| 32 | private MappingFactory() { } |
| 33 | |
| 34 | public static ParameterMapping createParameterMapping(String mappingType, int index, Class<?> type, Class<?> collectionType, |
| 35 | Class<?> beanType, Method getter, int[] sqlIndexes, String[] sqlColumns, boolean usesArray) { |
| 36 | MappingClassInfo info; |
| 37 | if (mappingType.equals("auto")) { |
| 38 | info = getDefaultParameterMappingInfo(type); |
| 39 | } else { |
| 40 | info = getParameterMappingInfo(mappingType); |
| 41 | } |
| 42 | if (info == null) { |
| 43 | throw new ValidationException("No mapping found for mapping type " + mappingType); |
| 44 | } |
| 45 | boolean found = false; |
| 46 | for (Class<?> mappableType : info.getMappableTypes()) { |
| 47 | if (mappableType.isAssignableFrom(type)) { |
| 48 | found = true; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | if (!found) { |
| 53 | throw new ValidationException("No mapping for " + type); |
| 54 | } |
| 55 | Class<?> mappingClass = info.getMapperClass(); |
| 56 | try { |
| 57 | ParameterMapping mapping = (ParameterMapping) mappingClass.newInstance(); |
| 58 | mapping.setParameters(index, type, collectionType, beanType, getter, sqlIndexes, sqlColumns, info.getAdapter(), usesArray); |
| 59 | return (ParameterMapping) mapping; |
| 60 | } catch (InstantiationException e) { |
| 61 | throw new RuntimeException(e); |
| 62 | } catch (IllegalAccessException e) { |
| 63 | throw new RuntimeException(e); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public static ResultMapping createResultMapping(String mappingType, Class<?> type, Class<?> collectionType, Class<?> beanType, |
| 68 | Method setter, int[] sqlIndexes, String[] sqlColumns, Class<?> mapKeyType, Integer constructorParameter, Constructor<?> constructor) { |
| 69 | MappingClassInfo info = null; |
| 70 | Class<?> resultType; |
| 71 | if (mapKeyType == null) { |
| 72 | resultType = type; |
| 73 | } else { |
| 74 | resultType = mapKeyType; |
| 75 | } |
| 76 | |
| 77 | if (mappingType.equals("auto")) { |
| 78 | info = getDefaultResultMappingInfo(resultType); |
| 79 | } else { |
| 80 | info = getResultMappingInfo(mappingType); |
| 81 | } |
| 82 | if (info == null) { |
| 83 | throw new ValidationException("No mapping found for type " + mappingType + " or " + resultType); |
| 84 | } |
| 85 | boolean found = false; |
| 86 | for (Class<?> mappableType : info.getMappableTypes()) { |
| 87 | if (mappableType.isAssignableFrom(resultType)) { |
| 88 | found = true; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (constructorParameter == null && !found) { |
| 93 | throw new ValidationException("No mapping for " + resultType); |
| 94 | } |
| 95 | Class<?> mappingClass = info.getMapperClass(); |
| 96 | try { |
| 97 | ResultMapping mapping = (ResultMapping) mappingClass.newInstance(); |
| 98 | mapping.setParameters(type, collectionType, beanType, setter, sqlIndexes, sqlColumns, |
| 99 | info.getAdapter(), mapKeyType, constructorParameter, constructor); |
| 100 | return (ResultMapping) mapping; |
| 101 | } catch (InstantiationException e) { |
| 102 | throw new RuntimeException(e); |
| 103 | } catch (IllegalAccessException e) { |
| 104 | throw new RuntimeException(e); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // ------- mapper registration ----------- |
| 109 | |
| 110 | private static Map<String, MappingClassInfo> registeredResultMappers = new HashMap<String, MappingClassInfo>(); |
| 111 | private static Map<String, MappingClassInfo> registeredParameterMappers = new HashMap<String, MappingClassInfo>(); |
| 112 | |
| 113 | private static Map<Class<?>, MappingClassInfo> defaultResultMappers = new HashMap<Class<?>, MappingClassInfo>(); |
| 114 | private static Map<Class<?>, MappingClassInfo> defaultParameterMappers = new HashMap<Class<?>, MappingClassInfo>(); |
| 115 | |
| 116 | public static class MappingClassInfo { |
| 117 | private Class<?> mapperClass; |
| 118 | private Set<Class<?>> mappableTypes; |
| 119 | private MappingAdapter adapter; |
| 120 | |
| 121 | public MappingClassInfo(Class<?> mapperClass, Set<Class<?>> mappableTypes) { |
| 122 | this(mapperClass, mappableTypes, null); |
| 123 | } |
| 124 | |
| 125 | public MappingClassInfo(Class<?> mapperClass, Set<Class<?>> mappableTypes, MappingAdapter adapter) { |
| 126 | this.mapperClass = mapperClass; |
| 127 | this.mappableTypes = mappableTypes; |
| 128 | this.adapter = adapter; |
| 129 | } |
| 130 | |
| 131 | public Set<Class<?>> getMappableTypes() { |
| 132 | return mappableTypes; |
| 133 | } |
| 134 | |
| 135 | public Class<?> getMapperClass() { |
| 136 | return (Class<?>) mapperClass; |
| 137 | } |
| 138 | |
| 139 | public MappingAdapter getAdapter() { |
| 140 | return adapter; |
| 141 | } |
| 142 | |
| 143 | public boolean isAdapter() { |
| 144 | return adapter != null; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | protected static void registerResultMapper(String type, Class<?> mapping, Set<Class<?>> types) { |
| 149 | if (!ResultMapping.class.isAssignableFrom(mapping)) { |
| 150 | throw new ValidationException("Mapping " + mapping.getName() + " must implement ResultMapping interface"); |
| 151 | } |
| 152 | MappingClassInfo info = new MappingClassInfo(mapping, types); |
| 153 | registeredResultMappers.put(type, info); |
| 154 | for (Class<?> t : types) { |
| 155 | if (!defaultResultMappers.containsKey(t)) { |
| 156 | defaultResultMappers.put(t, info); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | protected static void registerParameterMapper(String type, Class<?> mapping, Set<Class<?>> types) { |
| 162 | if (!ParameterMapping.class.isAssignableFrom(mapping)) { |
| 163 | throw new ValidationException("Mapping " + mapping.getName() + " must implement ParameterMapping interface"); |
| 164 | } |
| 165 | MappingClassInfo info = new MappingClassInfo(mapping, types); |
| 166 | registeredParameterMappers.put(type, info); |
| 167 | for (Class<?> t : types) { |
| 168 | if (!defaultParameterMappers.containsKey(t)) { |
| 169 | defaultParameterMappers.put(t, info); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public static void registerMapper(String type, MappingAdapter adapter) { |
| 175 | registerResultMapper(type, adapter); |
| 176 | registerParameterMapper(type, adapter); |
| 177 | } |
| 178 | |
| 179 | public static void unregisterMapper(String type) { |
| 180 | unregisterResultMapper(type); |
| 181 | unregisterParameterMapper(type); |
| 182 | } |
| 183 | |
| 184 | protected static void registerResultMapper(String type, MappingAdapter adapter) { |
| 185 | if (!registeredResultMappers.containsKey(type)) { |
| 186 | registeredResultMappers.put(type, new MappingClassInfo(AdapterMapping.class, adapter.getTypes(), adapter)); |
| 187 | } else { |
| 188 | throw new ValidationException("Type " + type + " already registered"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | protected static void registerParameterMapper(String type, MappingAdapter adapter) { |
| 193 | if (!registeredParameterMappers.containsKey(type)) { |
| 194 | registeredParameterMappers.put(type, new MappingClassInfo(AdapterMapping.class, adapter.getTypes(), adapter)); |
| 195 | } else { |
| 196 | throw new ValidationException("Type " + type + " already registered"); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | protected static void unregisterResultMapper(String type) { |
| 201 | registeredResultMappers.remove(type); |
| 202 | } |
| 203 | |
| 204 | protected static void unregisterParameterMapper(String type) { |
| 205 | registeredParameterMappers.remove(type); |
| 206 | } |
| 207 | |
| 208 | // -------------------------- |
| 209 | |
| 210 | public static MappingClassInfo getParameterMappingInfo(String type) { |
| 211 | return registeredParameterMappers.get(type); |
| 212 | } |
| 213 | |
| 214 | public static MappingClassInfo getResultMappingInfo(String type) { |
| 215 | return registeredResultMappers.get(type); |
| 216 | } |
| 217 | |
| 218 | public static MappingClassInfo getDefaultParameterMappingInfo(Class<?> type) { |
| 219 | return defaultParameterMappers.get(type); |
| 220 | } |
| 221 | |
| 222 | public static MappingClassInfo getDefaultResultMappingInfo(Class<?> type) { |
| 223 | return defaultResultMappers.get(type); |
| 224 | } |
| 225 | |
| 226 | // ------- register default mappers ----------- |
| 227 | |
| 228 | static { |
| 229 | registerResultMapper("string", AbstractCharacterMapping.StringMapping.class, AbstractCharacterMapping.StringMapping.getTypes()); |
| 230 | registerResultMapper("char", AbstractCharacterMapping.CharacterMapping.class, AbstractCharacterMapping.CharacterMapping.getTypes()); |
| 231 | registerResultMapper("boolean", AbstractNumberMapping.BooleanMapping.class, AbstractNumberMapping.BooleanMapping.getTypes()); |
| 232 | registerResultMapper("byte", AbstractNumberMapping.ByteMapping.class, AbstractNumberMapping.ByteMapping.getTypes()); |
| 233 | registerResultMapper("short", AbstractNumberMapping.ShortMapping.class, AbstractNumberMapping.ShortMapping.getTypes()); |
| 234 | registerResultMapper("int", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes()); |
| 235 | registerResultMapper("integer", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes()); |
| 236 | registerResultMapper("long", AbstractNumberMapping.LongMapping.class, AbstractNumberMapping.LongMapping.getTypes()); |
| 237 | registerResultMapper("float", AbstractNumberMapping.FloatMapping.class, AbstractNumberMapping.FloatMapping.getTypes()); |
| 238 | registerResultMapper("double", AbstractNumberMapping.DoubleMapping.class, AbstractNumberMapping.DoubleMapping.getTypes()); |
| 239 | registerResultMapper("date", AbstractDateTimeMapping.DateMapping.class, AbstractDateTimeMapping.DateMapping.getTypes()); |
| 240 | registerResultMapper("time", AbstractDateTimeMapping.TimeMapping.class, AbstractDateTimeMapping.TimeMapping.getTypes()); |
| 241 | registerResultMapper("timestamp", AbstractDateTimeMapping.TimestampMapping.class, AbstractDateTimeMapping.TimestampMapping.getTypes()); |
| 242 | |
| 243 | registerParameterMapper("string", AbstractCharacterMapping.StringMapping.class, AbstractCharacterMapping.StringMapping.getTypes()); |
| 244 | registerParameterMapper("char", AbstractCharacterMapping.CharacterMapping.class, AbstractCharacterMapping.CharacterMapping.getTypes()); |
| 245 | registerParameterMapper("boolean", AbstractNumberMapping.BooleanMapping.class, AbstractNumberMapping.BooleanMapping.getTypes()); |
| 246 | registerParameterMapper("byte", AbstractNumberMapping.ByteMapping.class, AbstractNumberMapping.ByteMapping.getTypes()); |
| 247 | registerParameterMapper("short", AbstractNumberMapping.ShortMapping.class, AbstractNumberMapping.ShortMapping.getTypes()); |
| 248 | registerParameterMapper("int", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes()); |
| 249 | registerParameterMapper("integer", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes()); |
| 250 | registerParameterMapper("long", AbstractNumberMapping.LongMapping.class, AbstractNumberMapping.LongMapping.getTypes()); |
| 251 | registerParameterMapper("float", AbstractNumberMapping.FloatMapping.class, AbstractNumberMapping.FloatMapping.getTypes()); |
| 252 | registerParameterMapper("double", AbstractNumberMapping.DoubleMapping.class, AbstractNumberMapping.DoubleMapping.getTypes()); |
| 253 | registerParameterMapper("date", AbstractDateTimeMapping.DateMapping.class, AbstractDateTimeMapping.DateMapping.getTypes()); |
| 254 | registerParameterMapper("time", AbstractDateTimeMapping.TimeMapping.class, AbstractDateTimeMapping.TimeMapping.getTypes()); |
| 255 | registerParameterMapper("timestamp", AbstractDateTimeMapping.TimestampMapping.class, AbstractDateTimeMapping.TimestampMapping.getTypes()); |
| 256 | } |
| 257 | } |