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/02/23 22:49:04 UTC

svn commit: r155080 - in lenya/trunk/src/java/org/apache/lenya/cms/site/usecases: ChangeNodeID.java Create.java CreateDocument.java CreateLanguage.java SiteUtility.java

Author: andreas
Date: Wed Feb 23 13:48:58 2005
New Revision: 155080

URL: http://svn.apache.org/viewcvs?view=rev&rev=155080
Log:
added precondition checks, allow to create documents from non-document URLs

Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/Create.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateLanguage.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUtility.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java?view=diff&r1=155079&r2=155080
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java Wed Feb 23 13:48:58 2005
@@ -50,7 +50,11 @@
         String nodeId = getParameterAsString(NODE_ID);
         Document parent = identityMap.getFactory().getParent(getSourceDocument());
         SiteUtility util = new SiteUtility();
-        addErrorMessages(util.canCreate(parent, nodeId, getSourceDocument().getLanguage()));
+        addErrorMessages(util.canCreate(identityMap,
+                getSourceDocument().getArea(),
+                parent,
+                nodeId,
+                getSourceDocument().getLanguage()));
     }
 
     /**

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/Create.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/Create.java?view=diff&r1=155079&r2=155080
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/Create.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/Create.java Wed Feb 23 13:48:58 2005
@@ -29,9 +29,11 @@
 import org.apache.lenya.cms.metadata.dublincore.DublinCore;
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentException;
+import org.apache.lenya.cms.publication.DocumentFactory;
 import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.URLInformation;
 import org.apache.lenya.cms.site.SiteManager;
-import org.apache.lenya.cms.usecase.DocumentUsecase;
+import org.apache.lenya.cms.usecase.WorkflowUsecase;
 import org.apache.lenya.workflow.WorkflowInstance;
 
 /**
@@ -39,7 +41,7 @@
  * 
  * @version $Id: Create.java 123982 2005-01-03 15:01:19Z andreas $
  */
-public abstract class Create extends DocumentUsecase {
+public abstract class Create extends WorkflowUsecase {
 
     protected static final String LANGUAGE = "language";
     protected static final String LANGUAGES = "languages";
@@ -61,13 +63,6 @@
         if (navigationTitle.equals("")) {
             addErrorMessage("The navigation title is required.");
         }
-
-        /*
-         * DocumentIdentityMap map = getSourceDocument().getIdentityMap();
-         * SiteManager manager = null; try { manager =
-         * getSourceDocument().getPublication().getSiteManager(map); } catch
-         * (SiteException e) { throw new UsecaseException(e); }
-         */
     }
 
     /**
@@ -87,7 +82,7 @@
         instance.getHistory().initialize(getSituation());
 
         setMetaData(document);
-        setTargetDocument(document);
+        setTargetURL(document.getCanonicalWebappURL());
     }
 
     /**
@@ -140,5 +135,31 @@
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         setParameter(DublinCore.ELEMENT_DATE, format.format(new GregorianCalendar().getTime()));
 
+    }
+    
+    /**
+     * @return The source document or <code>null</code> if the usecase was not
+     *         invoked on a document.
+     */
+    protected Document getSourceDocument() {
+        Document document = null;
+        String url = getSourceURL();
+        DocumentFactory factory = getUnitOfWork().getIdentityMap().getFactory();
+        try {
+            if (factory.isDocument(url)) {
+                document = factory.getFromURL(url);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        return document;
+    }
+
+    /**
+     * @return The area without the "info-" prefix.
+     */
+    public String getArea() {
+        URLInformation info = new URLInformation(getSourceURL());
+        return info.getArea();
     }
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java?view=diff&r1=155079&r2=155080
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java Wed Feb 23 13:48:58 2005
@@ -28,7 +28,7 @@
 
 /**
  * Usecase to create a document.
- *
+ * 
  * @version $Id:$
  */
 public class CreateDocument extends Create {
@@ -43,25 +43,33 @@
         super.initParameters();
 
         Document parent = getSourceDocument();
-        setParameter(PARENT_ID, parent.getId());
+        if (parent != null) {
+            setParameter(PARENT_ID, parent.getId());
+        } else {
+            setParameter(PARENT_ID, "");
+        }
 
-        String[] languages = parent.getPublication().getLanguages();
+        String[] languages = getUnitOfWork().getIdentityMap().getPublication().getLanguages();
         setParameter(LANGUAGES, languages);
     }
-    
+
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
      */
     protected void doCheckExecutionConditions() throws Exception {
         super.doCheckExecutionConditions();
-        
+
         String nodeId = getParameterAsString(DOCUMENT_ID);
         Document parent = getSourceDocument();
         String language = getParameterAsString(LANGUAGE);
         SiteUtility util = new SiteUtility();
-        addErrorMessages(util.canCreate(parent, nodeId, language));
+        addErrorMessages(util.canCreate(getUnitOfWork().getIdentityMap(),
+                getArea(),
+                parent,
+                nodeId,
+                language));
     }
-    
+
     /**
      * @see org.apache.lenya.cms.site.usecases.Create#createDocument()
      */
@@ -102,7 +110,7 @@
                 navigationTitle,
                 language,
                 Collections.EMPTY_MAP);
-        
+
         return document;
     }
 
@@ -112,4 +120,4 @@
     protected String getDocumentTypeName() {
         return getParameterAsString(DOCUMENT_TYPE);
     }
-}
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateLanguage.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateLanguage.java?view=diff&r1=155079&r2=155080
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateLanguage.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateLanguage.java Wed Feb 23 13:48:58 2005
@@ -24,6 +24,8 @@
 import org.apache.lenya.cms.authoring.ParentChildCreatorInterface;
 import org.apache.lenya.cms.metadata.dublincore.DublinCore;
 import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentBuildException;
