1 16 package com.ibatis.sqlmap.engine.exchange; 17 18 import com.ibatis.common.beans.Probe; 19 import com.ibatis.common.beans.ProbeFactory; 20 import com.ibatis.common.exception.NestedRuntimeException; 21 import com.ibatis.common.resources.Resources; 22 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap; 23 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping; 24 import com.ibatis.sqlmap.engine.mapping.result.ResultMap; 25 import com.ibatis.sqlmap.engine.mapping.result.ResultMapping; 26 import com.ibatis.sqlmap.engine.scope.RequestScope; 27 import com.ibatis.sqlmap.engine.type.TypeHandlerFactory; 28 29 import java.util.Map ; 30 31 34 public class ComplexDataExchange extends BaseDataExchange implements DataExchange { 35 36 private static final Probe PROBE = ProbeFactory.getProbe(); 37 38 42 public ComplexDataExchange(DataExchangeFactory dataExchangeFactory) { 43 super(dataExchangeFactory); 44 } 45 46 public void initialize(Map properties) { 47 } 48 49 public Object [] getData(RequestScope request, ParameterMap parameterMap, Object parameterObject) { 50 TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory(); 51 if (parameterObject == null) { 52 return new Object [1]; 53 } else { 54 if (typeHandlerFactory.hasTypeHandler(parameterObject.getClass())) { 55 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 56 Object [] data = new Object [mappings.length]; 57 for (int i = 0; i < mappings.length; i++) { 58 data[i] = parameterObject; 59 } 60 return data; 61 } else { 62 Object [] data = new Object [parameterMap.getParameterMappings().length]; 63 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 64 for (int i = 0; i < mappings.length; i++) { 65 data[i] = PROBE.getObject(parameterObject, mappings[i].getPropertyName()); 66 } 67 return data; 68 } 69 } 70 } 71 72 public Object setData(RequestScope request, ResultMap resultMap, Object resultObject, Object [] values) { 73 TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory(); 74 if (typeHandlerFactory.hasTypeHandler(resultMap.getResultClass())) { 75 return values[0]; 76 } else { 77 Object object = resultObject; 78 if (object == null) { 79 try { 80 object = Resources.instantiate(resultMap.getResultClass()); 81 } catch (Exception e) { 82 throw new NestedRuntimeException("JavaBeansDataExchange could not instantiate result class. Cause: " + e, e); 83 } 84 } 85 ResultMapping[] mappings = resultMap.getResultMappings(); 86 for (int i = 0; i < mappings.length; i++) { 87 PROBE.setObject(object, mappings[i].getPropertyName(), values[i]); 88 } 89 return object; 90 } 91 } 92 93 public Object setData(RequestScope request, ParameterMap parameterMap, Object parameterObject, Object [] values) { 94 TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory(); 95 if (typeHandlerFactory.hasTypeHandler(parameterMap.getParameterClass())) { 96 return values[0]; 97 } else { 98 Object object = parameterObject; 99 if (object == null) { 100 try { 101 object = Resources.instantiate(parameterMap.getParameterClass()); 102 } catch (Exception e) { 103 throw new NestedRuntimeException("JavaBeansDataExchange could not instantiate result class. Cause: " + e, e); 104 } 105 } 106 ParameterMapping[] mappings = parameterMap.getParameterMappings(); 107 for (int i = 0; i < mappings.length; i++) { 108 if (mappings[i].isOutputAllowed()) { 109 PROBE.setObject(object, mappings[i].getPropertyName(), values[i]); 110 } 111 } 112 return object; 113 } 114 } 115 116 } 117 | Popular Tags |