001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.callops;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.base.util.GeneralException;
026: import org.ofbiz.base.util.ObjectType;
027: import org.ofbiz.base.util.UtilValidate;
028: import org.ofbiz.minilang.SimpleMethod;
029: import org.ofbiz.minilang.method.ContextAccessor;
030: import org.ofbiz.minilang.method.MethodContext;
031: import org.ofbiz.minilang.method.MethodOperation;
032: import org.ofbiz.service.GenericServiceException;
033: import org.ofbiz.service.LocalDispatcher;
034: import org.ofbiz.service.ModelParam;
035: import org.ofbiz.service.ModelService;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Sets all Service parameters/attributes in the to-map using the map as a source
040: */
041: public class SetServiceFields extends MethodOperation {
042:
043: public static final String module = CallService.class.getName();
044:
045: String serviceName;
046: ContextAccessor mapAcsr;
047: ContextAccessor toMapAcsr;
048:
049: public SetServiceFields(Element element, SimpleMethod simpleMethod) {
050: super (element, simpleMethod);
051: serviceName = element.getAttribute("service-name");
052: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
053: toMapAcsr = new ContextAccessor(element
054: .getAttribute("to-map-name"));
055: }
056:
057: public boolean exec(MethodContext methodContext) {
058: String serviceName = methodContext
059: .expandString(this .serviceName);
060:
061: Map fromMap = (Map) mapAcsr.get(methodContext);
062: if (fromMap == null) {
063: Debug.logWarning(
064: "The from map in set-service-field was not found with name: "
065: + mapAcsr, module);
066: return true;
067: }
068:
069: Map toMap = (Map) toMapAcsr.get(methodContext);
070: if (toMap == null) {
071: toMap = new HashMap();
072: toMapAcsr.put(methodContext, toMap);
073: }
074:
075: LocalDispatcher dispatcher = methodContext.getDispatcher();
076: ModelService modelService = null;
077: try {
078: modelService = dispatcher.getDispatchContext()
079: .getModelService(serviceName);
080: } catch (GenericServiceException e) {
081: String errMsg = "In set-service-fields could not get service definition for service name ["
082: + serviceName + "]: " + e.toString();
083: Debug.logError(e, errMsg, module);
084: methodContext.setErrorReturn(errMsg, simpleMethod);
085: return false;
086: }
087: Iterator inModelParamIter = modelService.getInModelParamList()
088: .iterator();
089: while (inModelParamIter.hasNext()) {
090: ModelParam modelParam = (ModelParam) inModelParamIter
091: .next();
092:
093: if (fromMap.containsKey(modelParam.name)) {
094: Object value = fromMap.get(modelParam.name);
095:
096: if (UtilValidate.isNotEmpty(modelParam.type)) {
097: try {
098: value = ObjectType.simpleTypeConvert(value,
099: modelParam.type, null, null, false);
100: } catch (GeneralException e) {
101: String errMsg = "Could not convert field value for the parameter/attribute: ["
102: + modelParam.name
103: + "] on the ["
104: + serviceName
105: + "] service to the ["
106: + modelParam.type
107: + "] type for the value ["
108: + value
109: + "]: " + e.toString();
110: Debug.logError(e, errMsg, module);
111: throw new IllegalArgumentException(errMsg);
112: }
113: }
114:
115: toMap.put(modelParam.name, value);
116: }
117: }
118:
119: return true;
120: }
121:
122: public String rawString() {
123: // TODO: something more than the empty tag
124: return "<set-service-fields/>";
125: }
126:
127: public String expandedString(MethodContext methodContext) {
128: // TODO: something more than a stub/dummy
129: return this.rawString();
130: }
131: }
|