use of com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent in project VaadinUtils by rlsutton1.
the class BaseCrudView method commitContainerAndGetEntityFromDB.
/**
* 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 E commitContainerAndGetEntityFromDB() {
// don't really need an AtomicReference, just using it as a mutable
// final variable to be used in the callback
final AtomicReference<E> entityReference = new AtomicReference<>();
// call back to collect the id of the new record when the container
// fires the ItemSetChangeEvent
ItemSetChangeListener tempListener = new ItemSetChangeListener() {
private static final long serialVersionUID = 8086546233136795406L;
@Override
public void containerItemSetChange(ItemSetChangeEvent event) {
if (event instanceof ProviderChangedEvent) {
@SuppressWarnings("rawtypes") ProviderChangedEvent pce = (ProviderChangedEvent) event;
@SuppressWarnings("unchecked") Collection<E> affectedEntities = pce.getChangeEvent().getAffectedEntities();
if (affectedEntities.size() > 0) {
@SuppressWarnings("unchecked") E entity = (E) affectedEntities.toArray()[0];
entityReference.set(entity);
}
}
}
};
// Only remove the listeners if we are editing an existing entity. If it
// is a new entity then we need the listeners to update components with
// the new entity.
final LinkedList<ItemSetChangeListener> listeners = new LinkedList<>();
if (newEntity == null) {
listeners.addAll(container.getItemSetChangeListeners());
}
try {
// get existing listeners and remove them
for (ItemSetChangeListener listener : listeners) {
container.removeItemSetChangeListener(listener);
}
// add the temp listener
container.addItemSetChangeListener(tempListener);
// call commit
container.commit();
entityReference.set(EntityManagerProvider.getEntityManager().merge(entityReference.get()));
} catch (Exception e) {
ErrorWindow.showErrorWindow(e);
} finally {
// detach the temp listener
container.removeItemSetChangeListener(tempListener);
// restore the existing listeners
for (ItemSetChangeListener listener : listeners) {
container.addItemSetChangeListener(listener);
}
}
// return the entity
return entityReference.get();
}
use of com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent in project VaadinUtils by rlsutton1.
the class SingleEntityWizardStep method commitContainerAndGetEntityFromDB.
/**
* 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 E commitContainerAndGetEntityFromDB() {
// don't really need an AtomicReference, just using it as a mutable
// final variable to be used in the callback
final AtomicReference<E> newEntity = new AtomicReference<>();
// 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 = 9132090066374531277L;
@Override
public void containerItemSetChange(ItemSetChangeEvent event) {
if (event instanceof ProviderChangedEvent) {
@SuppressWarnings("rawtypes") ProviderChangedEvent pce = (ProviderChangedEvent) event;
@SuppressWarnings("unchecked") Collection<E> affectedEntities = pce.getChangeEvent().getAffectedEntities();
if (affectedEntities.size() > 0) {
@SuppressWarnings("unchecked") E id = (E) affectedEntities.toArray()[0];
newEntity.set(id);
}
}
}
};
try {
// add the listener
container.addItemSetChangeListener(tmp);
// call commit
container.commit();
newEntity.set(EntityManagerProvider.getEntityManager().merge(newEntity.get()));
} catch (com.vaadin.data.Buffered.SourceException e) {
if (e.getCause() instanceof javax.persistence.PersistenceException) {
javax.persistence.PersistenceException cause = (javax.persistence.PersistenceException) e.getCause();
Notification.show(cause.getCause().getMessage(), Type.ERROR_MESSAGE);
}
} finally {
// detach the listener
container.removeItemSetChangeListener(tmp);
}
// return the entity
return newEntity.get();
}
use of com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent 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