001: /*
002: * $Id: SetServiceFields.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.callops;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.minilang.SimpleMethod;
032: import org.ofbiz.minilang.method.ContextAccessor;
033: import org.ofbiz.minilang.method.MethodContext;
034: import org.ofbiz.minilang.method.MethodOperation;
035: import org.ofbiz.service.GenericServiceException;
036: import org.ofbiz.service.LocalDispatcher;
037: import org.ofbiz.service.ModelParam;
038: import org.ofbiz.service.ModelService;
039: import org.w3c.dom.Element;
040:
041: /**
042: * Sets all Service parameters/attributes in the to-map using the map as a source
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.1 $
046: * @since 2.2
047: */
048: public class SetServiceFields extends MethodOperation {
049:
050: public static final String module = CallService.class.getName();
051:
052: String serviceName;
053: ContextAccessor mapAcsr;
054: ContextAccessor toMapAcsr;
055:
056: public SetServiceFields(Element element, SimpleMethod simpleMethod) {
057: super (element, simpleMethod);
058: serviceName = element.getAttribute("service-name");
059: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
060: toMapAcsr = new ContextAccessor(element
061: .getAttribute("to-map-name"));
062: }
063:
064: public boolean exec(MethodContext methodContext) {
065: String serviceName = methodContext
066: .expandString(this .serviceName);
067:
068: Map fromMap = (Map) mapAcsr.get(methodContext);
069: if (fromMap == null) {
070: Debug.logWarning(
071: "The from map in set-service-field was not found with name: "
072: + mapAcsr, module);
073: return true;
074: }
075:
076: Map toMap = (Map) toMapAcsr.get(methodContext);
077: if (toMap == null) {
078: toMap = new HashMap();
079: toMapAcsr.put(methodContext, toMap);
080: }
081:
082: LocalDispatcher dispatcher = methodContext.getDispatcher();
083: ModelService modelService = null;
084: try {
085: modelService = dispatcher.getDispatchContext()
086: .getModelService(serviceName);
087: } catch (GenericServiceException e) {
088: String errMsg = "In set-service-fields could not get service definition for service name ["
089: + serviceName + "]: " + e.toString();
090: Debug.logError(e, errMsg, module);
091: methodContext.setErrorReturn(errMsg, simpleMethod);
092: return false;
093: }
094: Iterator inModelParamIter = modelService.getInModelParamList()
095: .iterator();
096: while (inModelParamIter.hasNext()) {
097: ModelParam modelParam = (ModelParam) inModelParamIter
098: .next();
099: if (fromMap.containsKey(modelParam.name)) {
100: toMap
101: .put(modelParam.name, fromMap
102: .get(modelParam.name));
103: }
104: }
105:
106: return true;
107: }
108: }
|