Search in sources :

Example 6 with EnumClass

use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.

the class InputDialogFacetProvider method loadEnumParameter.

@SuppressWarnings("unchecked")
protected InputParameter loadEnumParameter(Element paramEl, ComponentLoader.ComponentContext context) {
    String paramId = paramEl.attributeValue("id");
    String classFqn = paramEl.attributeValue("enumClass");
    InputParameter parameter;
    Class clazz = loadParamClass(paramEl, classFqn, context);
    if (EnumClass.class.isAssignableFrom(clazz)) {
        parameter = InputParameter.enumParameter(paramId, clazz).withCaption(loadParamCaption(paramEl, context)).withRequired(loadParamRequired(paramEl)).withRequiredMessage(loadRequiredMessage(paramEl, context));
        Field<?> field = loadField(paramId, paramEl, context);
        if (field != null) {
            parameter.withField(() -> field);
        }
    } else {
        throw new GuiDevelopmentException(String.format("Unable to create InputDialog parameter '%s'. Class '%s' is not enum class", paramId, classFqn), context);
    }
    return parameter;
}
Also used : GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) MetaClass(io.jmix.core.metamodel.model.MetaClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) InputParameter(io.jmix.ui.app.inputdialog.InputParameter)

Example 7 with EnumClass

use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.

the class ParamWrapper method getValue.

@Override
public Object getValue() {
    Object value = param.getValue();
    if (value instanceof String && !StringUtils.isEmpty((String) value) && !((String) value).startsWith(ParametersHelper.CASE_INSENSITIVE_MARKER)) {
        // try to wrap value for case-insensitive "like" search
        if (condition instanceof PropertyCondition || condition instanceof DynamicAttributesCondition) {
            String escapedValue = value.toString();
            if (condition.getEntityMetaClass() != null) {
                String thisStore = condition.getEntityMetaClass().getStore().getName();
                CubaProperties properties = AppBeans.get(CubaProperties.class);
                if (properties.getDisableEscapingLikeForDataStores() == null || !properties.getDisableEscapingLikeForDataStores().contains(thisStore)) {
                    escapedValue = QueryUtils.escapeForLike(escapedValue);
                }
            } else {
                escapedValue = QueryUtils.escapeForLike(escapedValue);
            }
            Op op = condition.getOperator();
            if (Op.CONTAINS.equals(op) || op.equals(Op.DOES_NOT_CONTAIN)) {
                value = wrapValueForLike(escapedValue);
            } else if (Op.STARTS_WITH.equals(op)) {
                value = wrapValueForLike(escapedValue, false, true);
            } else if (Op.ENDS_WITH.equals(op)) {
                value = wrapValueForLike(escapedValue, true, false);
            }
        } else if (condition instanceof CustomCondition) {
            String where = ((CustomCondition) condition).getWhere();
            Op op = condition.getOperator();
            Matcher matcher = LIKE_PATTERN.matcher(where);
            if (matcher.find()) {
                String stringValue = value.toString();
                boolean escape = StringUtils.isNotEmpty(matcher.group(3));
                if (escape) {
                    String escapeChar = matcher.group(4);
                    if (StringUtils.isNotEmpty(escapeChar)) {
                        stringValue = QueryUtils.escapeForLike(stringValue, escapeChar);
                    }
                }
                if (Op.STARTS_WITH.equals(op)) {
                    value = wrapValueForLike(stringValue, false, true);
                } else if (Op.ENDS_WITH.equals(op)) {
                    value = wrapValueForLike(stringValue, true, false);
                } else {
                    value = wrapValueForLike(stringValue);
                }
            }
        }
    } else if (value instanceof Entity) {
        value = EntityValues.getId(((Entity) value));
    } else if (value instanceof Collection) {
        List<Object> list = new ArrayList<>(((Collection) value).size());
        for (Object obj : ((Collection) value)) {
            list.add(obj instanceof Entity ? EntityValues.getId(((Entity) obj)) : obj);
        }
        value = list;
    } else if (value instanceof EnumClass) {
        value = ((EnumClass) value).getId();
    }
    return value;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) Entity(io.jmix.core.Entity) CustomCondition(com.haulmont.cuba.gui.components.filter.condition.CustomCondition) Matcher(java.util.regex.Matcher) DynamicAttributesCondition(com.haulmont.cuba.gui.components.filter.condition.DynamicAttributesCondition) ArrayList(java.util.ArrayList) CubaProperties(com.haulmont.cuba.CubaProperties) PropertyCondition(com.haulmont.cuba.gui.components.filter.condition.PropertyCondition) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) Collection(java.util.Collection)

