Search in sources :

Example 21 with MetaProperty

use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.

the class WebTable method setupAutowiredColumns.

@Override
protected void setupAutowiredColumns(EntityTableItems<E> entityTableSource) {
    Collection<MetaPropertyPath> paths = getAutowiredProperties(entityTableSource);
    for (MetaPropertyPath metaPropertyPath : paths) {
        MetaProperty property = metaPropertyPath.getMetaProperty();
        if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
            io.jmix.ui.component.Table.Column<E> column = addColumn(metaPropertyPath);
            String propertyName = property.getName();
            MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
            column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
            if (column instanceof Table.Column) {
                ((Table.Column<E>) column).setType(metaPropertyPath.getRangeJavaClass());
            }
        }
    }
}
Also used : Table(com.haulmont.cuba.gui.components.Table) AbstractTable(io.jmix.ui.component.impl.AbstractTable) JmixEnhancedTable(io.jmix.ui.widget.JmixEnhancedTable) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 22 with MetaProperty

use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.

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 FetchPlan getViewForField() {
    ValueSource valueSource = getValueSource();
    if (valueSource instanceof ContainerValueSource) {
        ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
        InstanceContainer container = containerValueSource.getContainer();
        FetchPlan view = container.getFetchPlan();
        if (view != null) {
            MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
            FetchPlan curView = view;
            if (metaPropertyPath != null) {
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    FetchPlanProperty viewProperty = curView.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curView = viewProperty.getFetchPlan();
                        if (curView == null)
                            return null;
                    }
                }
                MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
                if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
                    curView = fetchPlans.builder(curView).add(inverseMetaProperty.getName()).build();
                }
            }
            if (curView != view) {
                return curView;
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) LegacyCollectionDsValueSource(com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) InstanceContainer(io.jmix.ui.model.InstanceContainer) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 23 with MetaProperty

use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.

the class WebTokenList method updateMasterRefIfOptionsRefreshed.

/**
 * Sets master-entity reference to the value and remove master-entity reference
 * from options if they are not in nested container.
 *
 * @param value value items
 */
protected void updateMasterRefIfOptionsRefreshed(Collection<V> value) {
    if (!isRefreshOptionsEnabled()) {
        return;
    }
    if (!(getValueSource() instanceof ContainerValueSource)) {
        return;
    }
    EntityOptions<V> options = (EntityOptions<V>) getOptions();
    if (options == null) {
        return;
    }
    ContainerValueSource valueSource = (ContainerValueSource) getValueSource();
    MetaPropertyPath mpp = valueSource.getMetaPropertyPath();
    MetaProperty inverseProperty = mpp.getMetaProperty().getInverse();
    Object masterEntity = valueSource.getItem();
    if (inverseProperty == null || masterEntity == null) {
        return;
    }
    List<V> optionItems = getOptions().getOptions().collect(Collectors.toList());
    for (V option : optionItems) {
        // skip all options that did not load master-reference
        if (!entityStates.isLoaded(option, inverseProperty.getName())) {
            continue;
        }
        if (value.contains(option)) {
            // reset master-entity reference
            EntityValues.setValue(option, inverseProperty.getName(), masterEntity);
        } else {
            Entity ref = EntityValues.getValue(option, inverseProperty.getName());
            if (ref == null) {
                continue;
            }
            // remove ref to the master entity if option is not in value
            if (Objects.equals(EntityValues.getId(ref), EntityValues.getId(masterEntity))) {
                EntityValues.setValue(option, inverseProperty.getName(), null);
            }
        }
    }
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) ListEntityOptions(io.jmix.ui.component.data.options.ListEntityOptions) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) MapEntityOptions(io.jmix.ui.component.data.options.MapEntityOptions)

Example 24 with MetaProperty

use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.

the class WebEntityLinkField method afterCommitOpenedEntity.

