KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > exchange > JavaBeanDataExchange


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * DataExchange implementation for beans
35  */

36 public class JavaBeanDataExchange extends BaseDataExchange implements DataExchange {
37
38   private static final Object JavaDoc[] NO_DATA = new Object JavaDoc[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   /**
49    * Initializes the data exchange instance.
50    *
51    * @param properties
52    */

53   public void initialize(Map properties) {
54     Object JavaDoc 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 JavaDoc[] parameterPropNames = new String JavaDoc[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         // OUTPUT PARAMS
66
List JavaDoc outParamList = new ArrayList JavaDoc();
67         for (int i = 0; i < parameterPropNames.length; i++) {
68           if (parameterMappings[i].isOutputAllowed()) {
69             outParamList.add(parameterMappings[i].getPropertyName());
70           }
71         }
72         String JavaDoc[] outParams = (String JavaDoc[]) outParamList.toArray(new String JavaDoc[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 JavaDoc[] resultPropNames = new String JavaDoc[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 JavaDoc[] getData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject) {
90     if (parameterPlan != null) {
91       return parameterPlan.getProperties(parameterObject);
92     } else {
93       return NO_DATA;
94     }
95   }
96
97   public Object JavaDoc setData(RequestScope request, ResultMap resultMap, Object JavaDoc resultObject, Object JavaDoc[] values) {
98     if (resultPlan != null) {
99       Object JavaDoc 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 JavaDoc 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   // Bug ibatis-12
120
public Object JavaDoc setData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject, Object JavaDoc[] values) {
121     if (outParamPlan != null) {
122       Object JavaDoc object = parameterObject;
123       if (object == null) {
124         try {
125           object = Resources.instantiate(parameterMap.getParameterClass());
126         } catch (Exception JavaDoc 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 JavaDoc[] getOutputParamValues(ParameterMapping[] mappings, Object JavaDoc[] values) {
139     List JavaDoc outParamValues = new ArrayList JavaDoc();
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