001/**
002 * Copyright 2005-2016 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     * @return
046     * @throws AttributeValidationException
047     * @see MandatoryElementConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult,
048     *      Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint,
049     *      org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
050     */
051    @Override
052    public ProcessorResult process(DictionaryValidationResult result, Object value, final SimpleConstraint constraint,
053            AttributeValueReader attributeValueReader) throws AttributeValidationException {
054
055        ProcessorResult dataTypePR = dataTypeConstraintProcessor.process(result, value, constraint,
056                attributeValueReader);
057        ProcessorResult existencePR = existenceConstraintProcessor.process(result, value, constraint,
058                attributeValueReader);
059        ProcessorResult rangePR = rangeConstraintProcessor.process(result, value, constraint, attributeValueReader);
060        ProcessorResult lengthPR = lengthConstraintProcessor.process(result, value, constraint, attributeValueReader);
061        List<ConstraintValidationResult> cvrList = new ArrayList<ConstraintValidationResult>();
062        cvrList.addAll(existencePR.getConstraintValidationResults());
063        cvrList.addAll(rangePR.getConstraintValidationResults());
064        cvrList.addAll(lengthPR.getConstraintValidationResults());
065        cvrList.addAll(dataTypePR.getConstraintValidationResults());
066        return new ProcessorResult(cvrList);
067    }
068
069    @Override
070    public String getName() {
071        return CONSTRAINT_NAME;
072    }
073
074    @Override
075    public Class<? extends Constraint> getConstraintType() {
076        return SimpleConstraint.class;
077    }
078}