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