# HG changeset patch
# User Christophe Capon <christophe.capon@vilogia.fr>
# Date 1269594415 -3600
# Branch 5.3.0
# Node ID c4d1538258eb1bae4f1c3c1b4f931d7bafad568b
# Parent  c1cc5ea70ee2d80fd7bf4677f047994eea0e1ff1
More code cleanup

diff -r c1cc5ea70ee2 -r c4d1538258eb nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertManagementService.java
--- a/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertManagementService.java	Fri Mar 26 09:05:32 2010 +0100
+++ b/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertManagementService.java	Fri Mar 26 10:06:55 2010 +0100
@@ -20,7 +20,6 @@
 package org.nuxeo.ecm.alert;
 
 import java.io.Serializable;
-import java.util.List;
 import java.util.Map;
 
 import org.nuxeo.ecm.core.api.ClientException;
diff -r c1cc5ea70ee2 -r c4d1538258eb nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 09:05:32 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 10:06:55 2010 +0100
@@ -68,11 +68,11 @@
 import org.nuxeo.runtime.api.Framework;
 
 /**
- * 
+ *
  * Default implementation of the {@link UserPreferencesService}
- * 
+ *
  * @author <a href="mailto:christophe.capon@vilogia.fr">Christophe Capon</a>
- * 
+ *
  */
 
 public class AlertManagementServiceImpl implements AlertManagementService,
@@ -103,8 +103,9 @@
     private static UserSettingsService getUserSettingsService()
             throws Exception {
 
-        if (userSettingsService == null)
-            userSettingsService = (UserSettingsService) Framework.getService(UserSettingsService.class);
+        if (userSettingsService == null) {
+            userSettingsService = Framework.getService(UserSettingsService.class);
+        }
 
         return userSettingsService;
     }
@@ -145,16 +146,21 @@
 
     private CoreSession init(AlertContext ctx) throws Exception {
 
-        if (rs == null)
+        if (rs == null) {
             rs = Framework.getService(RenderingService.class);
-        if (helper == null)
+        }
+        if (helper == null) {
             helper = new EmailHelperImpl();
-        if (hrc1 == null)
+        }
+        if (hrc1 == null) {
             hrc1 = new HtmlHeader1RenderingContext();
-        if (hrc2 == null)
+        }
+        if (hrc2 == null) {
             hrc2 = new HtmlHeader2RenderingContext();
-        if (frc == null)
+        }
+        if (frc == null) {
             frc = new HtmlFooterRenderingContext();
+        }
 
         CoreSession coreSession = getCoreSession();
 
@@ -180,16 +186,18 @@
 
                 Entry<String, UserAlerts> alert = alertIter.next();
 
-                if (!allAlerts.containsKey(alert.getKey()))
+                if (!allAlerts.containsKey(alert.getKey())) {
                     allAlerts.put(alert.getKey(),
                             new UserAlerts(alert.getKey()));
+                }
 
                 UserAlerts userAlerts = allAlerts.get(alert.getKey());
 
                 Iterator<Alert> alIter = alert.getValue().getAlerts().iterator();
 
-                while (alIter.hasNext())
+                while (alIter.hasNext()) {
                     userAlerts.getAlerts().add(alIter.next());
+                }
 
             }
 
@@ -207,8 +215,9 @@
 
         while (iter.hasNext()) {
             List<String> tpl = iter.next().getValue().getTemplateList();
-            for (String t : tpl)
+            for (String t : tpl) {
                 rs.registerEngine(new AlertRenderingEngine(t));
+            }
         }
 
     }
@@ -219,8 +228,9 @@
 
         while (iter.hasNext()) {
             List<String> tpl = iter.next().getValue().getTemplateList();
-            for (String t : tpl)
+            for (String t : tpl) {
                 rs.unregisterEngine(t);
+            }
         }
 
         rs.unregisterEngine("alertHeader2");
