/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: ActImplBase.java 2576 2007-11-02 16:00:34Z drmlipp $
*
* $Log$
* Revision 1.4 2007/09/20 21:25:43 mlipp
* Fixed line length.
*
* Revision 1.3 2007/05/03 21:58:16 mlipp
* Internal refactoring for making better use of local EJBs.
*
* Revision 1.2 2006/09/29 12:32:08 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.5 2004/08/18 15:17:38 drmlipp
* Update to 1.2
*
* Revision 1.12 2004/03/25 15:52:45 lipp
* Ensure backward compatibility of serialzed instances.
*
* Revision 1.11 2004/03/25 14:41:47 lipp
* Added possibility to specify actual parameters as XML.
*
* Revision 1.10 2003/10/24 20:23:06 lipp
* Fixed exception handling.
*
* Revision 1.9 2003/09/25 11:01:20 lipp
* Fixed usage of jsScope (may not be used remotely).
*
* Revision 1.8 2003/09/04 08:46:44 lipp
* Fixed client dependency on rhino.jar.
*
* Revision 1.7 2003/07/02 18:44:38 lipp
* Removed inappropriate and annoying info messages.
*
* Revision 1.6 2003/07/01 15:49:55 montag
* jaxen-dom for XSLTTool.
*
* Revision 1.5 2003/06/27 08:51:45 lipp
* Fixed copyright/license information.
*
* Revision 1.4 2003/05/16 09:26:38 lipp
* Fixed actual parameter evaluation.
*
* Revision 1.3 2003/05/07 14:45:50 lipp
* Implemented synchronous subflow.
*
* Revision 1.2 2003/05/06 13:21:29 lipp
* Resolved cyclic dependency.
*
* Revision 1.1 2003/05/05 07:04:51 lipp
* Handling parameters for sub-flow now.
*
*/
package de.danet.an.workflow.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import de.danet.an.workflow.internalapi.ExtImplementationLocal;
import de.danet.an.workflow.internalapi.ExtProcessLocal;
import de.danet.an.workflow.localapi.ActivityLocal;
import de.danet.an.workflow.localapi.ProcessLocal;
import de.danet.an.workflow.omgcore.InvalidDataException;
import de.danet.an.workflow.omgcore.ProcessData;
import de.danet.an.workflow.omgcore.UpdateNotAllowedException;
import de.danet.an.workflow.api.DefaultProcessData;
import de.danet.an.workflow.api.FormalParameter;
/**
* This class provides some base functionallity for both tool and
* sub-process based activity implementations.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2576 $
*/
public abstract class ActImplBase
implements ExtImplementationLocal, Serializable {
/** Unique id. */
static final long serialVersionUID = -2129987329744599747L;
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog(ActImplBase.class);
/**
* Creates an instance of <code>ActImplBase</code>
* with all attributes initialized to default values.
*/
public ActImplBase () {
}
/**
* Return the actual parameters.
* @return actualParameters.
*/
public abstract Object[] actualParameters();
/**
* Create the parameter map for an invocation based on the
* given activity, its container and the formal parameters.
*
* @param process the process which the activity belongs to
* @param act the activity to which this tool implementation description
* is to be applied
* @param fps the formal parameters
* @return the parameter map
*/
public Map parameterMap
(ExtProcessLocal process, ActivityLocal act, FormalParameter[] fps) {
Map procDataMap = act.processContext();
Map params = new HashMap();
List evals = new ArrayList ();
List evalExprs = new ArrayList ();
for (int i = 0; i < fps.length; i++) {
if (fps[i].mode() == FormalParameter.Mode.OUT) {
params.put (fps[i].id(), null);
continue;
}
Object actParamExpr = actualParameters()[i];
Object actParam = null;
if (procDataMap.containsKey(actParamExpr)) {
actParam = procDataMap.get(actParamExpr);
logger.debug
("Value of \"" + actParamExpr + "\" for parameter \""
+ fps[i].id() + "\" of " + toString() + " called in "
+ act + " is "
+ (actParam == null ? "null" : actParam.toString()));
} else if (fps[i].mode() == FormalParameter.Mode.IN) {
evals.add (fps[i].id());
evalExprs.add (new Object[]{fps[i].type(), actParamExpr});
continue;
}
params.put (fps[i].id(), actParam);
}
Object[] evald = ((ExtProcessLocal)act.containerLocal()).evalExpressions
((Object[][])evalExprs.toArray(new Object[evalExprs.size()][]));
for (int i = 0; i < evals.size(); i++) {
Object actParam = evald[i];
if (actParam instanceof Byte) {
actParam = new Long (((Byte)actParam).longValue());
} else if (actParam instanceof Exception) {
logger.error ("Problem evaluating parameter \""
+ evalExprs.get(i) + "\": "
+ ((Exception)actParam).getMessage());
actParam = "";
}
if (logger.isDebugEnabled()) {
logger.debug
("Expression \"" + evalExprs.get(i) + "\" for parameter \""
+ evals.get(i) + "\" of " + toString() + " called in "
+ act + " evaluates to "
+ (actParam == null ? "null" : actParam.toString()));
}
params.put (evals.get(i), actParam);
}
return params;
}
/**
* Merge the result returned by an implementation into the process
* data. The names of the result data items must be formal parameter
* names which are mapped to process data items as defined by the
* actual parameters.
*
* @param process the process whose activity has invoked the implementation
* @param fps the formal parameters
* @param result the result data
* @throws InvalidDataException if the entries in the result do not
* match formal parameter names or excessive entries exist
*/
protected void mergeResult
(ProcessLocal process, FormalParameter[] fps, Map result)
throws InvalidDataException {
try {
ProcessData pd = new DefaultProcessData ();
Set items = new HashSet (result.keySet());
for (int i = 0; i < fps.length; i++) {
FormalParameter fp = fps[i];
if (fp.mode() == FormalParameter.Mode.IN) {
continue;
}
Object actParam = actualParameters()[i];
if (!result.containsKey(fp.id())) {
throw new InvalidDataException
("Result does not include out parameter \""
+ fp.id() + "\".");
}
pd.put (actParam, result.get(fp.id()));
items.remove (fp.id());
}
if (items.size() > 0) {
Iterator itr = items.iterator();
throw new InvalidDataException
("Result includes excessive parameter \""
+ (String)itr.next() + "\".");
}
process.setProcessContext (pd);
} catch (UpdateNotAllowedException e) {
// This is a bit curious, but the only possible
// mapping. And the OMG considers it "legal": "...or when
// an invalid attempt is made to update the results of an
// activity; lack of access rights might be one of those
// reasons."
throw new InvalidDataException ("Caused by: " + e.getMessage());
}
}
}