001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.krad.datadictionary.validation.processor;
017
018import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
019import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
020import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
021import org.kuali.rice.krad.datadictionary.validation.constraint.SimpleConstraint;
022import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
023import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
024import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
025
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * Processor for simple constraint which takes out each constraining value it contains and calls the appropriate
031 * processor
032 */
033public class SimpleConstraintProcessor extends MandatoryElementConstraintProcessor<SimpleConstraint> {
034
035    private static final String CONSTRAINT_NAME = "simple constraint";
036
037    RangeConstraintProcessor rangeConstraintProcessor = new RangeConstraintProcessor();
038    LengthConstraintProcessor lengthConstraintProcessor = new LengthConstraintProcessor();
039    ExistenceConstraintProcessor existenceConstraintProcessor = new ExistenceConstraintProcessor();
040    DataTypeConstraintProcessor dataTypeConstraintProcessor = new DataTypeConstraintProcessor();
041
042    /**
043     * Processes the SimpleConstraint by calling process on the other smaller constraints it represents and
044     * putting the results together in ProcessorResult
045     *
046     * @return processor result
047     * @throws AttributeValidationException
048     * @see MandatoryElementConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult,
049     *      Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint,
050     *      org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
051     */
052    @Override
053    public ProcessorResult process(DictionaryValidationResult result, Object value, final SimpleConstraint constraint,
054            AttributeValueReader attributeValueReader) throws AttributeValidationException {
055
056        ProcessorResult dataTypePR = dataTypeConstraintProcessor.process(result, value, constraint,
057                attributeValueReader);
058        ProcessorResult existencePR = existenceConstraintProcessor.process(result, value, constraint,
059                attributeValueReader);
060        ProcessorResult rangePR = rangeConstraintProcessor.process(result, value, constraint, attributeValueReader);
061        ProcessorResult lengthPR = lengthConstraintProcessor.process(result, value, constraint, attributeValueReader);
062        List<ConstraintValidationResult> cvrList = new ArrayList<ConstraintValidationResult>();
063        cvrList.addAll(existencePR.getConstraintValidationResults());
064        cvrList.addAll(rangePR.getConstraintValidationResults());
065        cvrList.addAll(lengthPR.getConstraintValidationResults());
066        cvrList.addAll(dataTypePR.getConstraintValidationResults());
067        return new ProcessorResult(cvrList);
068    }
069
070    @Override
071    public String getName() {
072        return CONSTRAINT_NAME;
073    }
074
075    @Override
076    public Class<? extends Constraint> getConstraintType() {
077        return SimpleConstraint.class;
078    }
079}