001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.core.io.marshallers.json.enrichers; 019 020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 022 023import java.io.IOException; 024import java.util.List; 025 026import org.codehaus.jackson.JsonGenerator; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 029import org.nuxeo.ecm.core.io.marshallers.json.document.DocumentModelListJsonWriter; 030import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper; 031import org.nuxeo.ecm.core.io.registry.reflect.Setup; 032 033/** 034 * Enrich {@link DocumentModel} Json. 035 * <p> 036 * Add document breadcrumb (list of all parents document) as json attachment. 037 * </p> 038 * <p> 039 * Enable if parameter enrichers.document=breadcrumb is present. 040 * </p> 041 * <p> 042 * Format is: 043 * 044 * <pre> 045 * {@code 046 * { 047 * "entity-type":"document", 048 * ... 049 * "contextParameters": { 050 * "breadcrumb": { see {@link DocumentModelListJsonWriter} for format } 051 * } 052 * } 053 * </pre> 054 * 055 * </p> 056 * 057 * @since 7.2 058 */ 059@Setup(mode = SINGLETON, priority = REFERENCE) 060public class BreadcrumbJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 061 062 public static final String NAME = "breadcrumb"; 063 064 public BreadcrumbJsonEnricher() { 065 super(NAME); 066 } 067 068 @Override 069 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 070 List<DocumentModel> parentDocuments = null; 071 try (SessionWrapper wrapper = ctx.getSession(document)) { 072 parentDocuments = wrapper.getSession().getParentDocuments(document.getRef()); 073 } 074 DocumentModelListImpl documentList = new DocumentModelListImpl(parentDocuments); 075 jg.writeFieldName(NAME); 076 writeEntity(documentList, jg); 077 } 078 079}