001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Miguel Nixo 018 */ 019package org.nuxeo.ecm.platform.ec.notification.automation; 020 021import org.nuxeo.ecm.automation.core.Constants; 022import org.nuxeo.ecm.automation.core.annotations.Context; 023import org.nuxeo.ecm.automation.core.annotations.Operation; 024import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 025import org.nuxeo.ecm.automation.core.annotations.Param; 026import org.nuxeo.ecm.automation.core.util.StringList; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentModelList; 030import org.nuxeo.ecm.core.api.NuxeoPrincipal; 031import org.nuxeo.ecm.platform.ec.notification.NotificationConstants; 032import org.nuxeo.ecm.platform.notification.api.NotificationManager; 033 034/** 035 * Operation to subscribe a user to the notifications of one or more of documents. 036 * 037 * @since 8.10 038 */ 039@Operation(id = SubscribeOperation.ID, category = Constants.CAT_DOCUMENT, label = "Subscribe document", 040 description = "Subscribe one or more documents. No value is returned.") 041public class SubscribeOperation { 042 043 public static final String ID = "Document.Subscribe"; 044 045 @Context 046 protected CoreSession coreSession; 047 048 @Context 049 protected NotificationManager notificationManager; 050 051 @Param(name = "notifications", required = false) 052 protected StringList notifications; 053 054 @OperationMethod 055 public DocumentModelList run(DocumentModelList docs) { 056 docs.forEach(this::run); 057 return docs; 058 } 059 060 @OperationMethod 061 public DocumentModel run(DocumentModel doc) { 062 NuxeoPrincipal principal = coreSession.getPrincipal(); 063 String username = NotificationConstants.USER_PREFIX + principal.getName(); 064 if (notifications == null || notifications.isEmpty()) { 065 // subscribe all available notifications 066 notificationManager.addSubscriptions(username, doc, false, principal); 067 } else { 068 // subscribe the specified notifications 069 for (String notification : notifications) { 070 notificationManager.addSubscription(username, notification, doc, false, principal, notification); 071 } 072 } 073 return doc; 074 } 075 076}