Example 8 with EnumClass

use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.

the class ParameterTransformerImpl method createParameterValue.

@Override
public ParameterValue createParameterValue(Field field, ScreenFragment widgetFragment) {
    ParameterValue parameterValue = null;
    ParameterType parameterType = getParameterType(field);
    try {
        if (parameterType == ENTITY) {
            Object entity = FieldUtils.readField(field, widgetFragment, true);
            if (entity != null) {
                WidgetParam ann = field.getAnnotation(WidgetParam.class);
                String fetchPlan = StringUtils.isNoneBlank(ann.fetchPlanName()) ? ann.fetchPlanName() : null;
                parameterValue = new EntityParameterValue(metadata.getClass(entity).getName(), EntityValues.getId(entity).toString(), fetchPlan);
            }
        } else if (parameterType == ENTITY_LIST) {
            List<Entity> listEntity = (List) FieldUtils.readField(field, widgetFragment, true);
            WidgetParam ann = field.getAnnotation(WidgetParam.class);
            if (listEntity != null) {
                String fetchPlan = StringUtils.isNoneBlank(ann.fetchPlanName()) ? ann.fetchPlanName() : null;
                List<EntityParameterValue> resultList = listEntity.stream().map(entity -> new EntityParameterValue(metadata.getClass(entity).getName(), EntityValues.getId(entity).toString(), fetchPlan)).collect(Collectors.toList());
                parameterValue = new EntityListParameterValue(resultList);
            }
        } else if (parameterType == ENUM) {
            EnumClass<String> rawValue = (EnumClass) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                String enumClassName = rawValue.getClass().toString();
                parameterValue = new EnumParameterValue(enumClassName);
            }
        } else if (parameterType == DATE) {
            Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new DateParameterValue(rawValue);
            }
        } else if (parameterType == DATETIME) {
            Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new DateParameterValue(rawValue);
            }
        } else if (parameterType == TIME) {
            Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new DateParameterValue(rawValue);
            }
        } else if (parameterType == ParameterType.UUID) {
            UUID rawValue = (UUID) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new UuidParameterValue(rawValue);
            }
        } else if (parameterType == INTEGER) {
            Integer rawValue = (Integer) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new IntegerParameterValue(rawValue);
            }
        } else if (parameterType == STRING) {
            String rawValue = (String) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new StringParameterValue(rawValue);
            }
        } else if (parameterType == DECIMAL) {
            BigDecimal rawValue = (BigDecimal) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new DecimalParameterValue(rawValue);
            }
        } else if (parameterType == BOOLEAN) {
            Boolean rawValue = (Boolean) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new BooleanParameterValue(rawValue);
            }
        } else if (parameterType == LONG) {
            Long rawValue = (Long) FieldUtils.readField(field, widgetFragment, true);
            if (rawValue != null) {
                parameterValue = new LongParameterValue(rawValue);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error on parameter init", e);
    }
    return parameterValue;
}
Also used : WidgetParam(io.jmix.dashboardsui.annotation.WidgetParam) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) List(java.util.List) UUID(java.util.UUID) ParameterType(io.jmix.dashboards.model.parameter.ParameterType) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 9 with EnumClass

use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.

the class AppPropertiesLocator method getTypeStringify.

