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.binding.PropertyAccessStrategy; 019 import org.springframework.rules.closure.BinaryConstraint; 020 import org.springframework.util.Assert; 021 022 /** 023 * A constraint that returns the result of a <code>boolean</code> 024 * expression that tests two variable bean property values. For example, 025 * <code>pet.ageAtFirstVisit > pet.currentAge</code> 026 * 027 * @author Keith Donald 028 */ 029 public class PropertiesConstraint extends AbstractPropertyConstraint { 030 031 private String otherPropertyName; 032 033 private BinaryConstraint beanPropertyExpression; 034 035 /** 036 * Creates a BeanPropertyExpression 037 * 038 * @param propertyName 039 * The first property participating in the expression. 040 * @param beanPropertyExpression 041 * The expression predicate that will test the two bean property 042 * values. 043 * @param otherPropertyName 044 * The second property participating in the expression. 045 */ 046 public PropertiesConstraint(String propertyName, BinaryConstraint beanPropertyExpression, String otherPropertyName) { 047 super(propertyName); 048 Assert.notNull(otherPropertyName, "otherPropertyName is required"); 049 Assert.notNull(beanPropertyExpression, "beanPropertyExpression is required"); 050 this.otherPropertyName = otherPropertyName; 051 this.beanPropertyExpression = beanPropertyExpression; 052 } 053 054 public boolean isCompoundRule() { 055 return true; 056 } 057 058 public boolean isDependentOn(String propertyName) { 059 return getPropertyName().equals(propertyName) || getOtherPropertyName().equals(propertyName); 060 } 061 062 public String getOtherPropertyName() { 063 return otherPropertyName; 064 } 065 066 public BinaryConstraint getConstraint() { 067 return beanPropertyExpression; 068 } 069 070 protected boolean test(PropertyAccessStrategy domainObjectAccessStrategy) { 071 return beanPropertyExpression.test(domainObjectAccessStrategy.getPropertyValue(getPropertyName()), 072 domainObjectAccessStrategy.getPropertyValue(getOtherPropertyName())); 073 } 074 075 public String toString() { 076 return super.toString() + " " + beanPropertyExpression.toString() + " " + otherPropertyName; 077 } 078 079 }