Author: jvdrean
Date: 2008-02-22 12:07:00 +0100 (Fri, 22 Feb 2008)
New Revision: 7914

Modified:
   
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListJob.java
   
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPlugin.java
   
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPluginApi.java
Log:
XPWATCHLIST-12 : Global user can't watch sub-wiki's documents

Modified: 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListJob.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListJob.java
 2008-02-22 10:52:05 UTC (rev 7913)
+++ 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListJob.java
 2008-02-22 11:07:00 UTC (rev 7914)
@@ -62,10 +62,14 @@
 
     protected com.xpn.xwiki.api.Context xcontext = null;
 
-    protected WatchListPluginApi notificationPlugin = null;
+    protected WatchListPluginApi watchlistPlugin = null;
 
+    protected WatchListPlugin plugin = null;
+
     protected int interval = 0;
 
+    protected String jobMailTemplate;
+
     protected String logprefix;
 
     /**
@@ -73,14 +77,17 @@
      *
      * @param context Context of the request
      */
-    public void init(JobExecutionContext context)
+    public void init(JobExecutionContext context) throws XWikiException
     {
         JobDataMap data = context.getJobDetail().getJobDataMap();
         xwiki = (com.xpn.xwiki.api.XWiki) data.get("xwiki");
         xcontext = (com.xpn.xwiki.api.Context) data.get("context");
-        notificationPlugin = (WatchListPluginApi) 
xwiki.getPlugin(WatchListPlugin.ID);
+        watchlistPlugin = (WatchListPluginApi) 
xwiki.getPlugin(WatchListPlugin.ID);
+        plugin = (WatchListPlugin) watchlistPlugin.getPlugin();
         xjob = (BaseObject) data.get("xjob");
-        interval = Integer.parseInt(xjob.getLargeStringValue("script"));
+        jobMailTemplate = xjob.getLargeStringValue("script").trim();
+        // retreive the interval from job name (1=hourly, 2=daily, etc)
+        interval = 
Integer.parseInt(xjob.getName().substring(xjob.getName().length() - 1));
         logprefix = "WatchList job " + xcontext.getDatabase() + ":" + 
xjob.getName() + " ";
     }
 
@@ -91,11 +98,11 @@
      */
     public void execute(JobExecutionContext context) throws 
