001: /*
002: * $Id: ServiceCondition.java,v 1.2 2003/08/19 17:45:18 jonesde Exp $
003: *
004: * Copyright (c) 2003 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: */
025: package org.ofbiz.workflow.impl;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import org.ofbiz.service.DispatchContext;
031: import org.ofbiz.service.GenericServiceException;
032: import org.ofbiz.service.LocalDispatcher;
033: import org.ofbiz.service.ModelService;
034: import org.ofbiz.workflow.EvaluationException;
035: import org.ofbiz.workflow.TransitionCondition;
036:
037: /**
038: * ServiceCondition - Invokes a special service for condition evaluation
039: *
040: * To call a service set a Transition ExtendedAttribute named 'serviceName', services are required
041: * to return a Boolean OUT parameter named 'evaluationResult'
042: *
043: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
044: * @version $Revision: 1.2 $
045: * @since 2.1
046: */
047: public class ServiceCondition implements TransitionCondition {
048:
049: /**
050: * @see org.ofbiz.workflow.TransitionCondition#evaluateCondition(java.util.Map, java.util.Map, java.lang.String, org.ofbiz.service.DispatchContext)
051: */
052: public Boolean evaluateCondition(Map context, Map attrs,
053: String expression, DispatchContext dctx)
054: throws EvaluationException {
055: // get the service to call
056: String serviceName = (String) attrs.get("serviceName");
057: if (serviceName == null || serviceName.length() == 0)
058: throw new EvaluationException(
059: "Invalid serviceName; be sure to set the serviceName ExtendedAttribute");
060:
061: // get the dispatcher
062: LocalDispatcher dispatcher = dctx.getDispatcher();
063: if (dispatcher == null)
064: throw new EvaluationException(
065: "Bad LocalDispatcher found in the DispatchContext");
066:
067: // create a map of all context and extended attributes, attributes will overwrite context values
068: Map newContext = new HashMap(context);
069: newContext.putAll(attrs);
070:
071: // build the context for the service
072: Map serviceContext = null;
073: ModelService model = null;
074: try {
075: model = dctx.getModelService(serviceName);
076: serviceContext = model.makeValid(newContext,
077: ModelService.IN_PARAM);
078: } catch (GenericServiceException e) {
079: throw new EvaluationException(
080: "Cannot get ModelService object for service named: "
081: + serviceName, e);
082: }
083:
084: // invoke the service
085: Map serviceResult = null;
086: try {
087: serviceResult = dispatcher.runSync(serviceName,
088: serviceContext);
089: } catch (GenericServiceException e) {
090: throw new EvaluationException(
091: "Cannot invoke the service named: " + serviceName,
092: e);
093: }
094:
095: // get the evaluationResult object from the result
096: Boolean evaluationResult = null;
097: try {
098: evaluationResult = (Boolean) serviceResult
099: .get("evaluationResult");
100: } catch (ClassCastException e) {
101: throw new EvaluationException(
102: "Service did not return a valid evaluationResult object");
103: }
104:
105: return evaluationResult;
106: }
107:
108: }
|