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 2006/08/10 18:24:27 UTC

svn commit: r430424 - in /lenya/trunk/src: impl/test/org/apache/lenya/cms/site/simple/ java/org/apache/lenya/cms/site/ java/org/apache/lenya/cms/site/simple/

Author: andreas
Date: Thu Aug 10 09:24:25 2006
New Revision: 430424

URL: http://svn.apache.org/viewvc?rev=430424&view=rev
Log:
Updated SimpleSiteManager to use UUIDs

Added:
    lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/
    lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/SimpleSiteManagerTest.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/site/AbstractSiteNode.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/Label.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStore.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStoreFactory.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteManager.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteNode.java

Added: lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/SimpleSiteManagerTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/SimpleSiteManagerTest.java?rev=430424&view=auto
==============================================================================
--- lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/SimpleSiteManagerTest.java (added)
+++ lenya/trunk/src/impl/test/org/apache/lenya/cms/site/simple/SimpleSiteManagerTest.java Thu Aug 10 09:24:25 2006
@@ -0,0 +1,85 @@
+/*
+ * 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.site.simple;
+
+import org.apache.avalon.framework.service.ServiceSelector;
+import org.apache.lenya.ac.impl.AbstractAccessControlTest;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.PublicationUtil;
+import org.apache.lenya.cms.repository.RepositoryUtil;
+import org.apache.lenya.cms.repository.Session;
+import org.apache.lenya.cms.site.Link;
+import org.apache.lenya.cms.site.SiteManager;
+import org.apache.lenya.cms.site.SiteNode;
+import org.apache.lenya.cms.site.SiteStructure;
+
+public class SimpleSiteManagerTest extends AbstractAccessControlTest {
+    
+    public void testSimpleSiteManager() throws Exception {
+        
+        Session session = RepositoryUtil.getSession(getManager(), getRequest());
+        DocumentFactory factory = DocumentUtil.createDocumentIdentityMap(getManager(), session);
+
+        Publication pub = PublicationUtil.getPublication(getManager(), "blog");
+        
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        
+        try {
+            selector = (ServiceSelector) getManager().lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(pub.getSiteManagerHint());
+            SiteStructure structure = siteManager.getSiteStructure(factory, pub, Publication.AUTHORING_AREA);
+            
+            assertTrue(structure.contains("/sidebar"));
+            
+            SiteNode node = structure.getNode("/sidebar");
+            assertNotNull(node);
+            assertNotNull(node.getUuid());
+            
+            String[] languages = node.getLanguages();
+            assertTrue(languages.length > 0);
+            for (int i = 0; i < languages.length; i++) {
+                Link link = node.getLink(languages[i]);
+                assertEquals(link.getLanguage(), languages[i]);
+                assertNotNull(link.getLabel());
+                Document doc = link.getDocument();
+                assertNotNull(doc);
+                
+                String docUuid = doc.getUUID();
+                String nodeUuid = node.getUuid();
+                
+                assertNotNull(doc.getUUID());
+                assertEquals(docUuid, nodeUuid);
+                assertEquals(doc.getLanguage(), link.getLanguage());
+            }
+        }
+        finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                getManager().release(selector);
+            }
+        }
+        
+    }
+    
+
+}

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/AbstractSiteNode.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/AbstractSiteNode.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/AbstractSiteNode.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/AbstractSiteNode.java Thu Aug 10 09:24:25 2006
@@ -16,7 +16,10 @@
  */
 package org.apache.lenya.cms.site;
 
+import java.util.Arrays;
+
 import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.util.Assert;
 
 /**
  * Abstract site node implementation.
@@ -29,8 +32,15 @@
 
     protected AbstractSiteNode(Publication publication, SiteStructure structure, String path, String uuid) {
         super();
+        
+        Assert.notNull("structure", structure);
         this.structure = structure;
+        
+        Assert.notNull("path", path);
+        Assert.isTrue("path starts with /", path.startsWith("/"));
         this.path = path;
+        
+        Assert.notNull("uuid", uuid);
         this.uuid = uuid;
     }
 
@@ -78,6 +88,10 @@
 
     public String getUuid() {
         return this.uuid;
+    }
+
+    public boolean hasLink(String language) {
+        return Arrays.asList(getLanguages()).contains(language);
     }
 
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/Label.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/Label.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/Label.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/Label.java Thu Aug 10 09:24:25 2006
@@ -113,14 +113,13 @@
     }
     
     private SiteNode node;
-    private String uuid;
     private DocumentFactory factory;
 
     public Document getDocument() {
         Publication pub = getNode().getStructure().getPublication();
         String area = getNode().getStructure().getArea();
         try {
-            return this.factory.get(pub, area, this.uuid, getLanguage());
+            return this.factory.get(pub, area, getNode().getUuid(), getLanguage());
         } catch (DocumentBuildException e) {
             throw new RuntimeException(e);
         }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStore.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStore.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStore.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStore.java Thu Aug 10 09:24:25 2006
@@ -16,20 +16,24 @@
  */
 package org.apache.lenya.cms.site.simple;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.lenya.cms.cocoon.source.SourceUtil;
 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.DocumentIdentifier;
 import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentIdentifier;
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.publication.util.CollectionImpl;
 import org.apache.lenya.cms.repository.Node;
 import org.apache.lenya.cms.site.SiteException;
 import org.apache.lenya.cms.site.SiteNode;
 import org.apache.lenya.cms.site.SiteStructure;
