1

I have a Jspresso application using listeners on my entity. The problem is that this listener isn't invoked when I modify the listened property.

Here is the listener defintion:

public WagonTransportOrderExtension(final WagonTransportOrder component) {

PropertyChangeListener nbVehiclesListener = new PropertyChangeListener() {

  @SuppressWarnings("unchecked")
  public void propertyChange(PropertyChangeEvent evt) {
    Integer nbVehicles = 0;
    Integer oldValue = getComponent().getNbVehiclesPersisted();

    for(LoadDetail detail : (Collection<LoadDetail>)evt.getNewValue()) {
      nbVehicles += detail.getQuantity();
    }

    getComponent().setNbVehiclesPersisted(nbVehicles);
    getComponent().firePropertyChange(
        WagonTransportOrder.NB_VEHICLES_PERSISTED, oldValue, nbVehicles);
  }
};

getComponent().addPropertyChangeListener(
    WagonTransportOrder.LOAD_DETAILS, nbVehiclesListener); 
}

And a simple use case of this entity:

WagonTransportOrder wagonTransportOrder = createEntityInstance(WagonTransportOrder)

Vehicle vehicle = createEntityInstance(Vehicle)
vehicle.setVin("00000000000000000")
save(vehicle)
wagonTransportOrder.addToLoadDetails(vehicle)

When debugging this, the addToLoadDetails() instruction does not lead to the invokation of the listener.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
Dealus
  • 25
  • 4

1 Answers1

0

The problem might be that an extension instance is lazily created when the first computed property implemented by this extension is accessed, e.g. the getter or the setter of the computed property is called.

So, as a general rule of thumb, extensions are not the good place to create and attach listeners to the entity (or component) instances unless those listeners are directly related to the computed properties implemented by this extension, in which case, the extension is obviously created.

If you always need these listeners, I would suggest to implement a life-cycle interceptor and add them (or re-attach them) in the onCreate / onClone / onLoad methods.

  • 1
    This solution works fine, but a side effect is that the listener is triggered on entity loading, which isn't always wanted as you said. I've choosen to implement a processor instead of a listener for this case as it makes more sence to only change my nbVehiclesPersisted when the collection is changed. Because it isn't exactly a computed property but a denormalization of a computed property which is not always used. – Dealus Apr 08 '15 at 08:33
  • The listener should not be called when attaching it. Do you see a different behavior ? Using a processor is definitely a serious alternative. – Vincent Vandenschrick Apr 09 '15 at 08:17