Search in sources :

Example 6 with InstanceContainer

use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.

the class PivotTableLoader method loadDataContainer.

protected void loadDataContainer(PivotTable pivotTable, Element element) {
    String dataContainerId = element.attributeValue("dataContainer");
    if (StringUtils.isNotEmpty(dataContainerId)) {
        FrameOwner frameOwner = getComponentContext().getFrame().getFrameOwner();
        ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
        CollectionContainer dataContainer;
        InstanceContainer container = screenData.getContainer(dataContainerId);
        if (container instanceof CollectionContainer) {
            dataContainer = (CollectionContainer) container;
        } else {
            throw new GuiDevelopmentException("Not a CollectionContainer: " + dataContainerId, context);
        }
        pivotTable.setDataProvider(new ContainerDataProvider(dataContainer));
    }
}
Also used : FrameOwner(io.jmix.ui.screen.FrameOwner) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) CollectionContainer(io.jmix.ui.model.CollectionContainer) InstanceContainer(io.jmix.ui.model.InstanceContainer) ContainerDataProvider(io.jmix.ui.data.impl.ContainerDataProvider) ScreenData(io.jmix.ui.model.ScreenData)

Example 7 with InstanceContainer

use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.

the class EditorBuilderProcessor method initializeNestedEntity.

protected <E> void initializeNestedEntity(E entity, Nested container) {
    InstanceContainer masterContainer = container.getMaster();
    String property = container.getProperty();
    MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
    MetaProperty metaProperty = masterMetaClass.getProperty(property);
    MetaProperty inverseProp = metaProperty.getInverse();
    if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
        Class<?> inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
        Class<?> containerEntityClass = extendedEntities.getEffectiveClass(((CollectionContainer) container).getEntityMetaClass());
        if (inversePropClass.isAssignableFrom(containerEntityClass)) {
            EntityValues.setValue(entity, inverseProp.getName(), masterContainer.getItem());
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) InstanceContainer(io.jmix.ui.model.InstanceContainer) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 8 with InstanceContainer

use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.

the class EditorBuilderProcessor method setupParentDataContext.

@Nullable
protected DataContext setupParentDataContext(FrameOwner origin, Screen screen, @Nullable InstanceContainer container, @Nullable DataContext parentContext) {
    DataContext dataContext = parentContext;
    if (dataContext == null && container instanceof Nested) {
        InstanceContainer masterContainer = ((Nested) container).getMaster();
        String property = ((Nested) container).getProperty();
        MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
        MetaProperty metaProperty = masterMetaClass.getProperty(property);
        if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
            dataContext = UiControllerUtils.getScreenData(origin).getDataContextOrNull();
        }
    }
    if (dataContext != null) {
        DataContext childContext = UiControllerUtils.getScreenData(screen).getDataContextOrNull();
        checkDataContext(screen, childContext);
        childContext.setParent(dataContext);
    }
    return dataContext;
}
Also used : DataContext(io.jmix.ui.model.DataContext) MetaClass(io.jmix.core.metamodel.model.MetaClass) Nested(io.jmix.ui.model.Nested) InstanceContainer(io.jmix.ui.model.InstanceContainer) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 9 with InstanceContainer

use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.

the class LookupBuilderProcessor method getFetchPlanForField.

/**
 * The method evaluates the fetch plan that is used for the entity in the given {@code field}
 * <p>
 * If the value for a component (e.g. {@link EntityPicker}) is selected from lookup screen then there may be cases
 * when in entities in lookup screen some attributes required in the editor are not loaded.
 *
 * @return a view or {@code null} if the fetch plan cannot be evaluated
 */
@Nullable
protected FetchPlan getFetchPlanForField(HasValue field) {
    if (field instanceof HasValueSource) {
        ValueSource valueSource = ((HasValueSource) field).getValueSource();
        if (valueSource instanceof ContainerValueSource) {
            ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
            InstanceContainer container = containerValueSource.getContainer();
            FetchPlan fetchPlan = container.getFetchPlan();
            if (fetchPlan != null) {
                MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
                FetchPlan curFetchPlan = fetchPlan;
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    FetchPlanProperty viewProperty = curFetchPlan.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curFetchPlan = viewProperty.getFetchPlan();
                    }
                    if (curFetchPlan == null)
                        break;
                }
                if (curFetchPlan != fetchPlan) {
                    return curFetchPlan;
                }
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) HasValueSource(io.jmix.ui.component.data.HasValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) InstanceContainer(io.jmix.ui.model.InstanceContainer) HasValueSource(io.jmix.ui.component.data.HasValueSource) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 10 with InstanceContainer

use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.

the class LookupBuilderProcessor method handleSelectionWithContainer.

protected <E> void handleSelectionWithContainer(LookupBuilder<E> builder, CollectionContainer<E> collectionDc, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    boolean initializeMasterReference = false;
    Object 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 = metadata.getClass(masterItem).getProperty(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());
    FetchPlan viewForCollectionContainer = screenProperties.isReloadUnfetchedAttributesFromLookupScreens() && collectionDc.getEntityMetaClass() != null && metadataTools.isJpaEntity(collectionDc.getEntityMetaClass()) ? getFetchPlanForCollectionContainer(collectionDc, initializeMasterReference, inverseMetaProperty) : null;
    for (E item : selectedItems) {
        if (!collectionDc.containsItem(EntityValues.getId(item))) {
            if (viewForCollectionContainer != null && !entityStates.isLoadedWithFetchPlan(item, viewForCollectionContainer)) {
                // noinspection unchecked
                item = (E) dataManager.load(Id.of(item)).fetchPlan(viewForCollectionContainer).one();
            }
            // track changes in the related instance
            E mergedItem = dataContext.merge(item);
            if (initializeMasterReference) {
                // change reference, now it will be marked as modified
                EntityValues.setValue(mergedItem, inverseMetaProperty.getName(), masterItem);
            }
            mergedItems.add(mergedItem);
        }
    }
    collectionDc.getMutableItems().addAll(mergedItems);
}
Also used : Nested(io.jmix.ui.model.Nested) InstanceContainer(io.jmix.ui.model.InstanceContainer) DataContext(io.jmix.ui.model.DataContext) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

InstanceContainer (io.jmix.ui.model.InstanceContainer)30 ScreenData (io.jmix.ui.model.ScreenData)12 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)11 CollectionContainer (io.jmix.ui.model.CollectionContainer)11 FrameOwner (io.jmix.ui.screen.FrameOwner)11 MetaClass (io.jmix.core.metamodel.model.MetaClass)6 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)6 Nullable (javax.annotation.Nullable)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)3 Datasource (com.haulmont.cuba.gui.data.Datasource)3 ContainerOptions (io.jmix.ui.component.data.options.ContainerOptions)3 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)3 ContainerDataProvider (io.jmix.ui.data.impl.ContainerDataProvider)3 Nested (io.jmix.ui.model.Nested)3 Map (java.util.Map)3 WeakHashMap (java.util.WeakHashMap)3 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)2 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)2 ValueSource (io.jmix.ui.component.data.ValueSource)2 DataContext (io.jmix.ui.model.DataContext)2