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