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