View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
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    *    https://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.sqlmap.engine.mapping.parameter.ParameterMap;
21  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
22  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
23  import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
24  import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil;
25  import com.ibatis.sqlmap.engine.scope.StatementScope;
26  import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
27  
28  import java.util.Map;
29  
30  /**
31   * A DataExchange implemtation for working with beans.
32   */
33  public class ComplexDataExchange extends BaseDataExchange implements DataExchange {
34  
35    /** The Constant PROBE. */
36    private static final Probe PROBE = ProbeFactory.getProbe();
37  
38    /**
39     * Constructor for the factory.
40     *
41     * @param dataExchangeFactory
42     *          - the factory
43     */
44    public ComplexDataExchange(DataExchangeFactory dataExchangeFactory) {
45      super(dataExchangeFactory);
46    }
47  
48    public void initialize(Map properties) {
49    }
50  
51    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
52      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
53      if (parameterObject == null) {
54        return new Object[1];
55      } else {
56        if (typeHandlerFactory.hasTypeHandler(parameterObject.getClass())) {
57          ParameterMapping[] mappings = parameterMap.getParameterMappings();
58          Object[] data = new Object[mappings.length];
59          for (int i = 0; i < mappings.length; i++) {
60            data[i] = parameterObject;
61          }
62          return data;
63        } else {
64          Object[] data = new Object[parameterMap.getParameterMappings().length];
65          ParameterMapping[] mappings = parameterMap.getParameterMappings();
66          for (int i = 0; i < mappings.length; i++) {
67            data[i] = PROBE.getObject(parameterObject, mappings[i].getPropertyName());
68          }
69          return data;
70        }
71      }
72    }
73  
74    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
75      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
76      if (typeHandlerFactory.hasTypeHandler(resultMap.getResultClass())) {
77        return values[0];
78      } else {
79        Object object = resultObject;
80        if (object == null) {
81          try {
82            object = ResultObjectFactoryUtil.createObjectThroughFactory(resultMap.getResultClass());
83          } catch (Exception e) {
84            throw new RuntimeException("JavaBeansDataExchange could not instantiate result class.  Cause: " + e, e);
85          }
86        }
87        ResultMapping[] mappings = resultMap.getResultMappings();
88        for (int i = 0; i < mappings.length; i++) {
89          PROBE.setObject(object, mappings[i].getPropertyName(), values[i]);
90        }
91        return object;
92      }
93    }
94  
95    public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
96        Object[] values) {
97      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
98      if (typeHandlerFactory.hasTypeHandler(parameterMap.getParameterClass())) {
99        return values[0];
100     } else {
101       Object object = parameterObject;
102       if (object == null) {
103         try {
104           object = ResultObjectFactoryUtil.createObjectThroughFactory(parameterMap.getParameterClass());
105         } catch (Exception e) {
106           throw new RuntimeException("JavaBeansDataExchange could not instantiate result class.  Cause: " + e, e);
107         }
108       }
109       ParameterMapping[] mappings = parameterMap.getParameterMappings();
110       for (int i = 0; i < mappings.length; i++) {
111         if (mappings[i].isOutputAllowed()) {
112           PROBE.setObject(object, mappings[i].getPropertyName(), values[i]);
113         }
114       }
115       return object;
116     }
117   }
118 
119 }