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.common.beans.Probe;
019: import com.ibatis.common.beans.ProbeFactory;
020:
021: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
022: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
023: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
024: import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
025: import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil;
026: import com.ibatis.sqlmap.engine.scope.RequestScope;
027: import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
028:
029: import java.util.Map;
030:
031: /**
032: * A DataExchange implemtation for working with beans
033: */
034: public class ComplexDataExchange extends BaseDataExchange implements
035: DataExchange {
036:
037: private static final Probe PROBE = ProbeFactory.getProbe();
038:
039: /**
040: * Constructor for the factory
041: * @param dataExchangeFactory - the factory
042: */
043: public ComplexDataExchange(DataExchangeFactory dataExchangeFactory) {
044: super (dataExchangeFactory);
045: }
046:
047: public void initialize(Map properties) {
048: }
049:
050: public Object[] getData(RequestScope request,
051: ParameterMap parameterMap, Object parameterObject) {
052: TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory()
053: .getTypeHandlerFactory();
054: if (parameterObject == null) {
055: return new Object[1];
056: } else {
057: if (typeHandlerFactory.hasTypeHandler(parameterObject
058: .getClass())) {
059: ParameterMapping[] mappings = parameterMap
060: .getParameterMappings();
061: Object[] data = new Object[mappings.length];
062: for (int i = 0; i < mappings.length; i++) {
063: data[i] = parameterObject;
064: }
065: return data;
066: } else {
067: Object[] data = new Object[parameterMap
068: .getParameterMappings().length];
069: ParameterMapping[] mappings = parameterMap
070: .getParameterMappings();
071: for (int i = 0; i < mappings.length; i++) {
072: data[i] = PROBE.getObject(parameterObject,
073: mappings[i].getPropertyName());
074: }
075: return data;
076: }
077: }
078: }
079:
080: public Object setData(RequestScope request, ResultMap resultMap,
081: Object resultObject, Object[] values) {
082: TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory()
083: .getTypeHandlerFactory();
084: if (typeHandlerFactory.hasTypeHandler(resultMap
085: .getResultClass())) {
086: return values[0];
087: } else {
088: Object object = resultObject;
089: if (object == null) {
090: try {
091: object = ResultObjectFactoryUtil
092: .createObjectThroughFactory(resultMap
093: .getResultClass());
094: } catch (Exception e) {
095: throw new RuntimeException(
096: "JavaBeansDataExchange could not instantiate result class. Cause: "
097: + e, e);
098: }
099: }
100: ResultMapping[] mappings = resultMap.getResultMappings();
101: for (int i = 0; i < mappings.length; i++) {
102: PROBE.setObject(object, mappings[i].getPropertyName(),
103: values[i]);
104: }
105: return object;
106: }
107: }
108:
109: public Object setData(RequestScope request,
110: ParameterMap parameterMap, Object parameterObject,
111: Object[] values) {
112: TypeHandlerFactory typeHandlerFactory = getDataExchangeFactory()
113: .getTypeHandlerFactory();
114: if (typeHandlerFactory.hasTypeHandler(parameterMap
115: .getParameterClass())) {
116: return values[0];
117: } else {
118: Object object = parameterObject;
119: if (object == null) {
120: try {
121: object = ResultObjectFactoryUtil
122: .createObjectThroughFactory(parameterMap
123: .getParameterClass());
124: } catch (Exception e) {
125: throw new RuntimeException(
126: "JavaBeansDataExchange could not instantiate result class. Cause: "
127: + e, e);
128: }
129: }
130: ParameterMapping[] mappings = parameterMap
131: .getParameterMappings();
132: for (int i = 0; i < mappings.length; i++) {
133: if (mappings[i].isOutputAllowed()) {
134: PROBE.setObject(object, mappings[i]
135: .getPropertyName(), values[i]);
136: }
137: }
138: return object;
139: }
140: }
141:
142: }
|