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.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; 036import com.thoughtworks.xstream.io.json.JsonWriter; 037 038/** 039 * Convert {@link Constraint} to Json. 040 * <p> 041 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing 042 * {@link Constraint}. 043 * </p> 044 * <p> 045 * This marshaller is also extensible: extend it and simply override 046 * {@link ExtensibleEntityJsonWriter#extend(Constraint, JsonWriter)}. 047 * </p> 048 * <p> 049 * Format is: 050 * 051 * <pre> 052 * {@code 053 * { 054 * "entity-type":"validation_constraint", 055 * "name": "CONSTRAINT_NAME", 056 * "parameters": { 057 * "PARAMETER1_NAME": "PARAMETER1_VALUE", 058 * "PARAMETER2_NAME": "PARAMETER2_VALUE", 059 * ... 060 * } 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) 071public class ConstraintJsonWriter extends ExtensibleEntityJsonWriter<Constraint> { 072 073 public static final String ENTITY_TYPE = "validation_constraint"; 074 075 public ConstraintJsonWriter() { 076 super(ENTITY_TYPE, Constraint.class); 077 } 078 079 @Override 080 protected void writeEntityBody(Constraint constraint, JsonGenerator jg) throws IOException { 081 Description description = constraint.getDescription(); 082 jg.writeStringField("name", description.getName()); 083 // constraint parameters 084 jg.writeObjectFieldStart("parameters"); 085 for (Map.Entry<String, Serializable> param : description.getParameters().entrySet()) { 086 jg.writeStringField(param.getKey(), param.getValue().toString()); 087 } 088 jg.writeEndObject(); 089 } 090 091}