lenya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andr...@apache.org
Subject cvs commit: cocoon-lenya/src/java/org/apache/lenya/workflow/impl History.java
Date Tue, 05 Aug 2003 16:29:06 GMT
andreas     2003/08/05 09:29:06

  Modified:    src/java/org/apache/lenya/cms/workflow WorkflowFactory.java
                        CMSHistory.java
               src/java/org/apache/lenya/workflow/impl History.java
  Log:
  - added method to move history file
  - using PolicyAuthorizer to obtain roles from request
  
  Revision  Changes    Path
  1.17      +20 -10    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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- WorkflowFactory.java	5 Aug 2003 12:00:16 -0000	1.16
  +++ WorkflowFactory.java	5 Aug 2003 16:29:06 -0000	1.17
  @@ -58,16 +58,18 @@
   import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.environment.Request;
   
  +import org.apache.lenya.cms.ac.AccessControlException;
   import org.apache.lenya.cms.ac.Role;
  +import org.apache.lenya.cms.ac2.PolicyAuthorizer;
   import org.apache.lenya.cms.publication.Document;
   import org.apache.lenya.cms.publication.Publication;
   import org.apache.lenya.workflow.*;
   import org.apache.lenya.workflow.Workflow;
  +import org.apache.lenya.workflow.impl.History;
   import org.apache.lenya.workflow.impl.WorkflowBuilder;
   
   import java.io.File;
   
  -import java.util.List;
   import java.util.Map;
   
   /**
  @@ -103,6 +105,17 @@
       }
   
       /**
  +     * Moves the history of a document.
  +     * @param oldDocument The document to move the instance for.
  +     * @param newDocument The new document.
  +     * @throws WorkflowException when something went wrong.
  +     */
  +    public void moveHistory(Document oldDocument, Document newDocument) throws WorkflowException
{
  +        assert oldDocument != null;
  +        new CMSHistory(oldDocument).move(newDocument);
  +    }
  +
  +    /**
        * Checks if a workflow is assigned to the document.
        * This is done by looking for the workflow history file.
        * @param document The document to check.
  @@ -149,15 +162,12 @@
        */
       public Situation buildSituation(Map objectModel) throws WorkflowException {
           Request request = ObjectModelHelper.getRequest(objectModel);
  -
  -        List roleList = (List) request.getAttribute(Role.class.getName());
  -
  -        if (roleList == null) {
  -            throw new WorkflowException("Request does not contain roles!");
  +        Role[] roles;
  +        try {
  +            roles = PolicyAuthorizer.getRoles(request);
  +        } catch (AccessControlException e) {
  +            throw new WorkflowException(e);
           }
  -
  -        Role[] roles = (Role[]) roleList.toArray(new Role[roleList.size()]);
  -
           return buildSituation(roles);
       }
   
  
  
  
  1.9       +22 -4     cocoon-lenya/src/java/org/apache/lenya/cms/workflow/CMSHistory.java
  
  Index: CMSHistory.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/workflow/CMSHistory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CMSHistory.java	5 Aug 2003 12:00:16 -0000	1.8
  +++ CMSHistory.java	5 Aug 2003 16:29:06 -0000	1.9
  @@ -111,11 +111,18 @@
   	 * @see org.apache.lenya.workflow.impl.History#getHistoryFile()
   	 */
       protected File getHistoryFile() {
  +        return getHistoryFile(getDocument());
  +    }
  +
  +    /**
  +     *  (non-Javadoc)
  +     * @see org.apache.lenya.workflow.impl.History#getHistoryFile()
  +     */
  +    protected File getHistoryFile(Document document) {
           File historyDirectory = new File(document.getPublication().getDirectory(), HISTORY_PATH);
  -		DocumentIdToPathMapper pathMapper=document.getPublication().getPathMapper();
  -		String documentPath = pathMapper.getPath(getDocument().getId(), ""); 
  +        DocumentIdToPathMapper pathMapper = document.getPublication().getPathMapper();
  +        String documentPath = pathMapper.getPath(document.getId(), document.getLanguage());

           File historyFile = new File(historyDirectory, documentPath);
  -
           return historyFile;
       }
   
  @@ -154,5 +161,16 @@
           String workflowId = getWorkflowId();
           CMSHistory newHistory = new CMSHistory(newDocument);
           newHistory.initialize(workflowId);
  +    }
  +    
  +    /**
  +     * Moves this history to a new document.
  +     * @param newDocument The new document.
  +     * @throws WorkflowException when something went wrong.
  +     */
  +    protected void move(Document newDocument) throws WorkflowException {
  +        assert newDocument != null;
  +        move(getHistoryFile(newDocument));
  +        setDocument(newDocument);
       }
   }
  
  
  
  1.9       +58 -21    cocoon-lenya/src/java/org/apache/lenya/workflow/impl/History.java
  
  Index: History.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/workflow/impl/History.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- History.java	5 Aug 2003 12:01:15 -0000	1.8
  +++ History.java	5 Aug 2003 16:29:06 -0000	1.9
  @@ -73,10 +73,13 @@
   import org.w3c.dom.Element;
   
   import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
  +import java.nio.channels.FileChannel;
   
   import javax.xml.transform.TransformerException;
   
  -
   /**
    * @author andreas
    *
  @@ -105,8 +108,8 @@
               file.getParentFile().mkdirs();
               file.createNewFile();
   
  -            NamespaceHelper helper = new NamespaceHelper(Workflow.NAMESPACE,
  -                    Workflow.DEFAULT_PREFIX, HISTORY_ELEMENT);
  +            NamespaceHelper helper =
  +                new NamespaceHelper(Workflow.NAMESPACE, Workflow.DEFAULT_PREFIX, HISTORY_ELEMENT);
   
               Element historyElement = helper.getDocument().getDocumentElement();
               historyElement.setAttribute(WORKFLOW_ATTRIBUTE, workflowId);
  @@ -129,7 +132,7 @@
   
       private WorkflowInstanceImpl instance = null;
       private String workflowId = null;
  -    
  +
       /**
        * Returns the namespace helper for the history file.
        * @return A namespace helper.
  @@ -145,7 +148,7 @@
           }
           return helper;
       }
  -    
  +
       /**
        * Returns the workflow ID for this history.
        * @return A string.
  @@ -218,8 +221,7 @@
        * @return A workflow instance object.
        * @throws WorkflowException if something goes wrong.
        */
  -    protected abstract WorkflowInstanceImpl createInstance()
  -        throws WorkflowException;
  +    protected abstract WorkflowInstanceImpl createInstance() throws WorkflowException;
   
       /**
        * Creates a new version element. This method is called after a tansition invocation.
  @@ -229,8 +231,11 @@
        * @param event The event that was invoked.
        * @return An XML element.
        */
  -    protected Element createVersionElement(NamespaceHelper helper, StateImpl state,
  -        Situation situation, Event event) {
  +    protected Element createVersionElement(
  +        NamespaceHelper helper,
  +        StateImpl state,
  +        Situation situation,
  +        Event event) {
           Element versionElement = helper.createElement(VERSION_ELEMENT);
           versionElement.setAttribute(STATE_ATTRIBUTE, state.getId());
           versionElement.setAttribute(EVENT_ATTRIBUTE, event.getName());
  @@ -253,12 +258,16 @@
               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);
   
               CMSSituation cmsSituation = (CMSSituation) situation;
  -            Element versionElement = createVersionElement(helper,
  -                    (StateImpl) instance.getCurrentState(), situation, event);
  +            Element versionElement =
  +                createVersionElement(
  +                    helper,
  +                    (StateImpl) instance.getCurrentState(),
  +                    situation,
  +                    event);
   
               root.appendChild(versionElement);
   
  @@ -281,8 +290,7 @@
        * Saves the state variables as children of the document element.
        * @param helper The helper that holds the document.
        */
  -    protected void saveVariables(NamespaceHelper helper)
  -        throws WorkflowException {
  +    protected void saveVariables(NamespaceHelper helper) throws WorkflowException {
           Element parent = helper.getDocument().getDocumentElement();
           BooleanVariable[] variables = getInstance().getWorkflowImpl().getVariables();
   
  @@ -291,9 +299,17 @@
               boolean value = getInstance().getValue(name);
   
               try {
  -                Element element = (Element) XPathAPI.selectSingleNode(parent,
  -                        "*[local-name() = '" + VARIABLE_ELEMENT + "']" + "[@" + NAME_ATTRIBUTE
+
  -                        " = '" + name + "']");
  +                Element element =
  +                    (Element) XPathAPI.selectSingleNode(
  +                        parent,
  +                        "*[local-name() = '"
  +                            + VARIABLE_ELEMENT
  +                            + "']"
  +                            + "[@"
  +                            + NAME_ATTRIBUTE
  +                            + " = '"
  +                            + name
  +                            + "']");
   
                   if (element == null) {
                       element = helper.createElement(VARIABLE_ELEMENT);
  @@ -336,8 +352,8 @@
       protected void restoreState(WorkflowInstanceImpl instance, NamespaceHelper helper)
           throws WorkflowException {
           State state;
  -        Element[] versionElements = helper.getChildren(helper.getDocument().getDocumentElement(),
  -                VERSION_ELEMENT);
  +        Element[] versionElements =
  +            helper.getChildren(helper.getDocument().getDocumentElement(), VERSION_ELEMENT);
   
           if (versionElements.length > 0) {
               Element lastElement = versionElements[versionElements.length - 1];
  @@ -349,4 +365,25 @@
   
           instance.setCurrentState(state);
       }
  +
  +    /**
  +     * Moves this history to a new file.
  +     * @param newFile The new file.
  +     * @throws WorkflowException when something went wrong.
  +     */
  +    protected void move(File newFile) throws WorkflowException {
  +
  +        try {
  +            newFile.getParentFile().mkdirs();
  +            newFile.createNewFile();
  +            FileChannel sourceChannel = new FileInputStream(getHistoryFile()).getChannel();
  +            FileChannel destinationChannel = new FileOutputStream(newFile).getChannel();
  +            destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
  +            sourceChannel.close();
  +            destinationChannel.close();
  +        } catch (IOException e) {
  +            throw new WorkflowException(e);
  +        }
  +    }
  +
   }
  
  
  

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


Mime
View raw message