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.types;
019
020import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023
024import java.io.IOException;
025import java.io.OutputStream;
026
027import org.codehaus.jackson.JsonGenerator;
028import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
029import org.nuxeo.ecm.core.io.marshallers.json.OutputStreamWithJsonWriter;
030import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
031import org.nuxeo.ecm.core.io.registry.Writer;
032import org.nuxeo.ecm.core.io.registry.reflect.Setup;
033import org.nuxeo.ecm.core.schema.types.CompositeType;
034import org.nuxeo.ecm.core.schema.types.Schema;
035
036import com.thoughtworks.xstream.io.json.JsonWriter;
037
038/**
039 * Convert {@link CompositeType} to Json.
040 * <p>
041 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing
042 * {@link CompositeType}.
043 * </p>
044 * <p>
045 * This marshaller is also extensible: extend it and simply override
046 * {@link ExtensibleEntityJsonWriter#extend(CompositeType, JsonWriter)}.
047 * </p>
048 * <p>
049 * Format is:
050 *
051 * <pre>
052 * {@code
053 * {
054 *   "entity-type":"facet",
055 *   "name": "FACET_NAME",
056 *   "schemas": [ { see {@link SchemaJsonWriter} for format }, { ... }, ... ],
057 *             <-- contextParameters if there are enrichers activated
058 *             <-- additional property provided by extend() method
059 * }
060 * </pre>
061 *
062 * </p>
063 *
064 * @since 7.2
065 */
066@Setup(mode = SINGLETON, priority = REFERENCE)
067public class FacetJsonWriter extends ExtensibleEntityJsonWriter<CompositeType> {
068
069    public static final String ENTITY_TYPE = "facet";
070
071    public FacetJsonWriter() {
072        super(ENTITY_TYPE, CompositeType.class);
073    }
074
075    @Override
076    protected void writeEntityBody(CompositeType facet, JsonGenerator jg) throws IOException {
077        jg.writeStringField("name", facet.getName());
078        if (facet.getSchemaNames() != null && facet.getSchemaNames().length > 0) {
079            jg.writeArrayFieldStart("schemas");
080            Writer<Schema> schemaWriter = registry.getWriter(ctx, Schema.class, APPLICATION_JSON_TYPE);
081            for (Schema schema : facet.getSchemas()) {
082                OutputStream out = new OutputStreamWithJsonWriter(jg);
083                schemaWriter.write(schema, Schema.class, Schema.class, APPLICATION_JSON_TYPE, out);
084            }
085            jg.writeEndArray();
086        }
087    }
088
089}