JobExecutionException
     {
-        // Set required objects
-        init(context);
+        try {
+            // Set required objects
+            init(context);
 
-        try {
-            // Retreive notification subscribers
+            // Retreive notification subscribers (all wikis)
             Collection subscribers = retrieveNotificationSubscribers();
             if (subscribers != null && subscribers.size() > 0) {
                 // Retreive updated documents
@@ -159,37 +166,36 @@
         if (spaceCriterion.length() == 0 && documentCriterion.length() == 0
             && query.length() == 0)
         {
-            /* Iterator docIt = updatedDocuments.iterator();
-            while (docIt.hasNext()) {
-                watchedDocuments.add(docIt.next());
-            } */
             return new ArrayList();
         }
 
         List filteredDocumentList = new ArrayList();
         String[] watchedSpaces = spaceCriterion.split(",");
+        String[] docArray = documentCriterion.split(",");
 
-        String[] docArray = documentCriterion.split(",");
         for (int i = 0; i < docArray.length; i++) {
             watchedDocuments.add(docArray[i]);
         }
 
         if (query.length() > 0) {
-            List queryDocuments = xwiki.searchDocuments(query);
+            List queryDocuments =
+                plugin.globalSearchDocuments(query, new ArrayList(), xcontext, 
xwiki);
             watchedDocuments.addAll(queryDocuments);
         }
 
         Iterator updatedDocumentsIt = updatedDocuments.iterator();
         while (updatedDocumentsIt.hasNext()) {
-            String updatedDocument = (String) updatedDocumentsIt.next();
-            String updatedDocumentSpace = 
xwiki.getDocument(updatedDocument).getSpace();
+            String updatedDocumentName = (String) updatedDocumentsIt.next();
+            Document updatedDocument = xwiki.getDocument(updatedDocumentName);
+            String updatedDocumentSpace =
+                updatedDocument.getWiki() + ":" + updatedDocument.getSpace();
             boolean documentAdded = false;
 
             for (int i = 0; i < watchedSpaces.length; i++) {
                 if (updatedDocumentSpace.equals(watchedSpaces[i])
-                    && xwiki.hasAccessLevel("view", subscriber, 
updatedDocument))
+                    && xwiki.hasAccessLevel("view", subscriber, 
updatedDocumentName))
                 {
-                    filteredDocumentList.add(updatedDocument);
+                    filteredDocumentList.add(updatedDocumentName);
                     documentAdded = true;
                     break;
                 }
@@ -201,10 +207,10 @@
                 Iterator watchedDocumentIt = watchedDocuments.iterator();
                 while (watchedDocumentIt.hasNext()) {
                     String watchedDocumentName = (String) 
watchedDocumentIt.next();
-                    if (updatedDocument.equals(watchedDocumentName)
-                        && xwiki.hasAccessLevel("view", subscriber, 
updatedDocument))
+                    if (updatedDocumentName.equals(watchedDocumentName)
+                        && xwiki.hasAccessLevel("view", subscriber, 
updatedDocumentName))
                     {
-                        filteredDocumentList.add(updatedDocument);
+                        filteredDocumentList.add(updatedDocumentName);
                         break;
                     }
                 }
@@ -223,13 +229,13 @@
      */
     protected List retrieveNotificationSubscribers() throws XWikiException
     {
-        List userDocs =
-            xwiki.searchDocuments(
-                ", BaseObject as obj, StringProperty as prop where 
obj.name=doc.fullName and obj.className='"
-                    + WatchListPlugin.WATCHLIST_CLASS +
-                    "' and prop.id.id=obj.id and prop.name='interval' " +
-                    "and prop.value='" + interval + "'");
-        return userDocs;
+        String request =
+            ", BaseObject as obj, StringProperty as prop where " +
+                "obj.name=doc.fullName and obj.className='"
+                + WatchListPlugin.WATCHLIST_CLASS +
+                "' and prop.id.id=obj.id and prop.name='interval' " +
+                "and prop.value='" + interval + "')";
+        return plugin.globalSearchDocuments(request, new ArrayList(), 
xcontext, xwiki);
     }
 
     /**
@@ -262,7 +268,8 @@
         List values = new ArrayList();
         values.add(dt.toDate());
         String updatedDocumentRequest = "where doc.date > ? order by doc.date 
desc";
-        return xwiki.searchDocuments(updatedDocumentRequest, 0, 0, values);
+
+        return plugin.globalSearchDocuments(updatedDocumentRequest, values, 
xcontext, xwiki);
     }
 
     /**
@@ -303,9 +310,13 @@
         // Get wiki administrator email (default : [EMAIL PROTECTED])
         String sender = xwiki.getXWikiPreference("admin_email", "[EMAIL 
PROTECTED]");
 
+        // Set email template
+        String mailTemplate =
+                xcontext.getMainWikiName() + ":" + 
WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE;
+
         // Send message from template
         int sendResult =
             emailService.sendMessageFromTemplate(sender, emailAddr, null,
-                null, language, xcontext.getMainWikiName() + ":" + 
WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE, vcontext);
+                null, language, mailTemplate, vcontext);
     }
 }

Modified: 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPlugin.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPlugin.java
      2008-02-22 10:52:05 UTC (rev 7913)
+++ 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPlugin.java
      2008-02-22 11:07:00 UTC (rev 7914)
@@ -19,10 +19,11 @@
  */
 package com.xpn.xwiki.plugin.watchlist;
 
-import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.api.XWiki;
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
 import com.xpn.xwiki.api.Api;
+import com.xpn.xwiki.api.Context;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.objects.classes.BaseClass;
@@ -30,12 +31,16 @@
 import com.xpn.xwiki.plugin.XWikiPluginInterface;
 import com.xpn.xwiki.plugin.mailsender.MailSenderPlugin;
 import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin;
-import com.xpn.xwiki.plugin.scheduler.SchedulerPluginApi;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Collections;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
 
 /**
  * Plugin that offers WatchList features to XWiki. These feature allow users 
to build lists of pages
@@ -95,14 +100,6 @@
     public void virtualInit(XWikiContext context)
     {
         super.virtualInit(context);
-        try {
-            initWatchListClass(context);
-            initEmailTemplate(context);
-            initWatchlistJobs(context);
-        } catch (XWikiException e) {
-            log.error("virtualInit", e);
-            e.printStackTrace();
-        }
     }
 
     /**
@@ -114,9 +111,11 @@
     {
         super.init(context);
         try {
+            // Main wiki
             initWatchListClass(context);
             initEmailTemplate(context);
             initWatchlistJobs(context);
+            sanitizeWatchlists(context);
         } catch (XWikiException e) {
             log.error("virtualInit", e);
             e.printStackTrace();
@@ -127,7 +126,7 @@
      * [EMAIL PROTECTED]
      *
      * @see 
com.xpn.xwiki.plugin.XWikiDefaultPlugin#getPluginApi(XWikiPluginInterface,
-     *XWikiContext)
+     *      XWikiContext)
      */
     public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context)
     {
@@ -152,11 +151,10 @@
     protected BaseClass initWatchListClass(XWikiContext context) throws 
XWikiException
     {
         XWikiDocument doc;
-        XWiki xwiki = context.getWiki();
         boolean needsUpdate = false;
 
         try {
-            doc = xwiki.getDocument(WATCHLIST_CLASS, context);
+            doc = context.getWiki().getDocument(WATCHLIST_CLASS, context);
         } catch (Exception e) {
             doc = new XWikiDocument();
             String[] spaceAndName = WATCHLIST_CLASS.split(".");
@@ -187,7 +185,7 @@
         }
 
         if (needsUpdate) {
-            xwiki.saveDocument(doc, context);
+            context.getWiki().saveDocument(doc, "", true, context);
         }
         return bclass;
     }
@@ -205,13 +203,12 @@
         XWikiContext context) throws XWikiException
     {
         XWikiDocument doc;
-        XWiki xwiki = context.getWiki();
         boolean needsUpdate = false;
         String jobClass = "com.xpn.xwiki.plugin.watchlist.WatchListJob";
 
         String docName = WATCHLIST_EMAIL_JOB_COMMON_NAME + interval;
         try {
-            doc = xwiki.getDocument(docName, context);
+            doc = context.getWiki().getDocument(docName, context);
             BaseObject obj = doc.getObject(SchedulerPlugin.XWIKI_JOB_CLASS);
             if (obj == null) {
                 needsUpdate = true;
@@ -236,8 +233,8 @@
             // set the needed context params
             // TODO create a watchlist application that holds those jobs as 
documents
             job.setStringValue("contextUser", "XWiki.Admin");
-            job.setStringValue("contextLang","en");
-            job.setStringValue("contextDatabase","xwiki");
+            job.setStringValue("contextLang", "en");
+            job.setStringValue("contextDatabase", "xwiki");
 
             doc.setContent("#includeInContext('XWiki.SchedulerJobSheet')");
             doc.setAuthor("XWiki.Admin");
@@ -247,7 +244,7 @@
             rights.setStringValue("groups", "XWiki.XWikiAdminGroup");
             rights.setStringValue("levels", "edit,delete");
             rights.setIntValue("allow", 1);
-            xwiki.saveDocument(doc, context);
+            context.getWiki().saveDocument(doc, "", true, context);
             ((SchedulerPlugin) context.getWiki().getPlugin("scheduler", 
context))
                 .scheduleJob(job, context);
         }
@@ -281,11 +278,11 @@
     protected void initEmailTemplate(XWikiContext context) throws 
XWikiException
     {
         XWikiDocument doc;
-        XWiki xwiki = context.getWiki();
         boolean needsUpdate = false;
 
         try {
-            doc = xwiki.getDocument(context.getMainXWiki() + ":" + 
WATCHLIST_EMAIL_TEMPLATE, context);
+            doc =
+                context.getWiki().getDocument(WATCHLIST_EMAIL_TEMPLATE, 
context);
             BaseObject obj = 
doc.getObject(MailSenderPlugin.EMAIL_XWIKI_CLASS_NAME);
             if (obj == null) {
                 needsUpdate = true;
@@ -339,76 +336,75 @@
             if ((content == null) || (content.equals(""))) {
                 doc.setContent("1 Notification message");
             }
-            xwiki.saveDocument(doc, context);
+            context.getWiki().saveDocument(doc, "", true, context);
         }
     }
 
     /**
-     * Is the watchedDocument in localUser WatchList ?
+     * Is the watchedDocument in user WatchList ?
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param watchedElement XWiki Document name
      * @param context Context of the request
      * @return True if the page is watched by user
      */
-    public boolean isWatched(String localUser, String watchedElement, 
XWikiContext context)
+    public boolean isWatched(String user, String watchedElement, XWikiContext 
context)
         throws XWikiException
     {
-        return this.getWatchedDocuments(localUser, 
context).contains(watchedElement) |
-            this.getWatchedSpaces(localUser, context).contains(watchedElement);
+        return this.getWatchedDocuments(user, 
context).contains(watchedElement) |
+            this.getWatchedSpaces(user, context).contains(watchedElement);
     }
 
     /**
-     * Creates a WatchList XWiki Object in the localUser's profile's page
+     * Creates a WatchList XWiki Object in the user's profile's page
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param context Context of the request
      * @throws XWikiException if the document cannot be saved
      */
-    public void createWatchListObject(String localUser, XWikiContext context) 
throws XWikiException
+    public void createWatchListObject(String user, XWikiContext context) 
throws XWikiException
     {
-        XWiki wiki = context.getWiki();
-        XWikiDocument userDocument = wiki.getDocument(localUser, context);
+        XWikiDocument userDocument = context.getWiki().getDocument(user, 
context);
         int nb = userDocument.createNewObject(WATCHLIST_CLASS, context);
         BaseObject wObj = userDocument.getObject(WATCHLIST_CLASS, nb);
         wObj.set("interval", "1", context);
-        wiki.saveDocument(userDocument, 
context.getMessageTool().get("watchlist.create.object"),
-            true, context);
+        context.getWiki()
+            .saveDocument(userDocument, 
context.getMessageTool().get("watchlist.create.object"),
+                true, context);
     }
 
     /**
-     * Gets the WatchList XWiki Object from localUser's profile's page
+     * Gets the WatchList XWiki Object from user's profile's page
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param context Context of the request
      * @return the WatchList XWiki BaseObject
      * @throws XWikiException if BaseObject creation fails
      */
-    private BaseObject getWatchListObject(String localUser, XWikiContext 
context)
+    public BaseObject getWatchListObject(String user, XWikiContext context)
         throws XWikiException
     {
-        XWikiDocument userDocument = context.getWiki().getDocument(localUser, 
context);
+        XWikiDocument userDocument = context.getWiki().getDocument(user, 
context);
         if (userDocument.getObjectNumbers(WATCHLIST_CLASS) == 0) {
-            this.createWatchListObject(localUser, context);
-            return this.getWatchListObject(localUser, context);
+            this.createWatchListObject(user, context);
+            return this.getWatchListObject(user, context);
         }
         return userDocument.getObject(WATCHLIST_CLASS);
     }
 
     /**
-     * Sets a largeString property in the localUser's WatchList Object, then 
saves the localUser's
-     * profile
+     * Sets a largeString property in the user's WatchList Object, then saves 
the user's profile
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param prop Property name (documents,spaces,query)
      * @param value Property value (list of documents,list of pages,hql query)
      * @param context Context of the request
      * @throws XWikiException if the user's profile cannot be saved
      */
-    private void setWatchListLargeStringProperty(String localUser, String 
prop, String value,
+    public void setWatchListLargeStringProperty(String user, String prop, 
String value,
         XWikiContext context) throws XWikiException
     {
-        XWikiDocument userDocument = context.getWiki().getDocument(localUser, 
context);
+        XWikiDocument userDocument = context.getWiki().getDocument(user, 
context);
         userDocument.setLargeStringValue(WATCHLIST_CLASS, prop, value);
         userDocument.isMinorEdit();
         context.getWiki().saveDocument(userDocument,
@@ -416,57 +412,59 @@
     }
 
     /**
-     * Get the list of documents watched by localUser
+     * Get the list of documents watched by user
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param context Context of the request
      * @return List of watched documents
      * @throws XWikiException if the WatchList Object cannot be retreived
      */
-    public List getWatchedDocuments(String localUser, XWikiContext context) 
throws XWikiException
+    public List getWatchedDocuments(String user, XWikiContext context) throws 
XWikiException
     {
-        BaseObject watchListObject = this.getWatchListObject(localUser, 
context);
+        BaseObject watchListObject = this.getWatchListObject(user, context);
         String watchedItems = 
watchListObject.getLargeStringValue("documents").trim();
         return Arrays.asList(watchedItems.split(","));
     }
 
     /**
-     * Get the list of spaces watched by localUser
+     * Get the list of spaces watched by user
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param context Context of the request
      * @return List of watched space
      * @throws XWikiException if the WatchList Object cannot be retreived
      */
-    public List getWatchedSpaces(String localUser, XWikiContext context) 
throws XWikiException
+    public List getWatchedSpaces(String user, XWikiContext context) throws 
XWikiException
     {
-        BaseObject watchListObject = this.getWatchListObject(localUser, 
context);
+        BaseObject watchListObject = this.getWatchListObject(user, context);
         String watchedItems = 
watchListObject.getLargeStringValue("spaces").trim();
         return Arrays.asList(watchedItems.split(","));
     }
 
     /**
-     * Add the specified element (document or space) to the corresponding list 
in the localUser's
+     * Add the specified element (document or space) to the corresponding list 
in the user's
      * WatchList
      *
-     * @param localUser XWikiUser
+     * @param user XWikiUser
      * @param newWatchedElement The name of the element to add (document of 
space)
      * @param isSpace True if the element is a space, false if it's a document
      * @param context Context of the request
      * @return True if the element was'nt already in list
      * @throws XWikiException if the modification hasn't been saved
      */
-    public boolean addWatchedElement(String localUser, String 
newWatchedElement,
+    public boolean addWatchedElement(String user, String newWatchedElement,
         boolean isSpace, XWikiContext context) throws XWikiException
     {
-        if (this.isWatched(localUser, newWatchedElement, context)) {
+        newWatchedElement = context.getDatabase() + ":" + newWatchedElement;
+
+        if (this.isWatched(user, newWatchedElement, context)) {
             return false;
         }
 
         String prop = isSpace ? "spaces" : "documents";
         List watchedItems = isSpace ?
-            this.getWatchedSpaces(localUser, context) :
-            this.getWatchedDocuments(localUser, context);
+            this.getWatchedSpaces(user, context) :
+            this.getWatchedDocuments(user, context);
 
         StringBuffer updatedWatchedElements = new StringBuffer();
         for (int i = 0; i < watchedItems.size(); i++) {
@@ -479,35 +477,37 @@
             updatedWatchedElements.append(",");
         }
         updatedWatchedElements.append(newWatchedElement);
-        this.setWatchListLargeStringProperty(localUser, prop, 
updatedWatchedElements.toString(),
+        this.setWatchListLargeStringProperty(user, prop, 
updatedWatchedElements.toString(),
             context);
         return true;
     }
 
     /**
-     * Remove the specified element (document or space) from the corresponding 
list in the
-     * localUser's WatchList
+     * Remove the specified element (document or space) from the corresponding 
list in the user's
+     * WatchList
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param watchedElement The name of the element to remove (document or 
space)
      * @param isSpace True if the element is a space, false if it's a document
      * @param context Context of the request
      * @return True if the element was in list and has been removed, false if 
the element was'nt in
      *         the list
-     * @throws XWikiException If the WatchList Object cannot be retreived or 
if the localUser's
-     * profile cannot be saved
+     * @throws XWikiException If the WatchList Object cannot be retreived or 
if the user's profile
+     * cannot be saved
      */
-    public boolean removeWatchedElement(String localUser, String 
watchedElement,
+    public boolean removeWatchedElement(String user, String watchedElement,
         boolean isSpace, XWikiContext context) throws XWikiException
     {
-        if (!this.isWatched(localUser, watchedElement, context)) {
+        watchedElement = context.getDatabase() + ":" + watchedElement;
+
+        if (!this.isWatched(user, watchedElement, context)) {
             return false;
         }
 
         String prop = isSpace ? "spaces" : "documents";
         List watchedItems = isSpace ?
-            this.getWatchedSpaces(localUser, context) :
-            this.getWatchedDocuments(localUser, context);
+            this.getWatchedSpaces(user, context) :
+            this.getWatchedDocuments(user, context);
 
         StringBuffer updatedWatchedElements = new StringBuffer();
         for (int i = 0; i < watchedItems.size(); i++) {
@@ -518,23 +518,22 @@
                 updatedWatchedElements.append(watchedItems.get(i));
             }
         }
-        this.setWatchListLargeStringProperty(localUser, prop, 
updatedWatchedElements.toString(),
+        this.setWatchListLargeStringProperty(user, prop, 
updatedWatchedElements.toString(),
             context);
         return true;
     }
 
     /**
-     * Get the list of the elements watched by localUser ordered by last 
modification date,
-     * descending
+     * Get the list of the elements watched by user ordered by last 
modification date, descending
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param context Context of the request
      * @return The list of the watched elements ordered by last modification 
date, descending
      * @throws XWikiException If the search request fails
      */
-    public List getWatchListWhatsNew(String localUser, XWikiContext context) 
throws XWikiException
+    public List getWatchListWhatsNew(String user, XWikiContext context) throws 
XWikiException
     {
-        BaseObject watchListObject = this.getWatchListObject(localUser, 
context);
+        BaseObject watchListObject = this.getWatchListObject(user, context);
         String watchedDocuments =
             
watchListObject.getLargeStringValue("documents").trim().replaceFirst("^,", "")
                 .replaceAll(",", "','");
@@ -546,4 +545,145 @@
             "order by doc.date desc";
         return context.getWiki().getStore().search(request, 20, 0, context);
     }
+
+    /**
+     * @return the full list of all database names of all defined virtual 
wikis. The database names
+     *         are computed from the names of documents having a 
XWiki.XWikiServerClass object
+     *         attached to them by removing the "XWiki.XWikiServer" prefix and 
making it lower case.
+     *         For example a page named "XWiki.XWikiServerMyDatabase" would 
return "mydatabase" as
+     *         the database name.
+     */
+    public List getVirtualWikisDatabaseNames(Context context, XWiki xwiki) 
throws XWikiException
+    {
+        List databaseNames = new ArrayList();
+
+        String database = context.getDatabase();
+        try {
+            context.setDatabase(context.getMainWikiName());
+
+            String hql =
+                ", BaseObject as obj, StringProperty as prop where 
obj.name=doc.fullName"
+                    +
+                    " and obj.name <> 'XWiki.XWikiServerClassTemplate' and 
obj.className='XWiki.XWikiServerClass' "
+                    + "and prop.id.id = obj.id ";
+            List list = xwiki.searchDocuments(hql);
+
+            for (Iterator it = list.iterator(); it.hasNext();) {
+                String docname = (String) it.next();
+                if (docname.startsWith("XWiki.XWikiServer")) {
+                    
databaseNames.add(docname.substring("XWiki.XWikiServer".length())
+                        .toLowerCase());
+                }
+            }
+        } finally {
+            context.setDatabase(database);
+        }
+
+        return databaseNames;
+    }
+
+    /**
+     * Search documents on all the wikis by passing HQL where clause values as 
parameters.
+     *
+     * @param request the HQL where clause.
+     * @param values the where clause values that replace the question marks 
(?)
+     * @return a list of document names prefixed with the wiki they come from 
ex :
+     *         xwiki:Main.WebHome
+     */
+    protected List globalSearchDocuments(String request, List values, Context 
context, XWiki xwiki)
+    {
+        String initialDb =
+            !context.getDatabase().equals("") ? context.getDatabase() :
+                context.getMainWikiName();
+        List wikiServers = Collections.EMPTY_LIST;
+        List results = new ArrayList();
+
+        if (xwiki.isVirtual()) {
+            try {
+                wikiServers = getVirtualWikisDatabaseNames(context, xwiki);
+                if (!wikiServers.contains(context.getMainWikiName())) {
+                    wikiServers.add(context.getMainWikiName());
+                }
+            } catch (Exception e) {
+                getLogger().error("error getting list of wiki servers!", e);
+            }
+        } else {
+            wikiServers = new ArrayList();
+            wikiServers.add(context.getMainWikiName());
+        }
+
+        try {
+            for (Iterator iter = wikiServers.iterator(); iter.hasNext();) {
+                String wiki = (String) iter.next();
+                String wikiPrefix = wiki + ":";
+                context.setDatabase(wiki);
+                try {
+                    List upDocsInWiki = xwiki.searchDocuments(request, 0, 0, 
values);
+                    Iterator it = upDocsInWiki.iterator();
+                    while (it.hasNext()) {
+                        results.add(wikiPrefix + it.next());
+                    }
+                } catch (Exception e) {
+                    getLogger().error("error getting list of documents in the 
wiki : " + wiki, e);
+                }
+            }
+        } finally {
+            context.setDatabase(initialDb);
+        }
+        return results;
+    }
+
+    /**
+     * Loop over all the watchlists stored in all the wikis. Verify if the 
database prefix is
+     * present on all the watchlist items, if not adds MainWiki as prefix.
+     */
+    protected void sanitizeWatchlists(XWikiContext context)
+    {
+        String request = ", BaseObject as obj where obj.name=doc.fullName and 
obj.className='"
+            + WatchListPlugin.WATCHLIST_CLASS + "'";
+        List subscribers = globalSearchDocuments(request, new ArrayList(), new 
Context(context),
+            new XWiki(context.getWiki(), context));
+        Iterator it = subscribers.iterator();
+        while (it.hasNext()) {
+            String user = (String) it.next();
+            try {
+
+                XWikiDocument userDocument =
+                    context.getWiki().getDocument(user, context);
+                BaseObject wobj = 
userDocument.getObject(WatchListPlugin.WATCHLIST_CLASS);
+                String docs = wobj.getLargeStringValue("documents").trim();
+                String spaces = wobj.getLargeStringValue("spaces").trim();
+                boolean update = false;
+
+                // Add db prefixes to document names stored in the watchlist 
object
+                Pattern p = Pattern.compile("(^|,)([^\\.,:]+)(\\.)([^\\.,]+)");
+                Matcher m = p.matcher(docs);
+                if (m.find()) {
+                    String newdocs = 
docs.replaceAll("(^|,)([^\\.,:]+)(\\.)([^\\.,]+)",
+                        "$1" + context.getMainXWiki() + ":$2$3$4");
+                    wobj.setLargeStringValue("documents", newdocs);
+                    getLogger().info("Sanitizing watchlist documents for user 
: " + user);
+                    update = true;
+                }
+
+                // Add db prefixes to space names stored in the watchlist 
object
+                p = Pattern.compile("(^|,)([^:,]+)(?=(,|$))");
+                m = p.matcher(spaces);
+                if (m.find()) {
+                    String newspaces = 
spaces.replaceAll("(^|,)([^:,]+)(?=(,|$))",
+                        "$1" + context.getMainXWiki() + ":$2");
+                    wobj.setLargeStringValue("spaces", newspaces);
+                    getLogger().info("Sanitizing watchlist spaces for user : " 
+ user);
+                    update = true;
+                }
+
+                if (update) {
+                    context.getWiki().saveDocument(userDocument, "", true, 
context);
+                }
+            } catch (Exception e) {
+                getLogger().error("Exception while sanitizing watchlist for 
user : " + user);
+                e.printStackTrace();
+            }
+        }
+    }
 }

Modified: 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPluginApi.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPluginApi.java
   2008-02-22 10:52:05 UTC (rev 7913)
+++ 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPluginApi.java
   2008-02-22 11:07:00 UTC (rev 7914)
@@ -58,8 +58,8 @@
      */
     public boolean isDocInWatchedSpaces() throws XWikiException
     {
-        return 
getWatchListPlugin().getWatchedSpaces(getXWikiContext().getLocalUser(),
-            getXWikiContext()).contains(context.getDoc().getSpace());
+        return 
getWatchListPlugin().getWatchedSpaces(getXWikiContext().getUser(),
+            getXWikiContext()).contains(context.getDatabase() + ":" + 
context.getDoc().getSpace());
     }
 
     /**
@@ -70,8 +70,8 @@
      */
     public boolean isDocumentWatched() throws XWikiException
     {
-        return 
getWatchListPlugin().getWatchedDocuments(context.getLocalUser(), context)
-            .contains(context.getDoc().getFullName());
+        return getWatchListPlugin().getWatchedDocuments(context.getUser(), 
context)
+            .contains(context.getDatabase() + ":" + 
context.getDoc().getFullName());
     }
 
     /**
@@ -83,22 +83,23 @@
      */
     public boolean addDocument(String wDoc) throws XWikiException
     {
-        return 
getWatchListPlugin().addWatchedElement(getXWikiContext().getLocalUser(),
+        return 
getWatchListPlugin().addWatchedElement(getXWikiContext().getUser(),
             wDoc, false, getXWikiContext());
     }
 
     /**
      * Allows Adminstrators to add the specified document in the specified 
user's WatchList
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param wDoc Document to add
      * @return True if the document wasn't already in the WatchList
      * @throws XWikiException If the user's WatchList Object cannot be 
retreived nor created
      */
-    public boolean addDocumentForUser(String localUser, String wDoc) throws 
XWikiException
+    public boolean addDocumentForUser(String user, String wDoc) throws 
XWikiException
     {
         return context.getWiki().getUser(context).hasAdminRights() &&
-            getWatchListPlugin().addWatchedElement(localUser, wDoc, false, 
getXWikiContext());
+            getWatchListPlugin().addWatchedElement(user, wDoc, false,
+                getXWikiContext());
     }
 
     /**
@@ -110,22 +111,23 @@
      */
     public boolean removeDocument(String wDoc) throws XWikiException
     {
-        return 
getWatchListPlugin().removeWatchedElement(getXWikiContext().getLocalUser(),
+        return 
getWatchListPlugin().removeWatchedElement(getXWikiContext().getUser(),
             wDoc, false, getXWikiContext());
     }
 
     /**
      * Allows Adminstrators to remove the specified document from the 
specified user's WatchList
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param wDoc Document to remove
      * @return True if the document was in the WatchList and has been removed
      * @throws XWikiException If the user's WatchList Object cannot be 
retreived nor created
      */
-    public boolean removeDocumentForUser(String localUser, String wDoc) throws 
XWikiException
+    public boolean removeDocumentForUser(String user, String wDoc) throws 
XWikiException
     {
         return context.getWiki().getUser(context).hasAdminRights() &&
-            getWatchListPlugin().removeWatchedElement(localUser, wDoc, false, 
getXWikiContext());
+            getWatchListPlugin().removeWatchedElement(user, wDoc,
+                false, getXWikiContext());
     }
 
     /**
@@ -136,8 +138,8 @@
      */
     public boolean isSpaceWatched() throws XWikiException
     {
-        return getWatchListPlugin().getWatchedSpaces(context.getLocalUser(), 
context)
-            .contains(context.getDoc().getSpace());
+        return getWatchListPlugin().getWatchedSpaces(context.getUser(), 
context)
+            .contains(context.getDatabase() + ":" + 
context.getDoc().getSpace());
     }
 
     /**
@@ -149,22 +151,23 @@
      */
     public boolean addSpace(String wSpace) throws XWikiException
     {
-        return 
getWatchListPlugin().addWatchedElement(getXWikiContext().getLocalUser(),
+        return 
getWatchListPlugin().addWatchedElement(getXWikiContext().getUser(),
             wSpace, true, getXWikiContext());
     }
 
     /**
      * Allows Administrators to add the specified space to the specified 
user's WatchList
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param wSpace Space to add
      * @return True if the space wasn't already in the user's WatchList and 
has been added
      * @throws XWikiException If the user's WatchList Object cannot be 
retreived nor created
      */
-    public boolean addSpaceForUser(String localUser, String wSpace) throws 
XWikiException
+    public boolean addSpaceForUser(String user, String wSpace) throws 
XWikiException
     {
         return context.getWiki().getUser(context).hasAdminRights() &&
-            getWatchListPlugin().addWatchedElement(localUser, wSpace, true, 
getXWikiContext());
+            getWatchListPlugin().addWatchedElement(user, wSpace, true,
+                getXWikiContext());
     }
 
     /**
@@ -176,22 +179,23 @@
      */
     public boolean removeSpace(String wSpace) throws XWikiException
     {
-        return 
getWatchListPlugin().removeWatchedElement(getXWikiContext().getLocalUser(),
-            wSpace, true, getXWikiContext());
+        return 
getWatchListPlugin().removeWatchedElement(getXWikiContext().getUser(),
+           wSpace, true, getXWikiContext());
     }
 
     /**
      * Allows Administrators to remove the specified space from the specified 
user's WatchList
      *
-     * @param localUser XWiki User
+     * @param user XWiki User
      * @param wSpace Space to remove
      * @return True if the space was in the user's WatchList and has been 
removed
      * @throws XWikiException If the user's WatchList Object cannot be 
retreived nor created
      */
-    public boolean removeSpaceForUser(String localUser, String wSpace) throws 
XWikiException
+    public boolean removeSpaceForUser(String user, String wSpace) throws 
XWikiException
     {
         return context.getWiki().getUser(context).hasAdminRights() &&
-            getWatchListPlugin().removeWatchedElement(localUser, wSpace, true, 
getXWikiContext());
+            getWatchListPlugin().removeWatchedElement(user, wSpace,
+                true, getXWikiContext());
     }
 
     /**
@@ -202,7 +206,7 @@
      */
     public List getWatchedDocuments() throws XWikiException
     {
-        return 
getWatchListPlugin().getWatchedDocuments(getXWikiContext().getLocalUser(), 
context);
+        return 
getWatchListPlugin().getWatchedDocuments(getXWikiContext().getUser(), context);
     }
 
     /**
@@ -213,7 +217,7 @@
      */
     public List getWatchedSpaces() throws XWikiException
     {
-        return 
getWatchListPlugin().getWatchedSpaces(getXWikiContext().getLocalUser(), 
context);
+        return 
getWatchListPlugin().getWatchedSpaces(getXWikiContext().getUser(), context);
     }
 
     /**
@@ -231,17 +235,16 @@
     }
 
     /**
-     * Get the list of the elements watched by localUser ordered by last 
modification date,
-     * descending
+     * Get the list of the elements watched by user ordered by last 
modification date, descending
      *
-     * @param localUser XWiki User
-     * @return the list of the elements watched by localUser ordered by last 
modification date,
+     * @param user XWiki User
+     * @return the list of the elements watched by user ordered by last 
modification date,
      *         descending
      * @throws XWikiException If the search request fails
      */
-    public List getWatchListWhatsNew(String localUser) throws XWikiException
+    public List getWatchListWhatsNew(String user) throws XWikiException
     {
-        return getWatchListPlugin().getWatchListWhatsNew(localUser, context);
+        return getWatchListPlugin().getWatchListWhatsNew(user, context);
     }
 
     /**

_______________________________________________
notifications mailing list
notifications@xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to