1 16 package com.ibatis.sqlmap.engine.exchange; 17 18 import com.ibatis.common.exception.NestedRuntimeException; 19 import com.ibatis.common.resources.Resources; 20 import com.ibatis.sqlmap.engine.accessplan.AccessPlan; 21 import com.ibatis.sqlmap.engine.accessplan.AccessPlanFactory; 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.ErrorContext; 27 import com.ibatis.sqlmap.engine.scope.RequestScope; 28 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 36 public class JavaBeanDataExchange extends BaseDataExchange implements DataExchange { 37 38 private static final Object [] NO_DATA = new Object [0]; 39 40 private AccessPlan resultPlan; 41 private AccessPlan parameterPlan; 42 private AccessPlan outParamPlan; 43 44 protected JavaBeanDataExchange(DataExchangeFactory dataExchangeFactory) { 45 super(dataExchangeFactory); 46 } 47 48 53 public void initialize(Map properties) { 54 Object map = properties.get("map"); 55 if (map instanceof ParameterMap) { 56 ParameterMap parameterMap = (ParameterMap) map; 57 if (parameterMap != null) { 58 ParameterMapping[] parameterMappings = parameterMap.getParameterMappings(); 59 String [] parameterPropNames = new String [parameterMappings.length]; 60 for (int i = 0; i < parameterPropNames.length; i++) { 61 parameterPropNames[i] = parameterMappings[i].getPropertyName(); 62 } 63 parameterPlan = AccessPlanFactory.getAccessPlan(parameterMap.getParameterClass(), parameterPropNames); 64 65 List outParamList = new ArrayList (); 67 for (int i = 0; i < parameterPropNames.length; i++) { 68 if (parameterMappings[i].isOutputAllowed()) { 69 outParamList.add(parameterMappings[i].getPropertyName()); 70 } 71 } 72 String [] outParams = (String []) outParamList.toArray(new String [outParamList.size()]); 73 outParamPlan = AccessPlanFactory.getAccessPlan(parameterMap.getParameterClass(), outParams); 74 } 75 76 } else if (map instanceof ResultMap) { 77 ResultMap resultMap = (ResultMap) map; 78 if (resultMap != null) { 79 ResultMapping[] resultMappings = resultMap.getResultMappings(); 80 String [] resultPropNames = new String [resultMappings.length]; 81 for (int i = 0; i < resultPropNames.length; i++) { 82 resultPropNames[i] = resultMappings[i].getPropertyName(); 83 } 84 resultPlan = AccessPlanFactory.getAccessPlan(resultMap.getResultClass(), resultPropNames); 85 } 86 } 87 } 88 89 public Object [] getData(RequestScope request, ParameterMap parameterMap, Object parameterObject) { 90 if (parameterPlan != null) { 91 return parameterPlan.getProperties(parameterObject); 92 } else { 93 return NO_DATA; 94 } 95 } 96 97 public Object setData(RequestScope request, ResultMap resultMap, Object resultObject, Object [] values) { 98 if (resultPlan != null) { 99 Object object = resultObject; 100 101 ErrorContext errorContext = request.getErrorContext(); 102 103 if (object == null) { 104 errorContext.setMoreInfo("The error occured while instantiating the result object"); 105 try { 106 object = Resources.instantiate(resultMap.getResultClass()); 107 } catch (Exception e) { 108 throw new NestedRuntimeException("JavaBeansDataExchange could not instantiate result class. Cause: " + e, e); 109 } 110 } 111 errorContext.setMoreInfo("The error happened while setting a property on the result object."); 112 resultPlan.setProperties(object, values); 113 return object; 114 } else { 115 return null; 116 } 117 } 118 119 public Object setData(RequestScope request, ParameterMap parameterMap, Object parameterObject, Object [] values) { 121 if (outParamPlan != null) { 122 Object object = parameterObject; 123 if (object == null) { 124 try { 125 object = Resources.instantiate(parameterMap.getParameterClass()); 126 } catch (Exception e) { 127 throw new NestedRuntimeException("JavaBeansDataExchange could not instantiate parameter class. Cause: " + e, e); 128 } 129 } 130 values = getOutputParamValues(parameterMap.getParameterMappings(), values); 131 outParamPlan.setProperties(object, values); 132 return object; 133 } else { 134 return null; 135 } 136 } 137 138 private Object [] getOutputParamValues(ParameterMapping[] mappings, Object [] values) { 139 List outParamValues = new ArrayList (); 140 for (int i = 0; i < mappings.length; i++) { 141 if (mappings[i].isOutputAllowed()) { 142 outParamValues.add(values[i]); 143 } 144 } 145 return outParamValues.toArray(); 146 } 147 148 } 149 | Popular Tags |