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);
}
}
}
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();
}
}
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));
}
}
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;
}
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;
}
Aggregations