001/*
002 * (C) Copyright 2014 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 *     tdelprat
018 */
019package org.nuxeo.ecm.restapi.jaxrs.io.types;
020
021import java.io.IOException;
022import java.io.OutputStream;
023
024import org.nuxeo.ecm.core.schema.DocumentType;
025import org.nuxeo.ecm.core.schema.types.ComplexType;
026import org.nuxeo.ecm.core.schema.types.CompositeType;
027import org.nuxeo.ecm.core.schema.types.Field;
028import org.nuxeo.ecm.core.schema.types.ListType;
029import org.nuxeo.ecm.core.schema.types.Schema;
030import org.nuxeo.ecm.core.schema.types.SimpleType;
031import org.nuxeo.ecm.core.schema.types.Type;
032
033import com.fasterxml.jackson.core.JsonEncoding;
034import com.fasterxml.jackson.core.JsonFactory;
035import com.fasterxml.jackson.core.JsonGenerator;
036
037public class AbstractTypeDefWriter {
038
039    public AbstractTypeDefWriter() {
040        super();
041    }
042
043    protected JsonGenerator getGenerator(OutputStream entityStream) throws IOException {
044        JsonFactory factory = new JsonFactory();
045        JsonGenerator jg = factory.createJsonGenerator(entityStream, JsonEncoding.UTF8);
046        jg.useDefaultPrettyPrinter();
047        return jg;
048    }
049
050    protected void writeSchema(JsonGenerator jg, Schema schema) throws IOException {
051        jg.writeObjectFieldStart(schema.getName());
052        jg.writeStringField("@prefix", schema.getNamespace().prefix);
053        for (Field field : schema.getFields()) {
054            writeField(jg, field);
055        }
056        jg.writeEndObject();
057    }
058
059    protected void writeSchemaObject(JsonGenerator jg, Schema schema) throws IOException {
060        jg.writeStartObject();
061        jg.writeStringField("name", schema.getName());
062        jg.writeStringField("@prefix", schema.getNamespace().prefix);
063        jg.writeObjectFieldStart("fields");
064        for (Field field : schema.getFields()) {
065            writeField(jg, field);
066        }
067        jg.writeEndObject();
068        jg.writeEndObject();
069    }
070
071    protected void writeDocType(JsonGenerator jg, DocumentType docType, boolean expandSchemas) throws IOException {
072
073        if (docType.getSuperType() != null) {
074            jg.writeStringField("parent", docType.getSuperType().getName());
075        } else {
076            jg.writeStringField("parent", "None!!!");
077        }
078
079        jg.writeArrayFieldStart("facets");
080        for (String facet : docType.getFacets()) {
081            jg.writeString(facet);
082        }
083        jg.writeEndArray();
084
085        jg.writeArrayFieldStart("schemas");
086        if (expandSchemas) {
087            for (Schema schema : docType.getSchemas()) {
088                writeSchemaObject(jg, schema);
089            }
090        } else {
091            for (String schema : docType.getSchemaNames()) {
092                jg.writeString(schema);
093            }
094        }
095        jg.writeEndArray();
096
097    }
098
099    protected void writeField(JsonGenerator jg, Field field) throws IOException {
100        Type type = field.getType();
101        if (!type.isComplexType()) {
102            if (type.isListType()) {
103                ListType lt = (ListType) type;
104                if (lt.getFieldType().isComplexType()) {
105                    if (lt.getFieldType().getName().equals("content")) {
106                        jg.writeStringField(field.getName().getLocalName(), "blob[]");
107
108                    } else {
109                        jg.writeObjectFieldStart(field.getName().getLocalName());
110                        buildComplexFields(jg, lt.getField());
111                        jg.writeStringField("type", "complex[]");
112                        jg.writeEndObject();
113                    }
114                } else {
115                    Type fieldType = lt.getFieldType();
116                    if (fieldType instanceof SimpleType) {
117                        SimpleType stype = (SimpleType) fieldType;
118                        fieldType = stype.getPrimitiveType();
119                    }
120                    jg.writeStringField(field.getName().getLocalName(), fieldType.getName() + "[]");
121                }
122            } else {
123                if (type instanceof SimpleType) {
124                    SimpleType stype = (SimpleType) type;
125                    type = stype.getPrimitiveType();
126                }
127                jg.writeStringField(field.getName().getLocalName(), type.getName());
128            }
129        } else {
130            if (type.getName().equals("content")) {
131                jg.writeStringField(field.getName().getLocalName(), "blob");
132            } else {
133
134                jg.writeObjectFieldStart(field.getName().getLocalName());
135                buildComplexFields(jg, field);
136                jg.writeStringField("type", "complex");
137                jg.writeEndObject();
138            }
139        }
140
141    }
142
143    protected void buildComplexFields(JsonGenerator jg, Field field) throws IOException {
144        ComplexType cplXType = (ComplexType) field.getType();
145        jg.writeObjectFieldStart("fields");
146        for (Field subField : cplXType.getFields()) {
147            writeField(jg, subField);
148        }
149        jg.writeEndObject();
150    }
151
152    protected void writeFacet(JsonGenerator jg, CompositeType facet, boolean expandSchemas) throws IOException {
153
154        jg.writeStringField("name", facet.getName());
155        if (facet.getSchemaNames() != null && facet.getSchemaNames().length > 0) {
156            jg.writeArrayFieldStart("schemas");
157            if (expandSchemas) {
158                for (Schema schema : facet.getSchemas()) {
159                    writeSchemaObject(jg, schema);
160                }
161            } else {
162                for (String schemaName : facet.getSchemaNames()) {
163                    jg.writeString(schemaName);
164                }
165            }
166            jg.writeEndArray();
167        }
168
169    }
170}