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.constraint;
017
018import org.kuali.rice.krad.datadictionary.parse.BeanTag;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
021
022import java.util.List;
023
024/**
025 * Must occur constraints are constraints that indicate some range of acceptable valid results. So a must occur
026 * constraint
027 * might indicate that between 1 and 3 prequisite constraints must be valid. For example, on a person object, it might
028 * be
029 * that one of three fields must be filled in:
030 *
031 * 1. username
032 * 2. email
033 * 3. phone number
034 *
035 * By imposing a must occur constraint on the person object iself, and setting three prequisite constraints below it,
036 * with a min of 1
037 * and a max of 3, this requirement can be enforced.
038 *
039 * A more complicated example might be that a US address is only valid if it provides either:
040 * (a) a city and state, or
041 * (b) a postal code
042 *
043 * To enforce this, a single must occur constraint would have two children: (1) a prequisite constraint on postal code,
044 * and (2) a must occur constraint
045 * with two child prequisite constraints, on city and state, respectively. By setting min=1/max=2 at the top must occur
046 * constraint,
047 * and min=2/max=2 at the leaf constraint, this requirement can be enforced.
048 *
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 * @since 1.1
051 */
052@BeanTag(name = "mustOccurConstraint", parent = "MustOccurConstraint")
053public class MustOccurConstraint extends BaseConstraint {
054
055    private List<PrerequisiteConstraint> prerequisiteConstraints;
056    private List<MustOccurConstraint> mustOccurConstraints;
057    private Integer min;
058    private Integer max;
059
060    @BeanTagAttribute(name = "prerequisiteConstraints", type = BeanTagAttribute.AttributeType.LISTBEAN)
061    public List<PrerequisiteConstraint> getPrerequisiteConstraints() {
062        return prerequisiteConstraints;
063    }
064
065    public void setPrerequisiteConstraints(List<PrerequisiteConstraint> prerequisiteConstraints) {
066        this.prerequisiteConstraints = prerequisiteConstraints;
067    }
068
069    @BeanTagAttribute(name = "mustOccurConstraints", type = BeanTagAttribute.AttributeType.LISTBEAN)
070    public List<MustOccurConstraint> getMustOccurConstraints() {
071        return mustOccurConstraints;
072    }
073
074    public void setMustOccurConstraints(List<MustOccurConstraint> occurs) {
075        this.mustOccurConstraints = occurs;
076    }
077
078    @BeanTagAttribute(name = "min")
079    public Integer getMin() {
080        return min;
081    }
082
083    public void setMin(Integer min) {
084        this.min = min;
085    }
086
087    @BeanTagAttribute(name = "max")
088    public Integer getMax() {
089        return max;
090    }
091
092    public void setMax(Integer max) {
093        this.max = max;
094    }
095
096    /**
097     * Validates different requirements of component compiling a series of reports detailing information on errors
098     * found in the component.  Used by the RiceDictionaryValidator.
099     *
100     * @param tracer Record of component's location
101     */
102    @Override
103    public void completeValidation(ValidationTrace tracer) {
104        tracer.addBean("MustOccurConstraint", getMessageKey());
105
106        if (getMax() <= 0) {
107            String currentValues[] = {"max =" + getMax()};
108            tracer.createWarning("Max must be greater than 0", currentValues);
109        }
110
111        if (getPrerequisiteConstraints() == null) {
112            String currentValues[] = {"prerequisiteConstraints =" + getPrerequisiteConstraints()};
113            tracer.createWarning("PrerequisiteConstraints cannot be null or empty", currentValues);
114        } else if (getPrerequisiteConstraints().size() == 0) {
115            String currentValues[] = {"prerequisiteConstraints.size =" + getPrerequisiteConstraints().size()};
116            tracer.createWarning("PrerequisiteConstraints cannot be null or empty", currentValues);
117            ;
118        } else {
119            for (int i = 0; i < getPrerequisiteConstraints().size(); i++) {
120                getPrerequisiteConstraints().get(i).completeValidation(tracer.getCopy());
121            }
122        }
123
124        if (getMustOccurConstraints() == null) {
125            String currentValues[] = {"mustOccurConstraints =" + getMustOccurConstraints()};
126            tracer.createWarning("MustOccurConstraints cannot be null or empty", currentValues);
127        } else if (getMustOccurConstraints().size() == 0) {
128            String currentValues[] = {"mustOccurConstraints.size =" + getMustOccurConstraints().size()};
129            tracer.createWarning("MustOccurConstraints cannot be null or empty", currentValues);
130        } else {
131            for (int i = 0; i < getMustOccurConstraints().size(); i++) {
132                getMustOccurConstraints().get(i).completeValidation(tracer.getCopy());
133            }
134        }
135
136        super.completeValidation(tracer.getCopy());
137    }
138}