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 */
016package org.forgerock.api.jackson;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.fasterxml.jackson.module.jsonSchema.jakarta.types.BooleanSchema;
020import jakarta.validation.ValidationException;
021import org.forgerock.api.enums.ReadPolicy;
022import org.forgerock.api.enums.WritePolicy;
023import org.forgerock.json.JsonValue;
024
025/**
026 * An extension to the Jackson {@code BooleanSchema} that includes the custom CREST JSON Schema attributes.
027 */
028public class CrestBooleanSchema extends BooleanSchema implements CrestReadWritePoliciesSchema, OrderedFieldSchema,
029        ValidatableSchema, WithExampleSchema<Boolean> {
030    private WritePolicy writePolicy;
031    private ReadPolicy readPolicy;
032    private Boolean errorOnWritePolicyFailure;
033    private Boolean returnOnDemand;
034    private Integer propertyOrder;
035    private Boolean example;
036
037    @Override
038    public WritePolicy getWritePolicy() {
039        return writePolicy;
040    }
041
042    @Override
043    public void setWritePolicy(WritePolicy policy) {
044        this.writePolicy = policy;
045    }
046
047    @Override
048    public ReadPolicy getReadPolicy() {
049        return readPolicy;
050    }
051
052    @Override
053    public void setReadPolicy(ReadPolicy readPolicy) {
054        this.readPolicy = readPolicy;
055    }
056
057    @Override
058    public Boolean getErrorOnWritePolicyFailure() {
059        return errorOnWritePolicyFailure;
060    }
061
062    @Override
063    public void setErrorOnWritePolicyFailure(Boolean errorOnWritePolicyFailure) {
064        this.errorOnWritePolicyFailure = errorOnWritePolicyFailure;
065    }
066
067    @Override
068    public Boolean getReturnOnDemand() {
069        return returnOnDemand;
070    }
071
072    @Override
073    public void setReturnOnDemand(Boolean returnOnDemand) {
074        this.returnOnDemand = returnOnDemand;
075    }
076
077    @Override
078    public Integer getPropertyOrder() {
079        return propertyOrder;
080    }
081
082    @Override
083    public void setPropertyOrder(Integer order) {
084        this.propertyOrder = order;
085    }
086
087    @Override
088    public void validate(JsonValue object) throws ValidationException {
089        if (!object.isBoolean()) {
090            throw new ValidationException("Expected boolean, but got " + object.getObject());
091        }
092    }
093
094    /**
095     * Gets read-only property. This method overrides the superclass' definition of "readOnly" being all lower-case,
096     * via the {@code JsonProperty} annotation.
097     *
098     * @return {@code true} if property is read-only, otherwise {@code false} or {@code null}
099     */
100    @JsonProperty("readOnly")
101    @Override
102    public Boolean getReadonly() {
103        return super.getReadonly();
104    }
105
106    @Override
107    public Boolean getExample() {
108        return example;
109    }
110
111    @Override
112    public void setExample(String example) {
113        this.example = Boolean.valueOf(example);
114    }
115}