1 24 package org.ofbiz.minilang.method.callops; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.GeneralException; 32 import org.ofbiz.base.util.ObjectType; 33 import org.ofbiz.base.util.UtilValidate; 34 import org.ofbiz.minilang.SimpleMethod; 35 import org.ofbiz.minilang.method.ContextAccessor; 36 import org.ofbiz.minilang.method.MethodContext; 37 import org.ofbiz.minilang.method.MethodOperation; 38 import org.ofbiz.service.GenericServiceException; 39 import org.ofbiz.service.LocalDispatcher; 40 import org.ofbiz.service.ModelParam; 41 import org.ofbiz.service.ModelService; 42 import org.w3c.dom.Element ; 43 44 51 public class SetServiceFields extends MethodOperation { 52 53 public static final String module = CallService.class.getName(); 54 55 String serviceName; 56 ContextAccessor mapAcsr; 57 ContextAccessor toMapAcsr; 58 59 public SetServiceFields(Element element, SimpleMethod simpleMethod) { 60 super(element, simpleMethod); 61 serviceName = element.getAttribute("service-name"); 62 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 63 toMapAcsr = new ContextAccessor(element.getAttribute("to-map-name")); 64 } 65 66 public boolean exec(MethodContext methodContext) { 67 String serviceName = methodContext.expandString(this.serviceName); 68 69 Map fromMap = (Map ) mapAcsr.get(methodContext); 70 if (fromMap == null) { 71 Debug.logWarning("The from map in set-service-field was not found with name: " + mapAcsr, module); 72 return true; 73 } 74 75 Map toMap = (Map ) toMapAcsr.get(methodContext); 76 if (toMap == null) { 77 toMap = new HashMap (); 78 toMapAcsr.put(methodContext, toMap); 79 } 80 81 LocalDispatcher dispatcher = methodContext.getDispatcher(); 82 ModelService modelService = null; 83 try { 84 modelService = dispatcher.getDispatchContext().getModelService(serviceName); 85 } catch (GenericServiceException e) { 86 String errMsg = "In set-service-fields could not get service definition for service name [" + serviceName + "]: " + e.toString(); 87 Debug.logError(e, errMsg, module); 88 methodContext.setErrorReturn(errMsg, simpleMethod); 89 return false; 90 } 91 Iterator inModelParamIter = modelService.getInModelParamList().iterator(); 92 while (inModelParamIter.hasNext()) { 93 ModelParam modelParam = (ModelParam) inModelParamIter.next(); 94 95 if (fromMap.containsKey(modelParam.name)) { 96 Object value = fromMap.get(modelParam.name); 97 98 if (UtilValidate.isNotEmpty(modelParam.type)) { 99 try { 100 value = ObjectType.simpleTypeConvert(value, modelParam.type, null, null, false); 101 } catch (GeneralException e) { 102 String errMsg = "Could not convert field value for the parameter/attribute: [" + modelParam.name + "] on the [" + serviceName + "] service to the [" + modelParam.type + "] type for the value [" + value + "]: " + e.toString(); 103 Debug.logError(e, errMsg, module); 104 throw new IllegalArgumentException (errMsg); 105 } 106 } 107 108 toMap.put(modelParam.name, value); 109 } 110 } 111 112 return true; 113 } 114 115 public String rawString() { 116 return "<set-service-fields/>"; 118 } 119 public String expandedString(MethodContext methodContext) { 120 return this.rawString(); 122 } 123 } 124 | Popular Tags |