+import org.apache.lenya.cms.publication.DocumentException;
 import org.apache.lenya.cms.publication.DocumentFactory;
 import org.apache.lenya.cms.publication.DocumentType;
 import org.apache.lenya.cms.publication.DocumentTypeBuilder;
@@ -37,7 +39,42 @@
  */
 public class CreateLanguage extends Create {
 
-    private String documentType;
+    private String documentTypeName;
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
+     */
+    protected void doCheckPreconditions() throws Exception {
+        super.doCheckPreconditions();
+
+        String area = getSourceDocument().getArea();
+        if (!area.equals(Publication.AUTHORING_AREA)) {
+            addErrorMessage("This operation is only supported in the authoring area.");
+        }
+
+        if (getNonExistingLanguages().isEmpty()) {
+            addErrorMessage("All language versions do already exist.");
+        }
+    }
+
+    /**
+     * @return All non-existing language strings for the source document.
+     * @throws DocumentBuildException if an error occurs.
+     * @throws DocumentException if an error occurs.
+     */
+    protected List getNonExistingLanguages() throws DocumentBuildException, DocumentException {
+        Document source = getSourceDocument();
+        List nonExistingLanguages = new ArrayList();
+        String[] languages = source.getPublication().getLanguages();
+        DocumentFactory factory = source.getIdentityMap().getFactory();
+        for (int i = 0; i < languages.length; i++) {
+            Document version = factory.get(source.getArea(), source.getId(), languages[i]);
+            if (!version.exists()) {
+                nonExistingLanguages.add(languages[i]);
+            }
+        }
+        return nonExistingLanguages;
+    }
 
     /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
@@ -49,20 +86,11 @@
         DocumentTypeResolver resolver = null;
 
         try {
-
             resolver = (DocumentTypeResolver) this.manager.lookup(DocumentTypeResolver.ROLE);
             DocumentType type = resolver.resolve(source);
-            this.documentType = type.getName();
+            this.documentTypeName = type.getName();
 
-            List nonExistingLanguages = new ArrayList();
-            String[] languages = source.getPublication().getLanguages();
-            DocumentFactory factory = source.getIdentityMap().getFactory();
-            for (int i = 0; i < languages.length; i++) {
-                Document version = factory.get(source.getArea(), source.getId(), languages[i]);
-                if (!version.exists()) {
-                    nonExistingLanguages.add(languages[i]);
-                }
-            }
+            List nonExistingLanguages = getNonExistingLanguages();
             setParameter(LANGUAGES, nonExistingLanguages.toArray(new String[nonExistingLanguages
                     .size()]));
 
@@ -71,7 +99,6 @@
         } finally {
             this.manager.release(resolver);
         }
-
     }
 
     /**
@@ -97,16 +124,21 @@
         DocumentFactory factory = source.getIdentityMap().getFactory();
         Document document = factory.get(area, source.getId(), language);
 
-        DocumentType _documentType = DocumentTypeBuilder.buildDocumentType(documentTypeName,
+        DocumentType documentType = DocumentTypeBuilder.buildDocumentType(documentTypeName,
                 publication);
 
-        String parentId = factory.getParent(document).getId().substring(1);
+        String parentId = "";
+        Document parent = factory.getParent(document);
+        if (parent != null) {
+            parentId = factory.getParent(document).getId().substring(1);
+        }
+        
         String childId = document.getName();
 
         File doctypesDirectory = new File(publication.getDirectory(),
                 DocumentTypeBuilder.DOCTYPE_DIRECTORY);
 
-        _documentType.getCreator().create(new File(doctypesDirectory, "samples"),
+        documentType.getCreator().create(new File(doctypesDirectory, "samples"),
                 new File(publication.getContentDirectory(area), parentId),
                 childId,
                 ParentChildCreatorInterface.BRANCH_NODE,
@@ -121,7 +153,7 @@
      * @see org.apache.lenya.cms.site.usecases.Create#getDocumentTypeName()
      */
     protected String getDocumentTypeName() {
-        return this.documentType;
+        return this.documentTypeName;
     }
 
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUtility.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUtility.java?view=diff&r1=155079&r2=155080
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUtility.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/SiteUtility.java Wed Feb 23 13:48:58 2005
@@ -34,7 +34,10 @@
     /**
      * Checks if a document can be created. This is the case if the document ID
      * is valid and the document does not yet exist.
-     * @param parent The parent of the document.
+     * @param identityMap The identity map to use.
+     * @param area The area.
+     * @param parent The parent of the document or <code>null</code> if the
+     *            document has no parent.
      * @param nodeId The node ID.
      * @param language The language.
      * @return An array of error messages. The array is empty if the document
@@ -42,22 +45,24 @@
      * @throws DocumentBuildException if an error occurs.
      * @throws DocumentException if an error occurs.
      */
-    public String[] canCreate(Document parent, String nodeId, String language)
-            throws DocumentBuildException, DocumentException {
+    public String[] canCreate(DocumentIdentityMap identityMap, String area, Document parent,
+            String nodeId, String language) throws DocumentBuildException, DocumentException {
 
         List errorMessages = new ArrayList();
-        String newDocumentId = parent.getId() + "/" + nodeId;
 
-        DocumentIdentityMap identityMap = parent.getIdentityMap();
+        String newDocumentId;
+        if (parent != null) {
+            newDocumentId = parent.getId() + "/" + nodeId;
+        } else {
+            newDocumentId = "/" + nodeId;
+        }
 
         if (nodeId.equals("")) {
             errorMessages.add("The document ID is required.");
         } else if (nodeId.indexOf("/") > -1) {
             errorMessages.add("The document ID may not contain a slash ('/').");
         } else if (identityMap.getFactory().isValidDocumentId(newDocumentId)) {
-            Document newDocument = identityMap.getFactory().get(parent.getArea(),
-                    newDocumentId,
-                    language);
+            Document newDocument = identityMap.getFactory().get(area, newDocumentId, language);
 
             if (newDocument.exists()) {
                 errorMessages.add("A document with this ID already exists.");



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