001: /*
002: * $Id: CallService.java,v 1.2 2003/09/14 05:40:41 jonesde 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.LinkedList;
029: import java.util.List;
030: import java.util.Locale;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.FlexibleServletAccessor;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.base.util.UtilXml;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.minilang.SimpleMethod;
039: import org.ofbiz.minilang.method.ContextAccessor;
040: import org.ofbiz.minilang.method.MethodContext;
041: import org.ofbiz.minilang.method.MethodOperation;
042: import org.ofbiz.service.GenericServiceException;
043: import org.ofbiz.service.ModelService;
044: import org.ofbiz.service.ServiceUtil;
045: import org.w3c.dom.Element;
046:
047: /**
048: * Calls a service using the given parameters
049: *
050: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
051: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
052: * @version $Revision: 1.2 $
053: * @since 2.0
054: */
055: public class CallService extends MethodOperation {
056:
057: public static final String module = CallService.class.getName();
058:
059: String serviceName;
060: ContextAccessor inMapAcsr;
061: String includeUserLoginStr;
062: String breakOnErrorStr;
063: String errorCode;
064: String successCode;
065:
066: FlexibleMessage errorPrefix;
067: FlexibleMessage errorSuffix;
068: FlexibleMessage successPrefix;
069: FlexibleMessage successSuffix;
070: FlexibleMessage messagePrefix;
071: FlexibleMessage messageSuffix;
072: FlexibleMessage defaultMessage;
073:
074: /** A list of strings with names of new maps to create */
075: List resultsToMap = new LinkedList();
076:
077: /** A list of ResultToFieldDef objects */
078: List resultToField = new LinkedList();
079:
080: /** the key is the request attribute name, the value is the result name to get */
081: Map resultToRequest = new HashMap();
082:
083: /** the key is the session attribute name, the value is the result name to get */
084: Map resultToSession = new HashMap();
085:
086: /** the key is the result entry name, the value is the result name to get */
087: Map resultToResult = new HashMap();
088:
089: public CallService(Element element, SimpleMethod simpleMethod) {
090: super (element, simpleMethod);
091: serviceName = element.getAttribute("service-name");
092: inMapAcsr = new ContextAccessor(element
093: .getAttribute("in-map-name"));
094: includeUserLoginStr = element
095: .getAttribute("include-user-login");
096: breakOnErrorStr = element.getAttribute("break-on-error");
097: errorCode = element.getAttribute("error-code");
098: if (errorCode == null || errorCode.length() == 0)
099: errorCode = "error";
100:
101: successCode = element.getAttribute("success-code");
102: if (successCode == null || successCode.length() == 0)
103: successCode = "success";
104:
105: errorPrefix = new FlexibleMessage(UtilXml.firstChildElement(
106: element, "error-prefix"), "service.error.prefix");
107: errorSuffix = new FlexibleMessage(UtilXml.firstChildElement(
108: element, "error-suffix"), "service.error.suffix");
109: successPrefix = new FlexibleMessage(UtilXml.firstChildElement(
110: element, "success-prefix"), "service.success.prefix");
111: successSuffix = new FlexibleMessage(UtilXml.firstChildElement(
112: element, "success-suffix"), "service.success.suffix");
113: messagePrefix = new FlexibleMessage(UtilXml.firstChildElement(
114: element, "message-prefix"), "service.message.prefix");
115: messageSuffix = new FlexibleMessage(UtilXml.firstChildElement(
116: element, "message-suffix"), "service.message.suffix");
117: defaultMessage = new FlexibleMessage(UtilXml.firstChildElement(
118: element, "default-message"), "service.default.message");
119:
120: List resultsToMapElements = UtilXml.childElementList(element,
121: "results-to-map");
122: if (resultsToMapElements != null
123: && resultsToMapElements.size() > 0) {
124: Iterator iter = resultsToMapElements.iterator();
125: while (iter.hasNext()) {
126: Element resultsToMapElement = (Element) iter.next();
127:
128: resultsToMap.add(resultsToMapElement
129: .getAttribute("map-name"));
130: }
131: }
132:
133: List resultToFieldElements = UtilXml.childElementList(element,
134: "result-to-field");
135: if (resultToFieldElements != null
136: && resultToFieldElements.size() > 0) {
137: Iterator iter = resultToFieldElements.iterator();
138: while (iter.hasNext()) {
139: Element resultToFieldElement = (Element) iter.next();
140: ResultToFieldDef rtfDef = new ResultToFieldDef();
141:
142: rtfDef.resultName = resultToFieldElement
143: .getAttribute("result-name");
144: rtfDef.mapAcsr = new ContextAccessor(
145: resultToFieldElement.getAttribute("map-name"));
146: rtfDef.fieldAcsr = new ContextAccessor(
147: resultToFieldElement.getAttribute("field-name"),
148: rtfDef.resultName);
149:
150: resultToField.add(rtfDef);
151: }
152: }
153:
154: // get result-to-request and result-to-session sub-ops
155: List resultToRequestElements = UtilXml.childElementList(
156: element, "result-to-request");
157: if (resultToRequestElements != null
158: && resultToRequestElements.size() > 0) {
159: Iterator iter = resultToRequestElements.iterator();
160: while (iter.hasNext()) {
161: Element resultToRequestElement = (Element) iter.next();
162: FlexibleServletAccessor reqAcsr = new FlexibleServletAccessor(
163: resultToRequestElement
164: .getAttribute("request-name"),
165: resultToRequestElement
166: .getAttribute("result-name"));
167: ContextAccessor resultAcsr = new ContextAccessor(
168: resultToRequestElement
169: .getAttribute("result-name"));
170: resultToRequest.put(reqAcsr, resultAcsr);
171: }
172: }
173:
174: List resultToSessionElements = UtilXml.childElementList(
175: element, "result-to-session");
176: if (resultToSessionElements != null
177: && resultToSessionElements.size() > 0) {
178: Iterator iter = resultToSessionElements.iterator();
179: while (iter.hasNext()) {
180: Element resultToSessionElement = (Element) iter.next();
181: FlexibleServletAccessor sesAcsr = new FlexibleServletAccessor(
182: resultToSessionElement
183: .getAttribute("session-name"),
184: resultToSessionElement
185: .getAttribute("result-name"));
186: ContextAccessor resultAcsr = new ContextAccessor(
187: resultToSessionElement
188: .getAttribute("result-name"));
189: resultToSession.put(sesAcsr, resultAcsr);
190: }
191: }
192:
193: List resultToResultElements = UtilXml.childElementList(element,
194: "result-to-result");
195: if (resultToResultElements != null
196: && resultToResultElements.size() > 0) {
197: Iterator iter = resultToResultElements.iterator();
198: while (iter.hasNext()) {
199: Element resultToResultElement = (Element) iter.next();
200: ContextAccessor serResAcsr = new ContextAccessor(
201: resultToResultElement
202: .getAttribute("service-result-name"),
203: resultToResultElement
204: .getAttribute("result-name"));
205: ContextAccessor resultAcsr = new ContextAccessor(
206: resultToResultElement
207: .getAttribute("result-name"));
208: resultToResult.put(serResAcsr, resultAcsr);
209: }
210: }
211: }
212:
213: public boolean exec(MethodContext methodContext) {
214: boolean includeUserLogin = !"false".equals(methodContext
215: .expandString(includeUserLoginStr));
216: boolean breakOnError = !"false".equals(methodContext
217: .expandString(breakOnErrorStr));
218:
219: String serviceName = methodContext
220: .expandString(this .serviceName);
221: String errorCode = methodContext.expandString(this .errorCode);
222: String successCode = methodContext
223: .expandString(this .successCode);
224:
225: Map inMap = null;
226: if (inMapAcsr.isEmpty()) {
227: inMap = new HashMap();
228: } else {
229: inMap = (Map) inMapAcsr.get(methodContext);
230: if (inMap == null) {
231: inMap = new HashMap();
232: inMapAcsr.put(methodContext, inMap);
233: }
234: }
235:
236: // before invoking the service, clear messages
237: if (methodContext.getMethodType() == MethodContext.EVENT) {
238: methodContext.removeEnv(simpleMethod
239: .getEventErrorMessageName());
240: methodContext.removeEnv(simpleMethod
241: .getEventEventMessageName());
242: methodContext.removeEnv(simpleMethod
243: .getEventResponseCodeName());
244: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
245: methodContext.removeEnv(simpleMethod
246: .getServiceErrorMessageName());
247: methodContext.removeEnv(simpleMethod
248: .getServiceSuccessMessageName());
249: methodContext.removeEnv(simpleMethod
250: .getServiceResponseMessageName());
251: }
252:
253: // invoke the service
254: Map result = null;
255:
256: // add UserLogin to context if expected
257: if (includeUserLogin) {
258: GenericValue userLogin = methodContext.getUserLogin();
259:
260: if (userLogin != null) {
261: inMap.put("userLogin", userLogin);
262: }
263: }
264:
265: // always add Locale to context unless null
266: Locale locale = methodContext.getLocale();
267: if (locale != null) {
268: inMap.put("locale", locale);
269: }
270:
271: try {
272: result = methodContext.getDispatcher().runSync(serviceName,
273: inMap);
274: } catch (GenericServiceException e) {
275: Debug.logError(e, module);
276: String errMsg = "ERROR: Could not complete the "
277: + simpleMethod.getShortDescription()
278: + " process [problem invoking the [" + serviceName
279: + "] service with the map named [" + inMapAcsr
280: + "] containing [" + inMap + "]: " + e.getMessage()
281: + "]";
282: if (methodContext.getMethodType() == MethodContext.EVENT) {
283: methodContext.putEnv(simpleMethod
284: .getEventErrorMessageName(), errMsg);
285: methodContext.putEnv(simpleMethod
286: .getEventResponseCodeName(), errorCode);
287: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
288: methodContext.putEnv(simpleMethod
289: .getServiceErrorMessageName(), errMsg);
290: methodContext.putEnv(simpleMethod
291: .getServiceResponseMessageName(), errorCode);
292: }
293: return false;
294: }
295:
296: if (resultsToMap.size() > 0) {
297: Iterator iter = resultsToMap.iterator();
298: while (iter.hasNext()) {
299: String mapName = (String) iter.next();
300: methodContext.putEnv(mapName, new HashMap(result));
301: }
302: }
303:
304: if (resultToField.size() > 0) {
305: Iterator iter = resultToField.iterator();
306: while (iter.hasNext()) {
307: ResultToFieldDef rtfDef = (ResultToFieldDef) iter
308: .next();
309: if (!rtfDef.mapAcsr.isEmpty()) {
310: Map tempMap = (Map) rtfDef.mapAcsr
311: .get(methodContext);
312: if (tempMap == null) {
313: tempMap = new HashMap();
314: rtfDef.mapAcsr.put(methodContext, tempMap);
315: }
316: rtfDef.fieldAcsr.put(tempMap, result
317: .get(rtfDef.resultName), methodContext);
318: } else {
319: rtfDef.fieldAcsr.put(methodContext, result
320: .get(rtfDef.resultName));
321: }
322: }
323: }
324:
325: // only run this if it is in an EVENT context
326: if (methodContext.getMethodType() == MethodContext.EVENT) {
327: if (resultToRequest.size() > 0) {
328: Iterator iter = resultToRequest.entrySet().iterator();
329: while (iter.hasNext()) {
330: Map.Entry entry = (Map.Entry) iter.next();
331: FlexibleServletAccessor requestAcsr = (FlexibleServletAccessor) entry
332: .getKey();
333: ContextAccessor resultAcsr = (ContextAccessor) entry
334: .getValue();
335: requestAcsr.put(methodContext.getRequest(),
336: resultAcsr.get(result, methodContext),
337: methodContext.getEnvMap());
338: }
339: }
340:
341: if (resultToSession.size() > 0) {
342: Iterator iter = resultToSession.entrySet().iterator();
343: while (iter.hasNext()) {
344: Map.Entry entry = (Map.Entry) iter.next();
345: FlexibleServletAccessor sessionAcsr = (FlexibleServletAccessor) entry
346: .getKey();
347: ContextAccessor resultAcsr = (ContextAccessor) entry
348: .getValue();
349: sessionAcsr.put(methodContext.getRequest()
350: .getSession(), resultAcsr.get(result,
351: methodContext), methodContext.getEnvMap());
352: }
353: }
354: }
355:
356: // only run this if it is in an SERVICE context
357: if (methodContext.getMethodType() == MethodContext.SERVICE) {
358: if (resultToResult.size() > 0) {
359: Iterator iter = resultToResult.entrySet().iterator();
360: while (iter.hasNext()) {
361: Map.Entry entry = (Map.Entry) iter.next();
362: ContextAccessor fromAcsr = (ContextAccessor) entry
363: .getKey();
364: ContextAccessor resultAcsr = (ContextAccessor) entry
365: .getValue();
366: fromAcsr.put(methodContext.getResults(), resultAcsr
367: .get(result, methodContext), methodContext);
368: }
369: }
370: }
371:
372: String errorPrefixStr = errorPrefix.getMessage(methodContext
373: .getLoader(), methodContext);
374: String errorSuffixStr = errorSuffix.getMessage(methodContext
375: .getLoader(), methodContext);
376: String successPrefixStr = successPrefix.getMessage(
377: methodContext.getLoader(), methodContext);
378: String successSuffixStr = successSuffix.getMessage(
379: methodContext.getLoader(), methodContext);
380: String messagePrefixStr = messagePrefix.getMessage(
381: methodContext.getLoader(), methodContext);
382: String messageSuffixStr = messageSuffix.getMessage(
383: methodContext.getLoader(), methodContext);
384:
385: String errorMessage = ServiceUtil.makeErrorMessage(result,
386: messagePrefixStr, messageSuffixStr, errorPrefixStr,
387: errorSuffixStr);
388: if (UtilValidate.isNotEmpty(errorMessage)) {
389: errorMessage += " calling service " + serviceName + " in "
390: + simpleMethod.getMethodName();
391: if (methodContext.getMethodType() == MethodContext.EVENT) {
392: methodContext.putEnv(simpleMethod
393: .getEventErrorMessageName(), errorMessage);
394: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
395: methodContext.putEnv(simpleMethod
396: .getServiceErrorMessageName(), errorMessage);
397: }
398: }
399:
400: String successMessage = ServiceUtil.makeSuccessMessage(result,
401: messagePrefixStr, messageSuffixStr, successPrefixStr,
402: successSuffixStr);
403: if (UtilValidate.isNotEmpty(successMessage)) {
404: if (methodContext.getMethodType() == MethodContext.EVENT) {
405: methodContext.putEnv(simpleMethod
406: .getEventEventMessageName(), successMessage);
407: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
408: methodContext
409: .putEnv(simpleMethod
410: .getServiceSuccessMessageName(),
411: successMessage);
412: }
413: }
414:
415: String defaultMessageStr = defaultMessage.getMessage(
416: methodContext.getLoader(), methodContext);
417: if (UtilValidate.isEmpty(errorMessage)
418: && UtilValidate.isEmpty(successMessage)
419: && UtilValidate.isNotEmpty(defaultMessageStr)) {
420: if (methodContext.getMethodType() == MethodContext.EVENT) {
421: methodContext.putEnv(simpleMethod
422: .getEventEventMessageName(), defaultMessageStr);
423: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
424: methodContext.putEnv(simpleMethod
425: .getServiceSuccessMessageName(),
426: defaultMessageStr);
427: }
428: }
429:
430: // handle the result
431: String responseCode = result
432: .containsKey(ModelService.RESPONSE_MESSAGE) ? (String) result
433: .get(ModelService.RESPONSE_MESSAGE)
434: : successCode;
435: if (methodContext.getMethodType() == MethodContext.EVENT) {
436: methodContext.putEnv(simpleMethod
437: .getEventResponseCodeName(), responseCode);
438: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
439: methodContext.putEnv(simpleMethod
440: .getServiceResponseMessageName(), responseCode);
441: }
442:
443: if (errorCode.equals(responseCode) && breakOnError) {
444: return false;
445: } else {
446: return true;
447: }
448: }
449:
450: public static class ResultToFieldDef {
451: public String resultName;
452: public ContextAccessor mapAcsr;
453: public ContextAccessor fieldAcsr;
454: }
455: }
|