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 2005/03/07 18:35:36 UTC

svn commit: r156434 - in lenya/trunk/src/java/org/apache/lenya: cms/publication/DocumentManagerImpl.java cms/workflow/WorkflowManager.java cms/workflow/WorkflowManagerImpl.java workflow/impl/HistoryImpl.java

Author: andreas
Date: Mon Mar  7 09:35:30 2005
New Revision: 156434

URL: http://svn.apache.org/viewcvs?view=rev&rev=156434
Log:
moved methods from DocumentManager to WorkflowManager, don't copy workflow history on itself

Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java
    lenya/trunk/src/java/org/apache/lenya/workflow/impl/HistoryImpl.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java?view=diff&r1=156433&r2=156434
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java Mon Mar  7 09:35:30 2005
@@ -35,10 +35,7 @@
 import org.apache.lenya.cms.publication.util.OrderedDocumentSet;
 import org.apache.lenya.cms.publication.util.UniqueDocumentId;
 import org.apache.lenya.cms.site.SiteManager;
-import org.apache.lenya.cms.workflow.WorkflowResolver;
-import org.apache.lenya.workflow.Situation;
-import org.apache.lenya.workflow.WorkflowException;
-import org.apache.lenya.workflow.WorkflowInstance;
+import org.apache.lenya.cms.workflow.WorkflowManager;
 
 /**
  * Abstract DocumentManager implementation.
@@ -62,26 +59,15 @@
 
         siteManager.add(document);
 
-        DocumentTypeResolver doctypeResolver = null;
-        WorkflowResolver workflowResolver = null;
+        WorkflowManager wfManager = null;
         try {
-            doctypeResolver = (DocumentTypeResolver) this.manager.lookup(DocumentTypeResolver.ROLE);
-            workflowResolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
-
-            DocumentType doctype = doctypeResolver.resolve(document);
-
-            if (doctype.hasWorkflow()) {
-                Situation situation = workflowResolver.getSituation();
-                workflowResolver.getWorkflowInstance(document).getHistory().initialize(situation);
-            }
-
-        } catch (ServiceException e) {
-            throw new PublicationException(e);
-        } catch (WorkflowException e) {
+            wfManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            wfManager.initializeHistory(document);
+        } catch (Exception e) {
             throw new PublicationException(e);
         } finally {
-            if (doctypeResolver != null) {
-                this.manager.release(doctypeResolver);
+            if (wfManager != null) {
+                this.manager.release(wfManager);
             }
         }
     }
@@ -102,67 +88,22 @@
                 destinationDocument);
 
         ResourcesManager resourcesManager = sourceDocument.getResourcesManager();
-        WorkflowResolver workflowResolver = null;
+        WorkflowManager workflowManager = null;
         try {
             resourcesManager.copyResourcesTo(destinationDocument);
 
-            workflowResolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
-            copyWorkflow(workflowResolver, sourceDocument, destinationDocument);
+            workflowManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            workflowManager.copyHistory(sourceDocument, destinationDocument);
         } catch (Exception e) {
             throw new PublicationException(e);
         } finally {
-            if (workflowResolver != null) {
-                this.manager.release(workflowResolver);
+            if (workflowManager != null) {
+                this.manager.release(workflowManager);
             }
         }
     }
 
     /**
-     * Moves the workflow history of a document.
-     * @param resolver The workflow resolver.
-     * @param sourceDocument The source document.
-     * @param destinationDocument The destination document.
-     * @throws WorkflowException if an error occurs.
-     */
-    protected void moveWorkflow(WorkflowResolver resolver, Document sourceDocument,
-            Document destinationDocument) throws WorkflowException {
-        copyWorkflow(resolver, sourceDocument, destinationDocument);
-        deleteWorkflow(resolver, sourceDocument);
-    }
-
-    /**
-     * Deletes the workflow history of a document.
-     * @param resolver The workflow resolver.
-     * @param sourceDocument The source document.
-     * @throws WorkflowException if an error occurs.
-     */
-    protected void deleteWorkflow(WorkflowResolver resolver, Document sourceDocument)
-            throws WorkflowException {
-        if (resolver.hasWorkflow(sourceDocument)) {
-            WorkflowInstance sourceInstance = resolver.getWorkflowInstance(sourceDocument);
-            sourceInstance.getHistory().delete();
-        }
-    }
-
-    /**
-     * Copies the workflow history of a document.
-     * @param resolver The workflow resolver.
-     * @param sourceDocument The source document.
-     * @param destinationDocument The destination document.
-     * @throws WorkflowException if an error occurs.
-     */
-    protected void copyWorkflow(WorkflowResolver resolver, Document sourceDocument,
-            Document destinationDocument) throws WorkflowException {
-        if (resolver.hasWorkflow(sourceDocument)) {
-            WorkflowInstance sourceInstance = resolver.getWorkflowInstance(sourceDocument);
-
-            WorkflowInstance destinationInstance = resolver
-                    .getWorkflowInstance(destinationDocument);
-            destinationInstance.getHistory().replaceWith(sourceInstance.getHistory());
-        }
-    }
-
-    /**
      * @see org.apache.lenya.cms.publication.DocumentManager#deleteDocument(org.apache.lenya.cms.publication.Document)
      */
     public void deleteDocument(Document document) throws PublicationException {
@@ -176,15 +117,15 @@
         ResourcesManager resourcesManager = document.getResourcesManager();
         resourcesManager.deleteResources();
 
-        WorkflowResolver workflowResolver = null;
+        WorkflowManager workflowManager = null;
         try {
-            workflowResolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
-            deleteWorkflow(workflowResolver, document);
+            workflowManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            workflowManager.deleteHistory(document);
         } catch (Exception e) {
             throw new PublicationException(e);
         } finally {
-            if (workflowResolver != null) {
-                this.manager.release(workflowResolver);
+            if (workflowManager != null) {
+                this.manager.release(workflowManager);
             }
         }
     }
@@ -199,18 +140,18 @@
         deleteDocument(sourceDocument);
 
         ResourcesManager resourcesManager = sourceDocument.getResourcesManager();
-        WorkflowResolver workflowResolver = null;
+        WorkflowManager workflowManager = null;
         try {
             resourcesManager.copyResourcesTo(destinationDocument);
             resourcesManager.deleteResources();
 
-            workflowResolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
-            moveWorkflow(workflowResolver, sourceDocument, destinationDocument);
+            workflowManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            workflowManager.moveHistory(sourceDocument, destinationDocument);
         } catch (Exception e) {
             throw new PublicationException(e);
         } finally {
-            if (workflowResolver != null) {
-                this.manager.release(workflowResolver);
+            if (workflowManager != null) {
+                this.manager.release(workflowManager);
             }
         }
     }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java?view=diff&r1=156433&r2=156434
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java Mon Mar  7 09:35:30 2005
@@ -17,53 +17,61 @@
 package org.apache.lenya.cms.workflow;
 
 import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.PublicationException;
 import org.apache.lenya.cms.publication.util.DocumentSet;
 import org.apache.lenya.workflow.WorkflowException;
 
 /**
- * Manager for workflow issues. This is the main entry point for workflow-related
- * tasks. You can safely invoke all methods for non-workflow documents.
- *
+ * Manager for workflow issues. This is the main entry point for
+ * workflow-related tasks. You can safely invoke all methods for non-workflow
+ * documents.
+ * 
  * @version $Id:$
  */
 public interface WorkflowManager {
-    
+
     /**
      * The Avalon role.
      */
     String ROLE = WorkflowManager.class.getName();
 
     /**
-     * Invokes a workflow event on a document. This is the same as <code>invoke(Document, String, true)</code>.
+     * Invokes a workflow event on a document. This is the same as
+     * <code>invoke(Document, String, true)</code>.
      * @param document The document.
      * @param event The name of the event.
-     * @throws WorkflowException if the event could not be invoked in the current situation.
+     * @throws WorkflowException if the event could not be invoked in the
+     *             current situation.
      */
     void invoke(Document document, String event) throws WorkflowException;
-    
+
     /**
      * Invokes a workflow event on a document.
      * @param document The document.
      * @param event The name of the event.
-     * @param force If this is set to <code>true</code>, the execution is forced, which
-     * means an exception is thrown if the document in the set does not support the event.
-     * If set to <code>false</code>, non-supporting documents are ignored.
-     * @throws WorkflowException if the event could not be invoked in the current situation.
+     * @param force If this is set to <code>true</code>, the execution is
+     *            forced, which means an exception is thrown if the document in
+     *            the set does not support the event. If set to
+     *            <code>false</code>, non-supporting documents are ignored.
+     * @throws WorkflowException if the event could not be invoked in the
+     *             current situation.
      */
     void invoke(Document document, String event, boolean force) throws WorkflowException;
-    
+
     /**
      * Invokes a workflow event on a document set.
      * @param documentSet The document.
      * @param event The event.
-     * @param force If this is set to <code>true</code>, the execution is forced, which
-     * means an exception is thrown if a document in the set does not support the event.
-     * If set to <code>false</code>, non-supporting documents are ignored.
-     * @throws WorkflowException if <code>force</code> is set to <code>true</code> and
-     * a document does not support the workflow event.
+     * @param force If this is set to <code>true</code>, the execution is
+     *            forced, which means an exception is thrown if a document in
+     *            the set does not support the event. If set to
+     *            <code>false</code>, non-supporting documents are ignored.
+     * @throws WorkflowException if <code>force</code> is set to
+     *             <code>true</code> and a document does not support the
+     *             workflow event.
      */
     void invokeOnAll(DocumentSet documentSet, String event, boolean force) throws WorkflowException;
-    
+
     /**
      * Checks if an event can be invoked on a document.
      * @param document The document.
@@ -71,7 +79,7 @@
      * @return A boolean value.
      */
     boolean canInvoke(Document document, String event);
-    
+
     /**
      * Checks if an event can be invoked on all documents in a set.
      * @param documents The documents.
@@ -79,5 +87,34 @@
      * @return if an error occurs.
      */
     boolean canInvokeOnAll(DocumentSet documents, String event);
-    
-}
+
+    /**
+     * Copies the workflow history from one document to another.
+     * @param source The source document.
+     * @param target The target document.
+     * @throws WorkflowException if the history could not be copied.
+     */
+    void copyHistory(Document source, Document target) throws WorkflowException;
+
+    /**
+     * Moves the workflow history of a document.
+     * @param source The source document.
+     * @param target The destination document.
+     * @throws WorkflowException if an error occurs.
+     */
+    void moveHistory(Document source, Document target) throws WorkflowException;
+
+    /**
+     * Deletes the workflow history of a document.
+     * @param document The document.
+     * @throws WorkflowException if an error occurs.
+     */
+    void deleteHistory(Document document) throws WorkflowException;
+
+    /**
+     * Initializes the workflow history of a document.
+     * @param document The document.
+     * @throws WorkflowException if an error occurs.
+     */
+    void initializeHistory(Document document) throws WorkflowException;
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java?view=diff&r1=156433&r2=156434
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java Mon Mar  7 09:35:30 2005
@@ -21,6 +21,8 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentType;
+import org.apache.lenya.cms.publication.DocumentTypeResolver;
 import org.apache.lenya.cms.publication.util.DocumentSet;
 import org.apache.lenya.workflow.Situation;
 import org.apache.lenya.workflow.WorkflowException;
@@ -144,6 +146,86 @@
      */
     public void service(ServiceManager manager) throws ServiceException {
         this.manager = manager;
+    }
+
+    /**
+     * @throws WorkflowException
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#copyHistory(org.apache.lenya.cms.publication.Document,
+     *      org.apache.lenya.cms.publication.Document)
+     */
+    public void copyHistory(Document source, Document target) throws WorkflowException {
+
+        WorkflowResolver resolver = null;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            if (resolver.hasWorkflow(source)) {
+                WorkflowInstance sourceInstance = resolver.getWorkflowInstance(source);
+                WorkflowInstance destinationInstance = resolver.getWorkflowInstance(target);
+                destinationInstance.getHistory().replaceWith(sourceInstance.getHistory());
+            }
+        } catch (ServiceException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#moveHistory(org.apache.lenya.cms.publication.Document,
+     *      org.apache.lenya.cms.publication.Document)
+     */
+    public void moveHistory(Document sourceDocument, Document destinationDocument)
+            throws WorkflowException {
+        copyHistory(sourceDocument, destinationDocument);
+        deleteHistory(sourceDocument);
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#deleteHistory(org.apache.lenya.cms.publication.Document)
+     */
+    public void deleteHistory(Document sourceDocument) throws WorkflowException {
+        WorkflowResolver resolver = null;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            if (resolver.hasWorkflow(sourceDocument)) {
+                WorkflowInstance sourceInstance = resolver.getWorkflowInstance(sourceDocument);
+                sourceInstance.getHistory().delete();
+            }
+        } catch (ServiceException e) {
+            throw new WorkflowException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#initializeHistory(org.apache.lenya.cms.publication.Document)
+     */
+    public void initializeHistory(Document document) throws WorkflowException {
+        DocumentTypeResolver doctypeResolver = null;
+        WorkflowResolver workflowResolver = null;
+        try {
+            doctypeResolver = (DocumentTypeResolver) this.manager.lookup(DocumentTypeResolver.ROLE);
+            workflowResolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+
+            DocumentType doctype = doctypeResolver.resolve(document);
+
+            if (doctype.hasWorkflow()) {
+                Situation situation = workflowResolver.getSituation();
+                workflowResolver.getWorkflowInstance(document).getHistory().initialize(situation);
+            }
+
+        } catch (ServiceException e) {
+            throw new WorkflowException(e);
+        } finally {
+            if (doctypeResolver != null) {
+                this.manager.release(doctypeResolver);
+            }
+        }
     }
 
 }

Modified: lenya/trunk/src/java/org/apache/lenya/workflow/impl/HistoryImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/workflow/impl/HistoryImpl.java?view=diff&r1=156433&r2=156434
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/workflow/impl/HistoryImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/workflow/impl/HistoryImpl.java Mon Mar  7 09:35:30 2005
@@ -284,37 +284,37 @@
      */
     public void transitionFired(WorkflowInstance _instance, Situation situation, String event,
             State resultingState) throws WorkflowException {
-            try {
-                this.lastState = resultingState;
-                org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(getHistoryFile());
-                Element root = xmlDocument.getDocumentElement();
+        try {
+            this.lastState = resultingState;
+            org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(getHistoryFile());
+            Element root = xmlDocument.getDocumentElement();
 
-                NamespaceHelper helper = new NamespaceHelper(Workflow.NAMESPACE,
-                        Workflow.DEFAULT_PREFIX, xmlDocument);
+            NamespaceHelper helper = new NamespaceHelper(Workflow.NAMESPACE,
+                    Workflow.DEFAULT_PREFIX, xmlDocument);
 
-                Element versionElement = createVersionElement(helper, (StateImpl) _instance
-                        .getCurrentState(), situation, event);
+            Element versionElement = createVersionElement(helper, (StateImpl) _instance
+                    .getCurrentState(), situation, event);
 
-                root.appendChild(versionElement);
+            root.appendChild(versionElement);
 
-                saveVariables(helper);
+            saveVariables(helper);
 
-                DocumentHelper.writeDocument(xmlDocument, getHistoryFile());
-            } catch (final DOMException e) {
-                throw new WorkflowException(e);
-            } catch (final TransformerConfigurationException e) {
-                throw new WorkflowException(e);
-            } catch (final ParserConfigurationException e) {
-                throw new WorkflowException(e);
-            } catch (final SAXException e) {
-                throw new WorkflowException(e);
-            } catch (final IOException e) {
-                throw new WorkflowException(e);
-            } catch (final WorkflowException e) {
-                throw new WorkflowException(e);
-            } catch (final TransformerException e) {
-                throw new WorkflowException(e);
-            }
+            DocumentHelper.writeDocument(xmlDocument, getHistoryFile());
+        } catch (final DOMException e) {
+            throw new WorkflowException(e);
+        } catch (final TransformerConfigurationException e) {
+            throw new WorkflowException(e);
+        } catch (final ParserConfigurationException e) {
+            throw new WorkflowException(e);
+        } catch (final SAXException e) {
+            throw new WorkflowException(e);
+        } catch (final IOException e) {
+            throw new WorkflowException(e);
+        } catch (final WorkflowException e) {
+            throw new WorkflowException(e);
+        } catch (final TransformerException e) {
+            throw new WorkflowException(e);
+        }
     }
 
     /**
@@ -414,17 +414,27 @@
      */
     protected void move(File newFile) throws WorkflowException {
 
-        copy(newFile);
+        try {
+            if (!newFile.getCanonicalPath().equals(getHistoryFile().getCanonicalPath())) {
+                copy(newFile);
 
-        File historyFile = getHistoryFile();
-        File directory = historyFile.getParentFile();
-        boolean deleted = historyFile.delete();
-        if (!deleted) {
-            throw new WorkflowException("The old history file could not be deleted!");
-        }
-        if (directory.exists() && directory.isDirectory() && directory.listFiles().length == 0) {
-            directory.delete();
+                File historyFile = getHistoryFile();
+                File directory = historyFile.getParentFile();
+                boolean deleted = historyFile.delete();
+                if (!deleted) {
+                    throw new WorkflowException("The old history file could not be deleted!");
+                }
+                if (directory.exists() && directory.isDirectory()
+                        && directory.listFiles().length == 0) {
+                    directory.delete();
+                }
+            }
+        } catch (final IOException e) {
+            throw new WorkflowException(e);
+        } catch (final WorkflowException e) {
+            throw e;
         }
+
     }
 
     /**
@@ -434,36 +444,44 @@
      */
     protected void copy(File newFile) throws WorkflowException {
 
-        FileInputStream sourceStream = null;
-        FileOutputStream destinationStream = null;
-        FileChannel sourceChannel = null;
-        FileChannel destinationChannel = null;
         try {
-            newFile.getParentFile().mkdirs();
-            newFile.createNewFile();
-            File historyFile = getHistoryFile();
-
-            sourceStream = new FileInputStream(historyFile);
-            sourceChannel = sourceStream.getChannel();
-
-            destinationStream = new FileOutputStream(newFile);
-            destinationChannel = destinationStream.getChannel();
-
-            destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
-            sourceStream.close();
-            destinationStream.close();
-        } catch (final IOException e) {
-            throw new WorkflowException(e);
-        } finally {
-            try {
-                if (sourceStream != null)
+            if (!newFile.getCanonicalPath().equals(getHistoryFile().getCanonicalPath())) {
+                FileInputStream sourceStream = null;
+                FileOutputStream destinationStream = null;
+                FileChannel sourceChannel = null;
+                FileChannel destinationChannel = null;
+                try {
+
+                    newFile.getParentFile().mkdirs();
+                    newFile.createNewFile();
+                    File historyFile = getHistoryFile();
+
+                    sourceStream = new FileInputStream(historyFile);
+                    sourceChannel = sourceStream.getChannel();
+
+                    destinationStream = new FileOutputStream(newFile);
+                    destinationChannel = destinationStream.getChannel();
+
+                    destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
                     sourceStream.close();
-                if (destinationStream != null)
                     destinationStream.close();
-            } catch (final IOException e) {
-                throw new WorkflowException(e);
+                } finally {
+                    try {
+                        if (sourceStream != null)
+                            sourceStream.close();
+                        if (destinationStream != null)
+                            destinationStream.close();
+                    } catch (final IOException e) {
+                        throw new WorkflowException(e);
+                    }
+                }
             }
+        } catch (final IOException e) {
+            throw new WorkflowException(e);
+        } catch (final WorkflowException e) {
+            throw e;
         }
+
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org