Search in sources :

Example 21 with Entity

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

the class EntityCombinedScreen method initBrowseCreateAction.

/**
 * Adds a CreateAction that removes selection in table, sets a newly created item to editDs
 * and enables controls for record editing.
 */
protected void initBrowseCreateAction() {
    ListComponent table = getTable();
    table.addAction(new CreateAction(table) {

        @SuppressWarnings("unchecked")
        @Override
        protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
            initNewItem(newItem);
            table.setSelected(Collections.emptyList());
            getFieldGroup().getDatasource().setItem(newItem);
            refreshOptionsForLookupFields();
            enableEditControls(true);
        }
    });
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) AbstractDatasource(com.haulmont.cuba.gui.data.impl.AbstractDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Entity(io.jmix.core.Entity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) CreateAction(com.haulmont.cuba.gui.components.actions.CreateAction)

Example 22 with Entity

use of io.jmix.core.Entity 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 23 with Entity

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

the class RestParseUtils method toObject.

@Nullable
public Object toObject(Type type, @Nullable String value, @Nullable String modelVersion) throws ParseException {
    if (value == null)
        return null;
    Class clazz;
    Class argumentTypeClass = null;
    if (type instanceof Class) {
        clazz = (Class) type;
    } else if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        if (actualTypeArguments.length > 0) {
            if (actualTypeArguments[0] instanceof Class) {
                argumentTypeClass = (Class) actualTypeArguments[0];
            } else if (actualTypeArguments[0] instanceof ParameterizedType) {
                argumentTypeClass = (Class) ((ParameterizedType) actualTypeArguments[0]).getRawType();
            }
        }
        clazz = (Class) parameterizedType.getRawType();
    } else {
        throw new RuntimeException("Cannot handle the method argument with type " + type.getTypeName());
    }
    if (String.class == clazz)
        return value;
    if (Integer.class == clazz || Integer.TYPE == clazz || Byte.class == clazz || Byte.TYPE == clazz || Short.class == clazz || Short.TYPE == clazz)
        return datatypeRegistry.get(Integer.class).parse(value);
    if (Date.class == clazz) {
        try {
            return datatypeRegistry.get(Date.class).parse(value);
        } catch (DateTimeParseException | ParseException e) {
            try {
                return datatypeRegistry.get(java.sql.Date.class).parse(value);
            } catch (ParseException e1) {
                return datatypeRegistry.get(Time.class).parse(value);
            }
        }
    }
    if (LocalDate.class == clazz) {
        return datatypeRegistry.get(LocalDate.class).parse(value);
    }
    if (LocalDateTime.class == clazz) {
        return datatypeRegistry.get(LocalDateTime.class).parse(value);
    }
    if (LocalTime.class == clazz) {
        return datatypeRegistry.get(LocalTime.class).parse(value);
    }
    if (OffsetDateTime.class == clazz) {
        return datatypeRegistry.get(OffsetDateTime.class).parse(value);
    }
    if (OffsetTime.class == clazz) {
        return datatypeRegistry.get(OffsetTime.class).parse(value);
    }
    if (Time.class == clazz) {
        LocalTime result = datatypeRegistry.get(LocalTime.class).parse(value);
        if (result == null) {
            return null;
        }
        return Time.valueOf(result);
    }
    if (BigDecimal.class == clazz)
        return datatypeRegistry.get(BigDecimal.class).parse(value);
    if (Boolean.class == clazz || Boolean.TYPE == clazz)
        return datatypeRegistry.get(Boolean.class).parse(value);
    if (Long.class == clazz || Long.TYPE == clazz)
        return datatypeRegistry.get(Long.class).parse(value);
    if (Double.class == clazz || Double.TYPE == clazz || Float.class == clazz || Float.TYPE == clazz)
        return datatypeRegistry.get(Double.class).parse(value);
    if (UUID.class == clazz)
        return UUID.fromString(value);
    if (Entity.class.isAssignableFrom(clazz)) {
        return entitySerializationAPI.entityFromJson(value, metadata.getClass(clazz));
    }
    if (Collection.class.isAssignableFrom(clazz)) {
        // if type argument for the collection is defined and is not entity, then do the basic deserialization
        if (argumentTypeClass != null) {
            if (!Entity.class.isAssignableFrom(argumentTypeClass)) {
                return deserialize(value, type);
            }
        }
        // if type argument for the collection is defined and is entity or if there is no type argument then try to
        // deserialize entities collection
        MetaClass metaClass = null;
        if (argumentTypeClass != null) {
            metaClass = metadata.getClass(argumentTypeClass);
            String entityName = restControllerUtils.transformEntityNameIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION);
            value = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, value);
        }
        return entitySerializationAPI.entitiesCollectionFromJson(value, metaClass);
    }
    return deserialize(value, clazz);
}
Also used : Entity(io.jmix.core.Entity) Time(java.sql.Time) ParameterizedType(java.lang.reflect.ParameterizedType) DateTimeParseException(java.time.format.DateTimeParseException) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaClass(io.jmix.core.metamodel.model.MetaClass) DateTimeParseException(java.time.format.DateTimeParseException) ParseException(java.text.ParseException) Nullable(javax.annotation.Nullable)

