001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.rules.constraint.property;
017    
018    import org.springframework.rules.constraint.CompoundConstraint;
019    import org.springframework.rules.constraint.LogicalOperator;
020    import org.springframework.util.Assert;
021    import org.springframework.util.StringUtils;
022    
023    /**
024     * @author Keith Donald
025     */
026    public class RequiredIfOthersPresent extends RequiredIfTrue {
027    
028        /**
029         * Tests that the property is required if all "other properties" are present. Present
030         * means they are "non null."
031         * 
032         * @param otherPropertyNames to test
033         */
034        public RequiredIfOthersPresent( String propertyName, String[] otherPropertyNames ) {
035            this(propertyName, otherPropertyNames, LogicalOperator.AND);
036        }
037    
038        /**
039         * Tests that the property is required if "other properties" are present. Present
040         * means they are "non null." The operator parameter determines how the set of other
041         * property names is handled. If AND, then all must be present before the primary
042         * proeprty will be required. If OR, then if any of the other properties are present,
043         * then the primary property will be required. the logical operator, either AND or OR.
044         * 
045         * @param otherPropertyNames to test
046         * @param operator Either AND or OR.
047         */
048        public RequiredIfOthersPresent( String propertyName, String[] otherPropertyNames, LogicalOperator operator ) {
049            super(propertyName);
050            Assert.notNull(otherPropertyNames, "otherPropertyNames is required");
051            Assert.notNull(operator, "operator is required");
052            Assert.notEmpty(otherPropertyNames, "otherPropertyNames must consist of at least one name");
053    
054            CompoundConstraint compoundConstraint = operator.createConstraint();
055            CompoundPropertyConstraint propertyConstraint = new CompoundPropertyConstraint(compoundConstraint);
056            for( int i = 0; i < otherPropertyNames.length; i++ ) {
057                propertyConstraint.add(new PropertyPresent(otherPropertyNames[i]));
058            }
059            setConstraint(propertyConstraint);
060        }
061    
062        /**
063         * Tests that the property is required if all "other properties" are present. Present
064         * means they are "non null."
065         * 
066         * @param otherPropertyNames one or more other properties, delimited by commas.
067         */
068        public RequiredIfOthersPresent( String propertyName, String otherPropertyNames ) {
069            this(propertyName, otherPropertyNames, LogicalOperator.AND);
070        }
071    
072        /**
073         * Tests that the property is required if all or any of the "other properties" are
074         * present.
075         * 
076         * @param otherPropertyNames one or more other properties, delimited by commas.
077         * @param operator the logical operator, either AND or OR.
078         */
079        public RequiredIfOthersPresent( String propertyName, String otherPropertyNames, LogicalOperator operator ) {
080            this(propertyName, StringUtils.commaDelimitedListToStringArray(otherPropertyNames), operator);
081        }
082    
083        public boolean isDependentOn( String propertyName ) {
084            return getPropertyName().equals(propertyName)
085                    || ((CompoundPropertyConstraint) getConstraint()).isDependentOn(propertyName);
086        }
087    
088        public boolean isCompoundRule() {
089            return true;
090        }
091    }