001/*
002 * Copyright 2008 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.ole.sys.document.validation.impl;
017
018import java.util.List;
019
020import org.kuali.ole.sys.document.validation.Validation;
021import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
022
023
024/**
025 * This validation represents a hiearchy of validations
026 */
027public class CompositeValidation implements Validation {
028    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CompositeValidation.class);
029    
030    protected List<Validation> subValidations;
031    protected boolean shouldQuitOnFail = false;
032
033    /**
034     * Validates each sub-validation in turn, returning the cumulative effect of each
035     * @see org.kuali.ole.rules.Validation#validate(java.lang.Object[])
036     */
037    public boolean validate(AttributedDocumentEvent event) {
038        if (subValidations == null) {
039            throw new IllegalStateException("A composite validation must have children validations");
040        }
041        
042        boolean result = true;
043        boolean currResult;
044        
045        for (Validation validation: subValidations) {
046            currResult = validation.stageValidation(event);
047            result &= currResult;
048            
049            if(!currResult) {
050                if ( LOG.isDebugEnabled() ) {
051                    LOG.debug(validation.getClass() + " failed");
052                }
053            }
054            
055            if (!currResult && validation.shouldQuitOnFail()) {
056                break;
057            }
058        }
059        
060        return result;
061    }
062
063    /**
064     * Just call validate!
065     * @see org.kuali.ole.sys.document.validation.Validation#stageValidation(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
066     */
067    public boolean stageValidation(AttributedDocumentEvent event) {
068        return validate(event);
069    }
070
071    /**
072     * Returns the List of validations to test
073     * @return a List of validations to test
074     */
075    public List<Validation> getValidations() {
076        return this.subValidations;
077    }
078    
079    /**
080     * Sets the List of validations to test
081     * @param validations a List of validations to test against
082     */
083    public void setValidations(List<Validation> validations) {
084        this.subValidations = validations;
085    }
086
087    /**
088     * Gets the shouldQuitOnFail attribute. 
089     * @return Returns the shouldQuitOnFail.
090     */
091    public boolean shouldQuitOnFail() {
092        return shouldQuitOnFail;
093    }
094
095    /**
096     * Sets the shouldQuitOnFail attribute value.
097     * @param shouldQuitOnFail The shouldQuitOnFail to set.
098     */
099    public void setQuitOnFail(boolean shouldQuitOnFail) {
100        this.shouldQuitOnFail = shouldQuitOnFail;
101    }
102}