Example 24 with Entity

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

the class MultiEntityDataLoader method loadData.

@Override
public List<Map<String, Object>> loadData(ReportQuery dataSet, BandData parentBand, Map<String, Object> params) {
    Map<String, Object> additionalParams = dataSet.getAdditionalParams();
    String paramName = (String) additionalParams.get(DataSet.LIST_ENTITIES_PARAM_NAME);
    if (StringUtils.isBlank(paramName)) {
        paramName = DEFAULT_LIST_ENTITIES_PARAM_NAME;
    }
    boolean hasNestedCollection = paramName.contains(NESTED_COLLECTION_SEPARATOR);
    String entityParameterName = StringUtils.substringBefore(paramName, NESTED_COLLECTION_SEPARATOR);
    String nestedCollectionName = StringUtils.substringAfter(paramName, NESTED_COLLECTION_SEPARATOR);
    FetchPlan nestedCollectionFetchPLan = null;
    dataSet = ProxyWrapper.unwrap(dataSet);
    Object entities = null;
    if (params.containsKey(paramName)) {
        entities = params.get(paramName);
    } else if (hasNestedCollection && params.containsKey(entityParameterName)) {
        Entity entity = (Entity) params.get(entityParameterName);
        entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        if (entity != null) {
            entities = EntityValues.getValueEx(entity, nestedCollectionName);
            if (dataSet instanceof DataSet) {
                FetchPlan entityFetchPlan = getFetchPlan(entity, (DataSet) dataSet);
                if (entityFetchPlan != null && entityFetchPlan.getProperty(nestedCollectionName) != null) {
                    // noinspection ConstantConditions
                    nestedCollectionFetchPLan = entityFetchPlan.getProperty(nestedCollectionName).getFetchPlan();
                }
            }
        }
    }
    if (!(entities instanceof Collection)) {
        if (hasNestedCollection) {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter, " + "or the entity does not contain nested collection '%s'", entityParameterName, nestedCollectionName));
        } else {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter or it has type other than collection", paramName));
        }
    }
    Collection<Entity> entitiesList = (Collection) entities;
    params.put(paramName, entitiesList);
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Entity entity : entitiesList) {
        if (!hasNestedCollection) {
            entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        }
        if (dataSet instanceof DataSet) {
            if (hasNestedCollection) {
                if (nestedCollectionFetchPLan != null) {
                    resultList.add(new EntityMap(entity, nestedCollectionFetchPLan, beanFactory));
                } else {
                    resultList.add(new EntityMap(entity, beanFactory));
                }
            } else {
                resultList.add(new EntityMap(entity, getFetchPlan(entity, (DataSet) dataSet), beanFactory));
            }
        } else {
            resultList.add(new EntityMap(entity, beanFactory));
        }
    }
    return resultList;
}
Also used : Entity(io.jmix.core.Entity) DataSet(io.jmix.reports.entity.DataSet) ArrayList(java.util.ArrayList) FetchPlan(io.jmix.core.FetchPlan) EntityMap(io.jmix.reports.app.EntityMap) Collection(java.util.Collection) EntityMap(io.jmix.reports.app.EntityMap) Map(java.util.Map)

Example 25 with Entity

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

the class BulkEditorWindow method createNestedEmbeddedDatasources.

protected void createNestedEmbeddedDatasources(Datasource masterDs, MetaClass metaClass, String fqnPrefix) {
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        if (MetaProperty.Type.ASSOCIATION == metaProperty.getType() || MetaProperty.Type.COMPOSITION == metaProperty.getType() || MetaProperty.Type.EMBEDDED == metaProperty.getType()) {
            String fqn = metaProperty.getName();
            if (StringUtils.isNotEmpty(fqnPrefix)) {
                fqn = fqnPrefix + "." + fqn;
            }
            if (managedEmbeddedProperties.contains(fqn) && metadataTools.isEmbedded(metaProperty)) {
                MetaClass propertyMetaClass = metaProperty.getRange().asClass();
                @SuppressWarnings("unchecked") NestedDatasource<Entity> propertyDs = new EmbeddedDatasourceImpl();
                propertyDs.setup(fqn + "Ds", masterDs, metaProperty.getName());
                propertyDs.setAllowCommit(false);
                createNestedEmbeddedDatasources(propertyDs, propertyMetaClass, fqn);
                datasources.put(fqn, propertyDs);
                dsContext.register(propertyDs);
            }
        }
    }
}
Also used : Entity(io.jmix.core.Entity) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) EmbeddedDatasourceImpl(com.haulmont.cuba.gui.data.impl.EmbeddedDatasourceImpl)

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