incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r225699 - in /incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl: ContentPersistenceServiceImpl.java ContentStoreServiceImpl.java MultiStoreIterator.java PersistenceSupportedTestCase.java
Date Thu, 28 Jul 2005 05:40:57 GMT
Author: clombart
Date: Wed Jul 27 22:40:52 2005
New Revision: 225699

URL: http://svn.apache.org/viewcvs?rev=225699&view=rev
Log:
Review object model (see GRFT-25)

Modified:
    incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentPersistenceServiceImpl.java
    incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentStoreServiceImpl.java
    incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/MultiStoreIterator.java
    incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/PersistenceSupportedTestCase.java

Modified: incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentPersistenceServiceImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentPersistenceServiceImpl.java?rev=225699&r1=225698&r2=225699&view=diff
==============================================================================
--- incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentPersistenceServiceImpl.java (original)
+++ incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentPersistenceServiceImpl.java Wed Jul 27 22:40:52 2005
@@ -1,689 +1,707 @@
-/*
- * Copyright 2000-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.portals.graffito.persistence.impl;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Hashtable;
-import java.util.Iterator;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.portals.graffito.model.impl.SysCmsClass;
-import org.apache.portals.graffito.persistence.ContentPersistenceException;
-import org.apache.portals.graffito.persistence.ContentPersistenceService;
-import org.apache.portals.graffito.persistence.ContentStoreService;
-import org.apache.portals.graffito.search.impl.FilterImpl;
-import org.apache.portals.graffito.store.ContentStore;
-import org.apache.portals.graffito.exception.CmsIncorrectServerException;
-import org.apache.portals.graffito.exception.CmsInstantiateException;
-import org.apache.portals.graffito.exception.CmsPermissionException;
-import org.apache.portals.graffito.model.CmsObject;
-import org.apache.portals.graffito.model.Document;
-import org.apache.portals.graffito.model.HistoryElement;
-import org.apache.portals.graffito.model.Server;
-import org.apache.portals.graffito.search.Filter;
-
-/**
- * Default implementation for
- * {@link org.apache.portals.graffito.persistence.ContentPersistenceService}
- *  
- */
-public class ContentPersistenceServiceImpl implements ContentPersistenceService
-{
-    private final static Log log = LogFactory.getLog(ContentPersistenceServiceImpl.class);
-
-    private static Hashtable omClasses = new Hashtable();
-
-    /**
-     * The Master Graffito Server. The "Graffito Master Server" contains information
-     * about external servers like WebDav server, DB server or other proprietary
-     * content servers. It may also contain some CmsObject but it is not
-     * mandatory.
-     *  
-     */
-    private Server graffitoMasterServer;
-
-    private ContentStoreService contentStoreService;
-
-    /**
-     * Constructor.
-     * 
-     * @param graffitoMasterServer  the Graffito Master Server.  
-     * @param contentStoreService The content store service used by this persistence store
-     */
-    public ContentPersistenceServiceImpl(Server graffitoMasterServer, ContentStoreService contentStoreService)
-    {
-        if (graffitoMasterServer == null)
-        {
-            throw new IllegalArgumentException("graffitoMasterServer cannot be null for PersistenceServiceImpl");
-        }
-        this.graffitoMasterServer = graffitoMasterServer;
-
-        if (contentStoreService == null)
-        {
-            throw new IllegalArgumentException("Content store service cannot be null for PersistenceServiceImpl");
-        }
-        this.contentStoreService = contentStoreService;
-
-        // Register all stores
-        registerAllStores();
-    }
-
-    /**
-     * @see ContentPersistenceService#createObject(java.lang.String)
-     */
-    public Object createObject(String omName) throws CmsInstantiateException
-    {
-        Object cmsObject = null;
-        Class omClass = (Class) omClasses.get(omName);
-        ContentStore store = null;
-        try
-        {
-            if (null == omClass)
-            {
-                store = getGraffitoMasterStore();
-                Filter filter = store.newFilter();
-                filter.addEqualTo("cmsClassId", omName);
-                SysCmsClass sysCmsClass = (SysCmsClass) store.getObjectByQuery(SysCmsClass.class, filter);
-                omClass = Class.forName(sysCmsClass.getCmsClass());
-                omClasses.put(omName, omClass);
-            }
-            cmsObject = omClass.newInstance();
-        }
-        catch (Exception e)
-        {
-            throw new CmsInstantiateException("CmsRespositoruModeService error : Failed to instantiate a cms object : " + omName
-                    + "- " + e);
-        }
-        return cmsObject;
-    }
-
-    /**
-     * @see ContentPersistenceService#insert(java.lang.Object)
-     */
-    public void insert(Object object) throws ContentPersistenceException, CmsPermissionException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(object);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            store.insert(object);
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException(e);
-        }
-    }
-
-    /**
-     * @see ContentPersistenceService#update(java.lang.Object)
-     */
-    public void update(Object object) throws ContentPersistenceException, CmsPermissionException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(object);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            store.update(object);
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException(e);
-        }
-    }
-
-    /**
-     * @see ContentPersistenceService#delete(java.lang.Object)
-     */
-    public void delete(Object object) throws ContentPersistenceException, CmsPermissionException
-    {
-        if (object == null)
-            return;
-
-        try
-        {
-            ContentStore store = findPersistenceStore(object);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            store.delete(object);
-
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException(e);
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#delete(java.lang.String)
-     */
-    public void delete(String uri) throws ContentPersistenceException, CmsPermissionException
-    {
-        try
-        {
-            Filter filter = this.newFilter();
-            filter.addEqualTo("uri", uri);
-            ContentStore store = findPersistenceStore(uri);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-
-            if (uri.equals(store.getScope()))
-            {
-                throw new ContentPersistenceException("Impossible to delete the server root : " + uri);
-            }
-
-            store.deleteAll(CmsObject.class, filter);
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException(e);
-        }
-
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#deleteAll(Class,
-     *      Filter)
-     */
-    public void deleteAll(Class clazz, Filter filter) throws ContentPersistenceException, CmsPermissionException
-    {
-        ContentStore store = null;
-        try
-        {
-            Iterator iterator = findPersistenceStores(filter).iterator();
-            while (iterator.hasNext())
-            {
-                store = (ContentStore) iterator.next();
-                store.deleteAll(clazz, filter);
-            }
-
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException(e);
-        }
-
-    }
-
-    /**
-     * @see ContentPersistenceService#getCmsObject(String)
-     */
-    public CmsObject getCmsObject(String uri) throws ContentPersistenceException, CmsPermissionException
-    {
-
-        return this.getCmsObject(uri, null);
-    }
-
-    /**
-     * @see ContentPersistenceService#getCmsObject(String, String)
-     */
-    public CmsObject getCmsObject(String uri, String versionNumber) throws ContentPersistenceException, CmsPermissionException
-    {
-        ContentStore store = findPersistenceStore(uri);
-        if (store == null)
-        {
-            throw new ContentPersistenceException("Impossible to find the associated store");
-        }
-
-        Filter filter = store.newFilter();
-
-        // if the versionNum is not provided, get the lastest document
-        // version
-        if (versionNumber == null)
-        {
-            filter.addEqualTo("isLastVersion", new Boolean(true));
-        }
-        else
-        {
-            filter.addEqualTo("versionNumber", versionNumber);
-        }
-
-        filter.addEqualTo("uri", uri);
-        CmsObject object = (CmsObject) store.getObjectByQuery(CmsObject.class, filter);
-        return object;
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getChildren(java.lang.String)
-     */
-    public Collection getChildren(String parentUri) throws ContentPersistenceException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(parentUri);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            return store.getChildren(parentUri);
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException("Impossible to get the children", e);
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getDocuments(java.lang.String)
-     */
-    public Collection getDocuments(String uri) throws ContentPersistenceException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(uri);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            return store.getDocuments(uri);
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException("Impossible to get the documents", e);
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getLinks(java.lang.String)
-     */
-    public Collection getLinks(String uri) throws ContentPersistenceException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(uri);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            return store.getLinks(uri);
-
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException("Impossible to get the links", e);
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getFolders(java.lang.String)
-     */
-    public Collection getFolders(String uri) throws ContentPersistenceException
-    {
-        try
-        {
-            ContentStore store = findPersistenceStore(uri);
-            if (store == null)
-            {
-                throw new ContentPersistenceException("Impossible to find the associated store");
-            }
-            return store.getFolders(uri);
-
-        }
-        catch (Exception e)
-        {
-            throw new ContentPersistenceException("Impossible to find the associated store", e);
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getHistory(org.apache.portals.graffito.model.Document)
-     */
-    public HistoryElement getHistory(Document document) throws ContentPersistenceException, CmsPermissionException
-    {
-        ContentStore store = findPersistenceStore(document);
-        if (store == null)
-        {
-            throw new ContentPersistenceException("Impossible to find the associated store");
-        }
-        return store.getHistory(document);
-
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#insertHistoryElement(org.apache.portals.graffito.model.HistoryElement)
-     */
-    public void insertHistoryElement(HistoryElement historyElement) throws ContentPersistenceException, CmsPermissionException
-    {
-        ContentStore store = findPersistenceStore(historyElement.getClass());
-        if (store == null)
-        {
-            throw new ContentPersistenceException("Impossible to find the associated store");
-        }
-        store.insertHistoryElement(historyElement);
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#newFilter()
-     */
-    public Filter newFilter()
-    {
-        return new FilterImpl();
-    }
-
-    /**
-     * @see ContentPersistenceService#getCollectionByQuery(Class,
-     *      org.apache.portals.graffito.search.Filter)
-     */
-    public Collection getCollectionByQuery(Class clazz, Filter filter) throws ContentPersistenceException
-    {
-
-        Collection foundStores = null;
-        ArrayList allCmsObjects = new ArrayList();
-
-        try
-        {
-            // Find all stores concerned by the filter criteria
-            foundStores = findPersistenceStores(filter);
-            Iterator storeIterator = foundStores.iterator();
-
-            // Loop on the found stores and execute the query
-            while (storeIterator.hasNext())
-            {
-                ContentStore store = (ContentStore) storeIterator.next();
-
-                Collection cmsObjects = store.getCollectionByQuery(clazz, filter);
-                allCmsObjects.addAll(cmsObjects);
-            }
-        }
-        catch (CmsPermissionException e)
-        {
-            return new ArrayList();
-        }
-        return allCmsObjects;
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getIteratorByQuery(java.lang.Class,
-     *      org.apache.portals.graffito.search.Filter)
-     */
-    public Iterator getIteratorByQuery(Class clazz, Filter filter) throws ContentPersistenceException
-    {
-        MultiStoreIterator multiStoreIterator = new MultiStoreIterator();
-        try
-        {
-
-            Collection foundStores = null;
-            // Find all stores concerned by the filter criteria
-            foundStores = findPersistenceStores(filter);
-            Iterator storeIterator = foundStores.iterator();
-            while (storeIterator.hasNext())
-            {
-                ContentStore store = (ContentStore) storeIterator.next();
-                Iterator iterator = store.getIteratorByQuery(clazz, filter);
-                multiStoreIterator.addIterator(iterator);
-
-            }
-            return multiStoreIterator;
-        }
-        catch (CmsPermissionException e)
-        {
-            return new MultiStoreIterator();
-        }
-
-    }
-
-    /**
-     * @see ContentPersistenceService#getObjectbyQuery(Class,
-     *      org.apache.portals.graffito.search.Filter)
-     */
-    public Object getObjectbyQuery(Class clazz, Filter filter) throws CmsPermissionException
-    {
-
-        ContentStore store = null;
-        Iterator iterator = findPersistenceStores(filter).iterator();
-        while (iterator.hasNext())
-        {
-            store = (ContentStore) iterator.next();
-            Object cmsObject = store.getObjectByQuery(clazz, filter);
-            if (cmsObject != null)
-            {
-                return cmsObject;
-            }
-        }
-
-        return null;
-    }
-
-    /**
-     * @see ContentPersistenceService#getGraffitoMasterStore()
-     */
-    public ContentStore getGraffitoMasterStore()
-    {
-        ContentStore store = contentStoreService.getStore(graffitoMasterServer.getAlias());
-
-        return store;
-    }
-
-    /**
-     * @see ContentPersistenceService#getStore(String)
-     */
-    public ContentStore getStore(String persistenceStoreName)
-    {
-        return contentStoreService.getStore(persistenceStoreName);
-    }
-
-    /**
-     * @see ContentPersistenceService#getServer(String)
-     */
-    public Server getServer(String scope) throws ContentPersistenceException, CmsPermissionException
-    {
-        if (scope.equals(graffitoMasterServer.getScope()))
-        {
-            return graffitoMasterServer;
-        }
-
-        // All server references are stored into the Graffito Master Store
-        ContentStore store = store = getGraffitoMasterStore();
-        Filter filter = store.newFilter();
-        filter.addEqualTo("scope", scope);
-        return (Server) store.getObjectByQuery(Server.class, filter);
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#registerServer(org.apache.portals.graffito.model.Server)
-     */
-    public void registerServer(Server server) throws CmsPermissionException, CmsIncorrectServerException
-    {
-        try
-        {
-            if (!contentStoreService.canRegisterServer(server))
-            {
-                throw new CmsIncorrectServerException("Impossible to instantiate the ContentStore for server "
-                        + server.getAlias() + " - The server scope already exists");
-            }
-            contentStoreService.newStore(server);
-        }
-        catch (CmsInstantiateException e)
-        {
-            throw new CmsIncorrectServerException("Impossible to instantiate the ContentStore for server " + server.getAlias(), e);
-
-        }
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#unRegisterServer(org.apache.portals.graffito.model.Server)
-     */
-    public void unRegisterServer(Server server) throws CmsPermissionException, CmsIncorrectServerException
-    {
-        // it is not authorized to unregister the Graffito Master Store.         
-        if (this.graffitoMasterServer.equals(server))
-        {
-            throw new CmsIncorrectServerException("Cannot unregister the Graffito Master Server");
-        }
-        contentStoreService.removeStore(server.getAlias());
-    }
-
-    /**
-     * @see org.apache.portals.graffito.persistence.ContentPersistenceService#getServers(boolean)
-     */
-    public Collection getServers(boolean withMasterServer)
-    {
-        try
-        {
-            ContentStore store = getGraffitoMasterStore();
-            Filter filter = store.newFilter();
-            Collection servers = store.getCollectionByQuery(Server.class, filter);
-            if (withMasterServer)
-            {
-                // Add also the Graffito Master Server
-                servers.add(graffitoMasterServer);
-            }
-            return servers;
-        }
-        catch (CmsPermissionException e)
-        {
-            return new ArrayList();
-        }
-    }
-
-    /**
-     * Find the persistence store associated to all kind of object (CmsObject,
-     * Server, ...)
-     * 
-     * @param object The object for which the persistence store has to be found
-     * @return the persistence store found or null
-     */
-    private ContentStore findPersistenceStore(Object object)
-    {
-        // if the ojbect is an instance of Server => Get the Graffito Master Server
-        // All servers references are stored into the Graffito Master Server
-        if (object instanceof Server)
-        {
-            return this.getGraffitoMasterStore();
-        }
-
-        //if the object is an instance of CmsObject, try to find a server which
-        // match to the object uri
-        if (object instanceof CmsObject)
-        {
-            return this.findPersistenceStore(((CmsObject) object).getUri());
-        }
-
-        return this.getGraffitoMasterStore();
-    }
-
-    /**
-     * Find the persistence store associated to an uri 
-     * 
-     * @param uri The uri for which the persistence store has to be found
-     * @return the persistence store found or null
-     */
-    private ContentStore findPersistenceStore(String uri)
-    {
-        Iterator servers = this.getServers(true).iterator();
-        while (servers.hasNext())
-        {
-            Server server = (Server) servers.next();
-            if (uri.startsWith(server.getScope()))
-            {
-                return this.findPersistenceStore(server);
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Find the persistence store associated to a server
-     * 
-     * @param server The server for which the persistence store has to be found
-     * @return the persistence store found or null
-     */
-    private ContentStore findPersistenceStore(Server server)
-    {
-        return getStore(server.getAlias());
-    }
-
-    /**
-     * Find the persistence stores associated to an filter. The query can be
-     * apply to more than one server. The scope attribute in the filter object
-     * defined in which server to look at.
-     * 
-     * @param filter The filter for which the persistence stores have to be found
-     * @return the persistence store collection or null
-     */
-    private Collection findPersistenceStores(Filter filter)
-    {
-        String scope = filter.getScope();
-        Collection allServers = this.getServers(true);
-        ArrayList selectedStores = new ArrayList();
-        // if there is no scope, return all stores
-        if (scope == null || scope.equals(""))
-        {
-            Iterator servers = allServers.iterator();
-            while (servers.hasNext())
-            {
-                Server server = (Server) servers.next();
-                ContentStore store = findPersistenceStore(server);
-                selectedStores.add(store);
-            }
-        }
-        else
-        {
-            //else loop on all servers in order to find thoses which match to
-            // the filter scope
-            Iterator servers = allServers.iterator();
-            while (servers.hasNext())
-            {
-                Server server = (Server) servers.next();
-                if (scope.startsWith(server.getScope()))
-                {
-                    ContentStore store = findPersistenceStore(server);
-                    selectedStores.add(store);
-                }
-            }
-        }
-        return selectedStores;
-    }
-
-    private void registerAllStores()
-    {
-        Server server = null;
-        try
-        {
-            // Add the Graffito Master Server
-            server = graffitoMasterServer;
-            contentStoreService.newStore(server);
-
-            // Get all server references and create one ContenStore per server.
-            // Each Server data object contains information on how to
-            // connect to the external server.
-            Iterator servers = this.getServers(false).iterator();
-            while (servers.hasNext())
-            {
-                server = (Server) servers.next();
-
-                contentStoreService.newStore(server);
-            }
-        }
-        catch (CmsInstantiateException e)
-        {
-            // If a store class is not found, just ignore it
-            // TODO : Need to register all incorrect server dao in order to publish them in an admin portlet
-            log.error("Impossible to find the store class : " + server.getStoreClassName());
-        }
-    }
-
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * 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.portals.graffito.persistence.impl;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.portals.graffito.exception.CmsIncorrectServerException;
+import org.apache.portals.graffito.exception.CmsInstantiateException;
+import org.apache.portals.graffito.exception.CmsPermissionException;
+import org.apache.portals.graffito.model.core.CmsObject;
+import org.apache.portals.graffito.model.core.HistoryElement;
+import org.apache.portals.graffito.model.core.VersionnedContent;
+import org.apache.portals.graffito.model.core.impl.SysCmsClass;
+import org.apache.portals.graffito.model.server.Server;
+import org.apache.portals.graffito.persistence.ContentPersistenceException;
+import org.apache.portals.graffito.persistence.ContentPersistenceService;
+import org.apache.portals.graffito.persistence.ContentStoreService;
+import org.apache.portals.graffito.services.search.Filter;
+import org.apache.portals.graffito.services.search.impl.FilterImpl;
+import org.apache.portals.graffito.store.ContentStore;
+
+/**
+ * Default implementation for
+ * {@link org.apache.portals.graffito.persistenceService.ContentPersistenceService}
+ *  
+ */
+public class ContentPersistenceServiceImpl implements ContentPersistenceService
+{
+    private final static Log log = LogFactory.getLog(ContentPersistenceServiceImpl.class);
+
+    private static Hashtable omClasses = new Hashtable();
+
+    /**
+     * The Master Graffito Server. The "Graffito Master Server" contains information
+     * about external servers like WebDav server, DB server or other proprietary
+     * content servers. It may also contain some CmsObject but it is not
+     * mandatory.
+     *  
+     */
+    private Server graffitoMasterServer;
+
+    private ContentStoreService contentStoreService;
+
+    /**
+     * Constructor.
+     * 
+     * @param graffitoMasterServer  the Graffito Master Server.  
+     * @param contentStoreService The content store service used by this persistenceService store
+     */
+    public ContentPersistenceServiceImpl(Server graffitoMasterServer, ContentStoreService contentStoreService)
+    {
+        if (graffitoMasterServer == null)
+        {
+            throw new IllegalArgumentException("graffitoMasterServer cannot be null for PersistenceServiceImpl");
+        }
+        this.graffitoMasterServer = graffitoMasterServer;
+
+        if (contentStoreService == null)
+        {
+            throw new IllegalArgumentException("Content store service cannot be null for PersistenceServiceImpl");
+        }
+        this.contentStoreService = contentStoreService;
+
+        // Register all stores
+        registerAllStores();
+    }
+
+    /**
+     * @see ContentPersistenceService#createObject(java.lang.String)
+     */
+    public Object createObject(String omName) throws CmsInstantiateException
+    {
+        Object cmsObject = null;
+        Class omClass = (Class) omClasses.get(omName);
+        ContentStore store = null;
+        try
+        {
+            if (null == omClass)
+            {
+                store = getGraffitoMasterStore();
+                Filter filter = store.newFilter();
+                filter.addEqualTo("cmsClassId", omName);
+                SysCmsClass sysCmsClass = (SysCmsClass) store.getObjectByQuery(SysCmsClass.class, filter);
+                omClass = Class.forName(sysCmsClass.getCmsClass());
+                omClasses.put(omName, omClass);
+            }
+            cmsObject = omClass.newInstance();
+        }
+        catch (Exception e)
+        {
+            throw new CmsInstantiateException("CmsRespositoruModeService error : Failed to instantiate a cms object : " + omName
+                    + "- " + e);
+        }
+        return cmsObject;
+    }
+
+    /**
+     * @see ContentPersistenceService#insert(java.lang.Object)
+     */
+    public void insert(Object object) throws ContentPersistenceException, CmsPermissionException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(object);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            store.insert(object);
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException(e);
+        }
+    }
+
+    /**
+     * @see ContentPersistenceService#update(java.lang.Object)
+     */
+    public void update(Object object) throws ContentPersistenceException, CmsPermissionException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(object);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            store.update(object);
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException(e);
+        }
+    }
+
+    /**
+     * @see ContentPersistenceService#delete(java.lang.Object)
+     */
+    public void delete(Object object) throws ContentPersistenceException, CmsPermissionException
+    {
+        if (object == null)
+            return;
+
+        try
+        {
+            ContentStore store = findPersistenceStore(object);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            store.delete(object);
+
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException(e);
+        }
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#delete(java.lang.String)
+     */
+    public void delete(String uri) throws ContentPersistenceException, CmsPermissionException
+    {
+        try
+        {
+            Filter filter = this.newFilter();
+            filter.addEqualTo("uri", uri);
+            ContentStore store = findPersistenceStore(uri);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+
+            if (uri.equals(store.getScope()))
+            {
+                throw new ContentPersistenceException("Impossible to delete the server root : " + uri);
+            }
+
+            store.deleteAll(CmsObject.class, filter);
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException(e);
+        }
+
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#deleteAll(Class,
+     *      Filter)
+     */
+    public void deleteAll(Class clazz, Filter filter) throws ContentPersistenceException, CmsPermissionException
+    {
+        ContentStore store = null;
+        try
+        {
+            Iterator iterator = findPersistenceStores(filter).iterator();
+            while (iterator.hasNext())
+            {
+                store = (ContentStore) iterator.next();
+                store.deleteAll(clazz, filter);
+            }
+
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException(e);
+        }
+
+    }
+
+    /**
+     * @see ContentPersistenceService#getCmsObject(String)
+     */
+    public CmsObject getCmsObject(String uri) throws ContentPersistenceException, CmsPermissionException
+    {
+
+        //return this.getCmsObject(uri, null);
+        ContentStore store = findPersistenceStore(uri);
+        if (store == null)
+        {
+            throw new ContentPersistenceException("Impossible to find the associated store");
+        }
+
+        Filter filter = store.newFilter();
+        filter.addEqualTo("uri", uri);
+        CmsObject object = (CmsObject) store.getObjectByQuery(CmsObject.class, filter);
+        return object;        
+    }
+
+    /**
+     * @see ContentPersistenceService#getCmsObject(String, String)
+     */
+    public CmsObject getCmsObject(String uri, String versionNumber) throws ContentPersistenceException, CmsPermissionException
+    {
+        ContentStore store = findPersistenceStore(uri);
+        if (store == null)
+        {
+            throw new ContentPersistenceException("Impossible to find the associated store");
+        }
+
+        Filter filter = store.newFilter();
+
+        // if the versionNum is not provided, get the lastest document
+        // version
+        if (versionNumber == null)
+        {
+            filter.addEqualTo("isLastVersion", new Boolean(true));
+        }
+        else
+        {
+            filter.addEqualTo("versionNumber", versionNumber);
+        }
+
+        filter.addEqualTo("uri", uri);
+        CmsObject object = (CmsObject) store.getObjectByQuery(VersionnedContent.class, filter);
+        return object;
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getChildren(java.lang.String)
+     */
+    public Collection getChildren(String parentUri) throws ContentPersistenceException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(parentUri);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            return store.getChildren(parentUri);
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException("Impossible to get the children", e);
+        }
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getContents(java.lang.String)
+     */
+    public Collection getContents(String uri) throws ContentPersistenceException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(uri);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            return store.getContents(uri); 
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException("Impossible to get the contents", e);
+        }
+
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getLinks(java.lang.String)
+     */
+    public Collection getLinks(String uri) throws ContentPersistenceException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(uri);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            return store.getLinks(uri);
+
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException("Impossible to get the links", e);
+        }
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getFolders(java.lang.String)
+     */
+    public Collection getFolders(String uri) throws ContentPersistenceException
+    {
+        try
+        {
+            ContentStore store = findPersistenceStore(uri);
+            if (store == null)
+            {
+                throw new ContentPersistenceException("Impossible to find the associated store");
+            }
+            return store.getFolders(uri);
+
+        }
+        catch (Exception e)
+        {
+            throw new ContentPersistenceException("Impossible to find the associated store", e);
+        }
+    }
+
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getHistory(org.apache.portals.graffito.model.core.VersionnedContent)
+     */
+    public HistoryElement getHistory(VersionnedContent versionnedContent) throws ContentPersistenceException, CmsPermissionException
+    {
+        ContentStore store = findPersistenceStore(versionnedContent);
+        if (store == null)
+        {
+            throw new ContentPersistenceException("Impossible to find the associated store");
+        }
+        return store.getHistory(versionnedContent);
+
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#insertHistoryElement(org.apache.portals.graffito.model.core.HistoryElement)
+     */
+    public void insertHistoryElement(HistoryElement historyElement) throws ContentPersistenceException, CmsPermissionException
+    {
+        ContentStore store = findPersistenceStore(historyElement.getClass());
+        if (store == null)
+        {
+            throw new ContentPersistenceException("Impossible to find the associated store");
+        }
+        store.insertHistoryElement(historyElement);
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#newFilter()
+     */
+    public Filter newFilter()
+    {
+        return new FilterImpl();
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getCollectionByQuery(java.lang.Class, org.apache.portals.graffito.services.search.Filter)
+     */
+    public Collection getCollectionByQuery(Class clazz, Filter filter) throws ContentPersistenceException
+    {
+
+        Collection foundStores = null;
+        ArrayList allCmsObjects = new ArrayList();
+
+        try
+        {
+            // Find all stores concerned by the filter criteria
+            foundStores = findPersistenceStores(filter);
+            Iterator storeIterator = foundStores.iterator();
+
+            // Loop on the found stores and execute the query
+            while (storeIterator.hasNext())
+            {
+                ContentStore store = (ContentStore) storeIterator.next();
+
+                Collection cmsObjects = store.getCollectionByQuery(clazz, filter);
+                allCmsObjects.addAll(cmsObjects);
+            }
+        }
+        catch (CmsPermissionException e)
+        {
+            return new ArrayList();
+        }
+        return allCmsObjects;
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getIteratorByQuery(java.lang.Class, org.apache.portals.graffito.services.search.Filter)
+     */
+    public Iterator getIteratorByQuery(Class clazz, Filter filter) throws ContentPersistenceException
+    {
+        MultiStoreIterator multiStoreIterator = new MultiStoreIterator();
+        try
+        {
+
+            Collection foundStores = null;
+            // Find all stores concerned by the filter criteria
+            foundStores = findPersistenceStores(filter);
+            Iterator storeIterator = foundStores.iterator();
+            while (storeIterator.hasNext())
+            {
+                ContentStore store = (ContentStore) storeIterator.next();
+                Iterator iterator = store.getIteratorByQuery(clazz, filter);
+                multiStoreIterator.addIterator(iterator);
+
+            }
+            return multiStoreIterator;
+        }
+        catch (CmsPermissionException e)
+        {
+            return new MultiStoreIterator();
+        }
+
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getObjectbyQuery(java.lang.Class, org.apache.portals.graffito.services.search.Filter)
+     */
+    public Object getObjectbyQuery(Class clazz, Filter filter) throws CmsPermissionException
+    {
+
+        ContentStore store = null;
+        Iterator iterator = findPersistenceStores(filter).iterator();
+        while (iterator.hasNext())
+        {
+            store = (ContentStore) iterator.next();
+            Object cmsObject = store.getObjectByQuery(clazz, filter);
+            if (cmsObject != null)
+            {
+                return cmsObject;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * @see ContentPersistenceService#getGraffitoMasterStore()
+     */
+    public ContentStore getGraffitoMasterStore()
+    {
+        ContentStore store = contentStoreService.getStore(graffitoMasterServer.getAlias());
+
+        return store;
+    }
+
+    /**
+     * @see ContentPersistenceService#getStore(String)
+     */
+    public ContentStore getStore(String persistenceStoreName)
+    {
+        return contentStoreService.getStore(persistenceStoreName);
+    }
+
+    /**
+     * @see ContentPersistenceService#getServer(String)
+     */
+    public Server getServer(String scope) throws ContentPersistenceException, CmsPermissionException
+    {
+        if (scope.equals(graffitoMasterServer.getScope()))
+        {
+            return graffitoMasterServer;
+        }
+
+        // All server references are stored into the Graffito Master Store
+        ContentStore store = store = getGraffitoMasterStore();
+        Filter filter = store.newFilter();
+        filter.addEqualTo("scope", scope);
+        return (Server) store.getObjectByQuery(Server.class, filter);
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#registerServer(org.apache.portals.graffito.model.server.Server)
+     */
+    public void registerServer(Server server) throws CmsPermissionException, CmsIncorrectServerException
+    {
+        try
+        {
+            if (!contentStoreService.canRegisterServer(server))
+            {
+                throw new CmsIncorrectServerException("Impossible to instantiate the ContentStore for server "
+                        + server.getAlias() + " - The server scope already exists");
+            }
+            contentStoreService.newStore(server);
+        }
+        catch (CmsInstantiateException e)
+        {
+            throw new CmsIncorrectServerException("Impossible to instantiate the ContentStore for server " + server.getAlias(), e);
+
+        }
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#unRegisterServer(org.apache.portals.graffito.model.server.Server)
+     */
+    public void unRegisterServer(Server server) throws CmsPermissionException, CmsIncorrectServerException
+    {
+        // it is not authorized to unregister the Graffito Master Store.         
+        if (this.graffitoMasterServer.equals(server))
+        {
+            throw new CmsIncorrectServerException("Cannot unregister the Graffito Master Server");
+        }
+        contentStoreService.removeStore(server.getAlias());
+    }
+
+    /**
+     * @see org.apache.portals.graffito.persistenceService.ContentPersistenceService#getServers(boolean)
+     */
+    public Collection getServers(boolean withMasterServer)
+    {
+        try
+        {
+            ContentStore store = getGraffitoMasterStore();
+            Filter filter = store.newFilter();
+            Collection servers = store.getCollectionByQuery(Server.class, filter);
+            if (withMasterServer)
+            {
+                // Add also the Graffito Master Server
+                servers.add(graffitoMasterServer);
+            }
+            return servers;
+        }
+        catch (CmsPermissionException e)
+        {
+            return new ArrayList();
+        }
+    }
+
+    /**
+     * Find the persistenceService store associated to all kind of object (CmsObject,
+     * Server, ...)
+     * 
+     * @param object The object for which the persistenceService store has to be found
+     * @return the persistenceService store found or null
+     */
+    private ContentStore findPersistenceStore(Object object)
+    {
+        // if the ojbect is an instance of Server => Get the Graffito Master Server
+        // All servers references are stored into the Graffito Master Server
+        if (object instanceof Server)
+        {
+            return this.getGraffitoMasterStore();
+        }
+
+        //if the object is an instance of CmsObject, try to find a server which
+        // match to the object uri
+        if (object instanceof CmsObject)
+        {
+            return this.findPersistenceStore(((CmsObject) object).getUri());
+        }
+
+        return this.getGraffitoMasterStore();
+    }
+
+    /**
+     * Find the persistenceService store associated to an uri 
+     * 
+     * @param uri The uri for which the persistenceService store has to be found
+     * @return the persistenceService store found or null
+     */
+    private ContentStore findPersistenceStore(String uri)
+    {
+        Iterator servers = this.getServers(true).iterator();
+        while (servers.hasNext())
+        {
+            Server server = (Server) servers.next();
+            if (uri.startsWith(server.getScope()))
+            {
+                return this.findPersistenceStore(server);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Find the persistenceService store associated to a server
+     * 
+     * @param server The server for which the persistenceService store has to be found
+     * @return the persistenceService store found or null
+     */
+    private ContentStore findPersistenceStore(Server server)
+    {
+        return getStore(server.getAlias());
+    }
+
+    /**
+     * Find the persistenceService stores associated to an filter. The query can be
+     * apply to more than one server. The scope attribute in the filter object
+     * defined in which server to look at.
+     * 
+     * @param filter The filter for which the persistenceService stores have to be found
+     * @return the persistenceService store collection or null
+     */
+    private Collection findPersistenceStores(Filter filter)
+    {
+        String scope = filter.getScope();
+        Collection allServers = this.getServers(true);
+        ArrayList selectedStores = new ArrayList();
+        // if there is no scope, return all stores
+        if (scope == null || scope.equals(""))
+        {
+            Iterator servers = allServers.iterator();
+            while (servers.hasNext())
+            {
+                Server server = (Server) servers.next();
+                ContentStore store = findPersistenceStore(server);
+                selectedStores.add(store);
+            }
+        }
+        else
+        {
+            //else loop on all servers in order to find thoses which match to
+            // the filter scope
+            Iterator servers = allServers.iterator();
+            while (servers.hasNext())
+            {
+                Server server = (Server) servers.next();
+                if (scope.startsWith(server.getScope()))
+                {
+                    ContentStore store = findPersistenceStore(server);
+                    selectedStores.add(store);
+                }
+            }
+        }
+        return selectedStores;
+    }
+
+    private void registerAllStores()
+    {
+        Server server = null;
+        try
+        {
+            // Add the Graffito Master Server
+            server = graffitoMasterServer;
+            contentStoreService.newStore(server);
+
+            // Get all server references and create one ContenStore per server.
+            // Each Server data object contains information on how to
+            // connect to the external server.
+            Iterator servers = this.getServers(false).iterator();
+            while (servers.hasNext())
+            {
+                server = (Server) servers.next();
+
+                contentStoreService.newStore(server);
+            }
+        }
+        catch (CmsInstantiateException e)
+        {
+            // If a store class is not found, just ignore it
+            // TODO : Need to register all incorrect server dao in order to publish them in an admin portlet
+            log.error("Impossible to find the store class : " + server.getStoreClassName());
+        }
+    }
+
 }

Modified: incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentStoreServiceImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentStoreServiceImpl.java?rev=225699&r1=225698&r2=225699&view=diff
==============================================================================
--- incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentStoreServiceImpl.java (original)
+++ incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/ContentStoreServiceImpl.java Wed Jul 27 22:40:52 2005
@@ -19,13 +19,13 @@
 import java.util.Hashtable;
 import java.util.Iterator;
 
+import org.apache.portals.graffito.exception.CmsInstantiateException;
+import org.apache.portals.graffito.model.server.Server;
 import org.apache.portals.graffito.persistence.ContentStoreService;
 import org.apache.portals.graffito.security.CmsAccessController;
 import org.apache.portals.graffito.store.ContentStore;
 import org.apache.portals.graffito.store.impl.ojb.GraffitoOJBStore;
 import org.apache.portals.graffito.store.impl.ojb.OjbFactory;
-import org.apache.portals.graffito.exception.CmsInstantiateException;
-import org.apache.portals.graffito.model.Server;
 
 /**
  * 
@@ -61,7 +61,7 @@
     }
     
     /**
-     * @see org.apache.portals.graffito.persistence.ContentStoreService#newStore(org.apache.portals.graffito.model.Server)
+     * @see org.apache.portals.graffito.persistenceService.ContentStoreService#newStore(org.apache.portals.graffito.model.Server)
      */
     public void newStore(Server server) throws CmsInstantiateException
     {
@@ -98,7 +98,7 @@
     }
 
     /**
-     * @see org.apache.portals.graffito.persistence.ContentStoreService#getStore(java.lang.String)
+     * @see org.apache.portals.graffito.persistenceService.ContentStoreService#getStore(java.lang.String)
      */
     public ContentStore getStore(String serverAlias)
     {
@@ -106,7 +106,7 @@
     }
 
     /**
-     * @see org.apache.portals.graffito.persistence.ContentStoreService#removeStore(java.lang.String)
+     * @see org.apache.portals.graffito.persistenceService.ContentStoreService#removeStore(java.lang.String)
      */
     public void removeStore(String serverAlias)
     {
@@ -119,7 +119,7 @@
     }
              
     /**
-     * @see org.apache.portals.graffito.persistence.ContentStoreService#canRegisterServer(org.apache.portals.graffito.model.Server)
+     * @see org.apache.portals.graffito.persistenceService.ContentStoreService#canRegisterServer(org.apache.portals.graffito.model.Server)
      * 
      * This implementation check if the new server scope is not yet used by an already registered server.
      */

Modified: incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/MultiStoreIterator.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/MultiStoreIterator.java?rev=225699&r1=225698&r2=225699&view=diff
==============================================================================
--- incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/MultiStoreIterator.java (original)
+++ incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/MultiStoreIterator.java Wed Jul 27 22:40:52 2005
@@ -20,7 +20,7 @@
 
 /**
  * 
- * This iterator is used by the persistence service in order to group together
+ * This iterator is used by the persistenceService service in order to group together
  * query iterators made on different stores.
  * 
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe</a>

Modified: incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/PersistenceSupportedTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/PersistenceSupportedTestCase.java?rev=225699&r1=225698&r2=225699&view=diff
==============================================================================
--- incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/PersistenceSupportedTestCase.java (original)
+++ incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/persistence/impl/PersistenceSupportedTestCase.java Wed Jul 27 22:40:52 2005
@@ -20,7 +20,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jetspeed.components.persistence.store.PersistenceStore;
-import org.apache.portals.graffito.model.impl.GraffitoServerImpl;
+import org.apache.portals.graffito.model.server.impl.GraffitoServerImpl;
 import org.apache.portals.graffito.persistence.ContentPersistenceService;
 import org.apache.portals.graffito.store.impl.ojb.OjbFactory;
 
@@ -56,7 +56,7 @@
         try 
         {
     	super.setUp();
-	
+    	                                                              
 		persistenceService = (ContentPersistenceService) ctx.getBean("org.apache.portals.graffito.persistence.ContentPersistenceService");
 		
         }



Mime
View raw message