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.sqlmap.engine.mapping.parameter.ParameterMap;
19  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
20  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
21  import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
22  import com.ibatis.sqlmap.engine.scope.StatementScope;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * DataExchange implementation for Map objects.
29   */
30  public class MapDataExchange extends BaseDataExchange implements DataExchange {
31  
32    /**
33     * Instantiates a new map data exchange.
34     *
35     * @param dataExchangeFactory
36     *          the data exchange factory
37     */
38    protected MapDataExchange(DataExchangeFactory dataExchangeFactory) {
39      super(dataExchangeFactory);
40    }
41  
42    @Override
43    public void initialize(Map properties) {
44    }
45  
46    @Override
47    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
48      if (!(parameterObject instanceof Map)) {
49        throw new RuntimeException("Error.  Object passed into MapDataExchange was not an instance of Map.");
50      }
51  
52      Object[] data = new Object[parameterMap.getParameterMappings().length];
53      Map map = (Map) parameterObject;
54      ParameterMapping[] mappings = parameterMap.getParameterMappings();
55      for (int i = 0; i < mappings.length; i++) {
56        data[i] = map.get(mappings[i].getPropertyName());
57      }
58      return data;
59    }
60  
61    @Override
62    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
63      if (resultObject != null && !(resultObject instanceof Map)) {
64        throw new RuntimeException("Error.  Object passed into MapDataExchange was not an instance of Map.");
65      }
66  
67      Map map = (Map) resultObject;
68      if (map == null) {
69        map = new HashMap<>();
70      }
71  
72      ResultMapping[] mappings = resultMap.getResultMappings();
73      for (int i = 0; i < mappings.length; i++) {
74        map.put(mappings[i].getPropertyName(), values[i]);
75      }
76  
77      return map;
78    }
79  
80    @Override
81    public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
82        Object[] values) {
83      if (parameterObject != null && !(parameterObject instanceof Map)) {
84        throw new RuntimeException("Error.  Object passed into MapDataExchange was not an instance of Map.");
85      }
86  
87      Map map = (Map) parameterObject;
88      if (map == null) {
89        map = new HashMap<>();
90      }
91  
92      ParameterMapping[] mappings = parameterMap.getParameterMappings();
93      for (int i = 0; i < mappings.length; i++) {
94        if (mappings[i].isOutputAllowed()) {
95          map.put(mappings[i].getPropertyName(), values[i]);
96        }
97      }
98  
99      return map;
100   }
101 
102 }