View Javadoc
1   /*
2    * Copyright 2004-2025 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    @Override
49    public void initialize(Map properties) {
50    }
51  
52    @Override
53    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
54      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
55      if (parameterObject == null) {
56        return new Object[1];
57      }
58      if (typeHandlerFactory.hasTypeHandler(parameterObject.getClass())) {
59        ParameterMapping[] mappings = parameterMap.getParameterMappings();
60        Object[] data = new Object[mappings.length];
61        for (int i = 0; i < mappings.length; i++) {
62          data[i] = parameterObject;
63        }
64        return data;
65      }
66      Object[] data = new Object[parameterMap.getParameterMappings().length];
67      ParameterMapping[] mappings = parameterMap.getParameterMappings();
68      for (int i = 0; i < mappings.length; i++) {
69        data[i] = PROBE.getObject(parameterObject, mappings[i].getPropertyName());
70      }
71      return data;
72    }
73  
74    @Override
75    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
76      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
77      if (typeHandlerFactory.hasTypeHandler(resultMap.getResultClass())) {
78        return values[0];
79      }
80      Object object = resultObject;
81      if (object == null) {
82        try {
83          object = ResultObjectFactoryUtil.createObjectThroughFactory(resultMap.getResultClass());
84        } catch (Exception e) {
85          throw new RuntimeException("JavaBeansDataExchange could not instantiate result class.  Cause: " + e, e);
86        }
87      }
88      ResultMapping[] mappings = resultMap.getResultMappings();
89      for (int i = 0; i < mappings.length; i++) {
90        PROBE.setObject(object, mappings[i].getPropertyName(), values[i]);
91      }
92      return object;
93    }
94  
95    @Override
96    public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
97        Object[] values) {
98      TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory().getTypeHandlerFactory();
99      if (typeHandlerFactory.hasTypeHandler(parameterMap.getParameterClass())) {
100       return values[0];
101     }
102     Object object = parameterObject;
103     if (object == null) {
104       try {
105         object = ResultObjectFactoryUtil.createObjectThroughFactory(parameterMap.getParameterClass());
106       } catch (Exception e) {
107         throw new RuntimeException("JavaBeansDataExchange could not instantiate result class.  Cause: " + e, e);
108       }
109     }
110     ParameterMapping[] mappings = parameterMap.getParameterMappings();
111     for (int i = 0; i < mappings.length; i++) {
112       if (mappings[i].isOutputAllowed()) {
113         PROBE.setObject(object, mappings[i].getPropertyName(), values[i]);
114       }
115     }
116     return object;
117   }
118 
119 }