Announcement Announcement Module
Collapse
No announcement yet.
When is a property visible via model.hasProperty? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • When is a property visible via model.hasProperty?

    Hi

    I have a weird issue... calling model.hasProperty("price") returns false but my class has getPrice/setPrice and the model was created via:
    model = FormModelHelper.createCompoundFormModel(myObject, "myObjectModel");

    Code:
    class MyObject {
       private BigDecimal price;
       public void setPrice(BigDecimal bd);
       prublic BigDecimal getPrice();...
    ...
    }
    
    MyObject myObject = new .... // and set some values
    
    ValidatingFormModel = model = FormModelHelper.createCompoundFormModel(myObject, "myObjectModel");
    
    ...
    model.hasProperty("price"); // RETURNS FALSE.... why????
    It seems that this is because this is a hidden property, ie not shown on the form? Is it a normal behaviour?

    I could have a piece of logic that needs to change something in the model, invisibly... like deducting a price from a set of other parameters... but I do not need to show it.

    Has anyone seen this before?

    thanks

    Benoit

  • #2
    Hi Benoit.

    I have not run into this before cause I have not needed to use it. However looking at the code it certainly seems that it is checking only those properties that are added to the form and have ValueModels associated with them.

    You might try this to get the price in the FormModel but not need to show it on the screen in a form. Then model.hasProperty("price") will return true;

    Code:
    FormModel model = getFormModel();
    model.add("price");
    HTH
    Trevor

    Comment


    • #3
      Hi Trevor,

      Thanks for your reply.

      Unfortunately this is not an option to me for 2 reasons:
      1/ I wanted to use this to actually detect whether the object behind the model had that property and if it did then do something, I did not want to force the model having that property...
      2/ Adding a property that does not exist seems to *really* mess up the model and cause all sorts of issues after a commit...

      Shouldn't hasProperty() return when it is in the model (regardless whether there is a ValueModel??? could it be considered as a bug???

      Thanks

      Benoit

      Comment


      • #4
        Hi Benoit.

        My understanding of this stuff is imperfect at best, but I will try to explain how I see it working. I am sure somebody much more knowledgeable than I can correct me.

        When you create a FormModel you give it a bean to create the model around. This in itself is quite useless. The model is not really aware of the properties of that bean that it is managing yet. Therefore you have have to tell the model what properties of this bean you would like it to manage. This is done through the add(String propertyName) of the form model. When this is called all kinds of stuff is done one of which is add a commit trigger to the ValueModel associated with that property and also add that property and its ValueModel to the propertyValueModels map. It is this map that the hasProperty(String propertyName) consults when the method is called.

        This is what happens when you use TableFormBuilder, it creates a binding for the property. In AbstractBinding the following is called on the formModel
        Code:
        this.propertyMetadata = this.formModel.getPropertyMetadata(formPropertyPath);
        getPropertyMetadata calls add(String propertyName) If it does not find existing meta data,creating the meta data and adding that property to the list of properties that you want the model to manage.

        So I guess to answer your question about it being a bug. I would have to say no. It seems to be behaving exactly as it is supposed to.

        If you want to detect if the object behind the model has a property , then you could probably do this. Of course if that property is being handled by the model then hasProperty(String propertyname) should return true.

        If you want to set values on that property then you will be bypassing all the nice ValueModel buffering and stuff. It would be like issuing a commit for that property.
        Code:
        BeanWrappel bw = new BeanWrapperImpl(getFormModel().getFormObject());
        if (bw.isReadableProperty("foo") && bw.isWritableProperty("foo")) {
               ......Do something
        }
        HTH
        Trevor

        Comment


        • #5
          Hi Trevor

          Thanks for your reply. I understand what you're saying and will use the suggested method.

          One point though, if you call model.add("aPropertyThatDoesNotExist") this seems to badly screw up the model for revert/commit.

          Best regards

          Benoit

          Comment


          • #6
            Hi Benoit,

            Trevor, despite his modesty, has it exactly right. The form model is a layer on top of the bean being handled and it only knows about the properties that have been added to it to manage. This ensures that any two calls to getValueModel get the same object for a given property and all the value models are linked to the commit/revert handling. The hasProperty is a test on the form model itself, not the underlying bean.

            Trevor also provides a good mechanism to see if the bean object has the desired property. BeanWrapperImpl contains a lot of support for property editors that you may not need. Another simple way would be to use the apache commons BeanUtils class (the "getSimpleProperty") method and see if it throws a NoSuchMethodException.

            Larry.

            Comment


            • #7
              Indeed, Trevor's mechanism works very well. thanks!

              Comment

              Working...
              X