Search in sources :

Example 1 with EntitiesAddedEvent

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);
        }
    }
}
Also used : ItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener) ProviderChangedEvent(com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent) LinkedList(java.util.LinkedList) UnsupportedFilterException(com.vaadin.data.util.filter.UnsupportedFilterException) InvalidValueException(com.vaadin.data.Validator.InvalidValueException) CommitException(com.vaadin.data.fieldgroup.FieldGroup.CommitException) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceException(javax.persistence.PersistenceException) EntitiesAddedEvent(com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesAddedEvent) EntitiesRemovedEvent(com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesRemovedEvent) ItemSetChangeEvent(com.vaadin.data.Container.ItemSetChangeEvent) EntitiesUpdatedEvent(com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesUpdatedEvent)

Aggregations

EntitiesAddedEvent (com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesAddedEvent)1 EntitiesRemovedEvent (com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesRemovedEvent)1 EntitiesUpdatedEvent (com.vaadin.addon.jpacontainer.EntityProviderChangeEvent.EntitiesUpdatedEvent)1 ProviderChangedEvent (com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent)1 ItemSetChangeEvent (com.vaadin.data.Container.ItemSetChangeEvent)1 ItemSetChangeListener (com.vaadin.data.Container.ItemSetChangeListener)1 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)1 CommitException (com.vaadin.data.fieldgroup.FieldGroup.CommitException)1 UnsupportedFilterException (com.vaadin.data.util.filter.UnsupportedFilterException)1 LinkedList (java.util.LinkedList)1 PersistenceException (javax.persistence.PersistenceException)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1