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.io; 020 021import org.nuxeo.ecm.core.api.DocumentModel; 022import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 023import org.nuxeo.ecm.core.io.registry.context.RenderingContext; 024import org.nuxeo.ecm.core.io.registry.reflect.Setup; 025import org.nuxeo.ecm.platform.ec.notification.NotificationConstants; 026import org.nuxeo.ecm.platform.ec.notification.SubscriptionAdapter; 027 028import com.fasterxml.jackson.core.JsonGenerator; 029 030import java.io.IOException; 031import java.util.List; 032 033import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 034import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 035 036/** 037 * Enricher that lists the current user's subscribed notifications for a particular document. 038 * 039 * @since 8.10 040 */ 041@Setup(mode = SINGLETON, priority = REFERENCE) 042public class NotificationsJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 043 044 public static final String NAME = "subscribedNotifications"; 045 046 public NotificationsJsonEnricher() { 047 super(NAME); 048 } 049 050 @Override 051 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 052 jg.writeFieldName(NAME); 053 jg.writeStartArray(); 054 try (RenderingContext.SessionWrapper wrapper = ctx.getSession(document)) { 055 String username = NotificationConstants.USER_PREFIX + wrapper.getSession().getPrincipal().getName(); 056 List<String> notifications = document.getAdapter(SubscriptionAdapter.class).getUserSubscriptions(username); 057 for (String notification : notifications) { 058 jg.writeString(notification); 059 } 060 } 061 jg.writeEndArray(); 062 } 063 064}