@Override
protected void afterCommitOpenedEntity(Object item) {
    if (getCollectionDatasourceFromOwner() == null) {
        super.afterCommitOpenedEntity(item);
        return;
    }
    MetaProperty metaProperty = getMetaPropertyForEditedValue();
    if (metaProperty != null && metaProperty.getRange().isClass()) {
        if (getValueSource() != null) {
            boolean ownerDsModified = false;
            boolean nonModifiedInTable = false;
            DatasourceImplementation ownerDs = null;
            if (getCollectionDatasourceFromOwner() != null) {
                ownerDs = ((DatasourceImplementation) getCollectionDatasourceFromOwner());
                nonModifiedInTable = !ownerDs.getItemsToUpdate().contains(((EntityValueSource) getValueSource()).getItem());
                ownerDsModified = ownerDs.isModified();
            }
            // noinspection unchecked
            setValueSilently((V) item);
            // remove from items to update if it was not modified before setValue
            if (ownerDs != null) {
                if (nonModifiedInTable) {
                    ownerDs.getItemsToUpdate().remove(getDatasource().getItem());
                }
                ownerDs.setModified(ownerDsModified);
            }
        } else {
            // noinspection unchecked
            setValue((V) item);
        }
    // if we edit property with non Entity type and set ListComponent owner
    } else if (owner != null) {
        if (getCollectionDatasourceFromOwner() != null) {
            // noinspection unchecked
            getCollectionDatasourceFromOwner().updateItem((Entity) item);
        }
        if (owner instanceof Focusable) {
            // focus owner
            ((Focusable) owner).focus();
        }
    // if we edit property with non Entity type
    } else {
        // noinspection unchecked
        setValueSilently((V) item);
    }
}
Also used : Entity(io.jmix.core.Entity) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 25 with MetaProperty

use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.

the class EntityFetcher method fetch.

@SuppressWarnings("unchecked")
protected void fetch(Object entity, FetchPlan fetchPlan, Map<Object, Set<FetchPlan>> visited, boolean optimizeForDetached) {
    Set<FetchPlan> fetchPlans = visited.get(entity);
    if (fetchPlans == null) {
        fetchPlans = new HashSet<>();
        visited.put(entity, fetchPlans);
    } else if (fetchPlans.contains(fetchPlan)) {
        return;
    }
    fetchPlans.add(fetchPlan);
    if (log.isTraceEnabled())
        log.trace("Fetching instance " + entity);
    MetaClass metaClass = metadata.getClass(entity.getClass());
    for (FetchPlanProperty property : fetchPlan.getProperties()) {
        MetaProperty metaProperty = metaClass.getProperty(property.getName());
        if (!metaProperty.getRange().isClass() && !isLazyFetchedLocalAttribute(metaProperty) || !metadataTools.isJpa(metaProperty))
            continue;
        if (log.isTraceEnabled())
            log.trace("Fetching property " + property.getName());
        Object value = EntityValues.getValue(entity, property.getName());
        FetchPlan propertyFetchPlan = property.getFetchPlan();
        if (value != null && propertyFetchPlan != null) {
            if (value instanceof Collection) {
                for (Object item : new ArrayList(((Collection) value))) {
                    if (item instanceof Entity) {
                        if (entityStates.isDetached(item)) {
                            fetchReloaded(item, propertyFetchPlan, visited, optimizeForDetached, managed -> {
                                if (value instanceof List) {
                                    List list = (List) value;
                                    list.set(list.indexOf(item), managed);
                                } else {
                                    Collection collection = (Collection) value;
                                    collection.remove(item);
                                    collection.add(managed);
                                }
                            });
                        } else {
                            fetch(item, propertyFetchPlan, visited, optimizeForDetached);
                        }
                    }
                }
            } else if (value instanceof Entity) {
                boolean isEmbeddable = EntitySystemAccess.isEmbeddable(value);
                if (!metaProperty.isReadOnly() && entityStates.isDetached(value) && !isEmbeddable) {
                    fetchReloaded(value, propertyFetchPlan, visited, optimizeForDetached, managed -> {
                        EntityValues.setValue(entity, property.getName(), managed);
                    });
                } else {
                    fetch(value, propertyFetchPlan, visited, optimizeForDetached);
                }
            }
        }
    }
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) EntityManager(javax.persistence.EntityManager) EntityValues(io.jmix.core.entity.EntityValues) Basic(javax.persistence.Basic) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) FetchType(javax.persistence.FetchType) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable) StoreAwareLocator(io.jmix.data.StoreAwareLocator) AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

MetaProperty (io.jmix.core.metamodel.model.MetaProperty)267 MetaClass (io.jmix.core.metamodel.model.MetaClass)162 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)53 Nullable (javax.annotation.Nullable)36 Range (io.jmix.core.metamodel.model.Range)23 Autowired (org.springframework.beans.factory.annotation.Autowired)23 Collectors (java.util.stream.Collectors)22 java.util (java.util)20 Component (org.springframework.stereotype.Component)18 Entity (io.jmix.core.Entity)17 EntityValues (io.jmix.core.entity.EntityValues)17 io.jmix.core (io.jmix.core)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 ArrayList (java.util.ArrayList)12 Collection (java.util.Collection)12 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)10 Metadata (io.jmix.core.Metadata)9 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)8