Bean Container iteration

Hi:

I have a Bean Container, and before adding a new bean item, I want to check if a previous bean already has a certain property (not the index/id)…

For example, for the following table:

ID—Name—property 1—property 2—property 3
1—Item A—1—0—0
2—Item B—1—0—0

I want to add the following bean:

Item A—0—1—0

The result will be:

1—Item A—1—0—0
2—Item B—1—0—0
3—Item A—0—1—0

but I want it to be:

1—Item A—1—1—0
2—Item B—1—0—0

how can I do this??

regards,

Hugo

Hi,

As far as I am aware, the only way is get all of the ItemIds, iterate, get the item for each one, and check the property. Here’s a little search function - so you’d have to check the container for the given propertyId/value before you add it.

You could also subclass BeanContainer and perform this checking in addItem, if you wanted to do this in several places.

Cheers,

Charles

  private Item findItem(Container container, String propertyId, Object value) {
    for (Object itemId : container.getItemIds()) {
      Item item = container.getItem(itemId);
      Object propertyValue = item.getItemProperty(propertyId).getValue();
      // Do Null Checks Here
      if(propertyValue.equals(value)){
        return item;
      }
    }
    return null;
  }

thanks Charles, it worked perfect…

Lovely!