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