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/12/01 17:05:29 UTC

cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/publication/file FilePublication.java

andreas     2003/12/01 08:05:29

  Modified:    src/java/org/apache/lenya/cms/publication
                        ResourcesManager.java Publication.java
                        AbstractPublication.java
               src/java/org/apache/lenya/cms/publication/task
                        PublicationTask.java
               src/java/org/apache/lenya/cms/publication/file
                        FilePublication.java
  Log:
  - extended publication API
  - deleting resources by ResourcesManager
  
  Revision  Changes    Path
  1.7       +16 -1     cocoon-lenya/src/java/org/apache/lenya/cms/publication/ResourcesManager.java
  
  Index: ResourcesManager.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/ResourcesManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResourcesManager.java	11 Sep 2003 17:23:59 -0000	1.6
  +++ ResourcesManager.java	1 Dec 2003 16:05:29 -0000	1.7
  @@ -146,4 +146,19 @@
           };
           return getFiles(filter);
       }
  +    
  +    /**
  +     * Deletes all resources.
  +     */
  +    public void deleteResources() {
  +        File[] resources = getResources();
  +        for (int i = 0; i < resources.length; i++) {
  +            resources[i].delete();
  +        }
  +        File directory = getPath();
  +        if (directory.listFiles().length == 0) {
  +            directory.delete();
  +        }
  +    }
  +    
   }
  
  
  
  1.38      +7 -0      cocoon-lenya/src/java/org/apache/lenya/cms/publication/Publication.java
  
  Index: Publication.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/Publication.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- Publication.java	28 Nov 2003 16:57:16 -0000	1.37
  +++ Publication.java	1 Dec 2003 16:05:29 -0000	1.38
  @@ -160,6 +160,13 @@
        */
       void copyDocumentSetToArea(DocumentSet documentSet, String destinationArea)
           throws PublicationException;
  +        
  +    /**
  +     * Deletes a document.
  +     * @param document The document to delete.
  +     * @throws PublicationException when something went wrong.
  +     */
  +    void deleteDocument(Document document) throws PublicationException;
   
       /**
        * Creates a version of the document object in another area.
  
  
  
  1.5       +67 -6     cocoon-lenya/src/java/org/apache/lenya/cms/publication/AbstractPublication.java
  
  Index: AbstractPublication.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/AbstractPublication.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbstractPublication.java	28 Nov 2003 16:57:16 -0000	1.4
  +++ AbstractPublication.java	1 Dec 2003 16:05:29 -0000	1.5
  @@ -467,13 +467,74 @@
       }
   
       /**
  -                 * Copies a document source.
  -                 * @param sourceDocument The source document.
  -                 * @param destinationDocument The destination document.
  -                 * @throws PublicationException when something went wrong.
  -                 */
  +     * Copies a document source.
  +     * @param sourceDocument The source document.
  +     * @param destinationDocument The destination document.
  +     * @throws PublicationException when something went wrong.
  +     */
       protected abstract void copyDocumentSource(
           Document sourceDocument,
           Document destinationDocument)
           throws PublicationException;
  +
  +    /**
  +     * @see org.apache.lenya.cms.publication.Publication#deleteDocument(org.apache.lenya.cms.publication.Document)
  +     */
  +    public void deleteDocument(Document document) throws PublicationException {
  +
  +        SiteTree tree;
  +        try {
  +            tree = getSiteTree(document.getArea());
  +        } catch (SiteTreeException e) {
  +            throw new PublicationException(e);
  +        }
  +
  +        SiteTreeNode node = tree.getNode(document.getId());
  +
  +        if (node == null) {
  +            throw new PublicationException(
  +                "Sitetree node for document [" + document + "] does not exist!");
  +        }
  +
  +        Label label = node.getLabel(document.getLanguage());
  +
  +        if (label == null) {
  +            throw new PublicationException(
  +                "Sitetree label for document ["
  +                    + document
  +                    + "] in language ["
  +                    + document.getLanguage()
  +                    + "]does not exist!");
  +        }
  +
  +        Label[] labels = node.getLabels();
  +        if (labels.length == 1 && node.getChildren().length > 0) {
  +            throw new PublicationException(
  +                "Cannot delete last language version of document ["
  +                    + document
  +                    + "] because this node has children.");
  +        }
  +
  +        node.removeLabel(label);
  +
  +        if (labels.length == 0) {
  +            tree.removeNode(document.getId());
  +        }
  +
  +        try {
  +            tree.save();
  +        } catch (SiteTreeException e) {
  +            throw new PublicationException(e);
  +        }
  +
  +        deleteDocumentSource(document);
  +    }
  +
  +    /**
  +     * Deletes the source of a document.
  +     * @param document The document to delete.
  +     * @throws PublicationException when something went wrong.
  +     */
  +    protected abstract void deleteDocumentSource(Document document) throws PublicationException;
  +
   }
  
  
  
  1.3       +191 -2    cocoon-lenya/src/java/org/apache/lenya/cms/publication/task/PublicationTask.java
  
  Index: PublicationTask.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/task/PublicationTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PublicationTask.java	27 Nov 2003 14:01:49 -0000	1.2
  +++ PublicationTask.java	1 Dec 2003 16:05:29 -0000	1.3
  @@ -55,11 +55,27 @@
   */
   package org.apache.lenya.cms.publication.task;
   
  +import java.io.File;
  +import java.io.IOException;
  +import java.util.ArrayList;
  +import java.util.Arrays;
  +import java.util.List;
  +
  +import org.apache.avalon.excalibur.io.FileUtil;
  +import org.apache.avalon.framework.parameters.ParameterException;
  +import org.apache.lenya.cms.publication.Document;
   import org.apache.lenya.cms.publication.Publication;
   import org.apache.lenya.cms.publication.PublicationFactory;
  +import org.apache.lenya.cms.publication.ResourcesManager;
   import org.apache.lenya.cms.task.AbstractTask;
   import org.apache.lenya.cms.task.ExecutionException;
   import org.apache.lenya.cms.task.Task;
  +import org.apache.lenya.cms.workflow.WorkflowFactory;
  +import org.apache.lenya.workflow.Event;
  +import org.apache.lenya.workflow.Situation;
  +import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
  +import org.apache.lenya.workflow.WorkflowException;
  +import org.apache.log4j.Category;
   
   /**
    * Abstract super class for publication-based tasks.
  @@ -67,7 +83,9 @@
    * @author <a href="mailto:andreas@apache.org">Andreas Hartmann</a>
    */
   public abstract class PublicationTask extends AbstractTask {
  -    
  +
  +    private static final Category log = Category.getInstance(PublicationTask.class);
  +
       private Publication publication;
   
       /**
  @@ -87,6 +105,177 @@
               }
           }
           return publication;
  +    }
  +
  +    /**
  +     * Copies the resources of a document to another document.
  +     * @param sourceDocument The source document.
  +     * @param destinationDocument The destination document.
  +     * @throws PublicationException when something went wrong.
  +     * @throws ExecutionException when something went wrong.
  +     * @throws IOException when something went wrong.
  +     */
  +    protected void copyResources(Document sourceDocument, Document destinationDocument)
  +        throws ExecutionException {
  +
  +        if (log.isDebugEnabled()) {
  +            log.debug("Copying resources");
  +        }
  +
  +        ResourcesManager sourceManager = new ResourcesManager(sourceDocument);
  +        ResourcesManager destinationManager = new ResourcesManager(destinationDocument);
  +
  +        List resourcesList = new ArrayList(Arrays.asList(sourceManager.getResources()));
  +        resourcesList.addAll(Arrays.asList(sourceManager.getMetaFiles()));
  +        File[] resources = (File[]) resourcesList.toArray(new File[resourcesList.size()]);
  +        File destinationDirectory = destinationManager.getPath();
  +
  +        for (int i = 0; i < resources.length; i++) {
  +            File destinationResource = new File(destinationDirectory, resources[i].getName());
  +
  +            if (log.isDebugEnabled()) {
  +                log.debug(
  +                    "Copy file ["
  +                        + resources[i].getAbsolutePath()
  +                        + "] to ["
  +                        + destinationResource.getAbsolutePath()
  +                        + "]");
  +            }
  +            try {
  +                FileUtil.copyFile(resources[i], destinationResource);
  +            } catch (IOException e) {
  +                throw new ExecutionException(e);
  +            }
  +        }
  +    }
  +
  +    public static final String PARAMETER_WORKFLOW_EVENT = "workflow-event";
  +    public static final String PARAMETER_USER_ID = "user-id";
  +    public static final String PARAMETER_IP_ADDRESS = "ip-address";
  +    public static final String PARAMETER_ROLE_IDS = "role-ids";
  +    public static final String ROLE_SEPARATOR_REGEXP = ",";
  +
  +    /**
  +     * Checks if the workflow event can be invoked on a document.
  +     * @param document The document.
  +     * @return A boolean value.
  +     * @throws ExecutionException when something went wrong.
  +     */
  +    protected boolean canWorkflowFire(Document document) throws ExecutionException {
  +
  +        boolean canFire = true;
  +
  +        WorkflowFactory factory = WorkflowFactory.newInstance();
  +        if (factory.hasWorkflow(document)) {
  +            try {
  +                String userId = getParameters().getParameter(PARAMETER_USER_ID);
  +                String machineIp = getParameters().getParameter(PARAMETER_IP_ADDRESS);
  +                SynchronizedWorkflowInstances instance;
  +                try {
  +                    instance = factory.buildSynchronizedInstance(document);
  +                } catch (WorkflowException e) {
  +                    throw new ExecutionException(e);
  +                }
  +                Situation situation = factory.buildSituation(getRoleIDs(), userId, machineIp);
  +
  +                Event event = getExecutableEvent(instance, situation);
  +                
  +                if (event == null) {
  +                    canFire = false;
  +                }
  +                
  +            }
  +            catch (Exception e) {
  +                throw new ExecutionException(e);
  +            }
  +        }
  +        return canFire;
  +    }
  +
  +    /**
  +     * Invokes the workflow on a document.
  +     * @param document The document.
  +     * @throws ParameterException when something went wrong.
  +     * @throws WorkflowException when something went wrong.
  +     */
  +    protected void triggerWorkflow(Document document) throws ExecutionException {
  +
  +        if (log.isDebugEnabled()) {
  +            log.debug("Trying to execute workflow on document [" + document.getId() + "].");
  +        }
  +
  +        WorkflowFactory factory = WorkflowFactory.newInstance();
  +        if (factory.hasWorkflow(document)) {
  +            try {
  +                String userId = getParameters().getParameter(PARAMETER_USER_ID);
  +                String machineIp = getParameters().getParameter(PARAMETER_IP_ADDRESS);
  +
  +                SynchronizedWorkflowInstances instance;
  +                try {
  +                    instance = factory.buildSynchronizedInstance(document);
  +                } catch (WorkflowException e) {
  +                    throw new ExecutionException(e);
  +                }
  +                Situation situation = factory.buildSituation(getRoleIDs(), userId, machineIp);
  +
  +                Event event = getExecutableEvent(instance, situation);
  +
  +                assert event != null;
  +
  +                if (log.isDebugEnabled()) {
  +                    log.debug("Invoking event [" + event.getName() + "]");
  +                }
  +                instance.invoke(situation, event);
  +                if (log.isDebugEnabled()) {
  +                    log.debug("Invoking transition completed.");
  +                }
  +            } catch (Exception e) {
  +                throw new ExecutionException(e);
  +            }
  +        } else {
  +            if (log.isDebugEnabled()) {
  +                log.debug("No workflow associated with document.");
  +            }
  +        }
  +
  +    }
  +
  +    /**
  +     * Returns the executable event for the provided {@link #PARAMETER_WORKFLOW_EVENT} parameter.
  +     * @param instance The workflow instance.
  +     * @param situation The situation.
  +     * @return An event.
  +     * @throws WorkflowException when something went wrong.
  +     * @throws ParameterException when the {@link #PARAMETER_WORKFLOW_EVENT} parameter could not be resolved.
  +     */
  +    protected Event getExecutableEvent(SynchronizedWorkflowInstances instance, Situation situation)
  +        throws WorkflowException, ParameterException {
  +
  +        String workflowEvent = getParameters().getParameter(PARAMETER_WORKFLOW_EVENT);
  +
  +        Event event = null;
  +        Event[] events = instance.getExecutableEvents(situation);
  +
  +        if (log.isDebugEnabled()) {
  +            log.debug("Resolved executable events.");
  +        }
  +
  +        for (int i = 0; i < events.length; i++) {
  +            if (events[i].getName().equals(workflowEvent)) {
  +                event = events[i];
  +            }
  +        }
  +        return event;
  +    }
  +
  +    /**
  +     * Returns the role IDs.
  +     * @return An array of strings.
  +     */
  +    protected String[] getRoleIDs() throws ParameterException {
  +        String rolesString = getParameters().getParameter(PARAMETER_ROLE_IDS);
  +        String[] roles = rolesString.split(ROLE_SEPARATOR_REGEXP);
  +        return roles;
       }
   
   }
  
  
  
  1.4       +13 -1     cocoon-lenya/src/java/org/apache/lenya/cms/publication/file/FilePublication.java
  
  Index: FilePublication.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/file/FilePublication.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FilePublication.java	28 Nov 2003 15:52:32 -0000	1.3
  +++ FilePublication.java	1 Dec 2003 16:05:29 -0000	1.4
  @@ -128,4 +128,16 @@
           }
       }
   
  +    /**
  +     * @see org.apache.lenya.cms.publication.AbstractPublication#deleteDocumentSource(org.apache.lenya.cms.publication.Document)
  +     */
  +    protected void deleteDocumentSource(Document document) throws PublicationException {
  +        File file = document.getFile();
  +        boolean deleted = file.delete();
  +        if (!deleted) {
  +            throw new PublicationException(
  +                "Source file of document [" + document.getId() + "] could not be deleted!");
  +        }
  +    }
  +
   }
  
  
  

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