KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.exception.NestedRuntimeException;
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.RequestScope;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * DataExchange implementation for Map objects
30  */

31 public class MapDataExchange extends BaseDataExchange implements DataExchange {
32
33   protected MapDataExchange(DataExchangeFactory dataExchangeFactory) {
34     super(dataExchangeFactory);
35   }
36
37   public void initialize(Map properties) {
38   }
39
40   public Object JavaDoc[] getData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject) {
41     if (!(parameterObject instanceof Map)) {
42       throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map.");
43     }
44
45     Object JavaDoc[] data = new Object JavaDoc[parameterMap.getParameterMappings().length];
46     Map map = (Map) parameterObject;
47     ParameterMapping[] mappings = parameterMap.getParameterMappings();
48     for (int i = 0; i < mappings.length; i++) {
49       data[i] = map.get(mappings[i].getPropertyName());
50     }
51     return data;
52   }
53
54   public Object JavaDoc setData(RequestScope request, ResultMap resultMap, Object JavaDoc resultObject, Object JavaDoc[] values) {
55     if (!(resultObject == null || resultObject instanceof Map)) {
56       throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map.");
57     }
58
59     Map map = (Map) resultObject;
60     if (map == null) {
61       map = new HashMap JavaDoc();
62     }
63
64     ResultMapping[] mappings = resultMap.getResultMappings();
65     for (int i = 0; i < mappings.length; i++) {
66       map.put(mappings[i].getPropertyName(), values[i]);
67     }
68
69     return map;
70   }
71
72   public Object JavaDoc setData(RequestScope request, ParameterMap parameterMap, Object JavaDoc parameterObject, Object JavaDoc[] values) {
73     if (!(parameterObject == null || parameterObject instanceof Map)) {
74       throw new NestedRuntimeException("Error. Object passed into MapDataExchange was not an instance of Map.");
75     }
76
77     Map map = (Map) parameterObject;
78     if (map == null) {
79       map = new HashMap JavaDoc();
80     }
81
82     ParameterMapping[] mappings = parameterMap.getParameterMappings();
83     for (int i = 0; i < mappings.length; i++) {
84       if (mappings[i].isOutputAllowed()) {
85         map.put(mappings[i].getPropertyName(), values[i]);
86       }
87     }
88
89     return map;
90   }
91
92 }
93
94
95
Popular Tags