implement an operation - Subscribe to an Alert

Hi

I would like to implement an operation to allow me to subscribe a user to an alert. So i am looking for the same behavior than if a go in Document Management - click on a file

  • select Alerts and click on Subscribe Modification.

In Java Doc i found the NotificationManager with the method; addSubscription(String username, String notification, DocumentModel doc, Boolean sendConfirmationEmail, NuxeoPrincipal principal, String notificationName)

But i am not sure what the two Strings “notification” and “notificationName” represents and how they are linked with the actual DocumentEventTypes.

I tried the following with no success;

@Param(name = "username")
protected String username;

@Param(name = "DocumentEventType")
protected String DocumentEventType;

@OperationMethod
public  run(DocumentModel doc) throws Exception{

NotificationManager  notificationManager = Framework.getService(NotificationManager.class);

notificationManager.addSubscription(username, DocumentEventType, doc, true, null, DocumentEventType);

If i get;

notificationManager.getSubscriptionsForUserOnDocument(username, doc.getId())

i can see a notification “documentModified” but i don't see it selected if i look in Document Management.

Does anyone have an idea on how to do this? …examples?… links to source code ?

tx

Michel

0 votes

3 answers

2017 views

ANSWER



“notification” is used in the UserSubscription to identify the notification:

UserSubscription subscription = new UserSubscription(notification, username, doc.getId());

whereas “notificationName” is only a label used in the sent email:

// options for confirmation email
options.put("recipients", username);
options.put("notifName", notificationName);
0 votes



hi Julient

Thanks for your answer

But i am still missing something ….

In the Nuxeo API how do you call the alerts that we see on the gui ? -Modification -Creation -Workflow Changed -Approval workflow started -Comments moderation

Is the string notification in the UserSubscription constructor and in the NotificationService.addSubscription method refering to those alerts ?

Is the method you post enough to create the subscription ? i don't get the green check mark in the GUI after calling it. But again i am not sure what string i have to pass for the notification.

UserSubscription subscription = new UserSubscription("Modification", "myUserName", doc.getId());

Is there a documentModel method returning the actual alerts on which a users have subscribe? It would help me to see how Nuxeo refers to those alerts..

thanks

11/14/2011


Hi,

Finally the USER_PREFIX constant resolved the problem. Concerning the principal it's not mandatory, but if you put it , the user will also receive a notification indicating that the Principal (Administrator in this context) subscribed him to the notification.

I didn't understand what the UserSubscription subscription = new UserSubscription(notification, username, doc.getId()); add to this. I can subscribe to a notification , receive the email , without it.

@Param(name = “username”)

protected String username;

@OperationMethod
public String run(DocumentModel doc) throws Exception{

    final String CONSTANT_CREATION = "Creation";

    try
    {

    //Create a notification Manager 
    NotificationManager  notificationManager = Framework.getService(NotificationManager.class);

    //add a subscription
    notificationManager.addSubscription(NotificationConstants.USER_PREFIX + username,CONSTANT_CREATION, doc, true, (NuxeoPrincipal)doc.getCoreSession().getPrincipal(), null);

    //save session
    session.save();
0 votes



Hi,

That's a nice approach:) First of all, for the username, you must prefix it for telling if it is a group or a user. The prefix to use are:

  • NotificationConstants.USER_PREFIX
  • NotificationConstants.GROUP_PREFIX

Then, I can see that the API expects the principal (you put null). You should probably put the principal of the current Session.

0 votes