[xwiki-notifications] r9623 - xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager

jvelociter (SVN) notifications at xwiki.org
Sat May 3 12:51:26 CEST 2008


Author: jvelociter
Date: 2008-05-03 12:51:26 +0200 (Sat, 03 May 2008)
New Revision: 9623

Added:
   xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspaceApplicationsManager.java
Modified:
   xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManager.java
   xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerApi.java
   xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerExtension.java
Log:
[refactoring] Started to move applications-related methods to a WorkspaceApplicationsManager + Applied checkstyle


Added: xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspaceApplicationsManager.java
===================================================================
--- xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspaceApplicationsManager.java	                        (rev 0)
+++ xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspaceApplicationsManager.java	2008-05-03 10:51:26 UTC (rev 9623)
@@ -0,0 +1,211 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package com.xpn.xwiki.plugin.workspacesmanager;
+
+import java.text.MessageFormat;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.objects.BaseObject;
+import com.xpn.xwiki.plugin.applicationmanager.ApplicationManagerPlugin;
+import com.xpn.xwiki.plugin.applicationmanager.ApplicationManagerPluginApi;
+import com.xpn.xwiki.plugin.applicationmanager.doc.XWikiApplication;
+import com.xpn.xwiki.plugin.spacemanager.api.SpaceManagerException;
+
+/**
+ * A Manager for Workspace Applications
+ * 
+ * @version $Id: $
+ */
+public class WorkspaceApplicationsManager
+{
+    public static final String XWIKI_SPACE_SEPARATOR = ".";
+
+    public static final String XWIKI_WORKSPACE_APPSEPARATOR = "_";
+    
+
+    /**
+     * Returns a map of all installed applications for the given workspace name. Keys of the map are
+     * the names of the installed application (as given by their application descriptor, see
+     * {@link com.xpn.xwiki.plugin.applicationmanager.doc.XWikiApplicationClass#FIELD_APPNAME}.
+     * Values of the map are the wiki spaces in which the application are installed.
+     * 
+     * @param spaceName the name of the space to retrieve applications for
+     * @throws SpaceManagerException
+     */
+    public Map<String, String> getApplicationsForSpace(String spaceName, XWikiContext context)
+        throws WorkspacesManagerException
+    {
+        String hql =
+            "select distinct doc.web from XWikiDocument as doc, XWikiDocument as sp"
+                + ", BaseObject as xws, BaseObject as prefs, StringProperty as parent"
+                + " where sp.web='" + spaceName
+                + "' and sp.name='WebPreferences' and xws.name=sp.fullName"
+                + " and xws.className='" + WorkspacesManagerExtension.WORKSPACE_SPACE_CLASS_NAME
+                + "'"
+                + " and prefs.className='XWiki.XWikiPreferences' and prefs.name=doc.fullName"
+                + " and parent.id.id=prefs.id and parent.id.name='parent' and parent.value='"
+                + spaceName + "'";
+        try {
+            List<String> applicationWebs =
+                context.getWiki().getStore().search(hql, 0, 0, context);
+            Map<String, String> result = new HashMap<String, String>();
+            for (String appWeb : applicationWebs) {
+                // Retrieve the application name based on it URL
+                // Convention for an app web : Space_Spacename_Appname
+                String appName =
+                    appWeb
+                        .substring(appWeb
+                            .lastIndexOf(WorkspacesManagerExtension.XWIKI_WORKSPACE_APPSEPARATOR) + 1);
+                result.put(appName, appWeb);
+            }
+            return result;
+        } catch (XWikiException e) {
+            throw new WorkspacesManagerException(e);
+        }
+    }
+    
+    public String getApplicationURL(String spaceName, String appName, String docName,
+        String queryString, XWikiContext context) throws WorkspacesManagerException
+    {
+        try {
+            String appWeb = getApplicationsForSpace(spaceName, context).get(appName);
+            if (appWeb == null || appWeb.equals(""))
+                throw new WorkspacesManagerException(WorkspacesManagerException.MODULE_PLUGIN_XWS,
+                    WorkspacesManagerException.ERROR_XWSMGR_APPNOTFOUNDFORSPACE,
+                    "Application could not be found for space");
+            return context.getWiki().getURL(
+                appWeb + WorkspacesManagerExtension.XWIKI_SPACE_SEPARATOR + docName, "view",
+                queryString, context);
+        } catch (SpaceManagerException e) {
+            throw new WorkspacesManagerException(e);
+        } catch (XWikiException e) {
+            throw new WorkspacesManagerException(e);
+        }
+    }
+    
+    /**
+     * Install an application in the given space, by copying or linking documents.
+     * Read the list of documents to include (link) and copy from the
+     * {@link ApplicationManagerPlugin} and save their content locally in a wiki
+     * space (web) composed of the space wiki name and the application name.
+     * Also make the application web inherits its rights from the space root
+     * space (web).
+     * 
+     * @param appName
+     *            the name of the application to install
+     * @param spaceName
+     *            the wiki name of the space to install the application in
+     * @throws SpaceManagerException
+     */
+    protected void installApplicationInSpace(String appName, String spaceName,
+            XWikiContext context) throws SpaceManagerException {
+        // TODO Note that this method makes a deviant usage of the application
+        // manager plugin and XWiki application objects.
+        // It uses the application field docsToInclude and documents
+        // to make a local installation (as opposed as cross-wiki global
+        // installation, which the application manager is initially designed
+        // for).
+        // In the future, the application manager should be able to
+        // handle local installation/local copy parameters, and the
+        // SpaceManagerPlugin implements a method to install a space
+        // from an application or application list.
+
+        // get the application manager api
+        ApplicationManagerPluginApi appmanager = (ApplicationManagerPluginApi) context
+                .getWiki().getPluginApi(ApplicationManagerPlugin.PLUGIN_NAME,
+                        context);
+        try {
+            // Retrieve the application descriptor
+            XWikiApplication app = appmanager.getApplicationDocument(appName);
+
+            if (app == null)
+                throw new SpaceManagerException();
+
+            String appSpace = spaceName + XWIKI_WORKSPACE_APPSEPARATOR
+                    + app.getAppName();
+
+            // Retrieve the application document list
+            Collection appDocs = app.getDocumentsNames(false, false);
+
+            // Retrieve the application documents to include
+            Collection docsToInclude = app.getDocsNameToInclude(true);
+
+            for (Iterator it = appDocs.iterator(); it.hasNext();) {
+                String docFullName = (String) it.next();
+                // If the doc is not in the include list,
+                // We copy it to the target space
+                if (!docsToInclude.contains(docFullName)) {
+                    String docName = docFullName.substring(docFullName
+                            .indexOf('.') + 1);
+                    String targetDocName = appSpace + XWIKI_SPACE_SEPARATOR
+                            + docName;
+                    context.getWiki().copyDocument(docFullName, targetDocName,
+                            true, context);
+                }
+            }
+
+            for (Iterator it = docsToInclude.iterator(); it.hasNext();) {
+                String docFullName = (String) it.next();
+                String docName = docFullName
+                        .substring(docFullName.indexOf('.') + 1);
+
+                // Compute the target doc name based on application name, space
+                // name and document
+                // name
+                // EX: Space_Wiki.WebHome for "Space" space name, "Wiki" appname
+                // and "WebHome" doc
+                String targetDocName = appSpace + XWIKI_SPACE_SEPARATOR
+                        + docName;
+                XWikiDocument targetDoc = context.getWiki().getDocument(
+                        targetDocName, context);
+
+                // Link the content with the application code
+                targetDoc.setContent(MessageFormat.format(
+                        "#includeInContext(\"{0}\")",
+                        new Object[] { docFullName }));
+
+                // Save the document
+                context.getWiki().saveDocument(targetDoc, context);
+            }
+
+            if (appDocs.size() > 0) {
+                // if we've installed anything,
+                // make the installed app inherit its right from the Workspace
+                // root web
+                XWikiDocument appPreferences = context.getWiki().getDocument(
+                        appSpace + XWIKI_SPACE_SEPARATOR + "WebPreferences",
+                        context);
+                BaseObject pObj = appPreferences.getObject(
+                        "XWiki.XWikiPreferences", true, context);
+                pObj.setStringValue("parent", spaceName);
+                context.getWiki().saveDocument(appPreferences, context);
+            }
+        } catch (XWikiException e) {
+            throw new SpaceManagerException(e);
+        }
+    }
+}

