001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016
017package org.forgerock.api.jackson;
018
019import javax.validation.ValidationException;
020
021import com.fasterxml.jackson.annotation.JsonProperty;
022import org.forgerock.api.enums.ReadPolicy;
023import org.forgerock.json.JsonValue;
024
025import com.fasterxml.jackson.module.jsonSchema.types.BooleanSchema;
026import org.forgerock.api.enums.WritePolicy;
027
028/**
029 * An extension to the Jackson {@code BooleanSchema} that includes the custom CREST JSON Schema attributes.
030 */
031public class CrestBooleanSchema extends BooleanSchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
032        ValidatableSchema, WithExampleSchema<Boolean> {
033    private WritePolicy writePolicy;
034    private ReadPolicy readPolicy;
035    private Boolean errorOnWritePolicyFailure;
036    private Boolean returnOnDemand;
037    private Integer propertyOrder;
038    private Boolean example;
039
040    @Override
041    public WritePolicy getWritePolicy() {
042        return writePolicy;
043    }
044
045    @Override
046    public void setWritePolicy(WritePolicy policy) {
047        this.writePolicy = policy;
048    }
049
050    @Override
051    public ReadPolicy getReadPolicy() {
052        return readPolicy;
053    }
054
055    @Override
056    public void setReadPolicy(ReadPolicy readPolicy) {
057        this.readPolicy = readPolicy;
058    }
059
060    @Override
061    public Boolean getErrorOnWritePolicyFailure() {
062        return errorOnWritePolicyFailure;
063    }
064
065    @Override
066    public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
067        this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
068    }
069
070    @Override
071    public Boolean getReturnOnDemand() {
072        return returnOnDemand;
073    }
074
075    @Override
076    public void setReturnOnDemand(Boolean returnOnDemand) {
077        this.returnOnDemand = returnOnDemand;
078    }
079
080    @Override
081    public Integer getPropertyOrder() {
082        return propertyOrder;
083    }
084
085    @Override
086    public void setPropertyOrder(Integer order) {
087        this.propertyOrder = order;
088    }
089
090    @Override
091    public void validate(JsonValue object) throws ValidationException {
092        if (!object.isBoolean()) {
093            throw new ValidationException("Expected boolean, but got " + object.getObject());
094        }
095    }
096
097    /**
098     * Gets read-only property. This method overrides the superclass' definition of "readOnly" being all lower-case,
099     * via the {@code JsonProperty} annotation.
100     *
101     * @return {@code true} if property is read-only, otherwise {@code false} or {@code null}
102     */
103    @JsonProperty("readOnly")
104    @Override
105    public Boolean getReadonly() {
106        return super.getReadonly();
107    }
108
109    @Override
110    public Boolean getExample() {
111        return example;
112    }
113
114    @Override
115    public void setExample(String example) {
116        this.example = Boolean.valueOf(example);
117    }
118}