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.krms.impl.validation;
017
018import org.kuali.rice.krms.framework.engine.Action;
019import org.kuali.rice.krms.framework.engine.BasicRule;
020import org.kuali.rice.krms.framework.engine.Proposition;
021import org.kuali.rice.krms.framework.type.ValidationRuleType;
022
023import java.util.List;
024
025/**
026 *
027 * A {@link org.kuali.rice.krms.framework.engine.Rule} that executes a {@link org.kuali.rice.krms.framework.engine.Action} when the {@link Proposition} is false,
028 * as opposed to {@link BasicRule} which executes its action when the proposition is true.
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class ValidationRule extends BasicRule {
033    private ValidationRuleType type = null;
034
035    /**
036     *
037     * @param type ValidationRuleType
038     * @param name Rule name
039     * @param proposition Proposition
040     * @param actions Rule Actions
041     * @throws IllegalArgumentException if type is null
042     */
043    public ValidationRule(ValidationRuleType type, String name, Proposition proposition, List<Action> actions) {
044        super(name, proposition, actions);
045        if (type == null) throw new IllegalArgumentException("type must not be null");
046        this.type = type;
047    }
048
049    /**
050     * Valid Validation Rules execute when the ruleExecutionResult is false.
051     * Invalid Validation Rules execute when the ruleExecutionResult is true.
052     * @param ruleExecutionResult result of the rules execution
053     * @return should the actions be executed
054     */
055    @Override
056    protected boolean shouldExecuteAction(boolean ruleExecutionResult) {
057        if (type == null || type.equals(ValidationRuleType.VALID)) {
058            return !ruleExecutionResult;
059        }
060        return ruleExecutionResult;
061    }
062}