@@ -504,9 +514,9 @@
 
     public EmailHelper setEmailHelper(EmailHelper emailHelper) {
 
-        EmailHelper old = this.helper;
+        EmailHelper old = helper;
 
-        this.helper = emailHelper;
+        helper = emailHelper;
 
         return old;
 
# HG changeset patch
# User Christophe Capon <christophe.capon@vilogia.fr>
# Date 1269621805 -3600
# Branch 5.3.0
# Node ID e1513cea2e7b59caeee395126ac6c921bdd01c1a
# Parent  c4d1538258eb1bae4f1c3c1b4f931d7bafad568b
Better session management on the AlertManagementServiceImpl, and no useless login
All unit tests OK

diff -r c4d1538258eb -r e1513cea2e7b nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertContext.java
--- a/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertContext.java	Fri Mar 26 10:06:55 2010 +0100
+++ b/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertContext.java	Fri Mar 26 17:43:25 2010 +0100
@@ -1,5 +1,7 @@
 package org.nuxeo.ecm.alert;
 
+import org.nuxeo.ecm.core.event.Event;
+
 public class AlertContext {
 
     private boolean noPurge = false;
@@ -11,15 +13,17 @@
     public boolean isNoPurge() {
         return noPurge;
     }
+    
 
-    public void setEventId(String eventId) {
-        this.eventId = eventId;
+    private Event event = null;
+    
+    public void setEvent(Event event) {
+        this.event = event;
     }
 
-    public String getEventId() {
-        return eventId;
+    public Event getEvent() {
+        return event;
     }
 
-    private String eventId = "none";
 
 }
diff -r c4d1538258eb -r e1513cea2e7b nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 10:06:55 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 17:43:25 2010 +0100
@@ -68,11 +68,11 @@
 import org.nuxeo.runtime.api.Framework;
 
 /**
- *
+ * 
  * Default implementation of the {@link UserPreferencesService}
- *
+ * 
  * @author <a href="mailto:christophe.capon@vilogia.fr">Christophe Capon</a>
- *
+ * 
  */
 
 public class AlertManagementServiceImpl implements AlertManagementService,
@@ -80,8 +80,6 @@
 
     private DocumentViewCodecManager docLocator;
 
-    private LoginContext loginContext = null;
-
     private static final long serialVersionUID = -2643607647191744171L;
 
     private static final Log log = LogFactory.getLog(AlertManagementServiceImpl.class);
@@ -118,7 +116,9 @@
 
         try {
 
-            CoreSession coreSession = init(ctx);
+            CoreSession coreSession = ctx.getEvent().getContext().getCoreSession();
+
+            init(ctx);
 
             initEngines(coreSession, ctx);
 
@@ -129,8 +129,6 @@
 
             termEngines(coreSession, ctx);
 
-            close(coreSession, ctx);
-
         } catch (Exception e) {
 
             throw new ClientException("processAlerts failed", e);
@@ -139,12 +137,7 @@
 
     }
 
-    private void close(CoreSession coreSession, AlertContext ctx)
-            throws LoginException {
-        closeSession(coreSession);
-    }
-
-    private CoreSession init(AlertContext ctx) throws Exception {
+    private void init(AlertContext ctx) throws Exception {
 
         if (rs == null) {
             rs = Framework.getService(RenderingService.class);
@@ -162,9 +155,6 @@
             frc = new HtmlFooterRenderingContext();
         }
 
-        CoreSession coreSession = getCoreSession();
-
-        return coreSession;
     }
 
     private Map<String, UserAlerts> getAvailableAlerts(CoreSession coreSession,
@@ -239,20 +229,6 @@
 
     }
 
-    private void closeSession(CoreSession coreSession) throws LoginException {
-
-        Repository.close(coreSession);
-
-    }
-
-    private CoreSession getCoreSession() throws Exception {
-
-        RepositoryManager mgr = Framework.getService(RepositoryManager.class);
-        Repository repository = mgr.getDefaultRepository();
-        return repository.open();
-
-    }
-
     public void registerProvider(String providerName, AlertProvider provider) {
 
         if (alertProviders.containsKey(providerName)) {
@@ -315,18 +291,20 @@
         AlertSettingsWrapper settings = new AlertSettingsWrapper(
                 userSettingsDoc);
 
-        if (ctx.getEventId() == null || ctx.getEventId().equals("none")) {
+        if (ctx.getEvent() == null || ctx.getEvent().getName().equals("none")) {
             log.warn("Received null eventId, assuming alerts must be sent");
         }
 
         Boolean go = false;
-        go |= (ctx.getEventId() == null || ctx.getEventId().equals("none"));
-        go |= settings.getSummaryFrequency().equals(
-                AlertSettingsType.SUMMARY_FREQUENCY_TWICE_DAY);
-        go |= (settings.getSummaryFrequency().equals(
-                AlertSettingsType.SUMMARY_FREQUENCY_DAILY) && ctx.getEventId().equals(
-                AlertEvent.MAIN_ALERT_EVENT));
-        go |= ctx.getEventId().equals(AlertEvent.MANUAL_ALERT_EVENT);
+        go |= (ctx.getEvent() == null || ctx.getEvent().getName().equals("none"));
+        if (ctx.getEvent() != null) {
+            go |= settings.getSummaryFrequency().equals(
+                    AlertSettingsType.SUMMARY_FREQUENCY_TWICE_DAY);
+            go |= (settings.getSummaryFrequency().equals(
+                    AlertSettingsType.SUMMARY_FREQUENCY_DAILY) && ctx.getEvent().getName().equals(
+                    AlertEvent.MAIN_ALERT_EVENT));
+            go |= ctx.getEvent().getName().equals(AlertEvent.MANUAL_ALERT_EVENT);
+        }
 
         if (go) {
 
diff -r c4d1538258eb -r e1513cea2e7b nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java	Fri Mar 26 10:06:55 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java	Fri Mar 26 17:43:25 2010 +0100
@@ -71,6 +71,7 @@
 
     private static final Log log = LogFactory.getLog(AlertEventListener.class);
 
+    
     /**
      * Processes the alert request events
      * 
@@ -101,49 +102,50 @@
                 event.isMarkedForRollBack() ? "true" : "false"));
         log.trace(String.format("event.isPublic()=%s",
                 event.isPublic() ? "true" : "false"));
-
-        LoginContext loginContext = null;
-        CoreSession coreSession = null;
+//
+//        LoginContext loginContext = null;
+//        CoreSession coreSession = null;
 
         try {
 
-            // Open a system session
-            log.trace("Open a system session");
-            loginContext = Framework.login();
-            RepositoryManager mgr = Framework.getService(RepositoryManager.class);
-            Repository repository = mgr.getDefaultRepository();
-            if (repository != null) {
-                coreSession = repository.open();
-            } else {
-                log.error("Unable to open the default repository");
-            }
-            if (coreSession == null) {
-                log.error("Unable to open the core session");
-                return;
-            }
+//            // Open a system session
+//            log.trace("Open a system session");
+//            
+//            loginContext = Framework.login();
+//            RepositoryManager mgr = Framework.getService(RepositoryManager.class);
+//            Repository repository = mgr.getDefaultRepository();
+//            if (repository != null) {
+//                coreSession = repository.open();
+//            } else {
+//                log.error("Unable to open the default repository");
+//            }
+//            if (coreSession == null) {
+//                log.error("Unable to open the core session");
+//                return;
+//            }
 
             // Create the alert context and start alert processing
             log.debug("Start alert processing");
             AlertContext ctxt = new AlertContext();
             ctxt.setNoPurge(false);
-            ctxt.setEventId(eventId);
+            ctxt.setEvent(event);
             getService().processAlerts(ctxt);
             log.debug("Successfully performed alert processing");
 
         } catch (Exception e) {
             log.error("Error during the alert processing", e);
         } finally {
-
-            // Try to end graciously
-            if (coreSession != null) {
-                CoreInstance.getInstance().close(coreSession);
-            }
-            if (loginContext != null) {
-                try {
-                    loginContext.logout();
-                } catch (LoginException e) {
-                }
-            }
+//
+//            // Try to end graciously
+//            if (coreSession != null) {
+//                CoreInstance.getInstance().close(coreSession);
+//            }
+//            if (loginContext != null) {
+//                try {
+//                    loginContext.logout();
+//                } catch (LoginException e) {
+//                }
+//            }
         }
     }
 
diff -r c4d1538258eb -r e1513cea2e7b nuxeo-platform-alert-core/src/test/java/org/nuxeo/ecm/alert/core/TestAlertCore.java
--- a/nuxeo-platform-alert-core/src/test/java/org/nuxeo/ecm/alert/core/TestAlertCore.java	Fri Mar 26 10:06:55 2010 +0100
+++ b/nuxeo-platform-alert-core/src/test/java/org/nuxeo/ecm/alert/core/TestAlertCore.java	Fri Mar 26 17:43:25 2010 +0100
@@ -28,7 +28,11 @@
 import org.nuxeo.ecm.alert.AlertEvent;
 import org.nuxeo.ecm.alert.AlertManagementService;
 import org.nuxeo.ecm.core.api.ClientException;
+import org.nuxeo.ecm.core.event.Event;
+import org.nuxeo.ecm.core.event.EventContext;
 import org.nuxeo.ecm.core.event.EventService;
+import org.nuxeo.ecm.core.event.impl.EventContextImpl;
+import org.nuxeo.ecm.core.event.impl.EventImpl;
 import org.nuxeo.runtime.api.Framework;
 
 /**
@@ -128,9 +132,10 @@
 
     public void test2Providers() throws ClientException {
 
-        AlertContext ctx = new AlertContext();
-        ctx.setEventId(AlertEvent.MANUAL_ALERT_EVENT);
-        service.processAlerts(ctx);
+        AlertContext actx = new AlertContext();
+        EventContext ectx = new EventContextImpl(getCoreSession(),getCoreSession().getPrincipal());
+        actx.setEvent(new EventImpl(AlertEvent.MANUAL_ALERT_EVENT, ectx)); 
+        service.processAlerts(actx);
         assertEquals(mock.getLastTo(), "testuser@domain.com");
         assertEquals(mock.getLastSubject(),
                 AlertManagementImplConstants.MAIL_SUBJECT);
diff -r c4d1538258eb -r e1513cea2e7b nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java
--- a/nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java	Fri Mar 26 10:06:55 2010 +0100
+++ b/nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java	Fri Mar 26 17:43:25 2010 +0100
@@ -157,4 +157,5 @@
 
     }
 
+
 }
# HG changeset patch
# User Christophe Capon <christophe.capon@vilogia.fr>
# Date 1269632321 -3600
# Branch 5.3.0
# Node ID 06d58f51fa103fce5cd23eb9c5138ef98b0886e4
# Parent  e1513cea2e7b59caeee395126ac6c921bdd01c1a
Clean session management

diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertProvider.java
--- a/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertProvider.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-alert-api/src/main/java/org/nuxeo/ecm/alert/AlertProvider.java	Fri Mar 26 20:38:41 2010 +0100
@@ -11,25 +11,25 @@
 public interface AlertProvider {
 
     /**
-     * Initializes the rendering service, usually by registering the engines
-     * used by the alert provider
+     * Get all the freemarker templates required to render the alerts
      * 
-     * @param rs rendering service to initialize
      */
 
     public List<String> getTemplateList();
 
     /**
-     * Initializes the rendering service, usually by registering the engines
-     * used by the alert provider
+     * Get all the pending alerts for this provider.
      * 
-     * @param rs rendering service to initialize
+     * @param coreSession Nuxeo session
+     * @return A map of {@link UserAlerts UserAlerts}, with the username as a
+     *         key. An empty list is returned when no alert is found.
      * @throws ClientException
      */
 
     public Map<String, UserAlerts> getAllPendingAlerts(CoreSession coreSession)
             throws ClientException;
 
+    
     /**
      * 
      * @param coreSession: The core session
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 20:38:41 2010 +0100
@@ -29,9 +29,6 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
-import javax.security.auth.login.LoginContext;
-import javax.security.auth.login.LoginException;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.jboss.seam.international.Messages;
@@ -52,8 +49,6 @@
 import org.nuxeo.ecm.core.api.DocumentLocation;
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
-import org.nuxeo.ecm.core.api.repository.Repository;
-import org.nuxeo.ecm.core.api.repository.RepositoryManager;
 import org.nuxeo.ecm.platform.ec.notification.service.NotificationServiceHelper;
 import org.nuxeo.ecm.platform.rendering.RenderingException;
 import org.nuxeo.ecm.platform.rendering.RenderingResult;
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/listener/AlertEventListener.java	Fri Mar 26 20:38:41 2010 +0100
@@ -19,18 +19,11 @@
 
 package org.nuxeo.ecm.alert.core.listener;
 
-import javax.security.auth.login.LoginContext;
-import javax.security.auth.login.LoginException;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.nuxeo.ecm.alert.AlertContext;
 import org.nuxeo.ecm.alert.AlertEvent;
 import org.nuxeo.ecm.alert.AlertManagementService;
-import org.nuxeo.ecm.core.api.CoreInstance;
-import org.nuxeo.ecm.core.api.CoreSession;
-import org.nuxeo.ecm.core.api.repository.Repository;
-import org.nuxeo.ecm.core.api.repository.RepositoryManager;
 import org.nuxeo.ecm.core.event.Event;
 import org.nuxeo.ecm.core.event.EventListener;
 import org.nuxeo.runtime.api.Framework;
@@ -88,42 +81,9 @@
         }
 
         log.debug(String.format("Received %s event", eventId));
-        log.trace(String.format("event.isCanceled()=%s",
-                event.isCanceled() ? "true" : "false"));
-        log.trace(String.format("event.isCommitEvent()=%s",
-                event.isCommitEvent() ? "true" : "false"));
-        log.trace(String.format("event.isImmediate()=%s",
-                event.isImmediate() ? "true" : "false"));
-        log.trace(String.format("event.isInline()=%s",
-                event.isInline() ? "true" : "false"));
-        log.trace(String.format("event.isLocal()=%s", event.isLocal() ? "true"
-                : "false"));
-        log.trace(String.format("event.isMarkedForRollBack()=%s",
-                event.isMarkedForRollBack() ? "true" : "false"));
-        log.trace(String.format("event.isPublic()=%s",
-                event.isPublic() ? "true" : "false"));
-//
-//        LoginContext loginContext = null;
-//        CoreSession coreSession = null;
 
         try {
 
-//            // Open a system session
-//            log.trace("Open a system session");
-//            
-//            loginContext = Framework.login();
-//            RepositoryManager mgr = Framework.getService(RepositoryManager.class);
-//            Repository repository = mgr.getDefaultRepository();
-//            if (repository != null) {
-//                coreSession = repository.open();
-//            } else {
-//                log.error("Unable to open the default repository");
-//            }
-//            if (coreSession == null) {
-//                log.error("Unable to open the core session");
-//                return;
-//            }
-
             // Create the alert context and start alert processing
             log.debug("Start alert processing");
             AlertContext ctxt = new AlertContext();
@@ -134,18 +94,6 @@
 
         } catch (Exception e) {
             log.error("Error during the alert processing", e);
-        } finally {
-//
-//            // Try to end graciously
-//            if (coreSession != null) {
-//                CoreInstance.getInstance().close(coreSession);
-//            }
-//            if (loginContext != null) {
-//                try {
-//                    loginContext.logout();
-//                } catch (LoginException e) {
-//                }
-//            }
         }
     }
 
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-deferredalert-api/src/main/java/org/nuxeo/ecm/deferredalert/DeferredAlertManagementService.java
--- a/nuxeo-platform-deferredalert-api/src/main/java/org/nuxeo/ecm/deferredalert/DeferredAlertManagementService.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-deferredalert-api/src/main/java/org/nuxeo/ecm/deferredalert/DeferredAlertManagementService.java	Fri Mar 26 20:38:41 2010 +0100
@@ -23,6 +23,7 @@
 import java.util.List;
 
 import org.nuxeo.ecm.core.api.ClientException;
+import org.nuxeo.ecm.core.api.CoreSession;
 
 /**
  * Alert management service
@@ -32,12 +33,12 @@
  */
 public interface DeferredAlertManagementService extends Serializable {
 
-    public void storeAlerts(String userName, List<String> alerts)
+    public void storeAlerts(String userName, List<String> alerts, CoreSession session)
             throws ClientException;
 
-    public void resetAlerts(String userName) throws ClientException;
+    public void resetAlerts(String userName, CoreSession session) throws ClientException;
 
-    public List<String> getPendingAlerts(String userName)
+    public List<String> getPendingAlerts(String userName, CoreSession session)
             throws ClientException;
 
 }
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java
--- a/nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-deferredalert-core/src/main/java/org/nuxeo/ecm/deferredalert/core/DeferredAlertManagementServiceImpl.java	Fri Mar 26 20:38:41 2010 +0100
@@ -30,10 +30,7 @@
 import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.core.api.DocumentModelList;
-import org.nuxeo.ecm.core.api.repository.Repository;
-import org.nuxeo.ecm.core.api.repository.RepositoryManager;
 import org.nuxeo.ecm.deferredalert.DeferredAlertManagementService;
-import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
 import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
 import org.nuxeo.runtime.api.Framework;
 
@@ -51,45 +48,18 @@
 public class DeferredAlertManagementServiceImpl implements
         DeferredAlertManagementService {
 
-    private CoreSession adminSession = null;
-
-    private LoginContext loginContext = null;
-
     private static final long serialVersionUID = -2643607647191744171L;
 
     private static final Log log = LogFactory.getLog(DeferredAlertManagementServiceImpl.class);
 
-    private CoreSession getAdminSession() throws ClientException {
-
-        try {
-            if (adminSession == null) {
-
-                loginContext = Framework.login();
-
-                if (loginContext == null) {
-                    log.warn("loginContext is null, probably in a test environment, continuing and hope for he best");
-                }
-
-                RepositoryManager mgr = Framework.getService(RepositoryManager.class);
-                Repository repository = mgr.getDefaultRepository();
-                adminSession = repository.open();
-            }
-        } catch (Exception e) {
-            throw new ClientException("Unable to open core session", e);
-        }
-
-        return adminSession;
-
-    }
-
-    public DeferredAlertManagementServiceImpl() {
+      public DeferredAlertManagementServiceImpl() {
         log.trace("Instanciating DeferredAlertManagementServiceImpl");
     }
 
-    public List<String> getPendingAlerts(String userName)
+    public List<String> getPendingAlerts(String userName, CoreSession session)
             throws ClientException {
 
-        DocumentModelList docs = getPendingAlertDocs(userName);
+        DocumentModelList docs = getPendingAlertDocs(userName, session);
 
         List<String> result = new ArrayList<String>();
 
@@ -100,38 +70,38 @@
         return result;
     }
 
-    private DocumentModelList getPendingAlertDocs(String userName)
+    private DocumentModelList getPendingAlertDocs(String userName, CoreSession session)
             throws ClientException {
-        DocumentModelList docs = getAdminSession().getChildren(
-                getUserWorkspace(getAdminSession(), userName).getRef(),
+        DocumentModelList docs = session.getChildren(
+                getUserWorkspace(session, userName).getRef(),
                 DeferredAlertType.DOCTYPE);
         return docs;
     }
 
-    public void resetAlerts(String userName) throws ClientException {
+    public void resetAlerts(String userName, CoreSession session) throws ClientException {
 
-        DocumentModelList pending = getPendingAlertDocs(userName);
+        DocumentModelList pending = getPendingAlertDocs(userName, session);
 
         for (DocumentModel doc : pending) {
-            DeferredAlertWrapper.delete(getAdminSession(),
+            DeferredAlertWrapper.delete(session,
                     new DeferredAlertWrapper(doc));
         }
 
-        getAdminSession().save();
+        session.save();
 
     }
 
-    public void storeAlerts(String userName, List<String> alerts)
+    public void storeAlerts(String userName, List<String> alerts, CoreSession session)
             throws ClientException {
 
-        DocumentModel uws = getUserWorkspace(getAdminSession(), userName);
+        DocumentModel uws = getUserWorkspace(session, userName);
 
         for (String alert : alerts) {
-            DeferredAlertWrapper.create(getAdminSession(),
+            DeferredAlertWrapper.create(session,
                     uws.getPathAsString(), userName, alert);
         }
 
-        getAdminSession().save();
+        session.save();
 
     }
 
@@ -143,9 +113,9 @@
             UserWorkspaceService uws = Framework.getService(UserWorkspaceService.class);
 
             DocumentModel userWorkspace = uws.getCurrentUserPersonalWorkspace(
-                    userName, getAdminSession().getRootDocument());
+                    userName, session.getRootDocument());
 
-            getAdminSession().save();
+            session.save();
 
             return userWorkspace;
 
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-deferredalert-types/src/main/java/com/nuxeo/ecm/deferredalert/types/DeferredAlertWrapper.java
--- a/nuxeo-platform-deferredalert-types/src/main/java/com/nuxeo/ecm/deferredalert/types/DeferredAlertWrapper.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-deferredalert-types/src/main/java/com/nuxeo/ecm/deferredalert/types/DeferredAlertWrapper.java	Fri Mar 26 20:38:41 2010 +0100
@@ -7,8 +7,6 @@
 import org.nuxeo.ecm.core.api.ClientException;
 import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
-import org.nuxeo.ecm.core.api.DocumentRef;
-import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;
 
 public class DeferredAlertWrapper {
 
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/defer/DeferredNotificationHelper.java
--- a/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/defer/DeferredNotificationHelper.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/defer/DeferredNotificationHelper.java	Fri Mar 26 20:38:41 2010 +0100
@@ -38,6 +38,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.deferredalert.DeferredAlertManagementService;
 import org.nuxeo.ecm.platform.ec.notification.NotificationConstants;
@@ -124,7 +125,9 @@
         List<String> alerts = new ArrayList<String>();
         alerts.add(bodyMail);
 
-        getService().storeAlerts(userPrincipal, alerts);
+        CoreSession session = ((DocumentModel) mail.get("document")).getCoreSession();
+        
+        getService().storeAlerts(userPrincipal, alerts, session);
 
         lc.logout();
 
diff -r e1513cea2e7b -r 06d58f51fa10 nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java
--- a/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java	Fri Mar 26 17:43:25 2010 +0100
+++ b/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java	Fri Mar 26 20:38:41 2010 +0100
@@ -37,6 +37,7 @@
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.core.api.DocumentModelList;
 import org.nuxeo.ecm.core.api.NuxeoPrincipal;
+import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
 import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
@@ -429,6 +430,7 @@
                 userPrincipal);
         // XXX hack, principals have only one model
         DataModel model = recepient.getModel().getDataModels().values().iterator().next();
+
         String email = (String) model.getData("email");
         String mailTemplate = "d_" + notif.getTemplate();
 
@@ -458,6 +460,7 @@
 
         try {
             deferredHelper.storeNotification(infoMap, userPrincipal);
+            
         } catch (Exception e) {
             throw new ClientException("Failed to send notification email ", e);
         }
# HG changeset patch
# User Christophe Capon <christophe.capon@vilogia.fr>
# Date 1269633029 -3600
# Branch 5.3.0
# Node ID 2dcf815029a017436f65a8fd1f6393b468adce14
# Parent  06d58f51fa103fce5cd23eb9c5138ef98b0886e4
Better session management for notification core

diff -r 06d58f51fa10 -r 2dcf815029a0 nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/NotificationEventListener.java
--- a/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/NotificationEventListener.java	Fri Mar 26 20:38:41 2010 +0100
+++ b/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/NotificationEventListener.java	Fri Mar 26 20:50:29 2010 +0100
@@ -231,7 +231,8 @@
         String userDest = (String) eventInfo.get(NotificationConstants.DESTINATION_KEY);
         NotificationImpl notif = (NotificationImpl) eventInfo.get(NotificationConstants.NOTIFICATION_KEY);
 
-        AlertSettingsWrapper as = getAlertSettings(userDest);
+        AlertSettingsWrapper as = getAlertSettings(userDest,
+                ctx.getCoreSession());
 
         String notificationPolicy = as.getNotificationPolicy();
 
@@ -302,11 +303,11 @@
         }
     }
 
-    private AlertSettingsWrapper getAlertSettings(String userPrincipal)
-            throws ClientException {
+    private AlertSettingsWrapper getAlertSettings(String userPrincipal,
+            CoreSession session) throws ClientException {
 
         DocumentModelList alertDocList = (DocumentModelList) getuserSettingsService().getUserSettings(
-                userPrincipal, AlertSettingsType.DOCTYPE);
+                session, userPrincipal, AlertSettingsType.DOCTYPE);
 
         DocumentModel alertDoc = alertDocList.get(0);
 
diff -r 06d58f51fa10 -r 2dcf815029a0 nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java
--- a/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java	Fri Mar 26 20:38:41 2010 +0100
+++ b/nuxeo-platform-notification-core/src/main/java/org/nuxeo/ecm/platform/ec/notification/service/NotificationService.java	Fri Mar 26 20:50:29 2010 +0100
@@ -400,7 +400,8 @@
             Map<String, Object> infoMap, String userPrincipal)
             throws ClientException {
 
-        AlertSettingsWrapper as = getAlertSettings(userPrincipal);
+        AlertSettingsWrapper as = getAlertSettings(userPrincipal,
+                ((DocumentModel) infoMap.get("document")).getCoreSession());
 
         String notificationPolicy = as.getNotificationPolicy();
 
@@ -460,17 +461,17 @@
 
         try {
             deferredHelper.storeNotification(infoMap, userPrincipal);
-            
+
         } catch (Exception e) {
             throw new ClientException("Failed to send notification email ", e);
         }
     }
 
-    private AlertSettingsWrapper getAlertSettings(String userPrincipal)
-            throws ClientException {
+    private AlertSettingsWrapper getAlertSettings(String userPrincipal,
+            CoreSession session) throws ClientException {
 
         DocumentModelList alertDocList = (DocumentModelList) getuserSettingsService().getUserSettings(
-                userPrincipal, AlertSettingsType.DOCTYPE);
+                session, userPrincipal, AlertSettingsType.DOCTYPE);
 
         DocumentModel alertDoc = alertDocList.get(0);
 
diff -r 06d58f51fa10 -r 2dcf815029a0 nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsService.java
--- a/nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsService.java	Fri Mar 26 20:38:41 2010 +0100
+++ b/nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsService.java	Fri Mar 26 20:50:29 2010 +0100
@@ -9,6 +9,7 @@
 
 public interface UserSettingsService {
 
+ 
     public DocumentModelList getCurrentUserSettings(CoreSession coreSession)
             throws ClientException;
 
@@ -18,15 +19,7 @@
     public DocumentModelList getCurrentUserSettings(CoreSession coreSession,
             String type) throws ClientException;
 
-    public DocumentModelList getUserSettings(String userName)
-            throws ClientException;
-
-    public DocumentModelList getUserSettings(String userName,
-            DocumentType filter) throws ClientException;
-
-    public DocumentModelList getUserSettings(String userName, String type)
-            throws ClientException;
-
+    
     public DocumentModelList getUserSettings(CoreSession coreSession,
             String userName) throws ClientException;
 
@@ -36,6 +29,7 @@
     public DocumentModelList getUserSettings(CoreSession coreSession,
             String userName, String type) throws ClientException;
 
+    
     public void registerProvider(String providerName,
             UserSettingsProvider provider) throws ClientException;
 
@@ -44,8 +38,4 @@
     public Map<String, UserSettingsProvider> getAllRegisteredProviders()
             throws ClientException;
 
-    public void initService() throws Exception;
-
-    public void termService() throws Exception;
-
 }
diff -r 06d58f51fa10 -r 2dcf815029a0 nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsServiceImplComponent.java
--- a/nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsServiceImplComponent.java	Fri Mar 26 20:38:41 2010 +0100
+++ b/nuxeo-platform-usersettings-api/src/main/java/org/nuxeo/ecm/usersettings/UserSettingsServiceImplComponent.java	Fri Mar 26 20:50:29 2010 +0100
@@ -64,7 +64,6 @@
             }
             try {
                 userSettingsService = (UserSettingsService) klass.newInstance();
-                userSettingsService.initService();
             } catch (Exception e) {
                 throw new ClientException("Failed to instantiate class "
                         + klass, e);
@@ -181,7 +180,6 @@
         if (UserSettingsConstants.BASE_EXTENSION_POINT.equals(extensionPoint)) {
             unregisterPendingProviders();
             try {
-                userSettingsService.termService();
             } catch (Exception e) {
                 log.error("Error when terminating service", e);
             }
diff -r 06d58f51fa10 -r 2dcf815029a0 nuxeo-platform-usersettings-core/src/main/java/org/nuxeo/ecm/usersettings/core/UserSettingsServiceImpl.java
--- a/nuxeo-platform-usersettings-core/src/main/java/org/nuxeo/ecm/usersettings/core/UserSettingsServiceImpl.java	Fri Mar 26 20:38:41 2010 +0100
+++ b/nuxeo-platform-usersettings-core/src/main/java/org/nuxeo/ecm/usersettings/core/UserSettingsServiceImpl.java	Fri Mar 26 20:50:29 2010 +0100
@@ -56,62 +56,14 @@
 
 public class UserSettingsServiceImpl implements UserSettingsService {
 
-    private LoginContext loginContext = null;
-
     private static final long serialVersionUID = -2643607647191744171L;
 
     private static final Log log = LogFactory.getLog(UserSettingsServiceImpl.class);
 
     private Map<String, UserSettingsProvider> UserSettingsProviders = new HashMap<String, UserSettingsProvider>();
 
-    private CoreSession adminSession = null;
-
     private DocumentModel root;
 
-    private void term() throws Exception {
-        closeSession();
-    }
-
-    private void init() throws Exception {
-    }
-
-    private void closeSession() throws LoginException {
-
-        if (adminSession != null) {
-            Repository.close(adminSession);
-            adminSession = null;
-        }
-
-        if (loginContext != null) {
-            loginContext.logout();
-            loginContext = null;
-        }
-
-    }
-
-    private CoreSession getAdminSession() throws ClientException {
-
-        try {
-            if (adminSession == null) {
-
-                loginContext = Framework.login();
-
-                if (loginContext == null) {
-                    log.warn("loginContext is null, probably in a test environment, continuing and hope for he best");
-                }
-
-                RepositoryManager mgr = Framework.getService(RepositoryManager.class);
-                Repository repository = mgr.getDefaultRepository();
-                adminSession = repository.open();
-            }
-        } catch (Exception e) {
-            throw new ClientException("Unable to open core session", e);
-        }
-
-        return adminSession;
-
-    }
-
     public void registerProvider(String providerName,
             UserSettingsProvider provider) throws ClientException {
 
@@ -179,14 +131,6 @@
         return result;
     }
 
-    public void initService() throws Exception {
-        init();
-    }
-
-    public void termService() throws Exception {
-        term();
-    }
-
     private DocumentModelList findUserSettings(CoreSession userSession,
             String userName, DocumentModel userWorkspace, String type)
             throws ClientException {
@@ -238,9 +182,9 @@
             UserWorkspaceService uws = Framework.getService(UserWorkspaceService.class);
 
             DocumentModel userWorkspace = uws.getCurrentUserPersonalWorkspace(
-                    userName, getAdminSession().getRootDocument());
+                    userName, session.getRootDocument());
 
-            getAdminSession().save();
+            session.save();
 
             return userWorkspace;
 
@@ -252,27 +196,6 @@
 
     }
 
-    public DocumentModelList getUserSettings(String userName)
-            throws ClientException {
-
-        return getUserSettings(getAdminSession(), userName);
-
-    }
-
-    public DocumentModelList getUserSettings(String userName,
-            DocumentType filter) throws ClientException {
-
-        return getUserSettings(getAdminSession(), userName, filter);
-
-    }
-
-    public DocumentModelList getUserSettings(String userName, String type)
-            throws ClientException {
-
-        return getUserSettings(getAdminSession(), userName, type);
-
-    }
-
     protected void initRoot(CoreSession userCoreSession, String userName,
             DocumentModel userWorkspace) throws ClientException {
 
# HG changeset patch
# User Christophe Capon <christophe.capon@vilogia.fr>
# Date 1269851240 -7200
# Branch 5.3.0
# Node ID 103954fc1f16b4f0792a8495714a3e67c717598b
# Parent  2dcf815029a017436f65a8fd1f6393b468adce14
Session management in restlets

diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java
--- a/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-alert-core/src/main/java/org/nuxeo/ecm/alert/core/AlertManagementServiceImpl.java	Mon Mar 29 10:27:20 2010 +0200
@@ -282,7 +282,7 @@
             throws Exception {
 
         DocumentModel userSettingsDoc = getUserSettingsService().getUserSettings(
-                userName, "AlertSettings").get(0);
+                coreSession, "AlertSettings").get(0);
         AlertSettingsWrapper settings = new AlertSettingsWrapper(
                 userSettingsDoc);
 
diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/FireAlertEventRestlet.java
--- a/nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/FireAlertEventRestlet.java	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/FireAlertEventRestlet.java	Mon Mar 29 10:27:20 2010 +0200
@@ -2,11 +2,27 @@
 
 import static org.jboss.seam.ScopeType.EVENT;
 
+import java.io.Serializable;
+import java.security.Principal;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
 import org.nuxeo.ecm.alert.AlertContext;
+import org.nuxeo.ecm.alert.AlertEvent;
 import org.nuxeo.ecm.alert.AlertManagementService;
 import org.nuxeo.ecm.alert.EmailHelper;
+import org.nuxeo.ecm.core.api.ClientException;
+import org.nuxeo.ecm.core.api.CoreSession;
+import org.nuxeo.ecm.core.api.repository.Repository;
+import org.nuxeo.ecm.core.api.repository.RepositoryManager;
+import org.nuxeo.ecm.core.event.Event;
+import org.nuxeo.ecm.core.event.EventContext;
+import org.nuxeo.ecm.core.event.impl.EventContextImpl;
+import org.nuxeo.ecm.core.event.impl.EventImpl;
 import org.nuxeo.ecm.platform.ui.web.restAPI.BaseNuxeoRestlet;
 import org.nuxeo.runtime.api.Framework;
 import org.restlet.data.MediaType;
@@ -22,6 +38,34 @@
             + "<head><title>Error</title></head>"
             + "<body>%%TEXT%%</body></html>";
 
+    private static Log log = LogFactory.getLog(FireAlertEventRestlet.class);
+    
+    protected CoreSession getCoreSession(Request req, Response res,
+            String repoName) {
+        try {
+            
+            Repository repository = null;
+
+            if (repoName == null) {
+                repository = Framework.getService(RepositoryManager.class).getDefaultRepository();
+            } else {
+                repository = Framework.getService(RepositoryManager.class).getRepository(
+                        repoName);
+            }
+
+            if (repository == null) {
+                throw new ClientException("Cannot get repository");
+            }
+            Map<String, Serializable> context = new HashMap<String, Serializable>();
+            context.put("principal", getSerializablePrincipal(req));
+            return repository.open(context);
+        } catch (Exception e) {
+            handleError(res, e);
+            return null;
+        }
+    }
+
+
     @Override
     public void handle(Request req, Response res) {
 
@@ -42,18 +86,20 @@
         } catch (Exception e) {
         }
 
-        String html = processAlerts(byMail, purge);
+        String html = processAlerts(byMail, purge, req, res);
 
         res.setEntity(html, MediaType.TEXT_HTML);
 
     }
 
-    private String processAlerts(boolean bymail, boolean purge) {
+    private String processAlerts(boolean bymail, boolean purge, Request req, Response res) {
 
         EmailHelperMock testHelper = new EmailHelperMock();
         EmailHelper initialHelper = null;
         AlertManagementService alertService = null;
 
+        CoreSession session = getCoreSession(req, res, null);
+        
         try {
 
             alertService = Framework.getService(AlertManagementService.class);
@@ -62,7 +108,11 @@
                 if (!bymail)
                     initialHelper = alertService.setEmailHelper(testHelper);
                 AlertContext ctxt = new AlertContext();
+                
+                EventContext eventCtxt = new EventContextImpl(session, getUserPrincipal(req));
+                Event ev = new EventImpl(AlertEvent.MAIN_ALERT_EVENT, eventCtxt);
                 ctxt.setNoPurge(!purge);
+                ctxt.setEvent(ev);
                 alertService.processAlerts(ctxt);
                 String result = testHelper.getLastBody();
                 alertService.setEmailHelper(initialHelper);
@@ -85,6 +135,15 @@
             return buildHtmlMessage(bdr.toString());
 
         }
+        
+        finally {
+            try {
+                Repository.close(session);
+            }
+            catch(Exception e) {
+                log.error("Unable to close session", e);
+            }
+        }
 
     }
 
diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/ScheduleAlertEventRestlet.java
--- a/nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/ScheduleAlertEventRestlet.java	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-alert-test/src/main/java/org/nuxeo/ecm/alert/core/test/ScheduleAlertEventRestlet.java	Mon Mar 29 10:27:20 2010 +0200
@@ -2,11 +2,24 @@
 
 import static org.jboss.seam.ScopeType.EVENT;
 
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
 import org.nuxeo.ecm.alert.AlertEvent;
+import org.nuxeo.ecm.core.api.ClientException;
+import org.nuxeo.ecm.core.api.CoreSession;
+import org.nuxeo.ecm.core.api.repository.Repository;
+import org.nuxeo.ecm.core.api.repository.RepositoryManager;
+import org.nuxeo.ecm.core.event.Event;
+import org.nuxeo.ecm.core.event.EventContext;
 import org.nuxeo.ecm.core.event.EventService;
 import org.nuxeo.ecm.core.event.impl.EventContextImpl;
+import org.nuxeo.ecm.core.event.impl.EventImpl;
 import org.nuxeo.ecm.platform.ui.web.restAPI.BaseNuxeoRestlet;
 import org.nuxeo.runtime.api.Framework;
 import org.restlet.data.MediaType;
@@ -17,6 +30,34 @@
 @Scope(EVENT)
 public class ScheduleAlertEventRestlet extends BaseNuxeoRestlet {
 
+    private static Log log = LogFactory.getLog(ScheduleAlertEventRestlet.class);
+
+    protected CoreSession getCoreSession(Request req, Response res,
+            String repoName) {
+
+        try {
+
+            Repository repository = null;
+
+            if (repoName == null) {
+                repository = Framework.getService(RepositoryManager.class).getDefaultRepository();
+            } else {
+                repository = Framework.getService(RepositoryManager.class).getRepository(
+                        repoName);
+            }
+
+            if (repository == null) {
+                throw new ClientException("Cannot get repository");
+            }
+            Map<String, Serializable> context = new HashMap<String, Serializable>();
+            context.put("principal", getSerializablePrincipal(req));
+            return repository.open(context);
+        } catch (Exception e) {
+            handleError(res, e);
+            return null;
+        }
+    }
+
     @Override
     public void handle(Request req, Response res) {
 
@@ -26,11 +67,24 @@
             eventId = getHttpRequest(req).getParameter("event");
         }
 
+        CoreSession session = getCoreSession(req, res, null);
+
         try {
-            Framework.getService(EventService.class).fireEvent(eventId,
-                    new EventContextImpl());
+
+            EventContext eventCtxt = new EventContextImpl(session, getUserPrincipal(req));
+            Event ev = new EventImpl(AlertEvent.MAIN_ALERT_EVENT, eventCtxt);
+
+            Framework.getService(EventService.class).fireEvent(ev.getName(),
+                    eventCtxt);
+
         } catch (Exception e) {
             req.setEntity(e.getMessage(), MediaType.TEXT_PLAIN);
+        } finally {
+            try {
+                Repository.close(session);
+            } catch (Exception e) {
+                log.error("Unable to close session", e);
+            }
         }
 
     }
diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-framework.xml
--- a/nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-framework.xml	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-framework.xml	Mon Mar 29 10:27:20 2010 +0200
@@ -1,9 +1,9 @@
 <?xml version="1.0"?>
 <component
-  name="org.nuxeo.ecm.deferredalert.DeferredAlertManagementServiceImplComponent">
+  name="org.nuxeo.ecm.deferredalert.core.DeferredAlertManagementServiceImplComponent">
   
   <implementation
-    class="org.nuxeo.ecm.deferredalert.DeferredAlertManagementServiceImplComponent" />
+    class="org.nuxeo.ecm.deferredalert.core.DeferredAlertManagementServiceImplComponent" />
 
   <service>
     <provide
diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-service-contrib.xml
--- a/nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-service-contrib.xml	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-deferredalert-core/src/main/resources/OSGI-INF/deferredalerts-service-contrib.xml	Mon Mar 29 10:27:20 2010 +0200
@@ -2,9 +2,9 @@
 <component
 	name="org.nuxeo.ecm.deferredalert.AlertManagementService">
 
-    <require>org.nuxeo.ecm.deferredalert.DeferredAlertManagementServiceImplComponent</require>
+    <require>org.nuxeo.ecm.deferredalert.core.DeferredAlertManagementServiceImplComponent</require>
     
-	<extension target="org.nuxeo.ecm.deferredalert.DeferredAlertManagementServiceImplComponent"
+	<extension target="org.nuxeo.ecm.deferredalert.core.DeferredAlertManagementServiceImplComponent"
 		point="deferredAlertManagementConfiguration">
 		
 		<deferredAlertManagement
diff -r 2dcf815029a0 -r 103954fc1f16 nuxeo-platform-usersettings-facade/src/main/java/org/nuxeo/ecm/platform/usersettings/ejb/UserSettingsServiceBean.java
--- a/nuxeo-platform-usersettings-facade/src/main/java/org/nuxeo/ecm/platform/usersettings/ejb/UserSettingsServiceBean.java	Fri Mar 26 20:50:29 2010 +0100
+++ b/nuxeo-platform-usersettings-facade/src/main/java/org/nuxeo/ecm/platform/usersettings/ejb/UserSettingsServiceBean.java	Mon Mar 29 10:27:20 2010 +0200
@@ -99,32 +99,6 @@
         }
     }
 
-    public DocumentModelList getUserSettings(String userName)
-            throws ClientException {
-        try {
-            return getUserSettingsService().getUserSettings(userName);
-        } catch (Throwable e) {
-            throw ClientException.wrap(e);
-        }
-    }
-
-    public DocumentModelList getUserSettings(String userName,
-            DocumentType filter) throws ClientException {
-        try {
-            return getUserSettingsService().getUserSettings(userName, filter);
-        } catch (Throwable e) {
-            throw ClientException.wrap(e);
-        }
-    }
-
-    public DocumentModelList getUserSettings(String userName, String type)
-            throws ClientException {
-        try {
-            return getUserSettingsService().getUserSettings(userName, type);
-        } catch (Throwable e) {
-            throw ClientException.wrap(e);
-        }
-    }
 
     public DocumentModelList getUserSettings(CoreSession coreSession,
             String userName) throws ClientException {
@@ -156,14 +130,6 @@
         }
     }
 
-    public void initService() throws Exception {
-        try {
-            getUserSettingsService().initService();
-        } catch (Throwable e) {
-            throw ClientException.wrap(e);
-        }
-    }
-
     public void registerProvider(String providerName,
             UserSettingsProvider provider) throws ClientException {
         try {
@@ -173,15 +139,7 @@
         }
     }
 
-    public void termService() throws Exception {
-        try {
-            getUserSettingsService().termService();
-        } catch (Throwable e) {
-            throw ClientException.wrap(e);
-        }
-    }
-
-    public void unRegisterProvider(String providerName) throws ClientException {
+     public void unRegisterProvider(String providerName) throws ClientException {
         try {
             getUserSettingsService().unRegisterProvider(providerName);
         } catch (Throwable e) {
