KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > SetServiceFields


1 /*
2  * $Id: SetServiceFields.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.callops;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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 JavaDoc;
43
44 /**
45  * Sets all Service parameters/attributes in the to-map using the map as a source
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 2.2
50  */

51 public class SetServiceFields extends MethodOperation {
52     
53     public static final String JavaDoc module = CallService.class.getName();
54     
55     String JavaDoc serviceName;
56     ContextAccessor mapAcsr;
57     ContextAccessor toMapAcsr;
58
59     public SetServiceFields(Element JavaDoc 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 JavaDoc serviceName = methodContext.expandString(this.serviceName);
68
69         Map JavaDoc fromMap = (Map JavaDoc) 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 JavaDoc toMap = (Map JavaDoc) toMapAcsr.get(methodContext);
76         if (toMap == null) {
77             toMap = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc inModelParamIter = modelService.getInModelParamList().iterator();
92         while (inModelParamIter.hasNext()) {
93             ModelParam modelParam = (ModelParam) inModelParamIter.next();
94             
95             if (fromMap.containsKey(modelParam.name)) {
96                 Object JavaDoc 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 JavaDoc 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 JavaDoc(errMsg);
105                     }
106                 }
107                 
108                 toMap.put(modelParam.name, value);
109             }
110         }
111         
112         return true;
113     }
114
115     public String JavaDoc rawString() {
116         // TODO: something more than the empty tag
117
return "<set-service-fields/>";
118     }
119     public String JavaDoc expandedString(MethodContext methodContext) {
120         // TODO: something more than a stub/dummy
121
return this.rawString();
122     }
123 }
124
Popular Tags