Search in sources :

Example 1 with EntityItemProperty

use of com.vaadin.addon.jpacontainer.EntityItemProperty in project VaadinUtils by rlsutton1.

the class BaseCrudView method enableDragAndDropOrdering.

/**
 * allows the user to sort the items in the list via drag and drop
 *
 * @param ordinalField
 */
public void enableDragAndDropOrdering(final SingularAttribute<E, Long> ordinalField) {
    dragAndDropOrderingEnabled = true;
    this.ordinalField = ordinalField;
    container.sort(new Object[] { ordinalField.getName() }, new boolean[] { true });
    this.entityTable.setDragMode(TableDragMode.ROW);
    this.entityTable.setDropHandler(new DropHandler() {

        private static final long serialVersionUID = -6024948983201516170L;

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return SourceIsTarget.get();
        }

        @SuppressWarnings("unchecked")
        @Override
        public void drop(DragAndDropEvent event) {
            if (isDirty()) {
                Notification.show("You must save first", Type.WARNING_MESSAGE);
                return;
            }
            Object draggedItemId = event.getTransferable().getData("itemId");
            EntityItem<E> dragged = container.getItem(draggedItemId);
            if (dragged != null) {
                EntityItemProperty draggedOrdinalProp = dragged.getItemProperty(ordinalField.getName());
                if (draggedOrdinalProp != null) {
                    AbstractSelectTargetDetails td = (AbstractSelectTargetDetails) event.getTargetDetails();
                    VerticalDropLocation dl = td.getDropLocation();
                    Object targetId = ((AbstractSelectTargetDetails) event.getTargetDetails()).getItemIdOver();
                    int idx = container.indexOfId(targetId);
                    if (dl == VerticalDropLocation.BOTTOM) {
                        // drop below so move the idx down one
                        idx++;
                    }
                    if (idx > -1) {
                        targetId = container.getIdByIndex(idx);
                    }
                    boolean added = false;
                    Long ctr = 1l;
                    for (Object id : container.getItemIds()) {
                        if (id.equals(targetId)) {
                            draggedOrdinalProp.setValue(ctr++);
                            added = true;
                        }
                        if (!id.equals(draggedItemId)) {
                            container.getItem(id).getItemProperty(ordinalField.getName()).setValue(ctr++);
                        }
                    }
                    if (!added) {
                        draggedOrdinalProp.setValue(ctr++);
                    }
                    container.commit();
                    if (dragAndDropListener != null) {
                        dragAndDropListener.dropped();
                    }
                    // to save.
                    try {
                        invokeTopLevelCrudSave();
                    } catch (Exception e) {
                        ErrorWindow.showErrorWindow(e);
                    }
                } else {
                    logger.error("draggedOrdinalProp is null");
                }
            } else {
                logger.error("dragged is null");
            }
        }
    });
}
Also used : AbstractSelectTargetDetails(com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails) VerticalDropLocation(com.vaadin.shared.ui.dd.VerticalDropLocation) DragAndDropEvent(com.vaadin.event.dd.DragAndDropEvent) EntityItemProperty(com.vaadin.addon.jpacontainer.EntityItemProperty) CommitException(com.vaadin.data.fieldgroup.FieldGroup.CommitException) EntityNotFoundException(javax.persistence.EntityNotFoundException) InvalidValueException(com.vaadin.data.Validator.InvalidValueException) ExecutionException(java.util.concurrent.ExecutionException) DropHandler(com.vaadin.event.dd.DropHandler) EntityItem(com.vaadin.addon.jpacontainer.EntityItem) AcceptCriterion(com.vaadin.event.dd.acceptcriteria.AcceptCriterion)

Example 2 with EntityItemProperty

use of com.vaadin.addon.jpacontainer.EntityItemProperty in project VaadinUtils by rlsutton1.

the class ChildCrudView method committed.

/**
 * this method is invoked when the parent saves, signalling the children
 * that they too should save.
 *
 * @throws Exception
 */
