Author: jvdrean
Date: 2008-02-25 16:58:56 +0100 (Mon, 25 Feb 2008)
New Revision: 7933

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
Log:
[cleanup] removed duplications and now using XWikiContext instead of Context in 
WatchListJob (XASCH-16)

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-25 15:53:04 UTC (rev 7932)
+++ 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListJob.java
 2008-02-25 15:58:56 UTC (rev 7933)
@@ -20,11 +20,12 @@
 package com.xpn.xwiki.plugin.watchlist;
 
 import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.api.Document;
 import com.xpn.xwiki.api.Object;
+import com.xpn.xwiki.api.Context;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.plugin.mailsender.MailSenderPlugin;
-import com.xpn.xwiki.plugin.mailsender.MailSenderPluginApi;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.velocity.VelocityContext;
@@ -56,14 +57,10 @@
 {
     private static final Log LOG = LogFactory.getLog(WatchListPlugin.class);
 
-    protected com.xpn.xwiki.api.XWiki xwiki = null;
-
     protected BaseObject xjob = null;
 
-    protected com.xpn.xwiki.api.Context xcontext = null;
+    protected XWikiContext context = null;
 
-    protected WatchListPluginApi watchlistPlugin = null;
-
     protected WatchListPlugin plugin = null;
 
     protected int interval = 0;
@@ -75,32 +72,30 @@
     /**
      * Sets objects required by the Job : XWiki, XWikiContext, 
WatchListPlugin, etc
      *
-     * @param context Context of the request
+     * @param jobContext Context of the request
      */
-    public void init(JobExecutionContext context) throws XWikiException
+    public void init(JobExecutionContext jobContext) 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");
-        watchlistPlugin = (WatchListPluginApi) 
xwiki.getPlugin(WatchListPlugin.ID);
-        plugin = (WatchListPlugin) watchlistPlugin.getPlugin();
+        JobDataMap data = jobContext.getJobDetail().getJobDataMap();
+        context = (XWikiContext) data.get("context");
+        plugin = (WatchListPlugin) 
context.getWiki().getPlugin(WatchListPlugin.ID, context);
         xjob = (BaseObject) data.get("xjob");
         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() + " ";
+        logprefix = "WatchList job " + context.getDatabase() + ":" + 
xjob.getName() + " ";
     }
 
     /**
      * Method called from the scheduler
      *
-     * @param context Context of the request
+     * @param jobContext Context of the request
      */