protected TypeStringify getTypeStringify(Method method) {
    try {
        Stringify stringifyAnn = method.getAnnotation(Stringify.class);
        if (stringifyAnn != null) {
            if ("".equals(stringifyAnn.method())) {
                return stringifyAnn.stringify().getDeclaredConstructor().newInstance();
            } else {
                String methodName = stringifyAnn.method();
                return new MethodTypeStringify(method.getReturnType().getMethod(methodName));
            }
        }
        Factory factoryAnn = method.getAnnotation(Factory.class);
        if (factoryAnn != null) {
            TypeStringify typeStringify = getTypeStringifyForFactory(factoryAnn.factory());
            if (typeStringify != null)
                return typeStringify;
        }
        if (Double.class.isAssignableFrom(method.getReturnType()) || Float.class.isAssignableFrom(method.getReturnType()) || double.class.isAssignableFrom(method.getReturnType()) || float.class.isAssignableFrom(method.getReturnType())) {
            return new DecimalStringify();
        }
        if (Date.class.isAssignableFrom(method.getReturnType())) {
            return new DateStringify();
        }
        if (Entity.class.isAssignableFrom(method.getReturnType())) {
            return new EntityStringify();
        }
        if (EnumClass.class.isAssignableFrom(method.getReturnType())) {
            EnumStore mode = method.getAnnotation(EnumStore.class);
            if (mode != null && EnumStoreMode.ID == mode.value()) {
                @SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) method.getReturnType();
                TypeStringify idStringify = TypeStringify.getInferred(ConfigUtil.getEnumIdType(enumeration));
                return new EnumClassStringify(idStringify);
            }
        }
        return TypeStringify.getInferred(method.getReturnType());
    } catch (Exception e) {
        log.warn("Error getting TypeStringify: " + e);
        return new PrimitiveTypeStringify();
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) ParseException(java.text.ParseException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 10 with EnumClass

use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.

the class AppPropertiesLocator method setDataType.

private void setDataType(Method method, AppPropertyEntity entity) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        if (returnType == boolean.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class));
        if (returnType == int.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class));
        if (returnType == long.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class));
        if (returnType == double.class || returnType == float.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
        if (returnType == char.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Character.class));
    } else if (returnType.isEnum()) {
        entity.setDataTypeName("enum");
        EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
        if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
            // noinspection unchecked
            Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
            entity.setEnumValues(Arrays.stream(enumeration.getEnumConstants()).map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
        } else {
            entity.setEnumValues(Arrays.stream(returnType.getEnumConstants()).map(Object::toString).collect(Collectors.joining(",")));
        }
    } else if (returnType == Float.class) {
        entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
    } else {
        Datatype<?> datatype = datatypes.get(returnType);
        if (datatype != null)
            entity.setDataTypeName(datatypes.getId(datatype));
        else
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
    }
    String dataTypeName = entity.getDataTypeName();
    if (!dataTypeName.equals("enum")) {
        Locale locale = userSessionSource.getLocale();
        Datatype datatype = Datatypes.get(dataTypeName);
        String v = null;
        try {
            v = entity.getDefaultValue();
            datatype.parse(v, locale);
            v = entity.getCurrentValue();
            datatype.parse(v, locale);
        } catch (ParseException e) {
            log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype, entity.getName());
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
        }
    }
}
Also used : LoadContext(com.haulmont.cuba.core.global.LoadContext) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) ClassMetadata(org.springframework.core.type.ClassMetadata) DatatypeRegistry(com.haulmont.chile.core.datatypes.DatatypeRegistry) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Metadata(com.haulmont.cuba.core.global.Metadata) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) MetadataReader(org.springframework.core.type.classreading.MetadataReader) ParseException(java.text.ParseException) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Resource(org.springframework.core.io.Resource) DataManager(com.haulmont.cuba.core.global.DataManager) com.haulmont.cuba.core.config.type(com.haulmont.cuba.core.config.type) Logger(org.slf4j.Logger) DecimalFormat(java.text.DecimalFormat) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) Datatype(io.jmix.core.metamodel.datatype.Datatype) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) Component(org.springframework.stereotype.Component) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) com.haulmont.cuba.core.config.defaults(com.haulmont.cuba.core.config.defaults) AppContext(com.haulmont.cuba.core.sys.AppContext) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) ParseException(java.text.ParseException) Datatype(io.jmix.core.metamodel.datatype.Datatype)

Aggregations

EnumClass (io.jmix.core.metamodel.datatype.impl.EnumClass)11 Method (java.lang.reflect.Method)3 EnumStore (com.haulmont.cuba.core.config.EnumStore)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)2 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)2 Component (org.springframework.stereotype.Component)2 DatatypeRegistry (com.haulmont.chile.core.datatypes.DatatypeRegistry)1 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)1 CubaProperties (com.haulmont.cuba.CubaProperties)1 com.haulmont.cuba.core.config.defaults (com.haulmont.cuba.core.config.defaults)1 Default (com.haulmont.cuba.core.config.defaults.Default)1 com.haulmont.cuba.core.config.type (com.haulmont.cuba.core.config.type)1 TypeStringify (com.haulmont.cuba.core.config.type.TypeStringify)1