@SuppressWarnings("unchecked")
@Override
public void committed(P newParentId) throws Exception {
    Long selectedIdBeforeSave = null;
    String currentGuid = null;
    if (getCurrent() != null) {
        selectedIdBeforeSave = getCurrent().getId();
        currentGuid = getCurrent().getGuid();
    }
    resetFiltersWithoutChangeEvents();
    saveEditsToTemp();
    int numberOfChildren = 0;
    for (Object id : container.getItemIds()) {
        numberOfChildren++;
        EntityItem<E> item = container.getItem(id);
        if (item != null) {
            EntityItemProperty reference = item.getItemProperty(childKey);
            if (reference == null) {
                loggerChildCrud.error("Child key " + childKey + " doesn't exist in the container " + container.getEntityClass());
            }
            if (reference == null || reference.getValue() == null) {
                try {
                    // problems with the parent when it's also a new parent)
                    if (item.getItemProperty(childKey).getType().isAssignableFrom(newParentId.getClass())) {
                        item.getItemProperty(childKey).setValue(newParentId);
                    } else {
                        loggerChildCrud.warn("Child key type is not the same as the Parent type, if it's an ID thats probably ok?");
                        // special handling when the child key is an
                        // id(Long)
                        // rather than an entity.
                        item.getItemProperty(childKey).setValue(translateParentId(newParentId.getId()));
                    }
                // item.getItemProperty(childKey).setValue(newParentId);
                } catch (Exception e) {
                    loggerChildCrud.error(e, e);
                }
            }
            extendedChildCommitProcessing(newParentId, item);
        } else {
            loggerChildCrud.error("Missing item {} from container", id);
        }
    }
    // container.commit();
    loggerChildCrud.info("Committing for " + this.getClass());
    commitContainerWithHooks();
    // on a new parent, the parent id changes and the container becomes
    // empty. so reset the parent filter and refresh the container
    createParentFilter(parentCrud.getContainerItem(newParentId.getId()));
    resetFiltersWithoutChangeEvents();
    // container.discard();
    container.refresh();
    int changeInItems = numberOfChildren - container.getItemIds().size();
    if (changeInItems != 0) {
        if (ignoreIncorrectNumberOfItems()) {
            loggerChildCrud.warn(changeInItems + ", The number of items in the container is not the same as it was before the refresh. ");
        } else {
            Notification.show("Some of the items may not have been saved, or another user may have been editing the same record at the same time.\n\n" + "You should refresh the browser page (press F5) and check that your changes were correctly saved.", Type.ERROR_MESSAGE);
            loggerChildCrud.error(changeInItems + ", The number of items in the container is not the same as it was before the refresh. " + this.getClass().getSimpleName() + " " + numberOfChildren + " != " + container.getItemIds().size());
        }
    }
    associateChildren(newParentId);
    dirty = false;
    entityTable.select(null);
    triggerFilter();
    if (selectedIdBeforeSave != null && selectedIdBeforeSave > 0) {
        entityTable.select(selectedIdBeforeSave);
    } else {
        if (getGuidAttribute() != null && currentGuid != null) {
            E entity = JpaBaseDao.getGenericDao(childType).findOneByAttribute(getGuidAttribute(), currentGuid);
            if (entity != null) {
                entityTable.select(entity.getId());
            } else {
                loggerChildCrud.warn("Unable to locate newly created child entity, will not be able to select it for the user.");
            }
        }
    }
}
Also used : EntityItemProperty(com.vaadin.addon.jpacontainer.EntityItemProperty) 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)

Example 3 with EntityItemProperty

use of com.vaadin.addon.jpacontainer.EntityItemProperty in project VaadinUtils by rlsutton1.

the class ChildCrudView method createParentFilter.

private void createParentFilter(EntityItem<P> item) throws InstantiationException, IllegalAccessException {
    parentFilter = new Compare.Equal(childKey, translateParentId(-1l));
    if (item != null) {
        EntityItemProperty key = item.getItemProperty(parentKey);
        Preconditions.checkNotNull(key, "parentKey " + parentKey + " doesn't exist in properties");
        parentId = key.getValue();
        if (parentId != null) {
            parentFilter = new Compare.Equal(childKey, translateParentId(parentId));
        }
    }
}
Also used : EntityItemProperty(com.vaadin.addon.jpacontainer.EntityItemProperty) Compare(com.vaadin.data.util.filter.Compare)

Aggregations

EntityItemProperty (com.vaadin.addon.jpacontainer.EntityItemProperty)3 InvalidValueException (com.vaadin.data.Validator.InvalidValueException)2 CommitException (com.vaadin.data.fieldgroup.FieldGroup.CommitException)2 EntityItem (com.vaadin.addon.jpacontainer.EntityItem)1 Compare (com.vaadin.data.util.filter.Compare)1 UnsupportedFilterException (com.vaadin.data.util.filter.UnsupportedFilterException)1 DragAndDropEvent (com.vaadin.event.dd.DragAndDropEvent)1 DropHandler (com.vaadin.event.dd.DropHandler)1 AcceptCriterion (com.vaadin.event.dd.acceptcriteria.AcceptCriterion)1 VerticalDropLocation (com.vaadin.shared.ui.dd.VerticalDropLocation)1 AbstractSelectTargetDetails (com.vaadin.ui.AbstractSelect.AbstractSelectTargetDetails)1 ExecutionException (java.util.concurrent.ExecutionException)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceException (javax.persistence.PersistenceException)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1