Search in sources :

Example 66 with Entity

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

the class CollectionPropertyDatasourceImpl method initCollection.

protected void initCollection() {
    Entity item = masterDs.getItem();
    if (item == null)
        throw new IllegalStateException("Item is null");
    Class<?> type = metaProperty.getJavaType();
    if (List.class.isAssignableFrom(type)) {
        EntityValues.setValue(item, metaProperty.getName(), new ArrayList());
    } else if (Set.class.isAssignableFrom(type)) {
        EntityValues.setValue(item, metaProperty.getName(), new LinkedHashSet());
    } else {
        throw new UnsupportedOperationException("Type " + type + " not supported, should implement List or Set");
    }
    if (EntityValues.getValue(item, metaProperty.getName()) == null) {
        throw new RuntimeException("Cannot set collection property " + metaProperty.getName() + ". Probably not contained in view.");
    }
}
Also used : Entity(io.jmix.core.Entity)

Example 67 with Entity

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

the class RelatedEntitiesBean method getRelatedIds.

protected List<Object> getRelatedIds(Collection<? extends Entity> selectedParents, MetaDataDescriptor descriptor) {
    if (selectedParents.isEmpty()) {
        return Collections.emptyList();
    } else {
        List<Object> parentIds = new ArrayList<>();
        for (Entity e : selectedParents) {
            parentIds.add(EntityValues.getId(e));
        }
        // noinspection UnnecessaryLocalVariable
        List<Object> relatedIds = relatedEntitiesService.getRelatedIds(parentIds, descriptor.getMetaClass().getName(), descriptor.getMetaProperty().getName());
        return relatedIds;
    }
}
Also used : FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) Entity(io.jmix.core.Entity)

Example 68 with Entity

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

the class EmbeddedDatasourceImpl method setItem.

@Override
public void setItem(T item) {
    backgroundWorker.checkUIAccess();
    if (getItem() != null) {
        metadata.getTools().copy(item, getItem());
        itemsToUpdate.add(item);
    } else {
        final Entity parentItem = masterDs.getItem();
        EntityValues.setValue(parentItem, metaProperty.getName(), item);
    }
    setModified(true);
    ((DatasourceImplementation) masterDs).modified(masterDs.getItem());
}
Also used : Entity(io.jmix.core.Entity)

Example 69 with Entity

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

the class EmbeddedDatasourceImpl method getItem.

@Override
public T getItem() {
    backgroundWorker.checkUIAccess();
    final Entity item = masterDs.getItem();
    return getItem(item);
}
Also used : Entity(io.jmix.core.Entity)

Example 70 with Entity

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

the class EntityCopyUtils method copyCompositions.

public static void copyCompositions(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    EntityValues.setId(dest, EntityValues.getId(source));
    Metadata metadata = AppBeans.get(Metadata.class);
    for (MetaProperty srcProperty : metadata.getClass(source).getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = metadata.getClass(dest).findProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = EntityValues.getValue(source, name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    // noinspection unchecked
                    Collection<Entity> srcCollection = (Collection) value;
                    // Copy first to a Set to remove duplicates that could be created on repeated editing newly
                    // added items
                    Collection<Entity> tmpCollection = new LinkedHashSet<>();
                    for (Entity item : srcCollection) {
                        Entity copy = copyCompositions(item);
                        tmpCollection.add(copy);
                    }
                    Collection<Entity> dstCollection;
                    if (!(value instanceof Set))
                        dstCollection = new ArrayList<>(tmpCollection);
                    else
                        dstCollection = tmpCollection;
                    EntityValues.setValue(dest, name, dstCollection);
                } else {
                    EntityValues.setValue(dest, name, EntityValues.getValue(source, name));
                }
            } catch (RuntimeException e) {
                Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause == null)
                    cause = e;
                // ignore exception on copy for not loaded fields
                if (!isNotLoadedAttributeException(cause))
                    throw e;
            }
        }
    }
    dest.__getEntityEntry().setDetached(source.__getEntityEntry().isDetached());
    dest.__getEntityEntry().setNew(source.__getEntityEntry().isNew());
// todo dynamic attributes
// destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entity(io.jmix.core.Entity) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Metadata(io.jmix.core.Metadata) ArrayList(java.util.ArrayList) Collection(java.util.Collection) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

Entity (io.jmix.core.Entity)94 MetaClass (io.jmix.core.metamodel.model.MetaClass)20 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)18 CommitContext (com.haulmont.cuba.core.global.CommitContext)10 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)10 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 Test (org.junit.jupiter.api.Test)10 Server (com.haulmont.cuba.core.model.common.Server)8 Datasource (com.haulmont.cuba.gui.data.Datasource)8 Collectors (java.util.stream.Collectors)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 FetchPlan (io.jmix.core.FetchPlan)7 Logger (org.slf4j.Logger)7 Metadata (io.jmix.core.Metadata)6 MetadataTools (io.jmix.core.MetadataTools)6 EntityValues (io.jmix.core.entity.EntityValues)6 java.util (java.util)6 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 LoggerFactory (org.slf4j.LoggerFactory)6