Why do we need to extend a component??
Consider a situation, when there is a OOTB component. By layering configuration files we can only change the existing property values of the component. But what if we need to add some new properties to the same component? Or there could be situation when we need to add some methods to an existing component Or override the method of existing component. These tasks cannot be done alone by configuration files, addition of new properties/methods requires class-level modifications. To accomplish this we extend the component.
The answer is even simpler..
For example, if we need to extend OOTB ProfileFormHandler component, we should know that its nucleus path is :
Now, let us assume, that our component "Person" existing at location "/atg/commerce/Person" and is out of the box. So, our assumed "Person" component is same as we discussed in ART#104 - ATG Components. Now our assumed out-of-the-box "Person" component's properties file looks like (this can be seen in dyn/admin):-
$class=com.mypackage.Person name=Stephen age=20
Now that we know where our component exists, and what are its properties, we need to follow the below steps:-
- Extend the class: Since addition of properties/methods has to be done at class level, we need to extend the class this component is referring to. Next, we have to extend it and add our custom code. Our OOTB component "Person" refers to com.mypackage.Person class. Therefore, we will now extend Person class and add our custom code. In the below example, we are adding an additional property "gender" to our extended class.
- Now that we have extended the person class, we need to do two things:-
- Layer our configuration file at the same location as OOTB component. i.e. /atg/commerce/Person (in our module's config-path).
- Override the $class nucleus property to use our custom (extended) class, to tell the component to use extended class instead of the original class; so that the new property "gender" is accommodated. (New methods can also be written in this class OR existing methods of "Person" class can be overridden if needed)
- Next, we provide the value to our very own newborn variable "gender".
Now, our OOTB atg component uses our custom class, with newly set-properties. Similarly, other components can be easily extended.
Let us move on.. and understand a new topic of ATG, known as droplets or dynamo servlet beans...