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: import com.ibatis.common.beans.ProbeFactory;
024:
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.Map;
028:
029: /**
030: * DataExchange implementation for List objects
031: */
032: public class ListDataExchange extends BaseDataExchange implements
033: DataExchange {
034:
035: protected ListDataExchange(DataExchangeFactory dataExchangeFactory) {
036: super (dataExchangeFactory);
037: }
038:
039: public void initialize(Map properties) {
040: }
041:
042: public Object[] getData(RequestScope request,
043: ParameterMap parameterMap, Object parameterObject) {
044: ParameterMapping[] mappings = parameterMap
045: .getParameterMappings();
046: Object[] data = new Object[mappings.length];
047: for (int i = 0; i < mappings.length; i++) {
048: String propName = mappings[i].getPropertyName();
049:
050: // parse on the '.' notation and get nested properties
051: String[] propertyArray = propName.split("\\.");
052:
053: if (propertyArray.length > 0) {
054: // iterate list of properties to discover values
055:
056: Object tempData = parameterObject;
057:
058: for (int x = 0; x < propertyArray.length; x++) {
059:
060: // is property an array reference
061: int arrayStartIndex = propertyArray[x].indexOf('[');
062:
063: if (arrayStartIndex == -1) {
064:
065: // is a normal property
066: tempData = ProbeFactory.getProbe().getObject(
067: tempData, propertyArray[x]);
068:
069: } else {
070:
071: int index = Integer.parseInt(propertyArray[x]
072: .substring(arrayStartIndex + 1,
073: propertyArray[x].length() - 1));
074: tempData = ((List) tempData).get(index);
075:
076: }
077:
078: }
079:
080: data[i] = tempData;
081:
082: } else {
083:
084: int index = Integer.parseInt((propName.substring(
085: propName.indexOf('[') + 1,
086: propName.length() - 1)));
087: data[i] = ((List) parameterObject).get(index);
088:
089: }
090:
091: }
092: return data;
093: }
094:
095: public Object setData(RequestScope request, ResultMap resultMap,
096: Object resultObject, Object[] values) {
097: ResultMapping[] mappings = resultMap.getResultMappings();
098: List data = new ArrayList();
099: for (int i = 0; i < mappings.length; i++) {
100: String propName = mappings[i].getPropertyName();
101: int index = Integer.parseInt((propName.substring(1,
102: propName.length() - 1)));
103: data.set(index, values[i]);
104: }
105: return data;
106: }
107:
108: public Object setData(RequestScope request,
109: ParameterMap parameterMap, Object parameterObject,
110: Object[] values) {
111: ParameterMapping[] mappings = parameterMap
112: .getParameterMappings();
113: List data = new ArrayList();
114: for (int i = 0; i < mappings.length; i++) {
115: if (mappings[i].isOutputAllowed()) {
116: String propName = mappings[i].getPropertyName();
117: int index = Integer.parseInt((propName.substring(1,
118: propName.length() - 1)));
119: data.set(index, values[i]);
120: }
121: }
122:
123: return data;
124: }
125:
126: }
|