use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class AbstractTable method getParsedAggregationValue.
@Nullable
protected Object getParsedAggregationValue(String value, Object columnId) throws ParseException {
Object parsedValue = value;
for (Column column : getColumns()) {
if (column.getId().equals(columnId)) {
AggregationInfo aggregationInfo = column.getAggregation();
if (aggregationInfo == null || aggregationInfo.getFormatter() != null) {
return parsedValue;
}
MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
Class<?> resultClass;
Range range = propertyPath != null ? propertyPath.getRange() : null;
if (range != null && range.isDatatype()) {
if (aggregationInfo.getType() == AggregationInfo.Type.COUNT) {
return parsedValue;
}
if (aggregationInfo.getStrategy() == null) {
Class<?> rangeJavaClass = propertyPath.getRangeJavaClass();
Aggregation aggregation = aggregations.get(rangeJavaClass);
resultClass = aggregation.getResultClass();
} else {
resultClass = aggregationInfo.getStrategy().getResultClass();
}
} else if (aggregationInfo.getStrategy() == null) {
return parsedValue;
} else {
resultClass = aggregationInfo.getStrategy().getResultClass();
}
parsedValue = datatypeRegistry.get(resultClass).parse(value, locale);
break;
}
}
return parsedValue;
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class AppSettingsToolsImpl method getDefaultPropertyValue.
@Nullable
@Override
public Object getDefaultPropertyValue(Class<? extends AppSettingsEntity> clazz, String propertyName) {
Field field = ReflectionUtils.findField(clazz, propertyName);
if (field == null) {
throw new IllegalArgumentException("Unable to find property " + propertyName + " for class " + clazz);
}
if (field.isAnnotationPresent(AppSettingsDefaultBoolean.class)) {
return field.getAnnotation(AppSettingsDefaultBoolean.class).value();
} else if (field.isAnnotationPresent(AppSettingsDefaultDouble.class)) {
return field.getAnnotation(AppSettingsDefaultDouble.class).value();
} else if (field.isAnnotationPresent(AppSettingsDefaultInt.class)) {
return field.getAnnotation(AppSettingsDefaultInt.class).value();
} else if (field.isAnnotationPresent(AppSettingsDefaultLong.class)) {
return field.getAnnotation(AppSettingsDefaultLong.class).value();
} else if (field.isAnnotationPresent(AppSettingsDefault.class)) {
String annotationValue = field.getAnnotation(AppSettingsDefault.class).value();
Range range = metadata.getClass(clazz).getProperty(propertyName).getRange();
try {
if (range.isEnum()) {
return range.asEnumeration().parse(annotationValue);
} else if (range.isClass() && !range.getCardinality().isMany()) {
MetaClass metaClass = range.asClass();
MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(metaClass);
if (primaryKeyProperty == null) {
log.warn("Primary pk property for metaClass {} cannot be determined", metaClass);
throw new IllegalStateException("Primary pk property for metaClass " + metaClass + " cannot be determined");
}
Object pkValue = Objects.requireNonNull(primaryKeyProperty.getRange().asDatatype().parse(annotationValue));
return dataManager.load(metaClass.getJavaClass()).id(pkValue).optional().orElse(null);
} else if (range.isDatatype()) {
return datatypeRegistry.get(range.asDatatype().getId()).parse(annotationValue);
} else {
return null;
}
} catch (ParseException e) {
log.warn("Unable to get default value for property {} and class {} due to exception :\n{}", propertyName, clazz, e.getMessage());
}
}
return null;
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class InspectorFormBuilder method addField.
/**
* Adds field to the specified form.
* If the field should be custom, adds it to the specified customFields collection
* which can be used later to create fieldGenerators
*
* @param metaProperty meta property of the item's property which field is creating
* @param form field group to which created field will be added
*/
protected void addField(InstanceContainer container, Form form, MetaProperty metaProperty, boolean isReadonly) {
MetaClass metaClass = container.getEntityMetaClass();
Range range = metaProperty.getRange();
boolean isRequired = isRequired(metaProperty);
UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, metaProperty.getName());
accessManager.applyRegisteredConstraints(attributeContext);
if (!attributeContext.canView())
return;
if (range.isClass()) {
UiEntityContext entityContext = new UiEntityContext(range.asClass());
accessManager.applyRegisteredConstraints(entityContext);
if (!entityContext.isViewPermitted()) {
return;
}
}
ValueSource valueSource = new ContainerValueSource<>(container, metaProperty.getName());
ComponentGenerationContext componentContext = new ComponentGenerationContext(metaClass, metaProperty.getName());
componentContext.setValueSource(valueSource);
Field field = (Field) uiComponentsGenerator.generate(componentContext);
if (requireTextArea(metaProperty, getItem(), MAX_TEXTFIELD_STRING_LENGTH)) {
field = uiComponents.create(TextArea.NAME);
}
if (isBoolean(metaProperty)) {
field = createBooleanField();
}
if (range.isClass()) {
EntityPicker pickerField = uiComponents.create(EntityPicker.class);
EntityLookupAction lookupAction = actions.create(EntityLookupAction.class);
lookupAction.setScreenClass(EntityInspectorBrowser.class);
lookupAction.setScreenOptionsSupplier(() -> new MapScreenOptions(ParamsMap.of("entity", metaProperty.getRange().asClass().getName())));
lookupAction.setOpenMode(OpenMode.THIS_TAB);
pickerField.addAction(lookupAction);
pickerField.addAction(actions.create(EntityClearAction.class));
field = pickerField;
}
field.setValueSource(valueSource);
field.setCaption(getPropertyCaption(metaClass, metaProperty));
field.setRequired(isRequired);
isReadonly = isReadonly || (disabledProperties != null && disabledProperties.contains(metaProperty.getName()));
if (range.isClass() && !metadataTools.isEmbedded(metaProperty)) {
field.setEditable(metadataTools.isOwningSide(metaProperty) && !isReadonly);
} else {
field.setEditable(!isReadonly);
}
field.setWidth(fieldWidth);
if (isRequired) {
field.setRequiredMessage(messageTools.getDefaultRequiredMessage(metaClass, metaProperty.getName()));
}
form.add(field);
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class ExcelExporter method formatValueCell.
protected void formatValueCell(Cell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) {
if (cellValue == null) {
if (metaPropertyPath != null && metaPropertyPath.getRange().isDatatype()) {
Class javaClass = metaPropertyPath.getRange().asDatatype().getJavaClass();
if (Boolean.class.equals(javaClass)) {
cellValue = false;
}
} else {
return;
}
}
String childCountValue = "";
if (groupChildCount != null) {
childCountValue = " (" + groupChildCount + ")";
}
if (cellValue instanceof Number) {
Number n = (Number) cellValue;
Datatype datatype = null;
if (metaPropertyPath != null) {
Range range = metaPropertyPath.getMetaProperty().getRange();
if (range.isDatatype()) {
datatype = range.asDatatype();
}
}
datatype = datatype == null ? datatypeRegistry.get(n.getClass()) : datatype;
String str;
// and we should skip it
if (sizersIndex == 0 && level > 0) {
str = createSpaceString(level) + datatype.format(n);
cell.setCellValue(str);
} else {
try {
str = datatype.format(n);
Number result = (Number) datatype.parse(str);
if (result != null) {
if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
cell.setCellValue(result.longValue());
cell.setCellStyle(integerFormatCellStyle);
} else {
cell.setCellValue(result.doubleValue());
cell.setCellStyle(doubleFormatCellStyle);
}
}
} catch (ParseException e) {
throw new RuntimeException("Unable to parse numeric value", e);
}
cell.setCellType(CellType.NUMERIC);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Date) {
Class javaClass = null;
if (metaPropertyPath != null) {
MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
if (metaProperty.getRange().isDatatype()) {
javaClass = metaProperty.getRange().asDatatype().getJavaClass();
}
}
Date date = (Date) cellValue;
cell.setCellValue(date);
if (Objects.equals(java.sql.Time.class, javaClass)) {
cell.setCellStyle(timeFormatCellStyle);
} else if (Objects.equals(java.sql.Date.class, javaClass)) {
cell.setCellStyle(dateFormatCellStyle);
} else {
cell.setCellStyle(dateTimeFormatCellStyle);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
String str = datatypeRegistry.get(Date.class).format(date);
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Boolean) {
String str = "";
if (sizersIndex == 0) {
str += createSpaceString(level);
}
str += ((Boolean) cellValue) ? getMessage("excelExporter.true") : getMessage("excelExporter.false");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Enum) {
String message = (sizersIndex == 0 ? createSpaceString(level) : "") + messages.getMessage((Enum) cellValue);
cell.setCellValue(message + childCountValue);
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(message, stdFont);
}
} else if (cellValue instanceof Entity) {
Object entityVal = cellValue;
String instanceName = metadataTools.getInstanceName(entityVal);
String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Collection) {
String str = "";
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof byte[]) {
String str = messages.getMessage("excelExporter.bytes");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else {
String strValue = cellValue == null ? "" : cellValue.toString();
String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
}
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class EntityDifferenceManagerImpl method getPropertyDifference.
/**
* Return difference between property values
*
* @param firstValue First value
* @param secondValue Second value
* @param metaProperty Meta Property
* @param fetchPlanProperty FetchPlan property
* @param diffBranch Branch with passed diffs
* @return Diff
*/
@Nullable
private EntityPropertyDifferenceModel getPropertyDifference(@Nullable Object firstValue, @Nullable Object secondValue, MetaProperty metaProperty, FetchPlanProperty fetchPlanProperty, Stack<Object> diffBranch) {
EntityPropertyDifferenceModel propertyDiff = null;
Range range = metaProperty.getRange();
if (range.isDatatype() || range.isEnum()) {
if (!Objects.equals(firstValue, secondValue)) {
EntityBasicPropertyDifferenceModel basicPropertyDiff = metadata.create(EntityBasicPropertyDifferenceModel.class);
basicPropertyDiff.setBeforeValue(firstValue);
basicPropertyDiff.setAfterValue(secondValue);
basicPropertyDiff.setMetaProperty(metaProperty);
propertyDiff = basicPropertyDiff;
}
} else if (range.getCardinality().isMany()) {
propertyDiff = getCollectionDiff(firstValue, secondValue, fetchPlanProperty, metaProperty, diffBranch);
} else if (range.isClass()) {
propertyDiff = getClassDiff(firstValue, secondValue, fetchPlanProperty, metaProperty, diffBranch);
}
return propertyDiff;
}
Aggregations