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.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    public void initialize(Map properties) {
45    }
46  
47    public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
48      ParameterMapping[] mappings = parameterMap.getParameterMappings();
49      Object[] data = new Object[mappings.length];
50      for (int i = 0; i < mappings.length; i++) {
51        String propName = mappings[i].getPropertyName();
52  
53        // parse on the '.' notation and get nested properties
54        String[] propertyArray = propName.split("\\.");
55  
56        if (propertyArray.length > 0) {
57          // iterate list of properties to discover values
58  
59          Object tempData = parameterObject;
60  
61          for (int x = 0; x < propertyArray.length; x++) {
62  
63            // is property an array reference
64            int arrayStartIndex = propertyArray[x].indexOf('[');
65  
66            if (arrayStartIndex == -1) {
67  
68              // is a normal property
69              tempData = ProbeFactory.getProbe().getObject(tempData, propertyArray[x]);
70  
71            } else {
72  
73              int index = Integer
74                  .parseInt(propertyArray[x].substring(arrayStartIndex + 1, propertyArray[x].length() - 1));
75              tempData = ((List) tempData).get(index);
76  
77            }
78  
79          }
80  
81          data[i] = tempData;
82  
83        } else {
84  
85          int index = Integer.parseInt((propName.substring(propName.indexOf('[') + 1, propName.length() - 1)));
86          data[i] = ((List) parameterObject).get(index);
87  
88        }
89  
90      }
91      return data;
92    }
93  
94    public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
95      ResultMapping[] mappings = resultMap.getResultMappings();
96      List data = new ArrayList();
97      for (int i = 0; i < mappings.length; i++) {
98        String propName = mappings[i].getPropertyName();
99        int index = Integer.parseInt((propName.substring(1, propName.length() - 1)));
100       data.set(index, values[i]);
101     }
102     return data;
103   }
104 
105   public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
106       Object[] values) {
107     ParameterMapping[] mappings = parameterMap.getParameterMappings();
108     List data = new ArrayList();
109     for (int i = 0; i < mappings.length; i++) {
110       if (mappings[i].isOutputAllowed()) {
111         String propName = mappings[i].getPropertyName();
112         int index = Integer.parseInt((propName.substring(1, propName.length() - 1)));
113         data.set(index, values[i]);
114       }
115     }
116 
117     return data;
118   }
119 
120 }