KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > exchange > ListDataExchange


1 /*
2  * Copyright 2004 Clinton Begin
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  * http://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.RequestScope;
23 import com.ibatis.common.beans.ProbeFactory;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * DataExchange implementation for List objects
31  */

32 public class ListDataExchange extends BaseDataExchange implements DataExchange {
33
34   protected ListDataExchange(DataExchangeFactory dataExchangeFactory) {
35     super(dataExchangeFactory);
36   }
37
38   public void initialize(Map properties) {
39   }
40
41   public Object JavaDoc[] getData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject) {
42     ParameterMapping[] mappings = parameterMap.getParameterMappings();
43     Object JavaDoc[] data = new Object JavaDoc[mappings.length];
44     for (int i = 0; i < mappings.length; i++) {
45       String JavaDoc propName = mappings[i].getPropertyName();
46
47       // parse on the '.' notation and get nested properties
48
String JavaDoc[] propertyArray = propName.split("\\.");
49
50       if(propertyArray.length > 0) {
51         // iterate list of properties to discover values
52

53         Object JavaDoc tempData = parameterObject;
54
55         for(int x=0; x<propertyArray.length; x++) {
56
57           // is property an array reference
58
int arrayStartIndex = propertyArray[x].indexOf('[');
59
60           if(arrayStartIndex == -1) {
61
62             // is a normal property
63
tempData = ProbeFactory.getProbe().getObject(tempData,propertyArray[x]);
64
65           } else {
66
67             int index = Integer.parseInt(propertyArray[x].substring(arrayStartIndex + 1, propertyArray[x].length() - 1));
68             tempData = ((List JavaDoc) tempData).get(index);
69
70           }
71
72         }
73
74         data[i] = tempData;
75
76       } else {
77
78         int index = Integer.parseInt((propName.substring(propName.indexOf('[') + 1, propName.length() - 1)));
79         data[i] = ((List JavaDoc) parameterObject).get(index);
80
81       }
82
83     }
84     return data;
85   }
86
87   public Object JavaDoc setData(RequestScope request, ResultMap resultMap, Object JavaDoc resultObject, Object JavaDoc[] values) {
88     ResultMapping[] mappings = resultMap.getResultMappings();
89     List JavaDoc data = new ArrayList JavaDoc();
90     for (int i = 0; i < mappings.length; i++) {
91       String JavaDoc propName = mappings[i].getPropertyName();
92       int index = Integer.parseInt((propName.substring(1, propName.length() - 1)));
93       data.set(index, values[i]);
94     }
95     return data;
96   }
97
98   public Object JavaDoc setData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject, Object JavaDoc[] values) {
99     ParameterMapping[] mappings = parameterMap.getParameterMappings();
100     List JavaDoc data = new ArrayList JavaDoc();
101     for (int i = 0; i < mappings.length; i++) {
102       if (mappings[i].isOutputAllowed()) {
103         String JavaDoc propName = mappings[i].getPropertyName();
104         int index = Integer.parseInt((propName.substring(1, propName.length() - 1)));
105         data.set(index, values[i]);
106       }
107     }
108
109     return data;
110   }
111
112 }
113
Popular Tags