Search in sources :

Example 6 with Range

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;
}
Also used : Aggregation(io.jmix.ui.component.data.aggregation.Aggregation) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable)

Example 7 with Range

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;
}
Also used : Range(io.jmix.core.metamodel.model.Range) Field(java.lang.reflect.Field) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetadataObject(io.jmix.core.metamodel.model.MetadataObject) ParseException(java.text.ParseException) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 8 with Range

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);
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) Range(io.jmix.core.metamodel.model.Range) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) EntityClearAction(io.jmix.ui.action.entitypicker.EntityClearAction) MetaClass(io.jmix.core.metamodel.model.MetaClass) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) EntityLookupAction(io.jmix.ui.action.entitypicker.EntityLookupAction) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions)

Example 9 with Range

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);
        }
    }
}
Also used : Entity(io.jmix.core.Entity) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Range(io.jmix.core.metamodel.model.Range) Datatype(io.jmix.core.metamodel.datatype.Datatype) ParseException(java.text.ParseException) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 10 with Range

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;
}
Also used : 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