-    public void execute(JobExecutionContext context) throws 
JobExecutionException
+    public void execute(JobExecutionContext jobContext) throws 
JobExecutionException
     {
         try {
             // Set required objects
-            init(context);
+            init(jobContext);
 
             // Retreive notification subscribers (all wikis)
             Collection subscribers = retrieveNotificationSubscribers();
@@ -112,7 +107,8 @@
                 while (it.hasNext()) {
                     try {
                         // Retreive WatchList Object for each subscribers
-                        Document subscriber = xwiki.getDocument((String) 
it.next());
+                        Document subscriber = new Document(
+                            context.getWiki().getDocument((String) it.next(), 
context), context);
                         Object userObj = 
subscriber.getObject("XWiki.XWikiUsers");
                         Object notificationCriteria =
                             
subscriber.getObject(WatchListPlugin.WATCHLIST_CLASS);
@@ -121,8 +117,8 @@
                         }
                         // Filter documents according to lists in the 
WatchList Object
                         List matchingDocuments =
-                            filter(updatedDocuments, notificationCriteria, 
subscriber
-                                .getFullName());
+                            filter(updatedDocuments, notificationCriteria,
+                                subscriber.getFullName());
 
                         // If there are matching documents, sends the 
notification
                         if (matchingDocuments.size() > 0) {
@@ -179,21 +175,23 @@
 
         if (query.length() > 0) {
             List queryDocuments =
-                plugin.globalSearchDocuments(query, 0, 0, new ArrayList(), 
xcontext, xwiki);
+                plugin.globalSearchDocuments(query, 0, 0, new ArrayList(), 
context);
             watchedDocuments.addAll(queryDocuments);
         }
 
         Iterator updatedDocumentsIt = updatedDocuments.iterator();
         while (updatedDocumentsIt.hasNext()) {
             String updatedDocumentName = (String) updatedDocumentsIt.next();
-            Document updatedDocument = xwiki.getDocument(updatedDocumentName);
+            Document updatedDocument =
+                new 
Document(context.getWiki().getDocument(updatedDocumentName, context), context);
             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, 
updatedDocumentName))
+                    && context.getWiki().getRightService()
+                    .hasAccessLevel("view", subscriber, updatedDocumentName, 
context))
                 {
                     filteredDocumentList.add(updatedDocumentName);
                     documentAdded = true;
@@ -208,7 +206,8 @@
                 while (watchedDocumentIt.hasNext()) {
                     String watchedDocumentName = (String) 
watchedDocumentIt.next();
                     if (updatedDocumentName.equals(watchedDocumentName)
-                        && xwiki.hasAccessLevel("view", subscriber, 
updatedDocumentName))
+                        && context.getWiki().getRightService()
+                        .hasAccessLevel("view", subscriber, 
updatedDocumentName, context))
                     {
                         filteredDocumentList.add(updatedDocumentName);
                         break;
@@ -235,7 +234,7 @@
                 + WatchListPlugin.WATCHLIST_CLASS +
                 "' and prop.id.id=obj.id and prop.name='interval' " +
                 "and prop.value='" + interval + "')";
-        return plugin.globalSearchDocuments(request, 0, 0, new ArrayList(), 
xcontext, xwiki);
+        return plugin.globalSearchDocuments(request, 0, 0, new ArrayList(), 
context);
     }
 
     /**
@@ -269,7 +268,7 @@
         values.add(dt.toDate());
         String updatedDocumentRequest = "where doc.date > ? order by doc.date 
desc";
 
-        return plugin.globalSearchDocuments(updatedDocumentRequest, 0, 0, 
values, xcontext, xwiki);
+        return plugin.globalSearchDocuments(updatedDocumentRequest, 0, 0, 
values, context);
     }
 
     /**
@@ -294,36 +293,36 @@
         vcontext.put("pseudo", userObj.display("first_name", "view"));
         vcontext.put("documents", updatedDocuments);
         vcontext.put("interval", new Integer(interval));
-        vcontext.put("xwiki", xwiki);
-        vcontext.put("context", xcontext);
+        vcontext.put("xwiki", new com.xpn.xwiki.api.XWiki(context.getWiki(), 
context));
+        vcontext.put("context", new Context(context));
 
         // Get wiki's default language (default en)
-        String language = xwiki.getXWikiPreference("default_language", "en");
+        String language = 
context.getWiki().getXWikiPreference("default_language", "en", context);
 
         // Get mailsenderplugin
-        MailSenderPluginApi emailService =
-            (MailSenderPluginApi) xwiki.getPlugin(MailSenderPlugin.ID);
+        MailSenderPlugin emailService =
+            (MailSenderPlugin) 
context.getWiki().getPlugin(MailSenderPlugin.ID, context);
         if (emailService == null) {
             return;
         }
 
         // Get wiki administrator email (default : [EMAIL PROTECTED])
-        String sender = xwiki.getXWikiPreference("admin_email", "[EMAIL 
PROTECTED]");
+        String sender = context.getWiki()
+            .getXWikiPreference("admin_email", "[EMAIL PROTECTED]", context);
 
         // Set email template
         String mailTemplate = "";
-        if (xwiki.exists(jobMailTemplate)) {
+        if (context.getWiki().exists(jobMailTemplate, context)) {
             mailTemplate = jobMailTemplate;
-        } else if (xwiki.exists(WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE)) {
+        } else if 
(context.getWiki().exists(WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE, context)) {
             mailTemplate = WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE;
         } else {
             mailTemplate =
-                xcontext.getMainWikiName() + ":" + 
WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE;
+                context.getMainXWiki() + ":" + 
WatchListPlugin.WATCHLIST_EMAIL_TEMPLATE;
         }
 
         // Send message from template
-        int sendResult =
-            emailService.sendMessageFromTemplate(sender, emailAddr, null,
-                null, language, mailTemplate, vcontext);
+        emailService.sendMailFromTemplate(mailTemplate, sender, emailAddr, 
null, null, language,
+            vcontext, context);
     }
 }

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-25 15:53:04 UTC (rev 7932)
+++ 
xwiki-platform/xwiki-plugins/trunk/watchlist/src/main/java/com/xpn/xwiki/plugin/watchlist/WatchListPlugin.java
      2008-02-25 15:58:56 UTC (rev 7933)
@@ -19,20 +19,26 @@
  */
 package com.xpn.xwiki.plugin.watchlist;
 
-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.api.*;
+import com.xpn.xwiki.api.Object;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.objects.classes.BaseClass;
 import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
 import com.xpn.xwiki.plugin.XWikiPluginInterface;
 import com.xpn.xwiki.plugin.mailsender.MailSenderPlugin;
+import com.xpn.xwiki.plugin.mailsender.MailSenderPluginApi;
 import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.VelocityContext;
+import org.joda.time.DateTime;
+import org.joda.time.Hours;
+import org.joda.time.Days;
+import org.joda.time.Weeks;
+import org.joda.time.Months;
 
 import java.util.Arrays;
 import java.util.List;
@@ -540,50 +546,16 @@
         String watchedSpaces =
             
watchListObject.getLargeStringValue("spaces").trim().replaceFirst("^,", "")
                 .replaceAll(",", "','");
+
+        // TODO : sort watched elements by wiki and query each of them 
+
         String request = "select doc.fullName from XWikiDocument as doc where 
doc.web in ('" +
             watchedSpaces + "') or doc.fullName in ('" + watchedDocuments + 
"') " +
             "order by doc.date desc";
-        return globalSearchDocuments(request, 20, 0, new ArrayList(), new 
Context(context),
-            new XWiki(context.getWiki(), context));
+        return globalSearchDocuments(request, 20, 0, new ArrayList(), 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.
@@ -592,26 +564,26 @@
      *         xwiki:Main.WebHome
      */
     protected List globalSearchDocuments(String request, int nb, int start, 
List values,
-        Context context, XWiki xwiki)
+        XWikiContext context)
     {
         String initialDb =
             !context.getDatabase().equals("") ? context.getDatabase() :
-                context.getMainWikiName();
+                context.getMainXWiki();
         List wikiServers = Collections.EMPTY_LIST;
         List results = new ArrayList();
 
-        if (xwiki.isVirtual()) {
+        if (context.getWiki().isVirtual()) {
             try {
-                wikiServers = getVirtualWikisDatabaseNames(context, xwiki);
-                if (!wikiServers.contains(context.getMainWikiName())) {
-                    wikiServers.add(context.getMainWikiName());
+                wikiServers = context.getWiki().getVirtualWikiList();
+                if (!wikiServers.contains(context.getMainXWiki())) {
+                    wikiServers.add(context.getMainXWiki());
                 }
             } catch (Exception e) {
                 getLogger().error("error getting list of wiki servers!", e);
             }
         } else {
             wikiServers = new ArrayList();
-            wikiServers.add(context.getMainWikiName());
+            wikiServers.add(context.getMainXWiki());
         }
 
         try {
@@ -620,8 +592,8 @@
                 String wikiPrefix = wiki + ":";
                 context.setDatabase(wiki);
                 try {
-                    // List upDocsInWiki = xwiki.searchDocuments(request, 0, 
0, values);
-                    List upDocsInWiki = xwiki.searchDocumentsNames(wiki, 
request, 0, 0, values);
+                    List upDocsInWiki = context.getWiki().getStore()
+                        .searchDocumentsNames(request, 0, 0, values, context);
                     Iterator it = upDocsInWiki.iterator();
                     while (it.hasNext()) {
                         results.add(wikiPrefix + it.next());
@@ -645,8 +617,7 @@
         String request = ", BaseObject as obj where obj.name=doc.fullName and 
obj.className='"
             + WatchListPlugin.WATCHLIST_CLASS + "'";
         List subscribers =
-            globalSearchDocuments(request, 0, 0, new ArrayList(), new 
Context(context),
-                new XWiki(context.getWiki(), context));
+            globalSearchDocuments(request, 0, 0, new ArrayList(), context);
         Iterator it = subscribers.iterator();
         while (it.hasNext()) {
             String user = (String) it.next();

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to