Search in sources :

Example 6 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(FetchPlan fetchPlan, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    if (fetchPlan != null && metadataTools.isJpaEntity(metaClass)) {
        for (FetchPlanProperty property : fetchPlan.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.findProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isJpa(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (!metadataTools.isJpa(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (fetchPlan != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", fetchPlan.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetadataTools(io.jmix.core.MetadataTools) FetchPlanProperty(io.jmix.core.FetchPlanProperty) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range) DevelopmentException(io.jmix.core.DevelopmentException)

Example 7 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class DsBuilder method buildDatasource.

public Datasource buildDatasource() {
    init();
    Datasource datasource;
    try {
        if (master == null && property == null) {
            if (dsClass == null) {
                datasource = createDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            datasource.setup(dsContext, dataSupplier, id, metaClass, view);
        } else {
            boolean isEmbedded = false;
            if (master != null) {
                MetaClass metaClass = master.getMetaClass();
                MetaProperty metaProperty = metaClass.findProperty(property);
                MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
                isEmbedded = metadataTools.isEmbedded(metaProperty);
            }
            if (dsClass == null) {
                datasource = isEmbedded ? createEmbeddedDatasource() : createPropertyDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            ((NestedDatasource) datasource).setup(id, master, property);
        }
    } catch (IllegalAccessException | InstantiationException e) {
        throw new RuntimeException(e);
    }
    datasource.setAllowCommit(allowCommit);
    registerDatasource(datasource);
    return datasource;
}
Also used : MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 8 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class TextFieldImpl method convertToPresentation.

@SuppressWarnings("unchecked")
@Override
protected String convertToPresentation(@Nullable V modelValue) throws ConversionException {
    if (formatter != null) {
        return nullToEmpty(formatter.apply(modelValue));
    }
    if (datatype != null) {
        return nullToEmpty(datatype.format(modelValue, locale));
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Range range = entityValueSource.getMetaPropertyPath().getRange();
        if (range.isDatatype()) {
            Datatype<V> propertyDataType = range.asDatatype();
            return nullToEmpty(propertyDataType.format(modelValue, locale));
        } else {
            setEditable(false);
            if (modelValue == null)
                return "";
            if (range.isClass()) {
                MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
                if (range.getCardinality().isMany()) {
                    return ((Collection<Object>) modelValue).stream().map(metadataTools::getInstanceName).collect(Collectors.joining(", "));
                } else {
                    return metadataTools.getInstanceName(modelValue);
                }
            } else if (range.isEnum()) {
                Messages messages = applicationContext.getBean(Messages.class);
                return messages.getMessage((Enum) modelValue);
            }
        }
    }
    return nullToEmpty(super.convertToPresentation(modelValue));
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetadataTools(io.jmix.core.MetadataTools) Messages(io.jmix.core.Messages) Collection(java.util.Collection) Range(io.jmix.core.metamodel.model.Range)

Example 9 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class DatasourceValueSource method setApplicationContext.

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    MetaClass metaClass = datasource instanceof RuntimePropsDatasource ? ((RuntimePropsDatasource<E>) datasource).resolveCategorizedEntityClass() : datasource.getMetaClass();
    MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
    this.metaPropertyPath = metadataTools.resolveMetaPropertyPath(metaClass, property);
    this.datasource.addStateChangeListener(this::datasourceStateChanged);
    this.datasource.addItemChangeListener(this::datasourceItemChanged);
    this.datasource.addItemPropertyChangeListener(this::datasourceItemPropertyChanged);
    if (datasource.getState() == Datasource.State.VALID) {
        setState(BindingState.ACTIVE);
    }
}
Also used : MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) RuntimePropsDatasource(com.haulmont.cuba.gui.data.RuntimePropsDatasource)

Example 10 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class LegacyCollectionDsValueSource method setApplicationContext.

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    metadata = applicationContext.getBean(Metadata.class);
    if (datasource instanceof NestedDatasource) {
        NestedDatasource nestedDs = (NestedDatasource) this.datasource;
        MetaProperty nestedDsProperty = nestedDs.getProperty();
        MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
        MetaClass masterDsEntityClass = nestedDs.getMaster().getMetaClass();
        MetaPropertyPath masterDsMpp = metadataTools.resolveMetaPropertyPath(masterDsEntityClass, nestedDsProperty.getName());
        if (masterDsMpp.getMetaProperty() != nestedDsProperty) {
            log.debug("Master Datasource property doesn't match with specified nested datasource property");
        } else {
            metaPropertyPath = masterDsMpp;
        }
    }
    datasource.addCollectionChangeListener(this::collectionChanged);
}
Also used : MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

MetadataTools (io.jmix.core.MetadataTools)14 MetaClass (io.jmix.core.metamodel.model.MetaClass)9 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)7 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)6 Metadata (com.haulmont.cuba.core.global.Metadata)3 Op (com.haulmont.cuba.core.global.filter.Op)2 OpManager (com.haulmont.cuba.core.global.filter.OpManager)2 Entity (io.jmix.core.Entity)2 MessageTools (io.jmix.core.MessageTools)2 Messages (io.jmix.core.Messages)2 Range (io.jmix.core.metamodel.model.Range)2 CubaProperties (com.haulmont.cuba.CubaProperties)1 PersistenceManagerClient (com.haulmont.cuba.client.sys.PersistenceManagerClient)1 LockService (com.haulmont.cuba.core.app.LockService)1 Security (com.haulmont.cuba.core.global.Security)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 PropertyCondition (com.haulmont.cuba.gui.components.filter.condition.PropertyCondition)1 NestedDatasource (com.haulmont.cuba.gui.data.NestedDatasource)1 RuntimePropsDatasource (com.haulmont.cuba.gui.data.RuntimePropsDatasource)1 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)1