Modified: xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManager.java
===================================================================
--- xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManager.java	2008-05-03 10:16:41 UTC (rev 9622)
+++ xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManager.java	2008-05-03 10:51:26 UTC (rev 9623)
@@ -50,11 +50,19 @@
      */
     public static final String WORKSPACESMANAGER_PLUGIN_NAME = "xwsmgr";
 
+    private WorkspaceApplicationsManager workspaceAppsManager;
+    
     public WorkspacesManager(String name, String className, XWikiContext context)
     {
         super(name, className, context);
+        workspaceAppsManager = new WorkspaceApplicationsManager();
     }
 
+    public WorkspaceApplicationsManager getApplicationsManager()
+    {
+        return workspaceAppsManager;
+    }
+    
     /**
      * {@inheritDoc}
      */
@@ -694,25 +702,6 @@
         }
     }
 
-    public String getApplicationURL(String spaceName, String appName, String docName,
-        String queryString, XWikiContext context) throws WorkspacesManagerException
-    {
-        try {
-            String appWeb = (String) getApplicationsForSpace(spaceName, context).get(appName);
-            if (appWeb == null || appWeb.equals(""))
-                throw new WorkspacesManagerException(WorkspacesManagerException.MODULE_PLUGIN_XWS,
-                    WorkspacesManagerException.ERROR_XWSMGR_APPNOTFOUNDFORSPACE,
-                    "Application could not be found for space");
-            return context.getWiki().getURL(
-                appWeb + WorkspacesManagerExtension.XWIKI_SPACE_SEPARATOR + docName, "view",
-                queryString, context);
-        } catch (SpaceManagerException e) {
-            throw new WorkspacesManagerException(e);
-        } catch (XWikiException e) {
-            throw new WorkspacesManagerException(e);
-        }
-    }
-
     public String getApplicationName(String docFullname, XWikiContext context)
         throws WorkspacesManagerException
     {
@@ -721,12 +710,11 @@
             Space rootSpace = getRootSpace(doc.getSpace(), context);
             if (rootSpace == null)
                 throw new WorkspacesManagerException();
-            Map apps = getApplicationsForSpace(rootSpace.getSpaceName(), context);
+            Map<String,String> apps = workspaceAppsManager.getApplicationsForSpace(rootSpace.getSpaceName(), context);
             if (apps.containsValue(doc.getSpace()))
-                for (Iterator it = apps.keySet().iterator(); it.hasNext();) {
-                    String name = (String) it.next();
-                    if (doc.getSpace().equals(apps.get(name)))
-                        return name;
+                for (String appName : apps.keySet()) {
+                    if (doc.getSpace().equals(apps.get(appName)))
+                        return appName;
                 }
             return new String();
         } catch (XWikiException e) {
@@ -734,10 +722,10 @@
         }
     }
 
-    public Collection getWebsForSpace(String spaceName, XWikiContext context)
+    public List<String> getWebsForSpace(String spaceName, XWikiContext context)
         throws WorkspacesManagerException
     {
-        Collection webs;
+        List<String> webs;
         String hql =
             "select distinct doc.web from XWikiDocument as doc, XWikiDocument as sp"
                 + ", BaseObject as xws, BaseObject as prefs, StringProperty as parent"
@@ -765,41 +753,6 @@
         return spaceName + "_" + applicationName;
     }
 
