Search in sources :

Example 1 with CrudEventType

use of au.com.vaadinutils.crud.events.CrudEventType in project VaadinUtils by rlsutton1.

the class BaseCrudView method save.

@Override
public void save() {
    boolean selected = false;
    try {
        commitFieldGroup();
        validateChildren();
        CrudEventType eventType = CrudEventType.EDIT;
        if (newEntity != null) {
            if (!okToSave(newEntity)) {
                return;
            }
            eventType = CrudEventType.CREATE;
            interceptSaveValues(newEntity);
            Object id = container.addEntity(newEntity.getEntity());
            EntityItem<E> item = container.getItem(id);
            fieldGroup.setItemDataSource(item);
            selected = true;
            if (restoreDelete) {
                activateEditMode(false);
                restoreDelete = false;
            }
            searchField.setReadOnly(false);
            clearButton.setEnabled(true);
        } else {
            EntityItem<E> current = entityTable.getCurrent();
            if (current != null) {
                if (!okToSave(current)) {
                    return;
                }
                interceptSaveValues(current);
            }
        }
        // commit the row to the database, and retrieve the possibly new
        // entity
        E newEntity = commitContainerAndGetEntityFromDB();
        if (newEntity == null) {
            throw new RuntimeException("An error occurred, unable to retrieve updated record. Failed to save changes");
        }
        newEntity = EntityManagerProvider.getEntityManager().merge(newEntity);
        for (ChildCrudListener<E> commitListener : childCrudListeners) {
            // of children
            if (commitListener.isDirty()) {
                commitListener.committed(newEntity);
            }
        }
        EntityManagerProvider.getEntityManager().flush();
        // children may have been added to the parent, evict the parent from
        // the JPA cache so it will get updated
        EntityManagerProvider.getEntityManager().getEntityManagerFactory().getCache().evict(entityClass, newEntity.getId());
        newEntity = EntityManagerProvider.merge(newEntity);
        postSaveAction(newEntity);
        EntityManagerProvider.getEntityManager().flush();
        reloadDataFromDB(newEntity.getId());
        CrudEventDistributer.publishEvent(this, eventType, newEntity);
        if (eventType == CrudEventType.CREATE) {
            addFilterToShowNewRow(newEntity);
            container.discard();
        }
        this.newEntity = null;
        searchField.setReadOnly(false);
        clearButton.setEnabled(true);
        // select has been moved to here because when it happens earlier,
        // child cruds are caused to discard their data before saving it for
        // a new record
        entityTable.select(newEntity.getId());
        splitPanel.showFirstComponent();
        Notification.show("Changes Saved", "Any changes you have made have been saved.", Type.HUMANIZED_MESSAGE);
        // return save/edit buttons to default settings
        buttonLayout.setDefaultState();
    } catch (Exception e) {
        if (e instanceof InvalidValueException) {
            handleInvalidValueException((InvalidValueException) e);
        } else if (e.getCause() instanceof InvalidValueException) {
            handleInvalidValueException((InvalidValueException) e.getCause());
        } else {
            ErrorWindow.showErrorWindow(e);
        }
    } finally {
        if (newEntity != null) {
            if (selected && entityTable.getCurrent() != null) {
                container.removeItem(entityTable.getCurrent());
            }
        }
        buttonLayout.setDefaultState();
    }
}
Also used : InvalidValueException(com.vaadin.data.Validator.InvalidValueException) CrudEventType(au.com.vaadinutils.crud.events.CrudEventType) CommitException(com.vaadin.data.fieldgroup.FieldGroup.CommitException) EntityNotFoundException(javax.persistence.EntityNotFoundException) InvalidValueException(com.vaadin.data.Validator.InvalidValueException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CrudEventType (au.com.vaadinutils.crud.events.CrudEventType)1 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)1 CommitException (com.vaadin.data.fieldgroup.FieldGroup.CommitException)1 ExecutionException (java.util.concurrent.ExecutionException)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1