001package org.nuxeo.sample;
002
003import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
004import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
005
006import java.io.IOException;
007import java.util.Collections;
008
009import org.codehaus.jackson.JsonGenerator;
010import org.nuxeo.ecm.core.api.DocumentModel;
011import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
012import org.nuxeo.ecm.core.io.registry.reflect.Setup;
013
014/**
015 * Enrich {@link nuxeo.ecm.core.api.DocumentModel} Json.
016 * <p>
017 * Format is:
018 *
019 * <pre>
020 * {@code
021 * {
022 *   ...
023 *   "contextParameters": {
024 *     "sample_document": { ... }
025 *   }
026 * }
027 * </pre>
028 * </p>
029 */
030@Setup(mode = SINGLETON, priority = REFERENCE)
031public class SampleDocumentEnricher extends AbstractJsonEnricher<DocumentModel> {
032
033    public static final String NAME = "sample_document";
034
035    public SampleDocumentEnricher() {
036        super(NAME);
037    }
038
039    @Override
040    public void write(JsonGenerator jg, DocumentModel obj) throws IOException {
041        // How to instanciate a Session if `obj` is a DocumentModel
042        //try (SessionWrapper wrapper = ctx.getSession(obj)) {
043        //    CoreSession session = wrapper.getSession();
044        //    ...
045        //}
046
047        jg.writeFieldName(NAME);
048        jg.writeObject(Collections.EMPTY_MAP);
049    }
050}