Search in sources :

Example 1 with ProviderChangedEvent

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();
}
Also used : ItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener) ItemSetChangeEvent(com.vaadin.data.Container.ItemSetChangeEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProviderChangedEvent(com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent) LinkedList(java.util.LinkedList) CommitException(com.vaadin.data.fieldgroup.FieldGroup.CommitException) EntityNotFoundException(javax.persistence.EntityNotFoundException) InvalidValueException(com.vaadin.data.Validator.InvalidValueException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProviderChangedEvent

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();
}
Also used : ItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProviderChangedEvent(com.vaadin.addon.jpacontainer.JPAContainer.ProviderChangedEvent) ItemSetChangeEvent(com.vaadin.data.Container.ItemSetChangeEvent)

Example 3 with ProviderChangedEvent

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);
        }
    }
}
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

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