-    // TODO refactor this
-    public Map getApplicationsForSpace(String spaceName, XWikiContext context)
-        throws SpaceManagerException
-    {
-        String hql =
-            "select distinct doc.web from XWikiDocument as doc, XWikiDocument as sp"
-                + ", BaseObject as xws, BaseObject as prefs, StringProperty as parent"
-                + " where sp.web='"
-                + spaceName
-                + "' and sp.name='WebPreferences' and xws.name=sp.fullName"
-                + " and xws.className='"
-                + ((WorkspacesManagerExtension) getSpaceManagerExtension())
-                    .getWorkspaceSpaceClassName() + "'"
-                + " and prefs.className='XWiki.XWikiPreferences' and prefs.name=doc.fullName"
-                + " and parent.id.id=prefs.id and parent.id.name='parent' and parent.value='"
-                + spaceName + "'";
-        try {
-            List apps = context.getWiki().getStore().search(hql, 0, 0, context);
-            Map result = new HashMap();
-            for (Iterator it = apps.iterator(); it.hasNext();) {
-                String appWeb = (String) it.next();
-                // Retrieve the application name based on it URL
-                // Convention for an app web : Space_Spacename_Appname
-                String appName =
-                    appWeb
-                        .substring(appWeb
-                            .lastIndexOf(WorkspacesManagerExtension.XWIKI_WORKSPACE_APPSEPARATOR) + 1);
-                result.put(appName, appWeb);
-            }
-            return result;
-        } catch (XWikiException e) {
-            throw new SpaceManagerException(e);
-        }
-    }
-
     protected Space newSpace(String spaceName, String spaceTitle, boolean create,
         XWikiContext context) throws SpaceManagerException
     {

Modified: xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerApi.java
===================================================================
--- xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerApi.java	2008-05-03 10:16:41 UTC (rev 9622)
+++ xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerApi.java	2008-05-03 10:51:26 UTC (rev 9623)
@@ -373,7 +373,7 @@
     public boolean hasApplication(String spaceName, String appName)
     {
         try {
-            return getWorkspacesManager().getApplicationsForSpace(spaceName, context)
+            return getWorkspacesManager().getApplicationsManager().getApplicationsForSpace(spaceName, context)
                 .containsKey(appName);
         } catch (SpaceManagerException e) {
             context.put("haserror", "1");
@@ -386,7 +386,7 @@
         String queryString)
     {
         try {
-            return getWorkspacesManager().getApplicationURL(spaceName, appName, docName,
+            return getWorkspacesManager().getApplicationsManager().getApplicationURL(spaceName, appName, docName,
                 queryString, context);
         } catch (WorkspacesManagerException e) {
             context.put("haserror", "1");
@@ -454,7 +454,8 @@
     public Collection getApplicationsNames(String spaceName)
     {
         try {
-            return getWorkspacesManager().getApplicationsForSpace(spaceName, context).keySet();
+            return getWorkspacesManager().getApplicationsManager().getApplicationsForSpace(
+                spaceName, context).keySet();
         } catch (SpaceManagerException e) {
             context.put("haserror", "1");
             context.put("lasterror", e.getMessage());
@@ -462,7 +463,7 @@
         }
     }
 
-    public Collection getWebsForSpace(String spaceName)
+    public List<String> getWebsForSpace(String spaceName)
     {
         try {
             return getWorkspacesManager().getWebsForSpace(spaceName, context);

Modified: xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerExtension.java
===================================================================
--- xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerExtension.java	2008-05-03 10:16:41 UTC (rev 9622)
+++ xwiki-products/xwiki-workspaces/trunk/plugins/workspacesmanager/src/main/java/com/xpn/xwiki/plugin/workspacesmanager/WorkspacesManagerExtension.java	2008-05-03 10:51:26 UTC (rev 9623)
@@ -20,13 +20,9 @@
 
 package com.xpn.xwiki.plugin.workspacesmanager;
 
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
@@ -39,12 +35,8 @@
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.objects.classes.BaseClass;
-import com.xpn.xwiki.plugin.activitystream.api.ActivityEvent;
 import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType;
-import com.xpn.xwiki.plugin.activitystream.impl.ActivityEventImpl;
-import com.xpn.xwiki.plugin.applicationmanager.ApplicationManagerPlugin;
-import com.xpn.xwiki.plugin.applicationmanager.ApplicationManagerPluginApi;
-import com.xpn.xwiki.plugin.applicationmanager.doc.XWikiApplication;
+import com.xpn.xwiki.plugin.spacemanager.api.Space;
 import com.xpn.xwiki.plugin.spacemanager.api.SpaceManager;
 import com.xpn.xwiki.plugin.spacemanager.api.SpaceManagerException;
 import com.xpn.xwiki.plugin.spacemanager.impl.SpaceManagerExtensionImpl;
@@ -55,562 +47,459 @@
  * 
  * @version $Id: $
  */
-public class WorkspacesManagerExtension extends SpaceManagerExtensionImpl {
-	public static final String XWIKI_SPACE_SEPARATOR = ".";
+public class WorkspacesManagerExtension extends SpaceManagerExtensionImpl
+{
+    public static final String XWIKI_SPACE_SEPARATOR = ".";
 
-	public static final String XWIKI_WORKSPACE_APPSEPARATOR = "_";
+    public static final String XWIKI_WORKSPACE_APPSEPARATOR = "_";
 
-	protected static final String WORKSPACE_SPACE_TYPE = "workspace";
+    protected static final String WORKSPACE_SPACE_TYPE = "workspace";
 
-	protected static final String WORKSPACE_SPACE_CLASS_NAME = "XWiki.WorkspaceSpaceClass";
+    protected static final String WORKSPACE_SPACE_CLASS_NAME = "XWiki.WorkspaceSpaceClass";
 
-	protected static final String WORKSPACE_ROLE_READER_CODE = "reader";
+    protected static final String WORKSPACE_ROLE_READER_CODE = "reader";
 
-	protected static final String WORKSPACE_ROLE_READER_LEVELS = "view, comment";
+    protected static final String WORKSPACE_ROLE_READER_LEVELS = "view, comment";
 
-	protected static final String WORKSPACE_ROLE_READER_GROUP = "ReaderGroup";
+    protected static final String WORKSPACE_ROLE_READER_GROUP = "ReaderGroup";
 
-	protected static final String WORKSPACE_ROLE_WRITER_CODE = "writer";
+    protected static final String WORKSPACE_ROLE_WRITER_CODE = "writer";
 
-	protected static final String WORKSPACE_ROLE_WRITER_LEVELS = "edit, view, comment";
+    protected static final String WORKSPACE_ROLE_WRITER_LEVELS = "edit, view, comment";
 
-	protected static final String WORKSPACE_ROLE_WRITER_GROUP = "WriterGroup";
+    protected static final String WORKSPACE_ROLE_WRITER_GROUP = "WriterGroup";
 
-	protected static final String WORKSPACE_PREFERENCES_SHEET = "XWSCode.SpacePreferences";
+    protected static final String WORKSPACE_PREFERENCES_SHEET = "XWSCode.SpacePreferences";
 
-	protected static final String WORKSPACE_EVENT_SPACE_CREATION_KEY = "xws.activities.workspacecreated";
+    protected static final String WORKSPACE_EVENT_SPACE_CREATION_KEY =
+        "xws.activities.workspacecreated";
 
-	protected static final Log LOG = LogFactory
-			.getLog(WorkspacesManagerExtension.class);
+    protected static final Log LOG = LogFactory.getLog(WorkspacesManagerExtension.class);
 
-	protected static Map roles = new HashMap();
+    protected static Map<String, String> roles = new HashMap<String, String>();
 
-	/**
-	 * {@inheritDoc}
-	 */
-	public void init(SpaceManager manager, XWikiContext context)
-			throws SpaceManagerException {
-		try {
-			updateWorkspaceSpaceClass(context);
-			this.sm = manager;
-			roles.put(WORKSPACE_ROLE_READER_CODE, WORKSPACE_ROLE_READER_GROUP);
-			roles.put(WORKSPACE_ROLE_WRITER_CODE, WORKSPACE_ROLE_WRITER_GROUP);
-		} catch (XWikiException e) {
-			throw new SpaceManagerException(e);
-		}
-	}
+    /**
+     * {@inheritDoc}
+     */
+    public void init(SpaceManager manager, XWikiContext context) throws SpaceManagerException
+    {
+        try {
+            updateWorkspaceSpaceClass(context);
+            this.sm = manager;
+            roles.put(WORKSPACE_ROLE_READER_CODE, WORKSPACE_ROLE_READER_GROUP);
+            roles.put(WORKSPACE_ROLE_WRITER_CODE, WORKSPACE_ROLE_WRITER_GROUP);
+        } catch (XWikiException e) {
+            throw new SpaceManagerException(e);
+        }
+    }
 
-	/**
-	 * {@inheritDoc}
-	 */
-	public void virtualInit(SpaceManager manager, XWikiContext context)
-			throws SpaceManagerException {
-		try {
-			updateWorkspaceSpaceClass(context);
-			this.sm = manager;
-			roles.put(WORKSPACE_ROLE_READER_CODE, WORKSPACE_ROLE_READER_GROUP);
-			roles.put(WORKSPACE_ROLE_WRITER_CODE, WORKSPACE_ROLE_WRITER_GROUP);
-		} catch (XWikiException e) {
-			throw new SpaceManagerException(e);
-		}
-	}
+    /**
+     * {@inheritDoc}
+     */
+    public void virtualInit(SpaceManager manager, XWikiContext context)
+        throws SpaceManagerException
+    {
+        try {
+            updateWorkspaceSpaceClass(context);
+            this.sm = manager;
+            roles.put(WORKSPACE_ROLE_READER_CODE, WORKSPACE_ROLE_READER_GROUP);
+            roles.put(WORKSPACE_ROLE_WRITER_CODE, WORKSPACE_ROLE_WRITER_GROUP);
+        } catch (XWikiException e) {
+            throw new SpaceManagerException(e);
+        }
+    }
 
-	/**
-	 * {@inheritDoc}
-	 */
-	public String getSpaceTypeName() {
-		return WORKSPACE_SPACE_TYPE;
-	}
+    /**
+     * {@inheritDoc}
+     */
+    public String getSpaceTypeName()
+    {
+        return WORKSPACE_SPACE_TYPE;
+    }
 
-	/**
-	 * @return the name of the document that holds a Workspace XClass
-	 *         definition.
-	 */
-	public String getWorkspaceSpaceClassName() {
-		return WORKSPACE_SPACE_CLASS_NAME;
-	}
+    /**
+     * @return the name of the document that holds a Workspace XClass definition.
+     */
+    public String getWorkspaceSpaceClassName()
+    {
+        return WORKSPACE_SPACE_CLASS_NAME;
+    }
 
-	/**
-	 * @return the full name of the document that holds the XWiki.XWikiGroups
-	 *         XObjects with all the workspace members
-	 */
-	public String getMemberGroupName(String spaceName) {
-		return spaceName + XWIKI_SPACE_SEPARATOR + "MemberGroup";
-	}
+    /**
+     * @return the full name of the document that holds the XWiki.XWikiGroups XObjects with all the
+     *         workspace members
+     */
+    public String getMemberGroupName(String spaceName)
+    {
+        return spaceName + XWIKI_SPACE_SEPARATOR + "MemberGroup";
+    }
 
-	/**
-	 * @return the full name of the document that holds the XWiki.XWikiGroups
-	 *         XObjects with all the workspace admins
-	 */
-	public String getAdminGroupName(String spaceName) {
-		return spaceName + XWIKI_SPACE_SEPARATOR + "AdminGroup";
-	}
+    /**
+     * @return the full name of the document that holds the XWiki.XWikiGroups XObjects with all the
+     *         workspace admins
+     */
+    public String getAdminGroupName(String spaceName)
+    {
+        return spaceName + XWIKI_SPACE_SEPARATOR + "AdminGroup";
+    }
 
-	/**
-	 * Updates the Workspace class definition
-	 * 
-	 * @throws XWikiException
-	 */
-	protected void updateWorkspaceSpaceClass(XWikiContext context)
-			throws XWikiException {
-		XWikiDocument doc;
-		XWiki xwiki = context.getWiki();
-		boolean needsUpdate = false;
+    /**
+     * Updates the Workspace class definition
+     * 
+     * @throws XWikiException
+     */
+    protected void updateWorkspaceSpaceClass(XWikiContext context) throws XWikiException
+    {
+        XWikiDocument doc;
+        XWiki xwiki = context.getWiki();
+        boolean needsUpdate = false;
 
-		try {
-			doc = xwiki.getDocument(getWorkspaceSpaceClassName(), context);
-		} catch (Exception e) {
-			doc = new XWikiDocument();
-			doc.setFullName(getWorkspaceSpaceClassName());
-			needsUpdate = true;
-		}
+        try {
+            doc = xwiki.getDocument(getWorkspaceSpaceClassName(), context);
+        } catch (Exception e) {
+            doc = new XWikiDocument();
+            doc.setFullName(getWorkspaceSpaceClassName());
+            needsUpdate = true;
+        }
 
-		BaseClass bclass = doc.getxWikiClass();
-		bclass.setName(getWorkspaceSpaceClassName());
+        BaseClass bclass = doc.getxWikiClass();
+        bclass.setName(getWorkspaceSpaceClassName());
 
-		String content = doc.getContent();
-		if ((content == null) || (content.equals(""))) {
-			needsUpdate = true;
-			doc.setContent("1 XWiki Workspace Space Class");
-		}
+        String content = doc.getContent();
+        if ((content == null) || (content.equals(""))) {
+            needsUpdate = true;
+            doc.setContent("1 XWiki Workspace Space Class");
+        }
 
-		String levelsFields = Workspace.WORKSPACE_ACCESSLEVEL_OPEN + "|"
-				+ Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC + "|"
-				+ Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE;
+        String levelsFields =
+            Workspace.WORKSPACE_ACCESSLEVEL_OPEN + "|" + Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC
+                + "|" + Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE;
 
-		String colorField = new String();
+        String colorField = new String();
 
-		for (int i = 0; i < Workspace.WORKSPACE_COLOR_VALUES.length; i++) {
-			String color = Workspace.WORKSPACE_COLOR_VALUES[i];
-			colorField += color;
-			if (i != (Workspace.WORKSPACE_COLOR_VALUES.length - 1))
-				colorField += "|";
-		}
+        for (int i = 0; i < Workspace.WORKSPACE_COLOR_VALUES.length; i++) {
+            String color = Workspace.WORKSPACE_COLOR_VALUES[i];
+            colorField += color;
+            if (i != (Workspace.WORKSPACE_COLOR_VALUES.length - 1))
+                colorField += "|";
+        }
 
-		needsUpdate |= bclass.addStaticListField(
-				Workspace.WORKSPACE_ACCESSLEVEL, "Workspace Access Level", 1,
-				false, levelsFields, "select");
-		needsUpdate |= bclass.addStaticListField(Workspace.WORKSPACE_SPACETYPE,
-				"Workspace Space Type", 1, false,
-				Workspace.WORKSPACE_SPACETYPE_WORK + "|"
-						+ Workspace.WORKSPACE_SPACETYPE_USER + "|"
-						+ Workspace.WORKSPACE_SPACETYPE_ORG, "select");
-		needsUpdate |= bclass.addStaticListField(Workspace.WORKSPACE_COLOR,
-				"Workspace Color", 1, false, colorField);
+        needsUpdate |=
+            bclass.addStaticListField(Workspace.WORKSPACE_ACCESSLEVEL, "Workspace Access Level",
+                1, false, levelsFields, "select");
+        needsUpdate |=
+            bclass.addStaticListField(Workspace.WORKSPACE_SPACETYPE, "Workspace Space Type", 1,
+                false, Workspace.WORKSPACE_SPACETYPE_WORK + "|"
+                    + Workspace.WORKSPACE_SPACETYPE_USER + "|"
+                    + Workspace.WORKSPACE_SPACETYPE_ORG, "select");
+        needsUpdate |=
+            bclass.addStaticListField(Workspace.WORKSPACE_COLOR, "Workspace Color", 1, false,
+                colorField);
 
-		if (needsUpdate) {
-			xwiki.saveDocument(doc, context);
-		}
+        if (needsUpdate) {
+            xwiki.saveDocument(doc, context);
+        }
 
-	}
+    }
 
-	/**
-	 * {@inheritDoc}
-	 */
-	public String getRoleGroupName(String spaceName, String roleName) {
-		String roleDocName = (String) roles.get(roleName);
-		if (roleDocName != null)
-			return spaceName + XWIKI_SPACE_SEPARATOR + roleDocName;
-		return null;
-	}
+    /**
+     * {@inheritDoc}
+     */
+    public String getRoleGroupName(String spaceName, String roleName)
+    {
+        String roleDocName = roles.get(roleName);
+        if (roleDocName != null)
+            return spaceName + XWIKI_SPACE_SEPARATOR + roleDocName;
+        return null;
+    }
 
-	/**
-	 * Operations executed after the actual {@link Space} creation. For a
-	 * workspace, consist in intializing the space rights, according to its
-	 * visivility, private or public, and publish the first story of its
-	 * activity stream . {@inheritDoc}
-	 */
-	public void postCreateSpace(String spaceName, XWikiContext context)
-			throws SpaceManagerException {
-		try {
-			// Set the workspace and its application rights
+    /**
+     * Operations executed after the actual {@link Space} creation. For a workspace, consist in
+     * intializing the space rights, according to its visivility, private or public, and publish the
+     * first story of its activity stream . {@inheritDoc}
+     */
+    public void postCreateSpace(String spaceName, XWikiContext context)
+        throws SpaceManagerException
+    {
+        try {
+            // Set the workspace and its application rights
 
-			Workspace w = (Workspace) sm.getSpace(spaceName, context);
-			// set rights for reader group
-			((WorkspacesManager) sm).setReadersRights(spaceName, context);
-			// set rights for writer group
-			((WorkspacesManager) sm).setWritersRights(spaceName, context);
-			// compute and save the access level
-			String accessLevel = getAccessLevelForRequest(context);
-			w.set(Workspace.WORKSPACE_ACCESSLEVEL, accessLevel);
-			w.saveWithProgrammingRights("Updated access level");
-			// set rights accordingly
-			if (accessLevel.equals(Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE)) {
-				makeSpacePrivate(spaceName, context);
-			} else if (accessLevel.equals(Workspace.WORKSPACE_ACCESSLEVEL_OPEN)) {
-				makeSpaceOpen(spaceName, context);
-			} else {
-				makeSpacePublic(spaceName, context);
-			}
+            Workspace w = (Workspace) sm.getSpace(spaceName, context);
+            // set rights for reader group
+            ((WorkspacesManager) sm).setReadersRights(spaceName, context);
+            // set rights for writer group
+            ((WorkspacesManager) sm).setWritersRights(spaceName, context);
+            // compute and save the access level
+            String accessLevel = getAccessLevelForRequest(context);
+            w.set(Workspace.WORKSPACE_ACCESSLEVEL, accessLevel);
+            w.saveWithProgrammingRights("Updated access level");
+            // set rights accordingly
+            if (accessLevel.equals(Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE)) {
+                makeSpacePrivate(spaceName, context);
+            } else if (accessLevel.equals(Workspace.WORKSPACE_ACCESSLEVEL_OPEN)) {
+                makeSpaceOpen(spaceName, context);
+            } else {
+                makeSpacePublic(spaceName, context);
+            }
 
-			// set content and install applications
-			prepareWorkspaceContent(spaceName, context);
+            // set content and install applications
+            prepareWorkspaceContent(spaceName, context);
 
-			// publish an activity story about the workspace creation
-			ActivityEvent ev = new ActivityEventImpl();
-			XWikiDocument doc = context.getWiki().getDocument(
-					w.getSpace() + ".WebHome", context);
+            // publish an activity story about the workspace creation
+            XWikiDocument doc = context.getWiki().getDocument(w.getSpace() + ".WebHome", context);
 
-			WorkspacesActivityStreamPluginApi wasApi = (WorkspacesActivityStreamPluginApi) context
-					.getWiki().getPluginApi("xwsactivitystream", context);
-			wasApi.addDocumentActivityEvent(w.getSpace(), new Document(doc,
-					context), ActivityEventType.CREATE_SPACE,
-					WORKSPACE_EVENT_SPACE_CREATION_KEY);
-		} catch (XWikiException e) {
-			throw new SpaceManagerException();
-		}
-	}
+            WorkspacesActivityStreamPluginApi wasApi =
+                (WorkspacesActivityStreamPluginApi) context.getWiki().getPluginApi(
+                    "xwsactivitystream", context);
+            wasApi.addDocumentActivityEvent(w.getSpace(), new Document(doc, context),
+                ActivityEventType.CREATE_SPACE, WORKSPACE_EVENT_SPACE_CREATION_KEY);
+        } catch (XWikiException e) {
+            throw new SpaceManagerException();
+        }
+    }
 
-	/**
-	 * After space deletion by the spacemanager plugin (see
-	 * {@link com.xpn.xwiki.plugin.spacemanager.api.SpaceManager#deleteSpace(String, boolean, XWikiContext)},
-	 * delete inherited spaces documents (Applications spaces documents) of the
-	 * workspace.
-	 * 
-	 * @param spaceName
-	 *            the name of the root space (web) of the workspace for which to
-	 *            delete application data. This is the name present in the
-	 *            'parent' field of the XWikiPreferences object of the
-	 *            application spaces (webs).
-	 * @param if
-	 *            true, will delete the application documents, will do nothing
-	 *            otherwise.
-	 */
-	public void postDeleteSpace(String spaceName, boolean deleteData,
-			XWikiContext context) {
-		if (deleteData) {
-			// Look for all documents that belong to a space that inherit from
-			// the Workspace root
-			// space
-			String hql = ", XWikiDocument as sp"
-					+ ", BaseObject as xws, BaseObject as prefs, StringProperty as parent"
-					+ " where sp.web='"
-					+ spaceName
-					+ "' and sp.name='WebPreferences' and xws.name=sp.fullName"
-					+ " and xws.className='"
-					+ getWorkspaceSpaceClassName()
-					+ "'"
-					+ " and prefs.className='XWiki.XWikiPreferences' and prefs.name=doc.fullName"
-					+ " and parent.id.id=prefs.id and parent.id.name='parent' and parent.value='"
-					+ spaceName + "'";
-			try {
-				List allSpaceDocs = context.getWiki().getStore()
-						.searchDocuments(hql, context);
-				for (Iterator it = allSpaceDocs.iterator(); it.hasNext();) {
-					XWikiDocument toBeDeleted = (XWikiDocument) it.next();
-					context.getWiki().deleteDocument(toBeDeleted, context);
-				}
-			} catch (XWikiException e) {
-				// silently fail
-			}
-		}
-	}
+    /**
+     * After space deletion by the spacemanager plugin (see
+     * {@link com.xpn.xwiki.plugin.spacemanager.api.SpaceManager#deleteSpace(String, boolean, XWikiContext)},
+     * delete inherited spaces documents (Applications spaces documents) of the workspace.
+     * 
+     * @param spaceName the name of the root space (web) of the workspace for which to delete
+     *            application data. This is the name present in the 'parent' field of the
+     *            XWikiPreferences object of the application spaces (webs).
+     * @param if true, will delete the application documents, will do nothing otherwise.
+     */
+    public void postDeleteSpace(String spaceName, boolean deleteData, XWikiContext context)
+    {
+        if (deleteData) {
+            // Look for all documents that belong to a space that inherit from
+            // the workspace root space.
+            String hql =
+                ", XWikiDocument as sp"
+                    + ", BaseObject as xws, BaseObject as prefs, StringProperty as parent"
+                    + " where sp.web='" + spaceName
+                    + "' and sp.name='WebPreferences' and xws.name=sp.fullName"
+                    + " and xws.className='" + getWorkspaceSpaceClassName() + "'"
+                    + " and prefs.className='XWiki.XWikiPreferences' and prefs.name=doc.fullName"
+                    + " and parent.id.id=prefs.id and parent.id.name='parent' and parent.value='"
+                    + spaceName + "'";
+            try {
+                List<XWikiDocument> allSpaceDocs =
+                    context.getWiki().getStore().searchDocuments(hql, context);
+                for (XWikiDocument toBeDeleted : allSpaceDocs) {
+                    context.getWiki().deleteDocument(toBeDeleted, context);
+                }
+            } catch (XWikiException e) {
+                // silently fail but log the error
+                LOG.error("Failed to delete workspace data after deleting workspace ["
+                    + spaceName + "]", e);
+            }
+        }
+    }
 
-	/**
-	 * Helper to check if an access level from a request correspond to an actual
-	 * level of a Workspace.
-	 */
-	private boolean isValidLevel(String level) {
-		if (level.equals(Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE)
-				|| level.equals(Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC)
-				|| level.equals(Workspace.WORKSPACE_ACCESSLEVEL_OPEN))
-			return true;
-		return false;
-	}
+    /**
+     * Helper to check if an access level from a request correspond to an actual level of a
+     * Workspace.
+     */
+    private boolean isValidLevel(String level)
+    {
+        if (level.equals(Workspace.WORKSPACE_ACCESSLEVEL_PRIVATE)
+            || level.equals(Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC)
+            || level.equals(Workspace.WORKSPACE_ACCESSLEVEL_OPEN))
+            return true;
+        return false;
+    }
 
-	/**
-	 * Helper to obtain the space type (@see Workspace.WORKSPACE_SPACETYPE) from
-	 * a workspace creation request.
-	 */
-	private String getSpaceTypeForRequest(XWikiContext context) {
-		String spaceType;
-		if (context.getRequest().getParameter(
-				getWorkspaceSpaceClassName() + "_0_spacetype") != null)
-			spaceType = context.getRequest().getParameter(
-					getWorkspaceSpaceClassName() + "_0_spacetype");
-		else
-			spaceType = Workspace.WORKSPACE_SPACETYPE_DEFAULT;
-		if (spaceType.equals(Workspace.WORKSPACE_SPACETYPE_USER)
-				|| spaceType.equals(Workspace.WORKSPACE_SPACETYPE_ORG)
-				|| spaceType.equals(Workspace.WORKSPACE_SPACETYPE_WORK))
-			return spaceType;
-		return Workspace.WORKSPACE_SPACETYPE_DEFAULT;
-	}
+    /**
+     * Helper to obtain the space type (@see Workspace.WORKSPACE_SPACETYPE) from a workspace
+     * creation request.
+     */
+    private String getSpaceTypeForRequest(XWikiContext context)
+    {
+        String spaceType;
+        if (context.getRequest().getParameter(getWorkspaceSpaceClassName() + "_0_spacetype") != null)
+            spaceType =
+                context.getRequest().getParameter(getWorkspaceSpaceClassName() + "_0_spacetype");
+        else
+            spaceType = Workspace.WORKSPACE_SPACETYPE_DEFAULT;
+        if (spaceType.equals(Workspace.WORKSPACE_SPACETYPE_USER)
+            || spaceType.equals(Workspace.WORKSPACE_SPACETYPE_ORG)
+            || spaceType.equals(Workspace.WORKSPACE_SPACETYPE_WORK))
+            return spaceType;
+        return Workspace.WORKSPACE_SPACETYPE_DEFAULT;
+    }
 
-	/**
-	 * Helper to obtain the space access level (@see
-	 * Workspace.WORKSPACE_ACCESSLEVEL) from a workspace creation request.
-	 * 
-	 * @param context
-	 * @return
-	 */
-	private String getAccessLevelForRequest(XWikiContext context) {
-		String accessLevel;
-		if (getSpaceTypeForRequest(context).equals(
-				Workspace.WORKSPACE_SPACETYPE_USER)) {
-			accessLevel = context.getWiki().getXWikiPreference(
-					"xws_userspace_defaultlevel", context);
-			if (!isValidLevel(accessLevel)) {
-				accessLevel = context.getWiki().Param(
-						"xwiki.workspaces.userspace.defaultlevel",
-						Workspace.WORKSPACE_DEFAULTACCESSLEVEL_USER);
-				if (!isValidLevel(accessLevel))
-					accessLevel = Workspace.WORKSPACE_DEFAULTACCESSLEVEL_USER;
-			}
-		} else if (getSpaceTypeForRequest(context).equals(
-				Workspace.WORKSPACE_SPACETYPE_ORG)) {
-			accessLevel = Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC;
-		} else {
-			accessLevel = context.getRequest().getParameter(
-					getWorkspaceSpaceClassName() + "_0_accesslevel");
-			if (!isValidLevel(accessLevel))
-				accessLevel = Workspace.WORKSPACE_DEFAULTACCESSLEVEL;
-		}
-		return accessLevel;
-	}
+    /**
+     * Helper to obtain the space access level (@see Workspace.WORKSPACE_ACCESSLEVEL) from a
+     * workspace creation request.
+     * 
+     * @param context
+     * @return
+     */
+    private String getAccessLevelForRequest(XWikiContext context)
+    {
+        String accessLevel;
+        if (getSpaceTypeForRequest(context).equals(Workspace.WORKSPACE_SPACETYPE_USER)) {
+            accessLevel =
+                context.getWiki().getXWikiPreference("xws_userspace_defaultlevel", context);
+            if (!isValidLevel(accessLevel)) {
+                accessLevel =
+                    context.getWiki().Param("xwiki.workspaces.userspace.defaultlevel",
+                        Workspace.WORKSPACE_DEFAULTACCESSLEVEL_USER);
+                if (!isValidLevel(accessLevel))
+                    accessLevel = Workspace.WORKSPACE_DEFAULTACCESSLEVEL_USER;
+            }
+        } else if (getSpaceTypeForRequest(context).equals(Workspace.WORKSPACE_SPACETYPE_ORG)) {
+            accessLevel = Workspace.WORKSPACE_ACCESSLEVEL_PUBLIC;
+        } else {
+            accessLevel =
+                context.getRequest()
+                    .getParameter(getWorkspaceSpaceClassName() + "_0_accesslevel");
+            if (!isValidLevel(accessLevel))
+                accessLevel = Workspace.WORKSPACE_DEFAULTACCESSLEVEL;
+        }
+        return accessLevel;
+    }
 
-	/**
-	 * Prepare a newly created Workspace by adding some document for it. Iterate
-	 * over an application list, and for each application to install in the
-	 * workspace, copy or link inside the workspace resources defined by the
-	 * application object.
-	 * 
-	 * @throws SpaceManagerException
-	 */
-	protected void prepareWorkspaceContent(String spaceName,
-			XWikiContext context) throws SpaceManagerException {
-		// set space WebPreferences content
-		try {
-			XWikiDocument pDoc = context.getWiki().getDocument(
-					spaceName + ".WebPreferences", context);
-			pDoc.setContent("#includeInContext('" + WORKSPACE_PREFERENCES_SHEET
-					+ "')");
-			context.getWiki().saveDocument(pDoc, context);
-		} catch (XWikiException e1) {
-			throw new WorkspacesManagerException(e1);
-		}
+    /**
+     * Prepare a newly created Workspace by adding some document for it. Iterate over an application
+     * list, and for each application to install in the workspace, copy or link inside the workspace
+     * resources defined by the application object.
+     * 
+     * @throws SpaceManagerException
+     */
+    protected void prepareWorkspaceContent(String spaceName, XWikiContext context)
+        throws SpaceManagerException
+    {
+        // set space WebPreferences content
+        try {
+            XWikiDocument pDoc =
+                context.getWiki().getDocument(spaceName + ".WebPreferences", context);
+            pDoc.setContent("#includeInContext('" + WORKSPACE_PREFERENCES_SHEET + "')");
+            context.getWiki().saveDocument(pDoc, context);
+        } catch (XWikiException e1) {
+            throw new WorkspacesManagerException(e1);
+        }
 
-		// install default applications
-		String applist = context.getWiki().Param(
-				"xwiki.workspaces.defaultapplications", "");
-		String[] apps = StringUtils.split(applist, " ,");
-		for (int i = 0; i < apps.length; i++) {
-			try {
-				addApplicationToSpace(apps[i], spaceName, context);
-			} catch (SpaceManagerException e) {
-				// silently fail, but log the error
-				if (LOG.isErrorEnabled())
-					LOG.error("Error while adding application" + apps[i], e);
-			}
-		}
-	}
+        // install default applications
+        String applist = context.getWiki().Param("xwiki.workspaces.defaultapplications", "");
+        String[] apps = StringUtils.split(applist, " ,");
 
-	/**
-	 * Save the rights of a workspace to make it private : deny view right to
-	 * XWiki.XWikiAllGroup and XWiki.XWikiGuest
-	 * 
-	 * @throws WorkspacesManagerException
-	 */
-	public void makeSpacePrivate(String spaceName, XWikiContext context)
-			throws WorkspacesManagerException {
-		try {
-			XWikiDocument doc = context.getWiki().getDocument(
-					spaceName + ".WebPreferences", context);
-			// Deny view to guest (in case global prefs are
-			// accidently/maliciously modified)
-			BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users",
-					"XWiki.XWikiGuest");
-			if (gg == null) {
-				gg = doc.newObject("XWiki.XWikiGlobalRights", context);
-				gg.setStringValue("users", "XWiki.XWikiGuest");
-			}
-			gg.setStringValue("levels", "view");
-			gg.setIntValue("allow", 0);
-			gg.setStringValue("groups", "");
-			context.getWiki().saveDocument(doc, context);
-		} catch (XWikiException e) {
-			throw new WorkspacesManagerException(e);
-		}
-	}
+        WorkspaceApplicationsManager appsManager = new WorkspaceApplicationsManager();
 
-	/**
-	 * Save the rights of a workspace to make it public : deny view right to
-	 * XWiki.XWikiGuest but allow view right to XWiki.XWikiAllGroup so that
-	 * authenticated users can access the Workspace.
-	 * 
-	 * @throws WorkspacesManagerException
-	 */
-	public void makeSpacePublic(String spaceName, XWikiContext context)
-			throws WorkspacesManagerException {
-		try {
-			// Allow view to all group
-			XWikiDocument doc = context.getWiki().getDocument(
-					spaceName + ".WebPreferences", context);
-			BaseObject ag = doc.getObject("XWiki.XWikiGlobalRights", "groups",
-					"XWiki.XWikiAllGroup");
-			if (ag == null) {
-				ag = doc.newObject("XWiki.XWikiGlobalRights", context);
-				ag.setStringValue("groups", "XWiki.XWikiAllGroup");
-			}
-			ag.setStringValue("levels", "view");
-			ag.setIntValue("allow", 1);
-			ag.setStringValue("users", "");
-			// Deny view to guest (in case global prefs are
-			// accidently/maliciously modified)
-			BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users",
-					"XWiki.XWikiGuest");
-			if (gg == null) {
-				gg = doc.newObject("XWiki.XWikiGlobalRights", context);
-				gg.setStringValue("users", "XWiki.XWikiGuest");
-			}
-			gg.setStringValue("levels", "view");
-			gg.setIntValue("allow", 0);
-			gg.setStringValue("groups", "");
-			context.getWiki().saveDocument(doc, context);
-		} catch (XWikiException e) {
-			throw new WorkspacesManagerException(e);
-		}
-	}
+        for (int i = 0; i < apps.length; i++) {
+            try {
+                appsManager.installApplicationInSpace(apps[i], spaceName, context);
+            } catch (SpaceManagerException e) {
+                // silently fail, but log the error
+                if (LOG.isErrorEnabled())
+                    LOG.error("Error while adding application" + apps[i], e);
+            }
+        }
+    }
 
-	/**
-	 * Save rights of a workspace to make it open : deny view to
-	 * XWiki.XWikiGuest but allow both wiew and edit to XWiki.XWikiAllGroup so
-	 * that authenticated users can access in view et edit mode the workspace.
-	 * 
-	 * @throws WorkspacesManagerException
-	 * 
-	 */
-	public void makeSpaceOpen(String spaceName, XWikiContext context)
-			throws WorkspacesManagerException {
-		try {
-			// Allow view and edit to all group
-			XWikiDocument doc = context.getWiki().getDocument(
-					spaceName + ".WebPreferences", context);
-			BaseObject ag = doc.getObject("XWiki.XWikiGlobalRights", "groups",
-					"XWiki.XWikiAllGroup");
-			if (ag == null) {
-				ag = doc.newObject("XWiki.XWikiGlobalRights", context);
-				ag.setStringValue("groups", "XWiki.XWikiAllGroup");
-			}
-			ag.setStringValue("levels", "view, comment, edit");
-			ag.setIntValue("allow", 1);
-			ag.setStringValue("users", "");
-			// Deny view to guest (in case global prefs are
-			// accidently/maliciously modified)
-			BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users",
-					"XWiki.XWikiGuest");
-			if (gg == null) {
-				gg = doc.newObject("XWiki.XWikiGlobalRights", context);
-				gg.setStringValue("users", "XWiki.XWikiGuest");
-			}
-			gg.setStringValue("levels", "view");
-			gg.setIntValue("allow", 0);
-			gg.setStringValue("groups", "");
-			context.getWiki().saveDocument(doc, context);
-		} catch (XWikiException e) {
-			throw new WorkspacesManagerException(e);
-		}
-	}
+    /**
+     * Save the rights of a workspace to make it private : deny view right to XWiki.XWikiAllGroup
+     * and XWiki.XWikiGuest
+     * 
+     * @throws WorkspacesManagerException
+     */
+    public void makeSpacePrivate(String spaceName, XWikiContext context)
+        throws WorkspacesManagerException
+    {
+        try {
+            XWikiDocument doc =
+                context.getWiki().getDocument(spaceName + ".WebPreferences", context);
+            // Deny view to guest (in case global prefs are
+            // accidently/maliciously modified)
+            BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users", "XWiki.XWikiGuest");
+            if (gg == null) {
+                gg = doc.newObject("XWiki.XWikiGlobalRights", context);
+                gg.setStringValue("users", "XWiki.XWikiGuest");
+            }
+            gg.setStringValue("levels", "view");
+            gg.setIntValue("allow", 0);
+            gg.setStringValue("groups", "");
+            context.getWiki().saveDocument(doc, context);
+        } catch (XWikiException e) {
+            throw new WorkspacesManagerException(e);
+        }
+    }
 
-	/**
-	 * Install an application in the space, by copying or linking documents.
-	 * Read the list of documents to include (link) and copy from the
-	 * {@link ApplicationManagerPlugin} and save their content locally in a wiki
-	 * space (web) composed of the space wiki name and the application name.
-	 * Also make the application web inherits its rights from the space root
-	 * space (web).
-	 * 
-	 * @param appName
-	 *            the name of the application to install
-	 * @param spaceName
-	 *            the wiki name of the space to install the application in
-	 * @throws SpaceManagerException
-	 */
-	protected void addApplicationToSpace(String appName, String spaceName,
-			XWikiContext context) throws SpaceManagerException {
-		// TODO Note that this method makes a deviant usage of the application
-		// manager plugin and XWiki application objects.
-		// It uses the application field docsToInclude and documents
-		// to make a local installation (as opposed as cross-wiki global
-		// installation, which the application manager is initially designed
-		// for).
-		// In the future, the application manager should be able to
-		// handle local installation/local copy parameters, and the
-		// SpaceManagerPlugin implements a method to install a space
-		// from an application or application list.
+    /**
+     * Save the rights of a workspace to make it public : deny view right to XWiki.XWikiGuest but
+     * allow view right to XWiki.XWikiAllGroup so that authenticated users can access the Workspace.
+     * 
+     * @throws WorkspacesManagerException
+     */
+    public void makeSpacePublic(String spaceName, XWikiContext context)
+        throws WorkspacesManagerException
+    {
+        try {
+            // Allow view to all group
+            XWikiDocument doc =
+                context.getWiki().getDocument(spaceName + ".WebPreferences", context);
+            BaseObject ag =
+                doc.getObject("XWiki.XWikiGlobalRights", "groups", "XWiki.XWikiAllGroup");
+            if (ag == null) {
+                ag = doc.newObject("XWiki.XWikiGlobalRights", context);
+                ag.setStringValue("groups", "XWiki.XWikiAllGroup");
+            }
+            ag.setStringValue("levels", "view");
+            ag.setIntValue("allow", 1);
+            ag.setStringValue("users", "");
+            // Deny view to guest (in case global prefs are
+            // accidently/maliciously modified)
+            BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users", "XWiki.XWikiGuest");
+            if (gg == null) {
+                gg = doc.newObject("XWiki.XWikiGlobalRights", context);
+                gg.setStringValue("users", "XWiki.XWikiGuest");
+            }
+            gg.setStringValue("levels", "view");
+            gg.setIntValue("allow", 0);
+            gg.setStringValue("groups", "");
+            context.getWiki().saveDocument(doc, context);
+        } catch (XWikiException e) {
+            throw new WorkspacesManagerException(e);
+        }
+    }
 
-		// get the application manager api
-		ApplicationManagerPluginApi appmanager = (ApplicationManagerPluginApi) context
-				.getWiki().getPluginApi(ApplicationManagerPlugin.PLUGIN_NAME,
-						context);
-		try {
-			// Retrieve the application descriptor
-			XWikiApplication app = appmanager.getApplicationDocument(appName);
+    /**
+     * Save rights of a workspace to make it open : deny view to XWiki.XWikiGuest but allow both
+     * wiew and edit to XWiki.XWikiAllGroup so that authenticated users can access in view et edit
+     * mode the workspace.
+     * 
+     * @throws WorkspacesManagerException
+     */
+    public void makeSpaceOpen(String spaceName, XWikiContext context)
+        throws WorkspacesManagerException
+    {
+        try {
+            // Allow view and edit to all group
+            XWikiDocument doc =
+                context.getWiki().getDocument(spaceName + ".WebPreferences", context);
+            BaseObject ag =
+                doc.getObject("XWiki.XWikiGlobalRights", "groups", "XWiki.XWikiAllGroup");
+            if (ag == null) {
+                ag = doc.newObject("XWiki.XWikiGlobalRights", context);
+                ag.setStringValue("groups", "XWiki.XWikiAllGroup");
+            }
+            ag.setStringValue("levels", "view, comment, edit");
+            ag.setIntValue("allow", 1);
+            ag.setStringValue("users", "");
+            // Deny view to guest (in case global prefs are
+            // accidently/maliciously modified)
+            BaseObject gg = doc.getObject("XWiki.XWikiGlobalRights", "users", "XWiki.XWikiGuest");
+            if (gg == null) {
+                gg = doc.newObject("XWiki.XWikiGlobalRights", context);
+                gg.setStringValue("users", "XWiki.XWikiGuest");
+            }
+            gg.setStringValue("levels", "view");
+            gg.setIntValue("allow", 0);
+            gg.setStringValue("groups", "");
+            context.getWiki().saveDocument(doc, context);
+        } catch (XWikiException e) {
+            throw new WorkspacesManagerException(e);
+        }
+    }
 
-			if (app == null)
-				throw new SpaceManagerException();
-
-			String appSpace = spaceName + XWIKI_WORKSPACE_APPSEPARATOR
-					+ app.getAppName();
-
-			// Retrieve the application document list
-			Collection appDocs = app.getDocumentsNames(false, false);
-
-			// Retrieve the application documents to include
-			Collection docsToInclude = app.getDocsNameToInclude(true);
-
-			for (Iterator it = appDocs.iterator(); it.hasNext();) {
-				String docFullName = (String) it.next();
-				// If the doc is not in the include list,
-				// We copy it to the target space
-				if (!docsToInclude.contains(docFullName)) {
-					String docName = docFullName.substring(docFullName
-							.indexOf('.') + 1);
-					String targetDocName = appSpace + XWIKI_SPACE_SEPARATOR
-							+ docName;
-					context.getWiki().copyDocument(docFullName, targetDocName,
-							true, context);
-				}
-			}
-
-			for (Iterator it = docsToInclude.iterator(); it.hasNext();) {
-				String docFullName = (String) it.next();
-				String docName = docFullName
-						.substring(docFullName.indexOf('.') + 1);
-
-				// Compute the target doc name based on application name, space
-				// name and document
-				// name
-				// EX: Space_Wiki.WebHome for "Space" space name, "Wiki" appname
-				// and "WebHome" doc
-				String targetDocName = appSpace + XWIKI_SPACE_SEPARATOR
-						+ docName;
-				XWikiDocument targetDoc = context.getWiki().getDocument(
-						targetDocName, context);
-
-				// Link the content with the application code
-				targetDoc.setContent(MessageFormat.format(
-						"#includeInContext(\"{0}\")",
-						new Object[] { docFullName }));
-
-				// Save the document
-				context.getWiki().saveDocument(targetDoc, context);
-			}
-
-			if (appDocs.size() > 0) {
-				// if we've installed anything,
-				// make the installed app inherit its right from the Workspace
-				// root web
-				XWikiDocument appPreferences = context.getWiki().getDocument(
-						appSpace + XWIKI_SPACE_SEPARATOR + "WebPreferences",
-						context);
-				BaseObject pObj = appPreferences.getObject(
-						"XWiki.XWikiPreferences", true, context);
-				pObj.setStringValue("parent", spaceName);
-				context.getWiki().saveDocument(appPreferences, context);
-			}
-		} catch (XWikiException e) {
-			throw new SpaceManagerException(e);
-		}
-	}
 }



More information about the notifications mailing list