01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.workflow.impl;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import org.ofbiz.service.DispatchContext;
24: import org.ofbiz.service.GenericServiceException;
25: import org.ofbiz.service.LocalDispatcher;
26: import org.ofbiz.service.ModelService;
27: import org.ofbiz.workflow.EvaluationException;
28: import org.ofbiz.workflow.TransitionCondition;
29:
30: /**
31: * ServiceCondition - Invokes a special service for condition evaluation
32: *
33: * To call a service set a Transition ExtendedAttribute named 'serviceName', services are required
34: * to return a Boolean OUT parameter named 'evaluationResult'
35: */
36: public class ServiceCondition implements TransitionCondition {
37:
38: /**
39: * @see org.ofbiz.workflow.TransitionCondition#evaluateCondition(java.util.Map, java.util.Map, java.lang.String, org.ofbiz.service.DispatchContext)
40: */
41: public Boolean evaluateCondition(Map context, Map attrs,
42: String expression, DispatchContext dctx)
43: throws EvaluationException {
44: // get the service to call
45: String serviceName = (String) attrs.get("serviceName");
46: if (serviceName == null || serviceName.length() == 0)
47: throw new EvaluationException(
48: "Invalid serviceName; be sure to set the serviceName ExtendedAttribute");
49:
50: // get the dispatcher
51: LocalDispatcher dispatcher = dctx.getDispatcher();
52: if (dispatcher == null)
53: throw new EvaluationException(
54: "Bad LocalDispatcher found in the DispatchContext");
55:
56: // create a map of all context and extended attributes, attributes will overwrite context values
57: Map newContext = new HashMap(context);
58: newContext.putAll(attrs);
59:
60: // build the context for the service
61: Map serviceContext = null;
62: ModelService model = null;
63: try {
64: model = dctx.getModelService(serviceName);
65: serviceContext = model.makeValid(newContext,
66: ModelService.IN_PARAM);
67: } catch (GenericServiceException e) {
68: throw new EvaluationException(
69: "Cannot get ModelService object for service named: "
70: + serviceName, e);
71: }
72:
73: // invoke the service
74: Map serviceResult = null;
75: try {
76: serviceResult = dispatcher.runSync(serviceName,
77: serviceContext);
78: } catch (GenericServiceException e) {
79: throw new EvaluationException(
80: "Cannot invoke the service named: " + serviceName,
81: e);
82: }
83:
84: // get the evaluationResult object from the result
85: Boolean evaluationResult = null;
86: try {
87: evaluationResult = (Boolean) serviceResult
88: .get("evaluationResult");
89: } catch (ClassCastException e) {
90: throw new EvaluationException(
91: "Service did not return a valid evaluationResult object");
92: }
93:
94: return evaluationResult;
95: }
96:
97: }
|