Search in sources :

Example 11 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class EntityImportViewBuilder method buildFromJsonObject.

protected EntityImportView buildFromJsonObject(JsonObject jsonObject, MetaClass metaClass) {
    EntityImportView view = new EntityImportView(metaClass.getJavaClass());
    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        String propertyName = entry.getKey();
        MetaProperty metaProperty = metaClass.getProperty(propertyName);
        if (metaProperty != null) {
            Range propertyRange = metaProperty.getRange();
            Class<?> propertyType = metaProperty.getJavaType();
            if (propertyRange.isDatatype() || propertyRange.isEnum()) {
                if (security.isEntityAttrUpdatePermitted(metaClass, propertyName))
                    view.addLocalProperty(propertyName);
            } else if (propertyRange.isClass()) {
                if (Entity.class.isAssignableFrom(propertyType)) {
                    if (metadataTools.isEmbedded(metaProperty)) {
                        MetaClass propertyMetaClass = metadata.getClass(propertyType);
                        JsonElement propertyJsonObject = entry.getValue();
                        if (!propertyJsonObject.isJsonObject()) {
                            throw new RuntimeException("JsonObject was expected for property " + propertyName);
                        }
                        if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) {
                            EntityImportView propertyImportView = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
                            view.addEmbeddedProperty(propertyName, propertyImportView);
                        }
                    } else {
                        MetaClass propertyMetaClass = metadata.getClass(propertyType);
                        if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
                            JsonElement propertyJsonObject = entry.getValue();
                            if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) {
                                if (propertyJsonObject.isJsonNull()) {
                                    // in case of null we must add such import behavior to update the reference with null value later
                                    if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                        view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
                                    } else {
                                        view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
                                    }
                                } else {
                                    if (!propertyJsonObject.isJsonObject()) {
                                        throw new RuntimeException("JsonObject was expected for property " + propertyName);
                                    }
                                    EntityImportView propertyImportView = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
                                    if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                        view.addManyToOneProperty(propertyName, propertyImportView);
                                    } else {
                                        view.addOneToOneProperty(propertyName, propertyImportView);
                                    }
                                }
                            }
                        } else {
                            if (security.isEntityAttrUpdatePermitted(metaClass, propertyName))
                                if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                    view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
                                } else {
                                    view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
                                }
                        }
                    }
                } else if (Collection.class.isAssignableFrom(propertyType)) {
                    MetaClass propertyMetaClass = metaProperty.getRange().asClass();
                    switch(metaProperty.getRange().getCardinality()) {
                        case MANY_TO_MANY:
                            if (security.isEntityAttrUpdatePermitted(metaClass, propertyName))
                                view.addManyToManyProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
                            break;
                        case ONE_TO_MANY:
                            if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
                                JsonElement compositionJsonArray = entry.getValue();
                                if (!compositionJsonArray.isJsonArray()) {
                                    throw new RuntimeException("JsonArray was expected for property " + propertyName);
                                }
                                EntityImportView propertyImportView = buildFromJsonArray(compositionJsonArray.getAsJsonArray(), propertyMetaClass);
                                if (security.isEntityAttrUpdatePermitted(metaClass, propertyName))
                                    view.addOneToManyProperty(propertyName, propertyImportView, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
                            }
                            break;
                    }
                }
            }
        }
    }
    return view;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) JsonElement(com.google.gson.JsonElement) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 12 with Range

use of com.haulmont.chile.core.model.Range in project cuba by cuba-platform.

the class PropertyWrapper method valueOf.

protected Object valueOf(Object newValue) throws Converter.ConversionException {
    if (newValue == null) {
        return null;
    }
    final Range range = propertyPath.getRange();
    if (range == null) {
        return newValue;
    } else {
        final Object obj;
        if (range.isDatatype()) {
            Datatype<Object> datatype = range.asDatatype();
            if (newValue instanceof String) {
                try {
                    newValue = Strings.emptyToNull((String) newValue);
                    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                    obj = datatype.parse((String) newValue, sessionSource.getLocale());
                } catch (ParseException e) {
                    throw new Converter.ConversionException(e);
                }
            } else {
                if (newValue.getClass().equals(datatype.getJavaClass())) {
                    return newValue;
                } else {
                    Datatype newValueDatatype = Datatypes.getNN(newValue.getClass());
                    // noinspection unchecked
                    String str = newValueDatatype.format(newValue);
                    try {
                        obj = datatype.parse(str);
                    } catch (ParseException e) {
                        throw new Converter.ConversionException(e);
                    }
                }
            }
        } else {
            obj = newValue;
        }
        return obj;
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Converter(com.vaadin.data.util.converter.Converter) ParseException(java.text.ParseException) Range(com.haulmont.chile.core.model.Range) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

Range (com.haulmont.chile.core.model.Range)12 MetaProperty (com.haulmont.chile.core.model.MetaProperty)7 MetaClass (com.haulmont.chile.core.model.MetaClass)6 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)3 Entity (com.haulmont.cuba.core.entity.Entity)3 Element (org.dom4j.Element)3 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)2 AppConfig (com.haulmont.cuba.gui.AppConfig)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Formatter (com.haulmont.cuba.gui.components.Formatter)2 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)2 DsContextImplementation (com.haulmont.cuba.gui.data.impl.DsContextImplementation)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 Inject (javax.inject.Inject)2 StringUtils (org.apache.commons.lang.StringUtils)2 Files (com.google.common.io.Files)1 JsonElement (com.google.gson.JsonElement)1