All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.uhndata.cards.formcompletionstatus.internal.MinMaxValueValidator Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.uhndata.cards.formcompletionstatus.internal;

import java.util.Map;
import java.util.Set;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.osgi.service.component.annotations.Component;

import io.uhndata.cards.formcompletionstatus.spi.AnswerValidator;

/**
 * An {@link MinMaxValueValidator} checks for each value if it is in the minValue ... maxValue range.
 *
 * @version $Id: 15e9875bb3296c73fcacb5eb6a83c98f0e95ff83 $
 */
@Component(immediate = true)
public class MinMaxValueValidator implements AnswerValidator
{
    private static final Set SUPPORTED_TYPES = Set.of("long", "double", "decimal");

    @Override
    public int getPriority()
    {
        return 50;
    }

    @Override
    public void validate(final NodeBuilder answer, final Node question, final boolean initialAnswer,
        final Map flags)
    {
        try {
            final String type = question.getProperty("dataType").getString();
            if (!SUPPORTED_TYPES.contains(type)) {
                // This only works on numerical types, nothing to do if this is not one of them
                return;
            }
            if (answer.hasProperty(PROP_VALUE)) {
                final double minValue =
                    question.hasProperty("minValue") ? question.getProperty("minValue").getDouble() : Double.NaN;
                final double maxValue =
                    question.hasProperty("maxValue") ? question.getProperty("maxValue").getDouble() : Double.NaN;

                final PropertyState answerProp = answer.getProperty(PROP_VALUE);
                // if any value is out of range, set FLAG_INVALID to true
                for (int i = 0; i < answerProp.count(); i++) {
                    final Double value = answerProp.getValue(Type.DOUBLE, i);
                    if (value < minValue || value > maxValue) {
                        flags.put(FLAG_INVALID, true);
                        break;
                    }
                }
            }
            // If the INVALID flag has not been explicitly set so far, remove it
            removeIfNotExplicitlySet(FLAG_INVALID, flags);
        } catch (final RepositoryException ex) {
            // If something goes wrong do nothing
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy