Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Integer Constraints in RulesSource?

  1. #1

    Default Integer Constraints in RulesSource?

    I experiment with making RulesSource similar to PetClinicValidationRulesSource. In PetClinicValidationRulesSource, all Constraints are based on String properties in domain objects. I cannot find any example for other property types like Integer. Example, how can I do each of the following for Integer properties (assume each is an independent constraint...that is, don't combine them in your reply please)?

    1) required and must be positive Integer
    2) required and must be Integer between -180 and 180
    3) required and must be Integer greater than 100
    4) required and must be Integer in set (1, 3, 5, 7)
    5) NOT required but if filled in it must be between 5 and 10

    Please forgiving me if I miss something in Petclinic or in docs!
    Stefano Rossi

    "If I want your opinion, I'll give it to you."

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Code:
            Rules integerRules = new Rules(TestBean.class) {
                protected void initRules() {
                    add("number1", all(new Constraint[] { required(), gt(0) });
                    add("number2", range(-180, 180));
                    add("number3", all(new Constraint[] { required(), gt(100) });
                    add("number4", all(new Constraint[] { required(), inGroup(new Object[] { 1, 3, 5 7 });
                    add("number5", if(present(), between(6, 10)));
                }
    
            };
    Pretty slick huh?
    Keith Donald
    Core Spring Development Team

  3. #3

    Default

    Quote Originally Posted by kdonald
    Pretty slick huh?
    Well, yes, except some compiler error. :cry:

    I fix first four like this:
    Code:
    add("number1", all(new Constraint[] { required(), gt("number1", new Integer(0)) }));
    
    add("number2", inRange("number2", new Integer(-180), new Integer(180)));
    
    add("number3", all(new Constraint[] { required(), gt("number3", new Integer(100)) }));
    
    add("number4", all(new Constraint[] { required(), inGroup(new Object[] {new Integer(1), new Integer(3), new Integer(5), new Integer(7)})}));
    Seems first four correct now with my changes? Main thing is need to make int to Integer.

    But problem is fifth example. First, "if" is Java keyword so can't use like this for method name. Second, present() and between() don't seem to exist in lastest code from CVS. Maybe I look in wrong place but I thinks these two method should be in org.springframework.rules.factory.Constraints class, no? Maybe you not check in yet? Or maybe not coming across in anonymous CVS yet because propagation delay?

    Please suggest help?
    Stefano Rossi

    "If I want your opinion, I'll give it to you."

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Steve--um, I haven't yet added those convenience methods to the Constraints factory yet. I should've mentioned!

    Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint! (I'll put a check in constrants that won't allow that to happen).

    I'm going to add them now ;-)
    Keith Donald
    Core Spring Development Team

  5. #5

    Default

    Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint! (I'll put a check in constrants that won't allow that to happen).
    Hmmn. Please explain?
    Stefano Rossi

    "If I want your opinion, I'll give it to you."

  6. #6

    Default

    Quote Originally Posted by kdonald
    Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint!
    Maybe you talking about how I added property names to constraint factory methods? For example I changed gt(0) to gt("number1", new Integer(0)) })...I added "number1" parameter here. If I don't add, it does not compiles. I do note that the one parameter version of gt() is actually declared in PropertyConstraints. However, DefaultRulesSource extends Constraints, not PropertyConstraints. So maybe this is the problem? How to fix?
    Stefano Rossi

    "If I want your opinion, I'll give it to you."

  7. #7
    Join Date
    Sep 2004
    Location
    Vancouver, BC, Canada
    Posts
    135

    Default

    Quote Originally Posted by steve_smith
    But problem is fifth example. First, "if" is Java keyword so can't use like this for method name.
    Keith -- I concur with Stefano. AFAIK you cannot use "if" as a method name. Perhaps you could call it "iff"...but this might be confusing because "iff" is a common abbreviation for "if and only if" in mathematics. Maybe "if_"?
    Cheers,
    Joe
    "All your bean are belong to us" - Spring Framework's IOC Container

  8. #8
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Guys, that was psudo code for convenience methods we should add.

    Yes, if() is invalid.

    I'm going with ifTrue()

    gimme a sec and I'll check it all in!!
    Keith Donald
    Core Spring Development Team

  9. #9
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Here are the new methods

    Code:
    	public Constraint eq(Object value) {
    		return getConstraints().eq(value);
    	}
    
    	public Constraint eq(int value) {
    		return getConstraints().eq(value);
    	}
    
    	public Constraint eq(Object value, Comparator comparator) {
    		return getConstraints().eq(value, comparator);
    	}
    
    	public Constraint gt(Comparable value) {
    		return getConstraints().gt(value);
    	}
    
    	public Constraint gt(int value) {
    		return getConstraints().gt(value);
    	}
    
    	public Constraint gt(Comparable value, Comparator comparator) {
    		return getConstraints().gt(value);
    	}
    
    	public Constraint gte(Comparable value) {
    		return getConstraints().gte(value);
    	}
    
    	public Constraint gte(int value) {
    		return getConstraints().gte(value);
    	}
    
    	public Constraint gte(Comparable value, Comparator comparator) {
    		return getConstraints().gte(value, comparator);
    	}
    
    	public Constraint lt(Comparable value) {
    		return getConstraints().lt(value);
    	}
    
    	public Constraint lt(int value) {
    		return getConstraints().lt(value);
    	}
    
    	public Constraint lt(Comparable value, Comparator comparator) {
    		return getConstraints().lt(value, comparator);
    	}
    
    	public Constraint lte(Comparable value) {
    		return getConstraints().lte(value);
    	}
    
    	public Constraint lte(int value) {
    		return getConstraints().lte(value);
    	}
    
    	public Constraint lte(Comparable value, Comparator comparator) {
    		return getConstraints().lte(value, comparator);
    	}
    
    	public Constraint range(Comparable min, Comparable max) {
    		return getConstraints().range(min, max);
    	}
    
    	public Constraint range(Object min, Object max, Comparator comparator) {
    		return getConstraints().range(min, max, comparator);
    	}
    
    	public Constraint range(int min, int max) {
    		return getConstraints().range(min, max);
    	}
    
    	public Constraint between(Comparable min, Comparable max) {
    		return getConstraints().between(min, max);
    	}
    
    	public Constraint between(Object min, Object max, Comparator comparator) {
    		return getConstraints().between(min, max, comparator);
    	}
    
    	public Constraint between(int min, int max) {
    		return getConstraints().between(min, max);
    	}
    
    	public Constraint present() {
    		return getConstraints().present();
    	}
    
    	public Constraint ifTrue(Constraint constraint, Constraint mustAlsoBeTrue) {
    		return getConstraints().ifTrue(constraint, mustAlsoBeTrue);
    	}
    
    	public Constraint ifTrueElse(Constraint constraint, Constraint mustAlsoBeTrue, Constraint elseMustAlsoBeTrue) {
    		return getConstraints().ifTrue(constraint, mustAlsoBeTrue, elseMustAlsoBeTrue);
    	}
    This is ConstraintsAccessor, which Rules, RulesSource and AbstractConstraint extend, for convenience constraints building. It'll be CVS'ed shortly.
    Keith Donald
    Core Spring Development Team

  10. #10

    Default

    Okay this is great! Now you allowed to take 15 minute break for Starbucks coffee! Maybe I let you have pumpkin scone too. :lol:
    Stefano Rossi

    "If I want your opinion, I'll give it to you."

Similar Threads

  1. JdbcTemplate and Integer Columns
    By jkookie in forum Data
    Replies: 3
    Last Post: Aug 9th, 2005, 09:58 AM
  2. Replies: 6
    Last Post: May 30th, 2005, 07:11 AM
  3. Replies: 2
    Last Post: Nov 5th, 2004, 03:47 PM
  4. Boolean, Date, Integer Fields on Form
    By steve_smith in forum Swing
    Replies: 2
    Last Post: Oct 27th, 2004, 01:34 AM
  5. PropertyEditorRegistry, RulesSource, ...
    By besbello in forum Swing
    Replies: 2
    Last Post: Aug 30th, 2004, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •