001    /*
002     * Copyright 2002-2006 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 java.util.Arrays;
019    import java.util.Collection;
020    
021    import org.springframework.binding.PropertyAccessStrategy;
022    import org.springframework.rules.constraint.InGroup;
023    import org.springframework.util.Assert;
024    
025    /**
026     * Property constraint which works like {@link InGroup} constraint but allows
027     * using a dynamic value list to determine if a property value is in a group of
028     * values.
029     * <p>
030     * To use it a second property field is required which contains the values for
031     * the 'in group' test
032     * 
033     * @author Mathias Broekelmann
034     * 
035     */
036    public class PropertyInGroupConstraint extends AbstractPropertyConstraint {
037    
038            public final String groupPropertyName;
039    
040            /**
041             * @param propertyName the property which contains the value to test against
042             * the group values
043             * @param groupPropertyName the property which contains the group values
044             */
045            public PropertyInGroupConstraint(String propertyName, String groupPropertyName) {
046                    super(propertyName);
047                    Assert.notNull(groupPropertyName, "The groupPropertyName to constrain is required");
048                    this.groupPropertyName = groupPropertyName;
049            }
050    
051            public boolean isDependentOn(String propertyName) {
052                    return super.isDependentOn(propertyName) || getGroupPropertyName().equals(propertyName);
053            }
054    
055            public String getGroupPropertyName() {
056                    return groupPropertyName;
057            }
058    
059            protected boolean test(PropertyAccessStrategy accessor) {
060                    Object propertyValue = accessor.getPropertyValue(getPropertyName());
061                    Collection values = getValues(accessor.getPropertyValue(getGroupPropertyName()));
062                    return values != null && values.contains(propertyValue);
063            }
064    
065            private Collection getValues(Object value) {
066                    if (value == null) {
067                            return null;
068                    }
069                    if (value instanceof Collection) {
070                            return (Collection) value;
071                    }
072                    if (value instanceof Object[]) {
073                            return Arrays.asList((Object[]) value);
074                    }
075                    throw new IllegalArgumentException("property " + getGroupPropertyName()
076                                    + " must contain a collection or an object array");
077            }
078    
079    }