Accessing nested Item properties within a RepositoryFormHandler programatically

Home/Java/ATG/Accessing nested Item properties within a RepositoryFormHandler programatically

If you have a RepositoryItem which has a collection of other RepositoryItems as a property, editing things on them via a single form and RepositoryFormHandler can pose some difficulties.

For instance, you have an Item called Garage, and it has a List of Car RepositoryItems as a property named cars. If you are using a different type of collection, some of the following will need to be adjusted accordingly, but the overall approach is the same. A Car Item has a name and an option picture as properties. If you have a form to display and allow for editing of a Garage, including all of its cars, which is handled by a sub-class of a RepositoryFormHandler, you can edit values on both the Garage, as well as the Cars within cars, without much difficulty.

What gets tricky is if you need to access form submission values for properties on the Cars. For instance if you wanted to allow a user to delete a car by simply blanking it’s name, or if you wanted to automatically assign a picture based on the make and model in the name field. You can’t just access the submitted but un-updated values (say from within the preUpdateItem method) using simple nested getValueProperties. Here is what you can do:

First you need to get the List of cars. This is actually an instance of a RepositoryFormList.

RepositoryFormList carsFormList = (RepositoryFormList) getValueProperty("cars");

This is a subclass of ArrayList, and can be iterated through to get at each car’s incoming values. Remember we aren’t dealing with the actual RepositoryItems, that would be easy, this is what to do in order to grab the form submitted values destined for properties on these sub-Items in the preUpdateItem life-cycle phase. The contents of this ArrayList are RepositoryFormHashtables.

Iterator iter = carsFormList.iterator();
while (iter.hasNext()) {
RepositoryFormHashtable rfh = (RepositoryFormHashtable) iter.next();
....

The incoming property values are in this Hashtable, keyed by the property’s CAPITALIZED name:

String incomingName = (String) rfh.get("NAME");

Now that you have the name you can see if it’s blank and then delete the related car Item (hint: the repositoryId of the car Item this set of properties is destined for is in the Hashtable with the key of “REPOSITORYID”), or parse it and search for a normalized version, or find a related image automatically, or whatever else you wanted to do with it prior to the actual handleUpdate phase.

You can also get the RepositoryItem by calling:
RepositoryItem rfhitem = rfh.getRepositoryItem();

To be on the safe side your best bet is validate that and do something like this:

final RepositoryItem rfhitem = rfh.getRepositoryItem();
MutableRepositoryItem carItem = null;
if (rfhitem instanceof MutableRepositoryItem) {
carItem = (MutableRepositoryItem) rfhitem;
} else {
if (!(rfh.isEmpty() || ((rfh.size() == 1) && (rfh.get("REPOSITORYID") != null)))) {
try {
carItem = this.getRepository().getItemForUpdate(rfhitem.getRepositoryId(),
MyConstants.CAR_ITEM_DESCRIPTOR);
} catch (final RepositoryException e) {
if (isLoggingError()) {
logError(
"GarageFormHandler.handleUpdate: repository error while accessing an car.",
e);
}
}
}
}

Then you can access the existing car Item in order to delete it, or compare values to the incoming values, etc…

By | 2017-05-18T15:18:53+00:00 August 27th, 2007|ATG|5 Comments

About the Author:

5 Comments

  1. Renji October 26, 2013 at 5:58 pm - Reply

    How do you set values to list of objects (multi) from the page? By reading this I was able to set and access direct item properties but not multi values.

    • Devon October 26, 2013 at 7:50 pm - Reply

      I’m not sure I understand? Can you give me a specific example or some code?

      • Renji October 27, 2013 at 12:06 pm - Reply

        If I go by your eg. Suppose I need to set values to garage as well as all cars at the same form. I guess car is a associated with garage as a multi list in repository With component item type. In this case I was not able to set values to cars from a single form like car[1].name. Basically not able to set values to repositoryformlist

        • Devon October 27, 2013 at 8:45 pm - Reply

          Renji,

          It may just be the late hour (and being stuck in an airport), but I’m having a hard time figuring out the exact JSP form->Code scenario you’re describing. I think you’re going to have to write some custom code to handle things, but I can’t tell you what that code should look like… Sorry I can’t be more helpful.

          • Renji October 27, 2013 at 8:59 pm - Reply

            Thanks Devon. I was trying to do without custom code, but it seems impossible without some custom code. I will try the custom way.. but the API doc on repositoryformlist keeps me bugging that there may be a way to do it as they have mentioned – “It has additional sub-properties that give you more flexibility when editing a list”. Thanks for your input.

Leave A Comment