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.ProbeFactory;
19  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
20  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
21  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
22  import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
23  import com.ibatis.sqlmap.engine.scope.StatementScope;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * DataExchange implementation for List objects.
31   */
32  public class ListDataExchange extends BaseDataExchange implements DataExchange {
33  
34    /**
35     * Instantiates a new list data exchange.
36     *
37     * @param dataExchangeFactory
38     *          the data exchange factory
39     */
40    protected ListDataExchange(DataExchangeFactory dataExchangeFactory) {
41      super(dataExchangeFactory);
42    }
43  
44    @Override
45    public void initialize(Map properties) {
46    }
47  
48    @Override
49    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
50      ParameterMapping[] mappings = parameterMap.getParameterMappings();
51      Object[] data = new Object[mappings.length];
52      for (int i = 0; i < mappings.length; i++) {
53        String propName = mappings[i].getPropertyName();
54  
55        // parse on the '.' notation and get nested properties
56        String[] propertyArray = propName.split("\\.");
57  
58        if (propertyArray.length > 0) {
59          // iterate list of properties to discover values
60  
61          Object tempData = parameterObject;
62  
63          for (String element : propertyArray) {
64  
65            // is property an array reference
66            int arrayStartIndex = element.indexOf('[');
67  
68            if (arrayStartIndex == -1) {
69  
70              // is a normal property
71              tempData = ProbeFactory.getProbe().getObject(tempData, element);
72  
73            } else {
74  
75              int index = Integer.parseInt(element.substring(arrayStartIndex + 1, element.length() - 1));
76              tempData = ((List) tempData).get(index);
77  
78            }
79  
80          }
81  
82          data[i] = tempData;
83  
84        } else {
85  
86          int index = Integer.parseInt(propName.substring(propName.indexOf('[') + 1, propName.length() - 1));
87          data[i] = ((List) parameterObject).get(index);
88  
89        }
90  
91      }
92      return data;
93    }
94  
95    @Override
96    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
97      ResultMapping[] mappings = resultMap.getResultMappings();
98      List data = new ArrayList<>();
99      for (int i = 0; i < mappings.length; i++) {
100       String propName = mappings[i].getPropertyName();
101       int index = Integer.parseInt(propName.substring(1, propName.length() - 1));
102       data.set(index, values[i]);
103     }
104     return data;
105   }
106 
107   @Override
108   public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
109       Object[] values) {
110     ParameterMapping[] mappings = parameterMap.getParameterMappings();
111     List data = new ArrayList<>();
112     for (int i = 0; i < mappings.length; i++) {
113       if (mappings[i].isOutputAllowed()) {
114         String propName = mappings[i].getPropertyName();
115         int index = Integer.parseInt(propName.substring(1, propName.length() - 1));
116         data.set(index, values[i]);
117       }
118     }
119 
120     return data;
121   }
122 
123 }