use of com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesAddedEvent in project VaadinUtils by rlsutton1.
the class ChildCrudView method commitContainerWithHooks.
/**
* commits the container and retrieves the new recordid
*
* we have to hook the ItemSetChangeListener to be able to get the database
* id of a new record.
*/
private void commitContainerWithHooks() {
// call back to collect the id of the new record when the container
// fires the ItemSetChangeEvent
ItemSetChangeListener tmp = new ItemSetChangeListener() {
private static final long serialVersionUID = -4893881323343394274L;
@Override
public void containerItemSetChange(ItemSetChangeEvent event) {
if (event instanceof ProviderChangedEvent) {
@SuppressWarnings("rawtypes") ProviderChangedEvent pce = (ProviderChangedEvent) event;
@SuppressWarnings("unchecked") EntityProviderChangeEvent<E> changeEvent = pce.getChangeEvent();
if (changeEvent instanceof EntitiesAddedEvent) {
Collection<E> affectedEntities = changeEvent.getAffectedEntities();
eventHandler.entitiesAdded(affectedEntities);
}
if (changeEvent instanceof EntitiesUpdatedEvent) {
Collection<E> affectedEntities = changeEvent.getAffectedEntities();
eventHandler.entitiesUpdated(affectedEntities);
}
if (changeEvent instanceof EntitiesRemovedEvent) {
Collection<E> affectedEntitys = changeEvent.getAffectedEntities();
eventHandler.entitiesDeleted(affectedEntitys);
}
}
}
};
final LinkedList<ItemSetChangeListener> listeners = new LinkedList<>(container.getItemSetChangeListeners());
try {
// get existing listeners and remove them
for (ItemSetChangeListener listener : listeners) {
container.removeItemSetChangeListener(listener);
}
// add the hook listener
container.addItemSetChangeListener(tmp);
// call commit
container.commit();
} catch (Exception e) {
ErrorWindow.showErrorWindow(e);
} finally {
// detach the hook listener
container.removeItemSetChangeListener(tmp);
// restore the existing listeners
for (ItemSetChangeListener listener : listeners) {
container.addItemSetChangeListener(listener);
}
}
}
Aggregations