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.validation;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026import java.io.Serializable;
027import java.util.Map;
028
029import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
030import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
031import org.nuxeo.ecm.core.io.registry.reflect.Setup;
032import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
033import org.nuxeo.ecm.core.schema.types.constraints.Constraint.Description;
034
035import com.fasterxml.jackson.core.JsonGenerator;
036
037/**
038 * Convert {@link Constraint} to Json.
039 * <p>
040 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing
041 * {@link Constraint}.
042 * <p>
043 * This marshaller is also extensible: extend it and simply override
044 * {@link ExtensibleEntityJsonWriter#extend(Object, JsonGenerator)}.
045 * <p>
046 * Format is:
047 *
048 * <pre>
049 * {
050 *   "entity-type":"validation_constraint",
051 *   "name": "CONSTRAINT_NAME",
052 *   "parameters": {
053 *     "PARAMETER1_NAME": "PARAMETER1_VALUE",
054 *     "PARAMETER2_NAME": "PARAMETER2_VALUE",
055 *     ...
056 *   }
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)
065public class ConstraintJsonWriter extends ExtensibleEntityJsonWriter<Constraint> {
066
067    public static final String ENTITY_TYPE = "validation_constraint";
068
069    public ConstraintJsonWriter() {
070        super(ENTITY_TYPE, Constraint.class);
071    }
072
073    @Override
074    protected void writeEntityBody(Constraint constraint, JsonGenerator jg) throws IOException {
075        Description description = constraint.getDescription();
076        jg.writeStringField("name", description.getName());
077        // constraint parameters
078        jg.writeObjectFieldStart("parameters");
079        for (Map.Entry<String, Serializable> param : description.getParameters().entrySet()) {
080            jg.writeStringField(param.getKey(), param.getValue().toString());
081        }
082        jg.writeEndObject();
083    }
084
085}