Search in sources :

Example 21 with Range

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

the class GsonSerializationSupport method readFields.

protected void readFields(JsonReader in, MetaClass metaClass, Entity entity) throws IOException {
    while (in.hasNext()) {
        String propertyName = in.nextName();
        MetaProperty property = metaClass.findProperty(propertyName);
        if (property != null && !property.isReadOnly() && !exclude(entity.getClass(), propertyName)) {
            Class<?> propertyType = property.getJavaType();
            Range propertyRange = property.getRange();
            if (propertyRange.isDatatype()) {
                Object value = readSimpleProperty(in, propertyType);
                EntityValues.setValue(entity, propertyName, value);
            } else if (propertyRange.isClass()) {
                if (Entity.class.isAssignableFrom(propertyType)) {
                    EntityValues.setValue(entity, propertyName, readEntity(in));
                } else if (Collection.class.isAssignableFrom(propertyType)) {
                    Collection entities = readCollection(in, propertyType);
                    EntityValues.setValue(entity, propertyName, entities);
                } else {
                    in.skipValue();
                }
            } else if (propertyRange.isEnum()) {
                String stringValue = in.nextString();
                try {
                    Object value = propertyRange.asEnumeration().parse(stringValue);
                    EntityValues.setValue(entity, propertyName, value);
                } catch (ParseException e) {
                    throw new RuntimeException(format("An error occurred while parsing enum. Class [%s]. Value [%s].", propertyType, stringValue), e);
                }
            }
        } else {
            readUnresolvedProperty(entity, propertyName, in);
        }
    }
}
Also used : ParseException(java.text.ParseException) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 22 with Range

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

the class AbstractTableExporter method formatValue.

