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 17:04:35 UTC

svn commit: r156428 - in lenya/trunk/src/java/org/apache/lenya: ./ cms/site/usecases/ cms/usecase/ cms/workflow/ cms/workflow/usecases/

Author: andreas
Date: Mon Mar  7 08:04:28 2005
New Revision: 156428

URL: http://svn.apache.org/viewcvs?view=rev&rev=156428
Log:
replaced WorkflowUsecase with WorkflowManager

Added:
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java
Removed:
    lenya/trunk/src/java/org/apache/lenya/cms/usecase/WorkflowUsecase.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUsecase.java
    lenya/trunk/src/java/org/apache/lenya/cms/usecase/DocumentUsecase.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowResolver.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/History.java
    lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/InvokeWorkflow.java
    lenya/trunk/src/java/org/apache/lenya/lenya.roles

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUsecase.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUsecase.java?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUsecase.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUsecase.java Mon Mar  7 08:04:28 2005
@@ -18,9 +18,8 @@
 
 import java.util.Arrays;
 
-import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.usecase.DocumentUsecase;
-import org.apache.lenya.workflow.WorkflowException;
+import org.apache.lenya.cms.workflow.WorkflowResolver;
 import org.apache.lenya.workflow.WorkflowInstance;
 
 /**
@@ -30,42 +29,35 @@
  */
 public class SiteUsecase extends DocumentUsecase {
 
-    protected Document doc = null;
-    protected WorkflowInstance instance = null;
-    protected static final String AREA = "area";
-    protected static final String DOCUMENTID = "documentid";
-    protected static final String DOCUMENTNAME = "documentname";
-    protected static final String LANGUAGEEXISTS = "languageexists";
     protected static final String STATE = "state";
     protected static final String ISLIVE = "is_live";
 
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#doInitialize()
-    /*TODO make common parameters available to site usecases: area, documentid, languageexists etc
-     * may need to take special areas into acccount, such as info-authoring */
+     */
     protected void doInitialize() {
         super.doInitialize();
+        WorkflowResolver resolver = null;
         try {
-            this.doc = getSourceDocument();
-            if (hasWorkflow(getSourceDocument())) {
-                this.instance = getWorkflowInstance(getSourceDocument());
-                setParameter(STATE, this.instance.getCurrentState().toString());
-                String[] variableNames = this.instance.getWorkflow().getVariableNames();
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            if (resolver.hasWorkflow(getSourceDocument())) {
+                WorkflowInstance instance = resolver.getWorkflowInstance(getSourceDocument());
+                setParameter(STATE, instance.getCurrentState().toString());
+                String[] variableNames = instance.getWorkflow().getVariableNames();
                 if (Arrays.asList(variableNames).contains(ISLIVE)) {
-                    setParameter("islive", Boolean.valueOf(this.instance.getValue(ISLIVE)));
+                    setParameter("islive", Boolean.valueOf(instance.getValue(ISLIVE)));
                 }
             } else {
                 setParameter("state", "");
             }
-        } catch (WorkflowException e) {
-        	getLogger().error("Could not get workflow state.");
-        	addErrorMessage("Could not get workflow state.");
+        } catch (Exception e) {
+            getLogger().error("Could not get workflow state.", e);
+            addErrorMessage("Could not get workflow state. See log files for details.");
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
         }
-
-        setParameter(AREA, this.doc.getArea());
-        setParameter(DOCUMENTID, this.doc.getId());
-        setParameter(DOCUMENTNAME, this.doc.getName());
-        setParameter(LANGUAGEEXISTS, "true");
     }
 
-}
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/usecase/DocumentUsecase.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/usecase/DocumentUsecase.java?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/usecase/DocumentUsecase.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/usecase/DocumentUsecase.java Mon Mar  7 08:04:28 2005
@@ -39,7 +39,7 @@
  * <li><code>document</code>- the document</li>
  * </ul>
  */
-public class DocumentUsecase extends WorkflowUsecase {
+public class DocumentUsecase extends AbstractUsecase {
 
     protected static final String DOCUMENT = "document";
     private String completeArea;
@@ -151,15 +151,6 @@
         } catch (DocumentBuildException e) {
             throw new RuntimeException(e);
         }
-    }
-
-    /**
-     * Triggers a workflow event on the source document by calling
-     * {@link #triggerWorkflow(String, Document)}.
-     * @param event the event.
-     */
-    protected void triggerWorkflow(String event) {
-        triggerWorkflow(event, getSourceDocument());
     }
 
     /**

Added: 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=auto&rev=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManager.java Mon Mar  7 08:04:28 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.workflow;
+
+import org.apache.lenya.cms.publication.Document;
+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.
+ *
+ * @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>.
+     * @param document The document.
+     * @param event The name of the event.
+     * @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.
+     */
+    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.
+     */
+    void invokeOnAll(DocumentSet documentSet, String event, boolean force) throws WorkflowException;
+    
+    /**
+     * Checks if an event can be invoked on a document.
+     * @param document The document.
+     * @param event The event.
+     * @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.
+     * @param event The event.
+     * @return if an error occurs.
+     */
+    boolean canInvokeOnAll(DocumentSet documents, String event);
+    
+}

