Search in sources :

Example 1 with InstanceContainer

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

the class AbstractComponentGenerationStrategy method setOptionsLoader.

protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
    InstanceContainer<?> container = valueSource.getContainer();
    Entity entity = container.getItemOrNull();
    if (entity != null) {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    }
    container.addItemChangeListener(e -> {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    });
    List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
    if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
        List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
        container.addItemPropertyChangeListener(e -> {
            if (dependsOnAttributesCodes.contains(e.getProperty())) {
                List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
                // noinspection unchecked
                lookupField.setOptions(new ListOptions(options));
                if (!options.contains(lookupField.getValue())) {
                    // noinspection unchecked
                    lookupField.setValue(null);
                }
            }
        });
    }
}
Also used : Resolution(com.haulmont.cuba.gui.components.DateField.Resolution) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Time(java.sql.Time) Date(java.util.Date) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) BooleanUtils(org.apache.commons.lang3.BooleanUtils) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) AppBeans(com.haulmont.cuba.core.global.AppBeans) StringUtils(org.apache.commons.lang3.StringUtils) MetaClass(com.haulmont.chile.core.model.MetaClass) java.time(java.time) Datatype(com.haulmont.chile.core.datatypes.Datatype) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) com.haulmont.cuba.core.app.dynamicattributes(com.haulmont.cuba.core.app.dynamicattributes) Method(java.lang.reflect.Method) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Nullable(javax.annotation.Nullable) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collection(java.util.Collection) Options(com.haulmont.cuba.gui.components.data.Options) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) Messages(com.haulmont.cuba.core.global.Messages) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CurrencyValue(com.haulmont.cuba.core.entity.annotation.CurrencyValue) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport) Consumer(java.util.function.Consumer) List(java.util.List) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) List(java.util.List)

Example 2 with InstanceContainer

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

the class RemoveOperation method commitIfNeeded.

protected void commitIfNeeded(Collection<? extends Entity> entitiesToRemove, CollectionContainer container, ScreenData screenData) {
    List<? extends Entity> entitiesToCommit = entitiesToRemove.stream().filter(entity -> !entityStates.isNew(entity)).collect(Collectors.toList());
    boolean needCommit = !entitiesToCommit.isEmpty();
    if (container instanceof Nested) {
        InstanceContainer masterContainer = ((Nested) container).getMaster();
        String property = ((Nested) container).getProperty();
        MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
        MetaProperty metaProperty = masterMetaClass.getPropertyNN(property);
        needCommit = needCommit && (metaProperty.getType() != MetaProperty.Type.COMPOSITION);
    }
    if (needCommit) {
        CommitContext commitContext = new CommitContext();
        for (Entity entity : entitiesToCommit) {
            commitContext.addInstanceToRemove(entity);
        }
        dataManager.commit(commitContext);
        for (Entity entity : entitiesToRemove) {
            screenData.getDataContext().evict(entity);
        }
    } else {
        for (Entity entity : entitiesToRemove) {
            screenData.getDataContext().remove(entity);
        }
    }
}
Also used : ListComponent(com.haulmont.cuba.gui.components.ListComponent) UiControllerUtils.getScreenContext(com.haulmont.cuba.gui.screen.UiControllerUtils.getScreenContext) java.util(java.util) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Table(com.haulmont.cuba.gui.components.Table) ScreenData(com.haulmont.cuba.gui.model.ScreenData) Focusable(com.haulmont.cuba.gui.components.Component.Focusable) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) Collectors(java.util.stream.Collectors) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Nested(com.haulmont.cuba.gui.model.Nested) WindowConfig(com.haulmont.cuba.gui.config.WindowConfig) com.haulmont.cuba.gui.screen(com.haulmont.cuba.gui.screen) DataGrid(com.haulmont.cuba.gui.components.DataGrid) DialogAction(com.haulmont.cuba.gui.components.DialogAction) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) Entity(com.haulmont.cuba.core.entity.Entity) CollectionContainer(com.haulmont.cuba.gui.model.CollectionContainer) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Nested(com.haulmont.cuba.gui.model.Nested) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 3 with InstanceContainer

use of com.haulmont.cuba.gui.model.InstanceContainer 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 4 with InstanceContainer

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

the class LookupBuilderProcessor method getViewForCollectionContainer.

/**
 * See {@link #getViewForField(HasValue)} javadoc.
 *
 * @return a view or null if the view cannot be evaluated
 */
@Nullable
protected <E extends Entity> View getViewForCollectionContainer(CollectionContainer<E> collectionDc, boolean initializeMasterReference, MetaProperty inverseMetaProperty) {
    View view = null;
    if (collectionDc instanceof Nested) {
        InstanceContainer masterDc = ((Nested) collectionDc).getMaster();
        View masterView = masterDc.getView();
        if (masterView != null) {
            String property = ((Nested) collectionDc).getProperty();
            ViewProperty viewProperty = masterView.getProperty(property);
            if (viewProperty != null) {
                view = viewProperty.getView();
                if (view != null && initializeMasterReference && inverseMetaProperty != null) {
                    view.addProperty(inverseMetaProperty.getName());
                }
            }
        }
    } else {
        view = collectionDc.getView();
    }
    return view;
}
Also used : Nested(com.haulmont.cuba.gui.model.Nested) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) Nullable(javax.annotation.Nullable)

Example 5 with InstanceContainer

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

the class WebTokenList method getViewForField.

/**
 * If the value for a component is selected from the lookup screen there may be cases when lookup screen contains a list of entities loaded with a
 * view that doesn't contain all attributes, required by the token list.
 * <p>
 * The method evaluates the view that was is for loading entities in the token list
 *
 * @return a view or null if the view cannot be evaluated
 */
@Nullable
protected View getViewForField() {
    ValueSource valueSource = getValueSource();
    if (valueSource instanceof ContainerValueSource) {
        ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
        InstanceContainer container = containerValueSource.getContainer();
        View view = container.getView();
        if (view != null) {
            MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
            View curView = view;
            if (metaPropertyPath != null) {
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    ViewProperty viewProperty = curView.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curView = viewProperty.getView();
                        if (curView == null)
                            return null;
                    }
                }
                MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
                if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
                    curView.addProperty(inverseMetaProperty.getName());
                }
            }
            if (curView != view) {
                return curView;
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) LegacyCollectionDsValueSource(com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Aggregations

InstanceContainer (com.haulmont.cuba.gui.model.InstanceContainer)18 ScreenData (com.haulmont.cuba.gui.model.ScreenData)9 CollectionContainer (com.haulmont.cuba.gui.model.CollectionContainer)8 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)8 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)7 MetaProperty (com.haulmont.chile.core.model.MetaProperty)6 MetaClass (com.haulmont.chile.core.model.MetaClass)5 Nested (com.haulmont.cuba.gui.model.Nested)5 Nullable (javax.annotation.Nullable)5 Entity (com.haulmont.cuba.core.entity.Entity)4 ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)4 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)2 Table (com.haulmont.cuba.gui.components.Table)2 GuiActionSupport (com.haulmont.cuba.gui.components.actions.GuiActionSupport)2