protected String formatValue(@Nullable Object cellValue, MetaPropertyPath metaPropertyPath) {
    if (cellValue == null) {
        if (metaPropertyPath.getRange().isDatatype()) {
            Class javaClass = metaPropertyPath.getRange().asDatatype().getJavaClass();
            if (Boolean.class.equals(javaClass)) {
                cellValue = false;
            }
        } else {
            return null;
        }
    }
    if (cellValue instanceof Number) {
        Number n = (Number) cellValue;
        Datatype datatype = null;
        Range range = metaPropertyPath.getMetaProperty().getRange();
        if (range.isDatatype()) {
            datatype = range.asDatatype();
        }
        datatype = datatype == null ? datatypeRegistry.get(n.getClass()) : datatype;
        return datatype.format(n);
    } else if (cellValue instanceof Date) {
        Class javaClass = null;
        MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
        if (metaProperty.getRange().isDatatype()) {
            javaClass = metaProperty.getRange().asDatatype().getJavaClass();
        }
        Date date = (Date) cellValue;
        if (Objects.equals(java.sql.Time.class, javaClass)) {
            return datatypeRegistry.get(java.sql.Time.class).format(date);
        } else if (Objects.equals(java.sql.Date.class, javaClass)) {
            return datatypeRegistry.get(java.sql.Date.class).format(date);
        } else {
            return datatypeRegistry.get(Date.class).format(date);
        }
    } else if (cellValue instanceof Boolean) {
        return String.valueOf(cellValue);
    } else if (cellValue instanceof Enum) {
        return messages.getMessage((Enum) cellValue);
    } else if (cellValue instanceof Entity) {
        return metadataTools.getInstanceName(cellValue);
    } else {
        return cellValue == null ? "" : cellValue.toString();
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) Range(io.jmix.core.metamodel.model.Range) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Date(java.util.Date) Datatype(io.jmix.core.metamodel.datatype.Datatype)

Example 23 with Range

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

the class DateIntervalDialog method filterOptionsByPropertyType.

protected void filterOptionsByPropertyType(@Nullable MetaPropertyPath mpp) {
    // DynAttr contains only Date or DateTime attributes.
    if (mpp == null) {
        return;
    }
    Range range = mpp.getRange();
    if (!range.isDatatype()) {
        throw new IllegalStateException("Value is not a simple type");
    }
    Class<?> javaClass = range.asDatatype().getJavaClass();
    if (timeClasses.contains(javaClass)) {
        timeUnitComboBox.setOptionsMap(getLocalizedEnumMap(Arrays.asList(TimeUnit.HOUR, TimeUnit.MINUTE)));
        List<Type> availableTypes = new ArrayList<>(Arrays.asList(Type.LAST, Type.NEXT));
        if (relativeMomentProvider != null) {
            availableTypes.add(Type.RELATIVE);
            relativeDateTimeComboBox.setOptionsMap(getLocalizedEnumMap(relativeMomentProvider.getRelativeTimeMoments()));
        }
        typeRadioButtonGroup.setOptionsMap(getLocalizedEnumMap(availableTypes));
    }
}
Also used : Type(io.jmix.ui.app.propertyfilter.dateinterval.model.BaseDateInterval.Type) Range(io.jmix.core.metamodel.model.Range)

Example 24 with Range

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

the class AggregatableDelegate method doAggregation.

protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
    Map<AggregationInfo, String> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        final Object value = doPropertyAggregation(aggregationInfo, itemIds);
        String formattedValue;
        if (aggregationInfo.getFormatter() != null) {
            formattedValue = aggregationInfo.getFormatter().apply(value);
        } else {
            // propertyPath could be null in case of custom aggregation
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            Range range = propertyPath != null ? propertyPath.getRange() : null;
            if (range != null && range.isDatatype()) {
                if (aggregationInfo.getType() != AggregationInfo.Type.COUNT) {
                    Class resultClass;
                    if (aggregationInfo.getStrategy() == null) {
                        Class rangeJavaClass = propertyPath.getRangeJavaClass();
                        Aggregation aggregation = aggregations.get(rangeJavaClass);
                        resultClass = aggregation.getResultClass();
                    } else {
                        resultClass = aggregationInfo.getStrategy().getResultClass();
                    }
                    Locale locale = currentAuthentication.getLocale();
                    formattedValue = datatypeRegistry.get(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            } else {
                if (aggregationInfo.getStrategy() != null) {
                    Class resultClass = aggregationInfo.getStrategy().getResultClass();
                    Locale locale = currentAuthentication.getLocale();
                    formattedValue = datatypeRegistry.get(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            }
        }
        aggregationResults.put(aggregationInfo, formattedValue);
    }
    return aggregationResults;
}
Also used : Aggregation(io.jmix.ui.component.data.aggregation.Aggregation) Locale(java.util.Locale) HashMap(java.util.HashMap) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) AggregationInfo(io.jmix.ui.component.AggregationInfo) Range(io.jmix.core.metamodel.model.Range)

Example 25 with Range

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

the class AbstractComponentGenerationStrategy method createComponentInternal.

@Nullable
protected Component createComponentInternal(ComponentGenerationContext context) {
    MetaClass metaClass = context.getMetaClass();
    MetaPropertyPath mpp = resolveMetaPropertyPath(metaClass, context.getProperty());
    if (mpp == null) {
        return null;
    }
    Range mppRange = mpp.getRange();
    Component resultComponent = null;
    if (Collection.class.isAssignableFrom(mpp.getMetaProperty().getJavaType())) {
        resultComponent = createCollectionField(context, mpp);
    } else if (mppRange.isDatatype()) {
        resultComponent = createDatatypeField(context, mpp);
    } else if (mppRange.isClass()) {
        resultComponent = createClassField(context, mpp);
    } else if (mppRange.isEnum()) {
        resultComponent = createEnumField(context);
    }
    return resultComponent;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable)

Aggregations

Range (io.jmix.core.metamodel.model.Range)27 MetaClass (io.jmix.core.metamodel.model.MetaClass)14 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)14 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)5 Nullable (javax.annotation.Nullable)5 MetadataTools (io.jmix.core.MetadataTools)3 Datatype (io.jmix.core.metamodel.datatype.Datatype)3 ParseException (java.text.ParseException)3 Entity (io.jmix.core.Entity)2 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)2 UiEntityContext (io.jmix.ui.accesscontext.UiEntityContext)2 ValueSource (io.jmix.ui.component.data.ValueSource)2 Aggregation (io.jmix.ui.component.data.aggregation.Aggregation)2 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)2 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Element (org.dom4j.Element)2