001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.exchange;
017:
018: import com.ibatis.sqlmap.engine.accessplan.AccessPlan;
019: import com.ibatis.sqlmap.engine.accessplan.AccessPlanFactory;
020: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
021: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
022: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
023: import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
024: import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil;
025: import com.ibatis.sqlmap.engine.scope.ErrorContext;
026: import com.ibatis.sqlmap.engine.scope.RequestScope;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Map;
031:
032: /**
033: * DataExchange implementation for beans
034: */
035: public class JavaBeanDataExchange extends BaseDataExchange implements
036: DataExchange {
037:
038: private static final Object[] NO_DATA = new Object[0];
039:
040: private AccessPlan resultPlan;
041: private AccessPlan parameterPlan;
042: private AccessPlan outParamPlan;
043:
044: protected JavaBeanDataExchange(
045: DataExchangeFactory dataExchangeFactory) {
046: super (dataExchangeFactory);
047: }
048:
049: /**
050: * Initializes the data exchange instance.
051: *
052: * @param properties
053: */
054: public void initialize(Map properties) {
055: Object map = properties.get("map");
056: if (map instanceof ParameterMap) {
057: ParameterMap parameterMap = (ParameterMap) map;
058: if (parameterMap != null) {
059: ParameterMapping[] parameterMappings = parameterMap
060: .getParameterMappings();
061: String[] parameterPropNames = new String[parameterMappings.length];
062: for (int i = 0; i < parameterPropNames.length; i++) {
063: parameterPropNames[i] = parameterMappings[i]
064: .getPropertyName();
065: }
066: parameterPlan = AccessPlanFactory.getAccessPlan(
067: parameterMap.getParameterClass(),
068: parameterPropNames);
069:
070: // OUTPUT PARAMS
071: List outParamList = new ArrayList();
072: for (int i = 0; i < parameterPropNames.length; i++) {
073: if (parameterMappings[i].isOutputAllowed()) {
074: outParamList.add(parameterMappings[i]
075: .getPropertyName());
076: }
077: }
078: String[] outParams = (String[]) outParamList
079: .toArray(new String[outParamList.size()]);
080: outParamPlan = AccessPlanFactory.getAccessPlan(
081: parameterMap.getParameterClass(), outParams);
082: }
083:
084: } else if (map instanceof ResultMap) {
085: ResultMap resultMap = (ResultMap) map;
086: if (resultMap != null) {
087: ResultMapping[] resultMappings = resultMap
088: .getResultMappings();
089: String[] resultPropNames = new String[resultMappings.length];
090: for (int i = 0; i < resultPropNames.length; i++) {
091: resultPropNames[i] = resultMappings[i]
092: .getPropertyName();
093: }
094: resultPlan = AccessPlanFactory.getAccessPlan(resultMap
095: .getResultClass(), resultPropNames);
096: }
097: }
098: }
099:
100: public Object[] getData(RequestScope request,
101: ParameterMap parameterMap, Object parameterObject) {
102: if (parameterPlan != null) {
103: return parameterPlan.getProperties(parameterObject);
104: } else {
105: return NO_DATA;
106: }
107: }
108:
109: public Object setData(RequestScope request, ResultMap resultMap,
110: Object resultObject, Object[] values) {
111: if (resultPlan != null) {
112: Object object = resultObject;
113:
114: ErrorContext errorContext = request.getErrorContext();
115:
116: if (object == null) {
117: errorContext
118: .setMoreInfo("The error occured while instantiating the result object");
119: try {
120: object = ResultObjectFactoryUtil
121: .createObjectThroughFactory(resultMap
122: .getResultClass());
123: } catch (Exception e) {
124: throw new RuntimeException(
125: "JavaBeansDataExchange could not instantiate result class. Cause: "
126: + e, e);
127: }
128: }
129: errorContext
130: .setMoreInfo("The error happened while setting a property on the result object.");
131: resultPlan.setProperties(object, values);
132: return object;
133: } else {
134: return null;
135: }
136: }
137:
138: // Bug ibatis-12
139: public Object setData(RequestScope request,
140: ParameterMap parameterMap, Object parameterObject,
141: Object[] values) {
142: if (outParamPlan != null) {
143: Object object = parameterObject;
144: if (object == null) {
145: try {
146: object = ResultObjectFactoryUtil
147: .createObjectThroughFactory(parameterMap
148: .getParameterClass());
149: } catch (Exception e) {
150: throw new RuntimeException(
151: "JavaBeansDataExchange could not instantiate parameter class. Cause: "
152: + e, e);
153: }
154: }
155: values = getOutputParamValues(parameterMap
156: .getParameterMappings(), values);
157: outParamPlan.setProperties(object, values);
158: return object;
159: } else {
160: return null;
161: }
162: }
163:
164: private Object[] getOutputParamValues(ParameterMapping[] mappings,
165: Object[] values) {
166: List outParamValues = new ArrayList();
167: for (int i = 0; i < mappings.length; i++) {
168: if (mappings[i].isOutputAllowed()) {
169: outParamValues.add(values[i]);
170: }
171: }
172: return outParamValues.toArray();
173: }
174:
175: }
|