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.
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.