Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

DefaultValueCalculator and Recalculating Values

Sean
2013-08-31
2013-09-02
  • Sean
    Sean
    2013-08-31

    Hi. I am using a calculator I have created in my project to calculate a person's age. This value will only be displayed in the user interface and will not persisted in the database. I used @DefaultValueCalculator to set the age property for display to the user and that works fine. However, I am having trouble getting this value to update if the date of birth (dob) is changed. Below is the code from the class for the entity. The ApplicantAge class is the name of the calculator saved in my project to calculate age. Again, the below works for displaying the age based on a date entered in the 'dob' field. I thought @Depends would cause the 'age' field to be updated whenever 'dob' is changed, but that's not the case.

    @DefaultValueCalculator(value=ApplicantAge.class,
            properties=@PropertyValue(
                    name="dob",
                    from="dob")
            )
    private int age;
    
    @Depends("dob")
    public int getAge() {
        return age;
    }
    

    Thanks in advance,
    Sean

     
  • Javier Paniza
    Javier Paniza
    2013-09-02

    Hi Sean,

    @DefaultValueCalculator is for giving the very initial value to a property, usually a persistent one, so when the property already have value it does not work.

    I think you're looking for a calculated property:

    // private int age; // Without attribute
    
    @Depends("dob")
    public int getAge() {
        int age = ... // Calculating
        ... // age
        return age;
    }
    

    Help others in this forum as I help you.
    Need more help? Get OpenXava professional support.