1 16 package com.ibatis.sqlmap.engine.exchange; 17 18 import com.ibatis.common.exception.NestedRuntimeException; 19 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap; 20 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping; 21 import com.ibatis.sqlmap.engine.mapping.result.ResultMap; 22 import com.ibatis.sqlmap.engine.mapping.result.ResultMapping; 23 import com.ibatis.sqlmap.engine.scope.RequestScope; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 31 public class MapDataExchange extends BaseDataExchange implements DataExchange { 32 33 protected MapDataExchange(DataExchangeFactory dataExchangeFactory) { 34 super(dataExchangeFactory); 35 } 36 37 public void initialize(Map properties) { 38 } 39 40 public Object [] getData(RequestScope request, ParameterMap parameterMap, Object parameterObject) { 41 if (!(parameterObject instanceof Map)) { 42 throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map."); 43 } 44 45 Object [] data = new Object [parameterMap.getParameterMappings().length]; 46 Map map = (Map) parameterObject; 47 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 48 for (int i = 0; i < mappings.length; i++) { 49 data[i] = map.get(mappings[i].getPropertyName()); 50 } 51 return data; 52 } 53 54 public Object setData(RequestScope request, ResultMap resultMap, Object resultObject, Object [] values) { 55 if (!(resultObject == null || resultObject instanceof Map)) { 56 throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map."); 57 } 58 59 Map map = (Map) resultObject; 60 if (map == null) { 61 map = new HashMap (); 62 } 63 64 ResultMapping[] mappings = resultMap.getResultMappings(); 65 for (int i = 0; i < mappings.length; i++) { 66 map.put(mappings[i].getPropertyName(), values[i]); 67 } 68 69 return map; 70 } 71 72 public Object setData(RequestScope request, ParameterMap parameterMap, Object parameterObject, Object [] values) { 73 if (!(parameterObject == null || parameterObject instanceof Map)) { 74 throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map."); 75 } 76 77 Map map = (Map) parameterObject; 78 if (map == null) { 79 map = new HashMap (); 80 } 81 82 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 83 for (int i = 0; i < mappings.length; i++) { 84 if (mappings[i].isOutputAllowed()) { 85 map.put(mappings[i].getPropertyName(), values[i]); 86 } 87 } 88 89 return map; 90 } 91 92 } 93 94 95 | Popular Tags |