KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
30
31 /**
32  * A DataExchange implemtation for working with beans
33  */

34 public class ComplexDataExchange extends BaseDataExchange implements DataExchange {
35
36   private static final Probe PROBE = ProbeFactory.getProbe();
37
38   /**
39    * Constructor for the factory
40    * @param dataExchangeFactory - the factory
41    */

42   public ComplexDataExchange(DataExchangeFactory dataExchangeFactory) {
43     super(dataExchangeFactory);
44   }
45
46   public void initialize(Map properties) {
47   }
48
49   public Object JavaDoc[] getData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject) {
50     TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
51     if (parameterObject == null) {
52       return new Object JavaDoc[1];
53     } else {
54       if (typeHandlerFactory.hasTypeHandler(parameterObject.getClass())) {
55         ParameterMapping[] mappings = parameterMap.getParameterMappings();
56         Object JavaDoc[] data = new Object JavaDoc[mappings.length];
57         for (int i = 0; i < mappings.length; i++) {
58           data[i] = parameterObject;
59         }
60         return data;
61       } else {
62         Object JavaDoc[] data = new Object JavaDoc[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 JavaDoc setData(RequestScope request, ResultMap resultMap, Object JavaDoc resultObject, Object JavaDoc[] values) {
73     TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
74     if (typeHandlerFactory.hasTypeHandler(resultMap.getResultClass())) {
75       return values[0];
76     } else {
77       Object JavaDoc object = resultObject;
78       if (object == null) {
79         try {
80           object = Resources.instantiate(resultMap.getResultClass());
81         } catch (Exception JavaDoc 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 JavaDoc setData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject, Object JavaDoc[] values) {
94     TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
95     if (typeHandlerFactory.hasTypeHandler(parameterMap.getParameterClass())) {
96       return values[0];
97     } else {
98       Object JavaDoc object = parameterObject;
99       if (object == null) {
100         try {
101           object = Resources.instantiate(parameterMap.getParameterClass());
102         } catch (Exception JavaDoc 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