001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.exchange;
017:
018: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
019: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
020: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
021: import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
022: import com.ibatis.sqlmap.engine.scope.RequestScope;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: /**
028: * DataExchange implementation for Map objects
029: */
030: public class MapDataExchange extends BaseDataExchange implements
031: DataExchange {
032:
033: protected MapDataExchange(DataExchangeFactory dataExchangeFactory) {
034: super (dataExchangeFactory);
035: }
036:
037: public void initialize(Map properties) {
038: }
039:
040: public Object[] getData(RequestScope request,
041: ParameterMap parameterMap, Object parameterObject) {
042: if (!(parameterObject instanceof Map)) {
043: throw new RuntimeException(
044: "Error. Object passed into MapDataExchange was not an instance of Map.");
045: }
046:
047: Object[] data = new Object[parameterMap.getParameterMappings().length];
048: Map map = (Map) parameterObject;
049: ParameterMapping[] mappings = parameterMap
050: .getParameterMappings();
051: for (int i = 0; i < mappings.length; i++) {
052: data[i] = map.get(mappings[i].getPropertyName());
053: }
054: return data;
055: }
056:
057: public Object setData(RequestScope request, ResultMap resultMap,
058: Object resultObject, Object[] values) {
059: if (!(resultObject == null || resultObject instanceof Map)) {
060: throw new RuntimeException(
061: "Error. Object passed into MapDataExchange was not an instance of Map.");
062: }
063:
064: Map map = (Map) resultObject;
065: if (map == null) {
066: map = new HashMap();
067: }
068:
069: ResultMapping[] mappings = resultMap.getResultMappings();
070: for (int i = 0; i < mappings.length; i++) {
071: map.put(mappings[i].getPropertyName(), values[i]);
072: }
073:
074: return map;
075: }
076:
077: public Object setData(RequestScope request,
078: ParameterMap parameterMap, Object parameterObject,
079: Object[] values) {
080: if (!(parameterObject == null || parameterObject instanceof Map)) {
081: throw new RuntimeException(
082: "Error. Object passed into MapDataExchange was not an instance of Map.");
083: }
084:
085: Map map = (Map) parameterObject;
086: if (map == null) {
087: map = new HashMap();
088: }
089:
090: ParameterMapping[] mappings = parameterMap
091: .getParameterMappings();
092: for (int i = 0; i < mappings.length; i++) {
093: if (mappings[i].isOutputAllowed()) {
094: map.put(mappings[i].getPropertyName(), values[i]);
095: }
096: }
097:
098: return map;
099: }
100:
101: }
|