KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > impl > ServiceCondition


1 /*
2  * $Id: ServiceCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 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  */

25 package org.ofbiz.workflow.impl;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.service.DispatchContext;
31 import org.ofbiz.service.GenericServiceException;
32 import org.ofbiz.service.LocalDispatcher;
33 import org.ofbiz.service.ModelService;
34 import org.ofbiz.workflow.EvaluationException;
35 import org.ofbiz.workflow.TransitionCondition;
36
37
38 /**
39  * ServiceCondition - Invokes a special service for condition evaluation
40  *
41  * To call a service set a Transition ExtendedAttribute named 'serviceName', services are required
42  * to return a Boolean OUT parameter named 'evaluationResult'
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 2.1
47  */

48 public class ServiceCondition implements TransitionCondition {
49
50     /**
51      * @see org.ofbiz.workflow.TransitionCondition#evaluateCondition(java.util.Map, java.util.Map, java.lang.String, org.ofbiz.service.DispatchContext)
52      */

53     public Boolean JavaDoc evaluateCondition(Map JavaDoc context, Map JavaDoc attrs, String JavaDoc expression, DispatchContext dctx) throws EvaluationException {
54         // get the service to call
55
String JavaDoc serviceName = (String JavaDoc) attrs.get("serviceName");
56         if (serviceName == null || serviceName.length() == 0)
57             throw new EvaluationException("Invalid serviceName; be sure to set the serviceName ExtendedAttribute");
58           
59         // get the dispatcher
60
LocalDispatcher dispatcher = dctx.getDispatcher();
61         if (dispatcher == null)
62             throw new EvaluationException("Bad LocalDispatcher found in the DispatchContext");
63         
64         // create a map of all context and extended attributes, attributes will overwrite context values
65
Map JavaDoc newContext = new HashMap JavaDoc(context);
66         newContext.putAll(attrs);
67         
68         // build the context for the service
69
Map JavaDoc serviceContext = null;
70         ModelService model = null;
71         try {
72             model = dctx.getModelService(serviceName);
73             serviceContext = model.makeValid(newContext, ModelService.IN_PARAM);
74         } catch (GenericServiceException e) {
75             throw new EvaluationException("Cannot get ModelService object for service named: " + serviceName, e);
76         }
77         
78         // invoke the service
79
Map JavaDoc serviceResult = null;
80         try {
81             serviceResult = dispatcher.runSync(serviceName, serviceContext);
82         } catch (GenericServiceException e) {
83             throw new EvaluationException("Cannot invoke the service named: " + serviceName, e);
84         }
85         
86         // get the evaluationResult object from the result
87
Boolean JavaDoc evaluationResult = null;
88         try {
89             evaluationResult = (Boolean JavaDoc) serviceResult.get("evaluationResult");
90         } catch (ClassCastException JavaDoc e) {
91             throw new EvaluationException("Service did not return a valid evaluationResult object");
92         }
93         
94         return evaluationResult;
95     }
96
97 }
98
Popular Tags