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/04/09 16:38:58 UTC

svn commit: r160686 - lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java

Author: andreas
Date: Sat Apr  9 07:38:58 2005
New Revision: 160686

URL: http://svn.apache.org/viewcvs?view=rev&rev=160686
Log:
added SiteUtil class

Added:
    lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java

Added: lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java?view=auto&rev=160686
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/SiteUtil.java Sat Apr  9 07:38:58 2005
@@ -0,0 +1,270 @@
+/*
+ * 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;
+
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceSelector;
+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.DocumentIdentityMap;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.util.DocumentSet;
+
+/**
+ * Utility to handle site structures.
+ * 
+ * @version $Id:$
+ */
+public class SiteUtil {
+
+    private SiteUtil() {
+    }
+
+    /**
+     * Returns a site structure object.
+     * @param map The identity map.
+     * @param publication The publication.
+     * @param area The area.
+     * @param manager The service manager.
+     * @return A site structure.
+     * @throws SiteException if an error occurs.
+     */
+    public static SiteStructure getSiteStructure(ServiceManager manager, DocumentIdentityMap map,
+            Publication publication, String area) throws SiteException {
+
+        SiteStructure structure = null;
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(publication.getSiteManagerHint());
+            structure = siteManager.getSiteStructure(map, publication, area);
+        } catch (Exception e) {
+            throw new SiteException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                manager.release(selector);
+            }
+        }
+        return structure;
+    }
+
+    /**
+     * Returns the site structure a document belongs to.
+     * @param manager The service manager.
+     * @param document The document.
+     * @return A site structure.
+     * @throws SiteException if an error occurs.
+     */
+    public static SiteStructure getSiteStructure(ServiceManager manager, Document document)
+            throws SiteException {
+        return SiteUtil.getSiteStructure(manager, document.getIdentityMap(), document
+                .getPublication(), document.getArea());
+    }
+
+    /**
+     * Returns a sub-site starting with a certain document, which includes the document itself and
+     * all documents which require this document, including all language versions.
+     * @param manager The service manager.
+     * @param document The top-level document.
+     * @return A document set.
+     * @throws SiteException if an error occurs.
+     */
+    public static DocumentSet getSubSite(ServiceManager manager, Document document)
+            throws SiteException {
+        DocumentSet set = null;
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(document.getPublication()
+                    .getSiteManagerHint());
+            set = new DocumentSet(siteManager.getRequiringResources(document));
+
+            String[] languages = document.getLanguages();
+            for (int i = 0; i < languages.length; i++) {
+                Document version = document.getIdentityMap().getLanguageVersion(document,
+                        languages[i]);
+                set.add(version);
+            }
+
+        } catch (Exception e) {
+            throw new SiteException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                manager.release(selector);
+            }
+        }
+        return set;
+    }
+
+    /**
+     * Sorts a document set in ascending order.
+     * @param manager The service manager.
+     * @param set The set.
+     * @throws SiteException if an error occurs.
+     */
+    public static void sortAscending(ServiceManager manager, DocumentSet set) throws SiteException {
+
+        if (set.isEmpty()) {
+            return;
+        }
+
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(set.getDocuments()[0].getPublication()
+                    .getSiteManagerHint());
+            siteManager.sortAscending(set);
+        } catch (Exception e) {
+            throw new SiteException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                manager.release(selector);
+            }
+        }
+    }
+
+    /**
+     * Sorts a document set in descending order.
+     * @param manager The service manager.
+     * @param set The set.
+     * @throws SiteException if an error occurs.
+     */
+    public static void sortDescending(ServiceManager manager, DocumentSet set) throws SiteException {
+        SiteUtil.sortAscending(manager, set);
+        set.reverse();
+    }
+
+    /**
+     * Replace the target documents.
+     */
+    public static final int MODE_REPLACE = 0;
+
+    /**
+     * Cancel the command if one of the target document(s) exists.
+     */
+    public static final int MODE_CANCEL = 1;
+
+    /**
+     * Change the ID of a target document if it already exists.
+     */
+    public static final int MODE_CHANGE_ID = 2;
+
+    /**
+     * Returns a document set that represents the transfer of a sub-site to another area.
+     * @param manager The service manager.
+     * @param source The source document.
+     * @param targetArea The target area.
+     * @param mode The mode: {@link #MODE_REPLACE},{@link #MODE_CANCEL},{@link #MODE_CHANGE_ID}.
+     * @return A document set.
+     * @throws SiteException if an error occurs.
+     */
+    public static DocumentSet getTransferedSubSite(ServiceManager manager, Document source,
+            String targetArea, int mode) throws SiteException {
+
+        DocumentSet set = new DocumentSet();
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(source.getPublication()
+                    .getSiteManagerHint());
+
+            DocumentSet subSite = SiteUtil.getSubSite(manager, source);
+            Document[] docs = subSite.getDocuments();
+            for (int i = 0; i < docs.length; i++) {
+                docs[i].lock();
+                Document target = SiteUtil.getTarget(siteManager, docs[i], targetArea, mode);
+                if (target != null) {
+                    set.add(target);
+                }
+            }
+
+        } catch (Exception e) {
+            throw new SiteException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                manager.release(selector);
+            }
+        }
+        return set;
+    }
+
+    protected static Document getTarget(SiteManager siteManager, Document source,
+            String targetArea, int mode) throws SiteException, DocumentException,
+            DocumentBuildException {
+        Document target = source.getIdentityMap().getAreaVersion(source, targetArea);
+        switch (mode) {
+        case MODE_REPLACE:
+            break;
+        case MODE_CANCEL:
+            if (target.exists()) {
+                target = null;
+            }
+            break;
+        case MODE_CHANGE_ID:
+            target = siteManager.getAvailableDocument(target);
+            break;
+        }
+        return target;
+    }
+
+    /**
+     * @see org.apache.lenya.cms.site.SiteManager#getAvailableDocument(Document)
+     * @param manager The service manager.
+     * @param document The document.
+     * @return A document.
+     * @throws SiteException if an error occurs.
+     */
+    public static Document getAvailableDocument(ServiceManager manager, Document document)
+            throws SiteException {
+        ServiceSelector selector = null;
+        SiteManager siteManager = null;
+        try {
+            selector = (ServiceSelector) manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(document.getPublication()
+                    .getSiteManagerHint());
+
+            return siteManager.getAvailableDocument(document);
+        } catch (Exception e) {
+            throw new SiteException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                manager.release(selector);
+            }
+        }
+    }
+
+}
\ No newline at end of file



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