Search in sources :

Example 6 with MetaProperty

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

the class DsContextImpl method replaceMasterCopies.

// Replace the reference to master entity with actual entity containing in the master datasource,
// because in case of nested property datasources there may be references to cloned master entities.
protected void replaceMasterCopies(Entity entity, NestedDatasource datasource) {
    Datasource masterDs = datasource.getMaster();
    MetaProperty metaProperty = datasource.getProperty();
    if (masterDs != null && metaProperty != null) {
        MetaProperty inverseProp = metaProperty.getInverse();
        if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
            MetaClass metaClass = metadata.getExtendedEntities().getEffectiveMetaClass(inverseProp.getDomain());
            if (metaClass.equals(datasource.getMetaClass()) && (PersistenceHelper.isLoaded(entity, inverseProp.getName()) && // replace master only if it's already set
            EntityValues.getValue(entity, inverseProp.getName()) != null)) {
                Object masterItem = null;
                if (masterDs instanceof CollectionDatasource) {
                    Entity value = EntityValues.getValue(entity, inverseProp.getName());
                    if (value != null) {
                        Object id = EntityValues.getId(value);
                        // noinspection unchecked
                        masterItem = ((CollectionDatasource) masterDs).getItem(id);
                    }
                } else {
                    masterItem = masterDs.getItem();
                }
                if (masterItem != null) {
                    // CAUTION need to rework this mechanism in case of two or more nested collection datasources
                    EntityValues.setValue(entity, inverseProp.getName(), masterItem, false);
                }
            }
        }
    }
}
Also used : Entity(io.jmix.core.Entity) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 7 with MetaProperty

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

the class DsContextImpl method repairReferences.

@SuppressWarnings("unchecked")
protected void repairReferences(Entity entity, Entity contextEntity) {
    MetaClass metaClass = metadata.getClass(entity.getClass());
    MetaClass contextEntityMetaClass = metadata.getClass(contextEntity.getClass());
    for (MetaProperty property : metaClass.getProperties()) {
        if (!property.getRange().isClass() || !property.getRange().asClass().equals(contextEntityMetaClass) || !PersistenceHelper.isLoaded(entity, property.getName()))
            continue;
        Object value = EntityValues.getValue(entity, property.getName());
        if (value != null) {
            if (property.getRange().getCardinality().isMany()) {
                Collection collection = (Collection) value;
                for (Object item : new ArrayList(collection)) {
                    if (contextEntity.equals(item) && contextEntity != item) {
                        if (collection instanceof List) {
                            List list = (List) collection;
                            list.set(list.indexOf(item), contextEntity);
                        } else {
                            collection.remove(item);
                            collection.add(contextEntity);
                        }
                    }
                }
            } else {
                if (contextEntity.equals(value) && contextEntity != value) {
                    EntityValues.setValue(entity, property.getName(), contextEntity);
                }
            }
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 8 with MetaProperty

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

the class PropertyDatasourceImpl method commit.

@SuppressWarnings("unchecked")
@Override
public void commit() {
    if (!allowCommit) {
        return;
    }
    if (getCommitMode() == CommitMode.PARENT) {
        if (parentDs == null) {
            throw new IllegalStateException("parentDs is null while commitMode=PARENT");
        }
        if (parentDs instanceof CollectionDatasource) {
            CollectionDatasource parentCollectionDs = (CollectionDatasource) parentDs;
            for (Entity item : itemsToCreate) {
                if (parentCollectionDs.containsItem(EntityValues.getId(item))) {
                    parentCollectionDs.modifyItem(item);
                } else {
                    parentCollectionDs.addItem(item);
                }
            }
            for (Entity item : itemsToUpdate) {
                parentCollectionDs.modifyItem(item);
            }
            for (Entity item : itemsToDelete) {
                parentCollectionDs.removeItem(item);
            }
            // after repeated edit of new items the parent datasource can contain items-to-create which are deleted
            // in this datasource, so we need to delete them
            Collection<Entity> parentItemsToCreate = ((DatasourceImplementation) parentCollectionDs).getItemsToCreate();
            for (Entity createdItem : new ArrayList<>(parentItemsToCreate)) {
                if (!this.itemsToCreate.contains(createdItem)) {
                    MetaProperty inverseProp = metaProperty.getInverse();
                    // delete only if they have the same master item
                    if (inverseProp != null && PersistenceHelper.isLoaded(createdItem, inverseProp.getName()) && Objects.equals(EntityValues.getValue(createdItem, inverseProp.getName()), masterDs.getItem())) {
                        parentCollectionDs.removeItem(createdItem);
                    }
                }
            }
        } else {
            Entity item = null;
            if (!itemsToCreate.isEmpty()) {
                item = itemsToCreate.iterator().next();
            } else if (!itemsToUpdate.isEmpty()) {
                item = itemsToUpdate.iterator().next();
            } else if (!itemsToDelete.isEmpty()) {
                item = itemsToDelete.iterator().next();
            }
            if (item != null) {
                parentDs.setItem(item);
            }
        }
        clearCommitLists();
        modified = false;
    }
}
Also used : Entity(io.jmix.core.Entity) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 9 with MetaProperty

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

the class ValueDatasourceDelegate method beforeLoadValues.

protected ValueLoadContext beforeLoadValues(Map<String, Object> params) {
    ValueLoadContext context = new ValueLoadContext();
    ValueLoadContext.Query q = (ValueLoadContext.Query) ds.createDataQuery(context, params);
    if (q == null) {
        ds.detachListener(ds.data.values());
        ds.data.clear();
        return null;
    }
    if (ds.firstResult > 0)
        q.setFirstResult(ds.firstResult);
    if (ds.maxResults > 0) {
        q.setMaxResults(ds.maxResults);
    }
    if (storeName != null)
        context.setStoreName(storeName);
    context.setHint(PersistenceHints.SOFT_DELETION, ds.isSoftDeletion());
    context.setIdName(idName);
    for (MetaProperty property : ds.metaClass.getProperties()) {
        context.addProperty(property.getName());
    }
    ds.dataLoadError = null;
    return context;
}
Also used : ValueLoadContext(com.haulmont.cuba.core.global.ValueLoadContext) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 10 with MetaProperty

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

the class ValueDatasourceDelegate method convertEnumValues.

protected void convertEnumValues(KeyValueEntity entity, List<MetaProperty> enumProperties) {
    try {
        for (MetaProperty enumProperty : enumProperties) {
            Object enumValue = entity.getValue(enumProperty.getName());
            if (enumValue != null) {
                Enumeration enumeration = enumProperty.getRange().asEnumeration();
                entity.setValue(enumProperty.getName(), enumeration.parse(String.valueOf(enumValue)));
            }
        }
    } catch (ParseException e) {
        throw new RuntimeException("Unable to convert enum id to enum instance for EnumClass");
    }
}
Also used : Enumeration(io.jmix.core.metamodel.datatype.Enumeration) ParseException(java.text.ParseException) 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