Search in sources :

Example 1 with DataContext

use of com.haulmont.cuba.gui.model.DataContext in project cuba by cuba-platform.

the class RefreshAction method execute.

/**
 * Executes the action.
 */
@Override
public void execute() {
    if (target == null) {
        throw new IllegalStateException("RefreshAction target is not set");
    }
    if (target.getItems() instanceof EmptyDataUnit) {
        return;
    }
    if (!(target.getItems() instanceof ContainerDataUnit)) {
        throw new IllegalStateException("RefreshAction target is null or does not implement SupportsContainerBinding");
    }
    CollectionContainer container = ((ContainerDataUnit) target.getItems()).getContainer();
    if (container == null) {
        throw new IllegalStateException("RefreshAction target is not bound to CollectionContainer");
    }
    DataLoader loader = null;
    if (container instanceof HasLoader) {
        loader = ((HasLoader) container).getLoader();
    }
    if (loader != null) {
        DataContext dataContext = loader.getDataContext();
        if (dataContext != null) {
            for (Object entity : container.getItems()) {
                dataContext.evict((Entity) entity);
            }
        }
        loader.load();
    } else {
        log.warn("RefreshAction '{}' target container has no loader, refresh is impossible", getId());
    }
}
Also used : DataLoader(com.haulmont.cuba.gui.model.DataLoader) DataContext(com.haulmont.cuba.gui.model.DataContext) EmptyDataUnit(com.haulmont.cuba.gui.components.data.meta.EmptyDataUnit) CollectionContainer(com.haulmont.cuba.gui.model.CollectionContainer) HasLoader(com.haulmont.cuba.gui.model.HasLoader) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)

Example 2 with DataContext

use of com.haulmont.cuba.gui.model.DataContext in project cuba by cuba-platform.

the class LookupBuilderProcessor method handleSelectionWithContainer.

protected <E extends Entity> void handleSelectionWithContainer(LookupBuilder<E> builder, CollectionContainer<E> collectionDc, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    boolean initializeMasterReference = false;
    Entity masterItem = null;
    MetaProperty inverseMetaProperty = null;
    // update holder reference if needed
    if (collectionDc instanceof Nested) {
        InstanceContainer masterDc = ((Nested) collectionDc).getMaster();
        String property = ((Nested) collectionDc).getProperty();
        masterItem = masterDc.getItem();
        MetaProperty metaProperty = masterItem.getMetaClass().getPropertyNN(property);
        inverseMetaProperty = metaProperty.getInverse();
        if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
            Class<?> inversePropClass = extendedEntities.getEffectiveClass(inverseMetaProperty.getDomain());
            Class<?> dcClass = extendedEntities.getEffectiveClass(collectionDc.getEntityMetaClass());
            initializeMasterReference = inversePropClass.isAssignableFrom(dcClass);
        }
    }
    DataContext dataContext = UiControllerUtils.getScreenData(builder.getOrigin()).getDataContext();
    List<E> mergedItems = new ArrayList<>(selectedItems.size());
    View viewForCollectionContainer = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && collectionDc.getEntityMetaClass() != null && metadataTools.isPersistent(collectionDc.getEntityMetaClass()) ? getViewForCollectionContainer(collectionDc, initializeMasterReference, inverseMetaProperty) : null;
    for (E item : selectedItems) {
        if (!collectionDc.containsItem(item.getId())) {
            if (viewForCollectionContainer != null && !entityStates.isLoadedWithView(item, viewForCollectionContainer)) {
                item = dataManager.reload(item, viewForCollectionContainer);
            }
            // track changes in the related instance
            E mergedItem = dataContext.merge(item);
            if (initializeMasterReference) {
                // change reference, now it will be marked as modified
                mergedItem.setValue(inverseMetaProperty.getName(), masterItem);
            }
            mergedItems.add(mergedItem);
        }
    }
    collectionDc.getMutableItems().addAll(mergedItems);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Nested(com.haulmont.cuba.gui.model.Nested) ArrayList(java.util.ArrayList) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) DataContext(com.haulmont.cuba.gui.model.DataContext) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 3 with DataContext

use of com.haulmont.cuba.gui.model.DataContext in project cuba by cuba-platform.

the class ClearAction method execute.

/**
 * Executes the action.
 */
@SuppressWarnings("unchecked")
@Override
public void execute() {
    // remove entity if it is a composition
    Object value = pickerField.getValue();
    ValueSource valueSource = pickerField.getValueSource();
    if (value != null && !value.equals(pickerField.getEmptyValue()) && valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) pickerField.getValueSource();
        Entity entity = (Entity) pickerField.getValue();
        if (entityValueSource.getMetaPropertyPath() != null && entityValueSource.getMetaPropertyPath().getMetaProperty().getType() == MetaProperty.Type.COMPOSITION) {
            FrameOwner screen = pickerField.getFrame().getFrameOwner();
            DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
            dataContext.remove(entity);
        }
    }
    // Set the value as if the user had set it
    pickerField.setValueFromUser(pickerField.getEmptyValue());
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DataContext(com.haulmont.cuba.gui.model.DataContext) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource)

Aggregations

DataContext (com.haulmont.cuba.gui.model.DataContext)3 Entity (com.haulmont.cuba.core.entity.Entity)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)1 ContainerDataUnit (com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)1 EmptyDataUnit (com.haulmont.cuba.gui.components.data.meta.EmptyDataUnit)1 EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)1 CollectionContainer (com.haulmont.cuba.gui.model.CollectionContainer)1 DataLoader (com.haulmont.cuba.gui.model.DataLoader)1 HasLoader (com.haulmont.cuba.gui.model.HasLoader)1 InstanceContainer (com.haulmont.cuba.gui.model.InstanceContainer)1 Nested (com.haulmont.cuba.gui.model.Nested)1 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)1 ArrayList (java.util.ArrayList)1