You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2003/06/06 15:35:18 UTC
cvs commit: cocoon-lenya/src/java/org/apache/lenya/workflow/impl WorkflowInstanceImpl.java WorkflowBuilder.java
andreas 2003/06/06 06:35:18
Modified: src/java/org/apache/lenya/cms/workflow WorkflowDocument.java
WorkflowFactory.java
src/java/org/apache/lenya/workflow WorkflowInstance.java
src/java/org/apache/lenya/workflow/impl
WorkflowInstanceImpl.java WorkflowBuilder.java
Log:
added workflow history handling
Revision Changes Path
1.4 +11 -108 cocoon-lenya/src/java/org/apache/lenya/cms/workflow/WorkflowDocument.java
Index: WorkflowDocument.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/workflow/WorkflowDocument.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WorkflowDocument.java 4 Jun 2003 09:13:10 -0000 1.3
+++ WorkflowDocument.java 6 Jun 2003 13:35:17 -0000 1.4
@@ -43,22 +43,12 @@
*/
package org.apache.lenya.cms.workflow;
-import java.io.File;
-
import org.apache.lenya.cms.publication.Document;
-import org.apache.lenya.cms.publication.DocumentType;
import org.apache.lenya.workflow.Event;
import org.apache.lenya.workflow.Situation;
-import org.apache.lenya.workflow.State;
import org.apache.lenya.workflow.WorkflowException;
-import org.apache.lenya.workflow.impl.EventImpl;
-import org.apache.lenya.workflow.impl.StateImpl;
-import org.apache.lenya.workflow.impl.WorkflowBuilder;
import org.apache.lenya.workflow.impl.WorkflowImpl;
import org.apache.lenya.workflow.impl.WorkflowInstanceImpl;
-import org.apache.lenya.xml.DocumentHelper;
-import org.apache.lenya.xml.NamespaceHelper;
-import org.w3c.dom.Element;
/**
* @author andreas
@@ -71,66 +61,10 @@
protected WorkflowDocument(Document document) throws WorkflowException {
assert document != null;
this.document = document;
-
- DocumentType type;
- org.w3c.dom.Document xmlDocument;
- File historyFile = getHistoryFile(document);
- if (!historyFile.exists()) {
- throw new WorkflowException(
- "There is no workflow assigned to this document " +
- "(history file does not exist)!");
- }
-
- try {
- xmlDocument = DocumentHelper.readDocument(historyFile);
- String documentTypeName =
- xmlDocument.getDocumentElement().getAttribute(DOCTYPE_ATTRIBUTE);
- assert documentTypeName != null;
- type = new DocumentType(documentTypeName);
- } catch (Exception e) {
- throw new WorkflowException(e);
- }
-
- WorkflowImpl workflow
- = (WorkflowImpl) WorkflowFactory.buildWorkflow(document.getPublication(), type);
- setWorkflow(workflow);
-
- // initialize instance state from last version element
- NamespaceHelper helper =
- new NamespaceHelper(
- WorkflowBuilder.NAMESPACE,
- WorkflowBuilder.DEFAULT_PREFIX,
- xmlDocument);
-
- Element versionElements[] = helper.getChildren(xmlDocument.getDocumentElement(), VERSION_ELEMENT);
- if (versionElements.length > 0) {
- Element lastElement = versionElements[versionElements.length - 1];
- String stateId = lastElement.getAttribute(STATE_ATTRIBUTE);
- State state;
- try {
- state = getState(stateId);
- } catch (WorkflowException e) {
- throw new WorkflowException(e);
- }
- setCurrentState(state);
- }
-
}
private Document document;
- public static final String HISTORY_PATH = "history".replace('/', File.separatorChar);
-
- protected static File getHistoryFile(Document document) {
-
- String documentPath = document.getId().replace('/', File.separatorChar) + ".xml";
-
- File workflowDirectory = new File(document.getPublication().getDirectory(), WorkflowFactory.WORKFLOW_DIRECTORY);
- File historyDirectory = new File(workflowDirectory, HISTORY_PATH);
- File historyFile = new File(historyDirectory, documentPath);
- return historyFile;
- }
-
/**
* Returns the document of this WorkflowDocument object.
* @return A document object.
@@ -139,50 +73,19 @@
return document;
}
- public static final String DOCTYPE_ATTRIBUTE = "doctype";
- public static final String VERSION_ELEMENT = "version";
- public static final String STATE_ATTRIBUTE = "state";
- public static final String USER_ATTRIBUTE = "user";
- public static final String EVENT_ATTRIBUTE = "event";
-
- /* (non-Javadoc)
- * @see org.apache.lenya.workflow.WorkflowInstance#invoke(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.Event)
- */
- public void invoke(Situation situation, Event event)
- throws WorkflowException {
- super.invoke(situation, event);
-
- File file = getHistoryFile(getDocument());
-
- try {
- org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(file);
- Element root = xmlDocument.getDocumentElement();
-
- NamespaceHelper helper =
- new NamespaceHelper(
- WorkflowBuilder.NAMESPACE,
- WorkflowBuilder.DEFAULT_PREFIX,
- xmlDocument);
-
- CMSSituation cmsSituation = (CMSSituation) situation;
-
- Element versionElement = helper.createElement(VERSION_ELEMENT);
- versionElement.setAttribute(STATE_ATTRIBUTE, ((StateImpl) getCurrentState()).getId());
- versionElement.setAttribute(USER_ATTRIBUTE, cmsSituation.getUser().getId());
- versionElement.setAttribute(EVENT_ATTRIBUTE, ((EventImpl) event).getName());
-
- root.appendChild(versionElement);
- DocumentHelper.writeDocument(xmlDocument, file);
-
- } catch (Exception e) {
- throw new WorkflowException(e);
- }
- }
-
public void invoke(Situation situation, String eventName) throws WorkflowException {
assert eventName != null;
Event event = ((WorkflowImpl) getWorkflow()).getEvent(eventName);
- invoke (situation, event);
+ invoke(situation, event);
}
-
+
+ /* (non-Javadoc)
+ * @see org.apache.lenya.workflow.impl.WorkflowInstanceImpl#getWorkflow(java.lang.String)
+ */
+ protected WorkflowImpl getWorkflow(String workflowName) throws WorkflowException {
+ WorkflowImpl workflow =
+ (WorkflowImpl) WorkflowFactory.buildWorkflow(document.getPublication(), workflowName);
+ return workflow;
+ }
+
}
1.7 +19 -45 cocoon-lenya/src/java/org/apache/lenya/cms/workflow/WorkflowFactory.java
Index: WorkflowFactory.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/workflow/WorkflowFactory.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- WorkflowFactory.java 4 Jun 2003 09:14:13 -0000 1.6
+++ WorkflowFactory.java 6 Jun 2003 13:35:17 -0000 1.7
@@ -17,16 +17,11 @@
import org.apache.lenya.cms.ac.User;
import org.apache.lenya.cms.ac.UserManager;
import org.apache.lenya.cms.publication.Document;
-import org.apache.lenya.cms.publication.DocumentType;
-import org.apache.lenya.cms.publication.DocumentTypeBuilder;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.publication.PublicationFactory;
import org.apache.lenya.workflow.*;
import org.apache.lenya.workflow.Workflow;
import org.apache.lenya.workflow.impl.WorkflowBuilder;
-import org.apache.lenya.xml.DocumentHelper;
-import org.apache.lenya.xml.NamespaceHelper;
-import org.w3c.dom.Element;
/**
*
@@ -45,15 +40,12 @@
return new WorkflowFactory();
}
- public static final String WORKFLOW_ELEMENT = "workflow";
- public static final String SRC_ATTRIBUTE = "src";
-
/*
* Creates a new workflow instance.
*/
public WorkflowInstance buildInstance(Document document) throws WorkflowException {
assert document != null;
- return new WorkflowDocument(document);
+ return new CMSHistory(document).getInstance();
}
/**
@@ -63,49 +55,19 @@
* @return <code>true</code> if the document has a workflow, <code>false</code> otherwise.
*/
public boolean hasWorkflow(Document document) {
- File historyFile = WorkflowDocument.getHistoryFile(document);
- return historyFile.exists();
+ return new CMSHistory(document).isInitialized();
}
- protected static Workflow buildWorkflow(Publication publication, DocumentType documentType)
+ protected static Workflow buildWorkflow(Publication publication, String workflowFileName)
throws WorkflowException {
assert publication != null;
- assert documentType != null;
-
- File doctypesDirectory =
- new File(publication.getDirectory(), DocumentTypeBuilder.DOCTYPE_DIRECTORY);
- File doctypeFile = new File(doctypesDirectory, documentType.getName() + ".xml");
-
- Workflow workflow;
+ assert workflowFileName != null && !"".equals(workflowFileName);
- try {
- org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(doctypeFile);
-
- NamespaceHelper helper =
- new NamespaceHelper(
- DocumentType.NAMESPACE,
- DocumentType.DEFAULT_PREFIX,
- xmlDocument);
- Element root = xmlDocument.getDocumentElement();
-
- Element workflowElement =
- (Element) root.getElementsByTagNameNS(
- DocumentType.NAMESPACE,
- WORKFLOW_ELEMENT).item(
- 0);
-
- String source = workflowElement.getAttribute(SRC_ATTRIBUTE);
- assert source != null;
-
- File publicationDirectory = publication.getDirectory();
- File workflowDirectory = new File(publicationDirectory, WORKFLOW_DIRECTORY);
- File workflowFile = new File(workflowDirectory, source);
+ File workflowDirectory = new File(publication.getDirectory(), WORKFLOW_DIRECTORY);
+ File workflowFile = new File(workflowDirectory, workflowFileName);
+ Workflow workflow = WorkflowBuilder.buildWorkflow(workflowFile);
- workflow = WorkflowBuilder.buildWorkflow(workflowFile);
- } catch (Exception e) {
- throw new WorkflowException(e);
- }
return workflow;
}
@@ -143,6 +105,18 @@
}
return buildSituation(user);
+ }
+
+
+ /**
+ * Initializes the history of a document.
+ * @param document The document object.
+ * @param type The document type the document belongs to.
+ * @param user The user who created the document.
+ * @throws WorkflowException When something goes wrong.
+ */
+ public static void initHistory(Document document, String workflowId) throws WorkflowException {
+ new CMSHistory(document).initialize(workflowId);
}
}
1.3 +11 -0 cocoon-lenya/src/java/org/apache/lenya/workflow/WorkflowInstance.java
Index: WorkflowInstance.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/workflow/WorkflowInstance.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WorkflowInstance.java 2 Jun 2003 17:33:37 -0000 1.2
+++ WorkflowInstance.java 6 Jun 2003 13:35:17 -0000 1.3
@@ -44,4 +44,15 @@
*/
boolean getValue(String variableName) throws WorkflowException;
+ /**
+ * Adds a workflow listener.
+ * @param listener The listener to add.
+ */
+ void addWorkflowListener(WorkflowListener listener);
+
+ /**
+ * Removes a workflow listener.
+ * @param listener The listener to remove.
+ */
+ void removeWorkflowListener(WorkflowListener listener);
}
1.3 +82 -19 cocoon-lenya/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java
Index: WorkflowInstanceImpl.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WorkflowInstanceImpl.java 2 Jun 2003 17:33:37 -0000 1.2
+++ WorkflowInstanceImpl.java 6 Jun 2003 13:35:18 -0000 1.3
@@ -6,8 +6,12 @@
package org.apache.lenya.workflow.impl;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -21,41 +25,41 @@
import org.apache.lenya.workflow.Workflow;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
+import org.apache.lenya.workflow.WorkflowListener;
/**
*
* @author andreas
*/
-public class WorkflowInstanceImpl implements WorkflowInstance {
+public abstract class WorkflowInstanceImpl implements WorkflowInstance {
/**
* Creates a new instance of WorkflowInstanceImpl.
*/
- public WorkflowInstanceImpl() {
- }
-
- /**
- * Creates a new instance of WorkflowInstanceImpl.
- */
- protected WorkflowInstanceImpl(WorkflowImpl workflow) {
- setWorkflow(workflow);
+ protected WorkflowInstanceImpl() {
}
private WorkflowImpl workflow;
/**
- * @return
+ * Returns the workflow object of this instance.
+ * @return A workflow object.
*/
public Workflow getWorkflow() {
return getWorkflowImpl();
}
+ /**
+ * Returns the workflow object of this instance.
+ * @return A workflow object.
+ */
protected WorkflowImpl getWorkflowImpl() {
return workflow;
}
- /** Returns the transitions that can fire for this user.
- *
+ /** Returns the events that can be invoked in a certain situation.
+ * @param situation The situation to check.
+ * @return The events that can be invoked.
*/
public Event[] getExecutableEvents(Situation situation) {
Transition transitions[] = getWorkflow().getLeavingTransitions(getCurrentState());
@@ -66,24 +70,39 @@
executableEvents.add(transitions[i].getEvent());
}
}
- return (Event[]) executableEvents.toArray(
- new Event[executableEvents.size()]);
+ return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
}
- /** Indicates that the user invoked an event.
- * @param user The user who invoked the event.
+ /** Invoke an event on this workflow instance.
+ * @param situation The situation when the event was invoked.
* @param event The event that was invoked.
- *
*/
public void invoke(Situation situation, Event event) throws WorkflowException {
+ if (Arrays.asList(getExecutableEvents(situation)).contains(event)) {
+ throw new WorkflowException(
+ "The event '"
+ + event
+ + "' cannot be invoked in the situation '"
+ + situation
+ + "'.");
+ }
Transition transitions[] = getWorkflow().getLeavingTransitions(getCurrentState());
for (int i = 0; i < transitions.length; i++) {
if (transitions[i].getEvent().equals(event)) {
fire((TransitionImpl) transitions[i]);
}
}
+ for (Iterator iter = listeners.iterator(); iter.hasNext();) {
+ WorkflowListener listener = (WorkflowListener) iter.next();
+ listener.transitionFired(this, situation, event);
+ }
}
+ /**
+ * Invokes a transition.
+ * @param transition The transition to invoke.
+ * @throws WorkflowException if something goes wrong.
+ */
protected void fire(TransitionImpl transition) throws WorkflowException {
Action actions[] = transition.getActions();
for (int i = 0; i < actions.length; i++) {
@@ -94,20 +113,25 @@
private State currentState;
+ /**
+ * Sets the current state of this instance.
+ * @param state The state to set.
+ */
protected void setCurrentState(State state) {
assert state != null && ((WorkflowImpl) getWorkflow()).containsState(state);
this.currentState = state;
}
/** Returns the current state of this WorkflowInstance.
- *
+ * @return A state object.
*/
public State getCurrentState() {
return currentState;
}
/**
- * @param workflow
+ * Sets the workflow of this instance.
+ * @param workflow A workflow object.
*/
protected void setWorkflow(WorkflowImpl workflow) {
assert workflow != null;
@@ -117,7 +141,26 @@
}
/**
+ * Sets the workflow of this instance.
+ * @param workflowName The identifier of the workflow.
+ * @throws WorkflowException if something goes wrong.
+ */
+ protected void setWorkflow(String workflowName) throws WorkflowException {
+ assert workflowName != null && !"".equals(workflowName);
+ setWorkflow(getWorkflow(workflowName));
+ }
+
+ /**
+ * Factory method to create a workflow object for a given identifier.
+ * @param workflow The workflow identifier.
+ * @return A workflow object.
+ */
+ protected abstract WorkflowImpl getWorkflow(String workflowName) throws WorkflowException;
+
+ /**
* Returns a workflow state for a given name.
+ * @param id The state id.
+ * @return A workflow object.
*/
protected State getState(String id) throws WorkflowException {
return getWorkflowImpl().getState(id);
@@ -125,6 +168,9 @@
private Map variableInstances = new HashMap();
+ /**
+ * Initializes the variable instances in the initial state.
+ */
protected void initVariableInstances() {
variableInstances.clear();
BooleanVariable variables[] = getWorkflowImpl().getVariables();
@@ -155,6 +201,23 @@
BooleanVariable variable = getWorkflowImpl().getVariable(variableName);
BooleanVariableInstance instance = getVariableInstance(variable);
return instance.getValue();
+ }
+
+ private List listeners = new ArrayList();
+
+ /* (non-Javadoc)
+ * @see org.apache.lenya.workflow.WorkflowInstance#addWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
+ */
+ public void addWorkflowListener(WorkflowListener listener) {
+ if (!listeners.contains(listener))
+ listeners.add(listener);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.lenya.workflow.WorkflowInstance#removeWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
+ */
+ public void removeWorkflowListener(WorkflowListener listener) {
+ listeners.remove(listener);
}
}
1.4 +3 -4 cocoon-lenya/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java
Index: WorkflowBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WorkflowBuilder.java 2 Jun 2003 17:34:31 -0000 1.3
+++ WorkflowBuilder.java 6 Jun 2003 13:35:18 -0000 1.4
@@ -15,7 +15,6 @@
import org.apache.lenya.workflow.Action;
import org.apache.lenya.workflow.Condition;
import org.apache.lenya.workflow.Event;
-import org.apache.lenya.workflow.Workflow;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.xml.DocumentHelper;
import org.apache.lenya.xml.NamespaceHelper;
@@ -36,8 +35,8 @@
public static final String NAMESPACE = "http://apache.org/cocoon/lenya/workflow/1.0";
public static final String DEFAULT_PREFIX = "wf";
- public static Workflow buildWorkflow(File file) throws WorkflowException {
- Workflow workflow;
+ public static WorkflowImpl buildWorkflow(File file) throws WorkflowException {
+ WorkflowImpl workflow;
try {
Document document = DocumentHelper.readDocument(file);
@@ -49,7 +48,7 @@
return workflow;
}
- public static Workflow buildWorkflow(Document document)
+ protected static WorkflowImpl buildWorkflow(Document document)
throws
ParserConfigurationException,
SAXException,
---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org