/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* 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: StateMapper.java 2205 2007-01-31 22:55:36Z mlipp $
*
* $Log$
* Revision 1.4 2007/01/31 12:24:07 drmlipp
* Design revisited.
*
* Revision 1.3 2007/01/30 21:06:40 mlipp
* Fixed handling of not explicitly initialized substates.
*
* Revision 1.2 2007/01/30 11:56:14 drmlipp
* Merged Wf-XML branch.
*
* Revision 1.1.2.3 2007/01/11 10:23:52 schnelle
* Creation of StateChanged notifications.
*
* Revision 1.1.2.2 2006/12/20 14:37:59 schnelle
* Implemented Factory GetDefinition.
*
* Revision 1.1.2.1 2006/12/20 13:32:25 schnelle
* Basic implementato of GetProperties for Instance and Activity.
*
*/
package de.danet.an.workflow.clients.wfxml;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import de.danet.an.workflow.omgcore.InvalidStateException;
/**
* This class provides a mapping facility of ASAP state definitions to OMG
* state definitions and vice versa.
*
* @author Dirk Schnelle
*/
class StateMapper {
/** Mapping of ASAP states to OMG states. */
private static final Map omgStates;
/** Mapping of OMG states to ASAP states. */
private static final Map asapStates;
private static final List asapStatesList;
static {
omgStates = new java.util.HashMap();
omgStates.put("open.notrunning", "open.not_running.not_started");
omgStates.put("open.notrunning.suspended", "open.not_running.suspended");
omgStates.put("open.running", "open.running");
omgStates.put("open.running.debug.completing", "open.running.debug.completing");
omgStates.put("closed.completed", "closed.completed");
omgStates.put("closed.abnormalCompleted", "closed.aborted");
omgStates.put("closed.abnormalCompleted.terminated", "closed.terminated");
omgStates.put("closed.abnormalCompleted.aborted", "closed.aborted");
asapStates = new java.util.HashMap();
asapStates.put("open", "open");
asapStates.put("open.not_running", "open.notrunning");
asapStates.put("open.not_running.not_started", "open.notrunning");
asapStates.put("open.not_running.suspended", "open.notrunning.suspended");
asapStates.put("open.running", "open.running");
asapStates.put("open.running.running", "open.running");
asapStates.put("open.running.debug.completing", "open.running.debug.completing");
asapStates.put("closed", "closed");
asapStates.put("closed.completed", "closed.completed");
asapStates.put("closed.completed.normal", "closed.completed");
asapStates.put("closed.terminated", "closed.abnormalCompleted.terminated");
asapStates.put("closed.aborted", "closed.abnormalCompleted.aborted");
asapStatesList = new ArrayList (asapStates.keySet());
Collections.sort (asapStatesList, new Comparator () {
public int compare (Object o1, Object o2) {
return ((String)o2).length() - ((String)o1).length();
}
});
}
/**
* Retrieves the name of the ASAP state for the given OMG state.
* @param omgState name of the OMG state.
* @return name of the correspondent ASAP state.
* @throws InvalidStateException
*/
static String omg2asapState(String omgState) {
String res = (String)asapStates.get(omgState);
if (res != null) {
return res;
}
for (Iterator i = asapStatesList.iterator(); i.hasNext();) {
String parentState = (String)i.next();
if (omgState.startsWith(parentState)) {
res = (String)asapStates.get(parentState);
break;
}
}
asapStates.put(omgState, res);
return res;
}
/**
* Retrieves the name of the OMG state for the given ASAP state.
* @param asapState name of the ASAP state.
* @return name of the correspondent OMG state.
* @throws InvalidStateException
*/
static String asap2omgState(String asapState)
throws InvalidStateException {
String omgState = (String) omgStates.get(asapState);
if (omgState == null) {
throw new InvalidStateException("unkonwn state: '" + asapState
+ "'");
}
return omgState;
}
}