001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.core.io.avro;
020
021import java.util.Arrays;
022
023import org.apache.avro.LogicalType;
024import org.apache.avro.Schema;
025import org.apache.avro.Schema.Field;
026import org.apache.avro.Schema.Type;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.runtime.avro.AvroSchemaFactory;
029import org.nuxeo.runtime.avro.AvroSchemaFactoryContext;
030
031/**
032 * @since 10.2
033 */
034public class DocumentModelSchemaFactory extends AvroSchemaFactory<DocumentModel> {
035
036    public DocumentModelSchemaFactory(AvroSchemaFactoryContext context) {
037        super(context);
038    }
039
040    @Override
041    public Schema createSchema(DocumentModel input) {
042        Schema typeSchema = context.createSchema(input.getDocumentType());
043        Schema schema = Schema.createRecord(getName(input), null, AvroConstants.ECM, false);
044        new LogicalType(AvroConstants.DOCUMENT_MODEL).addToSchema(schema);
045        schema.setFields(Arrays.asList(
046                // mandatory
047                new Field(AvroConstants.UUID, Schema.create(Type.STRING), null, (Object) null),
048                new Field(AvroConstants.PATH, Schema.create(Type.STRING), null, (Object) null),
049                new Field(AvroConstants.NAME, Schema.create(Type.STRING), null, (Object) null),
050                new Field(AvroConstants.TITLE, Schema.create(Type.STRING), null, (Object) null),
051                new Field(AvroConstants.REPOSITORY_NAME, Schema.create(Type.STRING), null, (Object) null),
052                new Field(AvroConstants.PRIMARY_TYPE, Schema.create(Type.STRING), null, (Object) null),
053                new Field(AvroConstants.CHANGE_TOKEN, Schema.create(Type.STRING), null, (Object) null),
054                new Field(AvroConstants.CURRENT_LIFE_CYCLE_STATE, Schema.create(Type.STRING), null, (Object) null),
055                new Field(AvroConstants.IS_PROXY, Schema.create(Type.BOOLEAN), null, (Object) null),
056                new Field(AvroConstants.IS_TRASHED, Schema.create(Type.BOOLEAN), null, (Object) null),
057                new Field(AvroConstants.IS_VERSION, Schema.create(Type.BOOLEAN), null, (Object) null),
058                new Field(AvroConstants.IS_CHECKEDIN, Schema.create(Type.BOOLEAN), null, (Object) null),
059                new Field(AvroConstants.IS_LATEST_VERSION, Schema.create(Type.BOOLEAN), null, (Object) null),
060                new Field(AvroConstants.IS_LATEST_MAJOR_VERSION, Schema.create(Type.BOOLEAN), null, (Object) null),
061                new Field(AvroConstants.IS_RECORD, Schema.create(Type.BOOLEAN), null, Boolean.FALSE),
062                new Field(AvroConstants.HAS_LEGAL_HOLD, Schema.create(Type.BOOLEAN), null, Boolean.FALSE),
063                // nullable
064                new Field(AvroConstants.PARENT_ID, nullable(Schema.create(Type.STRING)), null, (Object) null),
065                new Field(AvroConstants.VERSION_LABEL, nullable(Schema.create(Type.STRING)), null, (Object) null),
066                new Field(AvroConstants.VERSION_VERSIONABLE_ID, nullable(Schema.create(Type.STRING)), null,
067                        (Object) null),
068                new Field(AvroConstants.POS, nullable(Schema.create(Type.LONG)), null, (Object) null),
069                new Field(AvroConstants.MIXIN_TYPES, nullable(Schema.createArray(Schema.create(Type.STRING))), null,
070                        (Object) null),
071                new Field(AvroConstants.TAGS, nullable(Schema.createArray(Schema.create(Type.STRING))), null,
072                        (Object) null),
073                new Field(AvroConstants.RETAIN_UNTIL, nullable(Schema.create(Type.LONG)), null, (Object) null),
074                new Field(AvroConstants.DOCUMENT_TYPE, typeSchema, null, (Object) null)));
075        return schema;
076    }
077
078    @Override
079    public String getName(DocumentModel input) {
080        return context.getService().encodeName(input.getName());
081    }
082
083}