+import org.apache.lenya.transaction.TransactionException;
 import org.apache.lenya.xml.NamespaceHelper;
 import org.w3c.dom.Element;
 
@@ -40,8 +44,6 @@
  */
 public class DocumentStore extends CollectionImpl implements SiteStructure {
 
-    protected static final String DOCUMENT_PATH = "/site";
-    
     /**
      * The identifiable type.
      */
@@ -50,20 +52,27 @@
     /**
      * @param manager The service manager.
      * @param map The identity map.
-     * @param publication The publication.
+     * @param pub The publication.
      * @param area The area.
+     * @param uuid The UUID.
      * @param _logger The logger.
      * @throws DocumentException if an error occurs.
      */
-    public DocumentStore(ServiceManager manager, DocumentFactory map, Publication publication,
-            String area, Logger _logger) throws DocumentException {
-        super(manager, map, new DocumentIdentifier(publication,
-                area,
-                DOCUMENT_PATH,
-                publication.getDefaultLanguage()), _logger);
+    public DocumentStore(ServiceManager manager, DocumentFactory map, Publication pub, String area,
+            String uuid, Logger _logger) throws DocumentException {
+        super(manager,
+                map,
+                new DocumentIdentifier(pub, area, uuid, pub.getDefaultLanguage()),
+                _logger);
     }
 
+    protected static final String NAMESPACE = "http://apache.org/lenya/sitemanagement/simple/1.0";
+
     protected static final String ATTRIBUTE_LANGUAGE = "xml:lang";
+    protected static final String ATTRIBUTE_PATH = "path";
+
+    private Map path2uuid = new HashMap();
+    private Map uuid2path = new HashMap();
 
     /**
      * @see org.apache.lenya.cms.publication.util.CollectionImpl#createDocumentElement(org.apache.lenya.cms.publication.Document,
@@ -73,6 +82,7 @@
             throws DocumentException {
         Element element = super.createDocumentElement(document, helper);
         element.setAttribute(ATTRIBUTE_LANGUAGE, document.getLanguage());
+        element.setAttribute(ATTRIBUTE_PATH, getPath(document.getUUID()));
         return element;
     }
 
@@ -82,10 +92,15 @@
     protected Document loadDocument(Element documentElement) throws DocumentBuildException {
         String uuid = documentElement.getAttribute(ATTRIBUTE_UUID);
         String language = documentElement.getAttribute(ATTRIBUTE_LANGUAGE);
+        String path = documentElement.getAttribute(ATTRIBUTE_PATH);
         Document document = getDelegate().getFactory().get(getDelegate().getPublication(),
                 getDelegate().getArea(),
                 uuid,
                 language);
+        if (!this.uuid2path.containsKey(uuid)) {
+            this.uuid2path.put(uuid, path);
+            this.path2uuid.put(path, uuid);
+        }
         return document;
     }
 
@@ -106,13 +121,13 @@
     }
 
     public boolean contains(String path) {
-        return true;
+        return path2uuid().containsKey(path);
     }
 
     public boolean containsUuid(String uuid) {
-        return getDocument(uuid) != null;
+        return uuid2path().containsKey(uuid);
     }
-    
+
     protected Document getDocument(String uuid) {
         Document[] docs;
         try {
@@ -129,12 +144,24 @@
     }
 
     public SiteNode getByUuid(String uuid) throws SiteException {
-        // TODO Auto-generated method stub
-        return null;
+        String path = getPath(uuid);
+        return new SimpleSiteNode(this, path, uuid);
+    }
+
+    protected String getPath(String uuid) {
+        return (String) uuid2path().get(uuid);
     }
 
     public SiteNode getNode(String path) throws SiteException {
-        return null;
+        if (!contains(path)) {
+            throw new SiteException(this + " does not contain the path [" + path + "]");
+        }
+        String uuid = (String) path2uuid().get(path);
+        return new SimpleSiteNode(this, path, uuid);
+    }
+    
+    public String toString() {
+        return getPublication().getId() + ":" + getArea();
     }
 
     public Publication getPublication() {
@@ -144,5 +171,39 @@
     public String getArea() {
         return getDelegate().getArea();
     }
+
+    public void add(String path, Document document) throws DocumentException {
+        super.add(document);
+        String uuid = document.getUUID();
+        if (!uuid2path().containsKey(uuid)) {
+            uuid2path().put(uuid, path);
+            path2uuid().put(path, uuid);
+        }
+    }
+
+    public void setPath(Document document, String path) throws TransactionException {
+        uuid2path().put(document.getUUID(), path);
+        path2uuid().put(path, document.getUUID());
+        save();
+    }
     
+    protected Map path2uuid() {
+        try {
+            load();
+        } catch (DocumentException e) {
+            throw new RuntimeException(e);
+        }
+        return this.path2uuid;
+    }
+    
+    protected Map uuid2path() {
+        try {
+            load();
+        } catch (DocumentException e) {
+            throw new RuntimeException(e);
+        }
+        return this.uuid2path;
+    }
+
+
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStoreFactory.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStoreFactory.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStoreFactory.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/simple/DocumentStoreFactory.java Thu Aug 10 09:24:25 2006
@@ -28,6 +28,7 @@
 import org.apache.lenya.cms.repository.RepositoryItem;
 import org.apache.lenya.cms.repository.RepositoryItemFactory;
 import org.apache.lenya.cms.repository.Session;
+import org.apache.lenya.util.Assert;
 
 /**
  * Factory for sitetree objects.
@@ -61,14 +62,18 @@
      */
     public RepositoryItem buildItem(Session session, String key) throws RepositoryException {
         String[] snippets = key.split(":");
+        
+        Assert.isTrue("key [" + key + "] is invalid!", snippets.length == 3);
+        
         String publicationId = snippets[0];
         String area = snippets[1];
+        String uuid = snippets[2];
         DocumentStore store;
         try {
             Publication publication = PublicationUtil.getPublication(this.manager, publicationId);
 
             DocumentFactory docMap = DocumentUtil.createDocumentIdentityMap(this.manager, session);
-            store = new DocumentStore(this.manager, docMap, publication, area, getLogger());
+            store = new DocumentStore(this.manager, docMap, publication, area, uuid, getLogger());
         } catch (Exception e) {
             throw new RepositoryException(e);
         }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteManager.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteManager.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteManager.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteManager.java Thu Aug 10 09:24:25 2006
@@ -19,6 +19,7 @@
 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.cocoon.source.SourceUtil;
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentException;
 import org.apache.lenya.cms.publication.DocumentFactory;
@@ -26,9 +27,10 @@
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.repository.RepositoryItemFactory;
 import org.apache.lenya.cms.site.AbstractSiteManager;
-import org.apache.lenya.cms.site.SiteNode;
 import org.apache.lenya.cms.site.SiteException;
+import org.apache.lenya.cms.site.SiteNode;
 import org.apache.lenya.cms.site.SiteStructure;
+import org.apache.lenya.transaction.TransactionException;
 
 /**
  * Simple site manager which does not imply structural information. The documents are stored in
@@ -77,11 +79,11 @@
     /**
      * @see org.apache.lenya.cms.site.SiteManager#add(org.apache.lenya.cms.publication.Document)
      */
-    public void add(Document document) throws SiteException {
+    public void add(String path, Document document) throws SiteException {
 
         DocumentStore store = getStore(document);
         try {
-            store.add(document);
+            store.add(path, document);
         } catch (DocumentException e) {
             throw new SiteException(e);
         }
@@ -120,18 +122,28 @@
         return store;
     }
 
+    protected String getCollectionUuid(Publication pub, String area) {
+        String sourceUri = pub.getContentURI(area) + DOCUMENT_PATH;
+        try {
+            org.w3c.dom.Document xml = SourceUtil.readDOM(sourceUri, manager);
+            if (!xml.getDocumentElement().hasAttribute("uuid")) {
+                throw new RuntimeException("The document element of [" + sourceUri + "] doesn't contain a uuid attribute!");
+            }
+            return xml.getDocumentElement().getAttribute("uuid");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    protected static final String DOCUMENT_PATH = "/site.xml";
+
     /**
      * @param publication The publication.
      * @param area The area.
      * @return The key to store sitetree objects in the identity map.
      */
     protected String getKey(Publication publication, String area) {
-        return publication.getId() + ":" + area;
-    }
-
-    public String getUUID(DocumentFactory factory, Publication pub, String area, String path)
-            throws SiteException {
-        throw new IllegalStateException("Not implemented yet!");
+        return publication.getId() + ":" + area + ":" + getCollectionUuid(publication, area);
     }
 
     /**
@@ -140,11 +152,11 @@
     public boolean contains(Document resource) throws SiteException {
 
         try {
-            if (resource.getPath().equals(DocumentStore.DOCUMENT_PATH)) {
+            DocumentStore store = getStore(resource);
+            if (resource.equals(store)) {
                 return true;
             }
-
-            return getStore(resource).contains(resource);
+            return store.contains(resource);
         } catch (DocumentException e) {
             throw new SiteException(e);
         }
@@ -157,14 +169,14 @@
         try {
             boolean contains = false;
 
-            String[] languages = resource.getPublication().getLanguages();
+            String[] languages = resource.getLanguages();
             for (int i = 0; i < languages.length; i++) {
-                Document doc = resource.getFactory().getLanguageVersion(resource, languages[i]);
+                Document doc = resource.getTranslation(languages[i]);
                 DocumentStore store = getStore(doc);
                 contains = contains || store.contains(doc);
             }
 
-            return getStore(resource).contains(resource);
+            return contains;
         } catch (Exception e) {
             throw new SiteException(e);
         }
@@ -244,7 +256,8 @@
     }
 
     /**
-     * @see org.apache.lenya.cms.site.SiteManager#getAvailableDocument(org.apache.lenya.cms.publication.Document)
+     * @see org.apache.lenya.cms.site.SiteManager#getAvailableLocator(DocumentFactory,
+     *      DocumentLocator)
      */
     public DocumentLocator getAvailableLocator(DocumentFactory factory, DocumentLocator document)
             throws SiteException {
@@ -255,36 +268,17 @@
         return true;
     }
 
-    public String getPath(DocumentFactory factory, Publication pub, String area, String uuid)
-            throws SiteException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public String getUUID(String area, String path) throws SiteException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public void add(String path, Document document) throws SiteException {
-        // TODO Auto-generated method stub
-
-    }
-
     public boolean contains(DocumentFactory factory, Publication pub, String area, String path)
             throws SiteException {
-        // TODO Auto-generated method stub
-        return false;
-    }
-
-    public boolean contains(DocumentFactory factory, DocumentLocator locator) throws SiteException {
-        // TODO Auto-generated method stub
-        return false;
+        return getStore(factory, pub, area).contains(path);
     }
 
     public void set(String path, Document document) throws SiteException {
-        // TODO Auto-generated method stub
-        
+        try {
+            getStore(document).setPath(document, path);
+        } catch (TransactionException e) {
+            throw new SiteException(e);
+        }
     }
 
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteNode.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteNode.java?rev=430424&r1=430423&r2=430424&view=diff
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteNode.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/simple/SimpleSiteNode.java Thu Aug 10 09:24:25 2006
@@ -16,38 +16,52 @@
  */
 package org.apache.lenya.cms.site.simple;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentException;
 import org.apache.lenya.cms.site.AbstractSiteNode;
+import org.apache.lenya.cms.site.Label;
 import org.apache.lenya.cms.site.Link;
 import org.apache.lenya.cms.site.SiteException;
 import org.apache.lenya.cms.site.SiteStructure;
 
 public class SimpleSiteNode extends AbstractSiteNode {
     
-    private String uuid;
-    
     public SimpleSiteNode(DocumentStore store, String path, String uuid) {
         super(store.getPublication(), store, path, uuid);
     }
 
-    public String[] getLanguages() {
-        return null;
-    }
-
     public Link getLink(String language) throws SiteException {
-        // TODO Auto-generated method stub
-        return null;
+        DocumentStore store = (DocumentStore) getStructure();
+        return new Label(store.getDelegate().getFactory(), this, "", language);
     }
 
     public String getName() {
-        // TODO Auto-generated method stub
-        return null;
+        String[] steps = getPath().split("/");
+        return steps[steps.length - 1];
     }
 
-     public boolean hasLink(String language) {
-        // TODO Auto-generated method stub
-        return false;
+    public String[] getLanguages() {
+        DocumentStore store = (DocumentStore) getStructure();
+        List languages = new ArrayList();
+        Document[] docs;
+        try {
+            docs = store.getDocuments();
+        } catch (DocumentException e) {
+            throw new RuntimeException(e);
+        }
+        for (int i = 0; i < docs.length; i++) {
+            if (docs[i].getUUID().equals(getUuid())) {
+                if (languages.contains(docs[i].getLanguage())) {
+                    throw new RuntimeException("Document [" + docs[i] + "] is contained twice!");
+                }
+                languages.add(docs[i].getLanguage());
+            }
+        }
+        return (String[]) languages.toArray(new String[languages.size()]);
     }
 
 }



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