How to make the ItemPropertyChangeListener trigger only when specific property is changed

Is it possible to make the ItemProperyChangeListener only trigger when a specific property is changed.

In my use case. I have a dropdown that contains facilities, when the user picks a new facility, some values are pulled from the facility entity and set. However, those values may be incorrect, so the user should be able to edit the value to something new. The issue I am having, is any change to any property is triggering the ItemProperyChangeListener. Is there a way to specific which property it should be watching for a change?

This is what I have currently

@Override
public void init(Map<String, Object> params) {
    cl_CensusReportDs.addItemPropertyChangeListener(e -> bedValues());
}

private void bedValues() {
     Integer ab = getItem().getFacility().getActualBeds();
     Integer bb = getItem().getFacility().getBudgetedBeds();
     getItem().setActualBeds(ab);
     getItem().setBudgetedBeds(bb);
}

I think my solution might be to use the ValueChangeListener instead. However now get an unfetched attribute error when I attempt to open an item.

IllegalStateException: Cannot get unfetched attribute [actualBeds] from detached object 
com.company.chsforms.entity.Facility-2 [detached].

It is puzzling because when using the itemPropertyChangeListener, everything was working. All relevant views have the field necessary fields added.

@Named("fieldGroup.facilityField")
private LookupPickerField facilityField;
......
@Override
public void init(Map<String, Object> params) {
    facilityField.addValueChangeListener(e -> bedValues());
}

private void bedValues() {
    Integer ab = getItem().getFacility().getActualBeds();
    Integer bb = getItem().getFacility().getBudgetedBeds();
    getItem().setActualBeds(ab);
    getItem().setBudgetedBeds(bb);
}

ItemPropertyChangeListener should serve your purpose.
The above exception may be because your view doesn’t include ActualBeds, BudgetedBeds objects.

1 Like

Hi,

The ItemPropertyChangeEvent contains the information about the changed property:

if ("name".equals(e.getProperty()) {
  // do somthing
}

Regarding the exception, I agree with the suggestion above.

Regards,
Gleb

2 Likes

I believe this is exactly what I needed. As for the views, that issue only came up when attempting to use a value change listener. The views never changed.

Thanks!