Added: 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=auto&rev=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowManagerImpl.java Mon Mar  7 08:04:28 2005
@@ -0,0 +1,149 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.workflow;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+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.util.DocumentSet;
+import org.apache.lenya.workflow.Situation;
+import org.apache.lenya.workflow.WorkflowException;
+import org.apache.lenya.workflow.WorkflowInstance;
+
+/**
+ * Workflow manager implementation.
+ * 
+ * @version $Id:$
+ */
+public class WorkflowManagerImpl extends AbstractLogEnabled implements WorkflowManager, Serviceable {
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#invoke(org.apache.lenya.cms.publication.Document,
+     *      java.lang.String, boolean)
+     */
+    public void invoke(Document document, String event, boolean force) throws WorkflowException {
+        WorkflowResolver resolver = null;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            if (resolver.hasWorkflow(document)) {
+                WorkflowInstance instance = resolver.getWorkflowInstance(document);
+                Situation situation = resolver.getSituation();
+                if (force && !instance.canInvoke(situation, event)) {
+                    throw new WorkflowException("The event [" + event
+                            + "] cannot be invoked on the document [" + document
+                            + "] in the situation [" + situation + "]");
+                }
+                instance.invoke(situation, event);
+            }
+        } catch (ServiceException e) {
+            throw new WorkflowException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#invoke(org.apache.lenya.cms.publication.Document,
+     *      java.lang.String)
+     */
+    public void invoke(Document document, String event) throws WorkflowException {
+        invoke(document, event, true);
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#invokeOnAll(org.apache.lenya.cms.publication.util.DocumentSet,
+     *      java.lang.String, boolean)
+     */
+    public void invokeOnAll(DocumentSet documentSet, String event, boolean force)
+            throws WorkflowException {
+        WorkflowResolver resolver = null;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            Document[] documents = documentSet.getDocuments();
+            for (int i = 0; i < documents.length; i++) {
+                invoke(documents[i], event, force);
+            }
+        } catch (ServiceException e) {
+            throw new WorkflowException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#canInvoke(org.apache.lenya.cms.publication.Document,
+     *      java.lang.String)
+     */
+    public boolean canInvoke(Document document, String event) {
+        WorkflowResolver resolver = null;
+        boolean canInvoke = true;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            if (resolver.hasWorkflow(document)) {
+                WorkflowInstance instance = resolver.getWorkflowInstance(document);
+                Situation situation = resolver.getSituation();
+                canInvoke = instance.canInvoke(situation, event);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+        return canInvoke;
+    }
+
+    /**
+     * @see org.apache.lenya.cms.workflow.WorkflowManager#canInvokeOnAll(org.apache.lenya.cms.publication.util.DocumentSet,
+     *      java.lang.String)
+     */
+    public boolean canInvokeOnAll(DocumentSet documents, String event) {
+        WorkflowResolver resolver = null;
+        boolean canInvoke = true;
+        try {
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            Document[] documentArray = documents.getDocuments();
+            for (int i = 0; i < documentArray.length; i++) {
+                canInvoke = canInvoke && canInvoke(documentArray[i], event);
+            }
+        } catch (ServiceException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+        return canInvoke;
+    }
+
+    protected ServiceManager manager;
+
+    /**
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowResolver.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowResolver.java?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowResolver.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/WorkflowResolver.java Mon Mar  7 08:04:28 2005
@@ -25,7 +25,7 @@
 /**
  * Class to resolve workflow-related objects.
  * 
- * @version $Id:$
+ * @version $Id$
  */
 public interface WorkflowResolver {
 

Modified: lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/History.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/History.java?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/History.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/History.java Mon Mar  7 08:04:28 2005
@@ -18,7 +18,7 @@
 
 import org.apache.lenya.cms.usecase.DocumentUsecase;
 import org.apache.lenya.cms.workflow.CMSHistory;
-import org.apache.lenya.workflow.WorkflowException;
+import org.apache.lenya.cms.workflow.WorkflowResolver;
 import org.apache.lenya.workflow.WorkflowInstance;
 import org.apache.lenya.workflow.impl.Version;
 
@@ -28,21 +28,27 @@
  * @version $Id$
  */
 public class History extends DocumentUsecase {
-    
+
     /**
-     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
-     * TODO get wf variables, get date and machine ip for versions
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters() TODO
+     *      get wf variables, get date and machine ip for versions
      */
     protected void initParameters() {
         super.initParameters();
 
+        WorkflowResolver resolver = null;
         try {
-            WorkflowInstance instance = getWorkflowInstance(getSourceDocument());
-            CMSHistory history = (CMSHistory)instance.getHistory();
+            resolver = (WorkflowResolver) this.manager.lookup(WorkflowResolver.ROLE);
+            WorkflowInstance instance = resolver.getWorkflowInstance(getSourceDocument());
+            CMSHistory history = (CMSHistory) instance.getHistory();
             Version[] versions = history.getVersions();
             setParameter("versions", versions);
-        } catch (final WorkflowException e) {
+        } catch (final Exception e) {
             throw new RuntimeException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
         }
     }
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/InvokeWorkflow.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/InvokeWorkflow.java?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/InvokeWorkflow.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/workflow/usecases/InvokeWorkflow.java Mon Mar  7 08:04:28 2005
@@ -16,8 +16,11 @@
  */
 package org.apache.lenya.cms.workflow.usecases;
 
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.lenya.cms.usecase.DocumentUsecase;
-import org.apache.lenya.workflow.WorkflowInstance;
+import org.apache.lenya.cms.workflow.WorkflowManager;
 
 /**
  * Invoke a workflow event on the current document. The event is obtained from
@@ -25,18 +28,15 @@
  * 
  * @version $Id:$
  */
-public class InvokeWorkflow extends DocumentUsecase {
+public class InvokeWorkflow extends DocumentUsecase implements Configurable {
 
-    /**
-     * The name of the event request parameter.
-     */
-    public static final String EVENT = "lenya.event";
+    private String event;
 
     /**
      * @return The workflow event to use.
      */
     protected String getEvent() {
-        return getParameterAsString(EVENT);
+        return event;
     }
 
     /**
@@ -44,16 +44,22 @@
      */
     protected void doCheckPreconditions() throws Exception {
         super.doCheckPreconditions();
-        
+
         if (!getErrorMessages().isEmpty()) {
             return;
         }
-        
-        String eventName = getParameterAsString(EVENT);
-        WorkflowInstance instance = getWorkflowInstance(getSourceDocument());
-        if (!instance.canInvoke(getSituation(), eventName)) {
-            addErrorMessage("The event [" + eventName + "] is not executable on document ["
-                    + getSourceDocument() + "].");
+
+        WorkflowManager workflowManager = null;
+        try {
+            workflowManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            if (!workflowManager.canInvoke(getSourceDocument(), getEvent())) {
+                addErrorMessage("The event [" + getEvent() + "] is not executable on document ["
+                        + getSourceDocument() + "].");
+            }
+        } finally {
+            if (workflowManager != null) {
+                this.manager.release(workflowManager);
+            }
         }
     }
 
@@ -63,7 +69,25 @@
     protected void doExecute() throws Exception {
         super.doExecute();
 
-        String eventName = getParameterAsString(EVENT);
-        triggerWorkflow(eventName);
+        WorkflowManager workflowManager = null;
+        try {
+            workflowManager = (WorkflowManager) this.manager.lookup(WorkflowManager.ROLE);
+            workflowManager.invoke(getSourceDocument(), getEvent());
+        } finally {
+            if (workflowManager != null) {
+                this.manager.release(workflowManager);
+            }
+        }
+    }
+    
+    protected static final String ELEMENT_EVENT = "event";
+    protected static final String ATTRIBUTE_ID = "id";
+
+    /**
+     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration config) throws ConfigurationException {
+        this.event = config.getChild(ELEMENT_EVENT).getAttribute(ATTRIBUTE_ID);
     }
+
 }

Modified: lenya/trunk/src/java/org/apache/lenya/lenya.roles
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/lenya.roles?view=diff&r1=156427&r2=156428
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/lenya.roles (original)
+++ lenya/trunk/src/java/org/apache/lenya/lenya.roles Mon Mar  7 08:04:28 2005
@@ -106,12 +106,16 @@
   	    shorthand="workflow-resolver"
   	    default-class="org.apache.lenya.cms.workflow.WorkflowResolverImpl"/>
         
+  <role name="org.apache.lenya.cms.workflow.WorkflowManager"
+  	    shorthand="workflow-manager"
+  	    default-class="org.apache.lenya.cms.workflow.WorkflowManagerImpl"/>
+        
   <role name="org.apache.lenya.cms.site.usecases.LinkRewriter"
   	    shorthand="link-rewriter"
   	    default-class="org.apache.lenya.cms.site.usecases.LinkRewriterImpl"/>
         
   <role name="org.apache.lenya.cms.publication.DocumentManager"
-  	    shorthand="link-rewriter"
+  	    shorthand="document-manager"
   	    default-class="org.apache.lenya.cms.publication.DocumentManagerImpl"/>
         
 </role-list>



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