16 Jul 2011

Using attribute's custom properties in model layer

Sometimes we need to change properties of the VO's attributes dynamically at run time, depending on some business logic. Sometimes we need  to pass some extra information from the model layer to the view layer. We can use attributes' custom properties.

For example, I need to change attribute's label and make it readonly on the fly. In my ViewObjectImpl class I have the following method:

 public void setCustomHints() {      
     AttributeDefImpl attr = ((AttributeDefImpl) findAttributeDef("DepartmentName"));
     attr.setProperty("customLabel", "The custom label");
     attr.setProperty("readonly", Boolean.TRUE);
 }

The setCustomHints method is going to be executed on a commandButton's action. And the  jspx page contains the following piece of code:

          <af:inputText value="#{bindings.DepartmentName.inputValue}"
                       
                        label="#{bindings.DepartmentName.hints.customLabel}"
                        readonly="#{bindings.DepartmentName.hints.readonly}"
                       
                        columns="#{bindings.DepartmentName.hints.displayWidth}"
                        maximumLength="#{bindings.DepartmentName.hints.precision}"
                        shortDesc="#{bindings.DepartmentName.hints.tooltip}"
                        id="it4" partialTriggers="cb1">
            <f:validator binding="#{bindings.DepartmentName.validator}"/>
          </af:inputText>
          <af:commandButton actionListener="#{bindings.setCustomHints.execute}"
                            text="setCustomHints"
                            disabled="#{!bindings.setCustomHints.enabled}"
                            id="cb1"/>

Additionally, I set up default values for customLabel and readonly custom properties at the VO's definition page:


And finally, we've got the following result:













The sample application for this post is available.

2 comments:

  1. Eugene,

    Nice post - however for the specific use case (determine at runtime if the attribute should be updatable) - you should just override isAttributeUpdatable()

    The concept is a good one, though

    :)

    john

    ReplyDelete
  2. Hi John!
    Yes, you are right. But this is just an example of passing a value for any attribute from the model layer.

    ReplyDelete

Post Comment