Search in sources :

Example 1 with EnumClass

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

the class TypeFactory method getInstance.

/**
 * Get a TypeFactory instance appropriate for the return type of the
 * specified configuration interface method.
 *
 * @param configInterface The configuration interface.
 * @param method          The method.
 * @return An appropriate TypeFactory.
 * @throws IllegalArgumentException If the type is not supported.
 */
public static TypeFactory getInstance(Class<?> configInterface, Method method) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        returnType = ClassUtils.primitiveToWrapper(returnType);
    }
    Factory factory = ConfigUtil.getAnnotation(configInterface, method, Factory.class, true);
    if (factory != null) {
        try {
            if ("".equals(factory.method())) {
                return factory.factory().newInstance();
            } else {
                String methodName = factory.method();
                Method factoryMethod = returnType.getMethod(methodName, String.class);
                if (!isAcceptableMethod(returnType, factoryMethod)) {
                    throw new IllegalArgumentException("Invalid factory method: " + factoryMethod);
                }
                return new StaticTypeFactory(factoryMethod);
            }
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Unable to instantiate an type factory", e);
        }
    } else {
        if (Entity.class.isAssignableFrom(returnType)) {
            return AppBeans.get(ENTITY_FACTORY_BEAN_NAME, TypeFactory.class);
        } else {
            if (EnumClass.class.isAssignableFrom(returnType)) {
                EnumStore mode = ConfigUtil.getAnnotation(configInterface, method, EnumStore.class, true);
                if (mode != null && EnumStoreMode.ID == mode.value()) {
                    @SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
                    Class<?> idType = ConfigUtil.getEnumIdType(enumeration);
                    TypeFactory idFactory = getInferred(idType);
                    try {
                        Method fromIdMethod = returnType.getMethod("fromId", idType);
                        if (!isAcceptableMethod(returnType, fromIdMethod) || idFactory == null) {
                            throw new IllegalArgumentException("Cannot use method as factory method: " + method);
                        }
                        return new EnumClassFactory(idFactory, fromIdMethod);
                    } catch (NoSuchMethodException e) {
                        throw new IllegalArgumentException("fromId method is not found for " + enumeration.getName());
                    }
                }
            }
            TypeFactory factoryT = getInferred(returnType);
            if (factoryT == null) {
                throw new IllegalArgumentException("Unsupported return type for " + method);
            }
            return factoryT;
        }
    }
}
Also used : Method(java.lang.reflect.Method) EnumStore(com.haulmont.cuba.core.config.EnumStore) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 2 with EnumClass

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

the class TypeStringify method getInstance.

/**
 * Get a TypeStringify instance appropriate for the parameter type of the
 * specified configuration interface method.
 *
 * @param configInterface The configuration interface.
 * @param method          The method.
 * @return An appropriate TypeStringify.
 * @throws IllegalArgumentException If the type is not supported.
 */
public static TypeStringify getInstance(Class<?> configInterface, Method method) {
    Class<?> methodType = ConfigUtil.getMethodType(method);
    try {
        Stringify stringify = ConfigUtil.getAnnotation(configInterface, method, Stringify.class, true);
        if (stringify != null) {
            if ("".equals(stringify.method())) {
                return stringify.stringify().newInstance();
            } else {
                String methodName = stringify.method();
                return new MethodTypeStringify(methodType.getMethod(methodName));
            }
        } else {
            if ((method.getParameterTypes().length > 0)) {
                if (Entity.class.isAssignableFrom(method.getParameterTypes()[0])) {
                    return new EntityStringify();
                }
            }
            if (EnumClass.class.isAssignableFrom(methodType)) {
                EnumStore mode = ConfigUtil.getAnnotation(configInterface, method, EnumStore.class, true);
                if (mode != null && EnumStoreMode.ID == mode.value()) {
                    @SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) methodType;
                    TypeStringify idStringify = getInferred(ConfigUtil.getEnumIdType(enumeration));
                    return new EnumClassStringify(idStringify);
                }
            }
            return getInferred(methodType);
        }
    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | RuntimeException e) {
        throw new RuntimeException("Type stringify error", e);
    }
}
Also used : EnumStore(com.haulmont.cuba.core.config.EnumStore) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 3 with EnumClass

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

the class ConfigUtil method getDefaultValue.

/**
 * Get the default value of a configuration interface method. If a
 * {@link Default} annotation is present then that string is converted
 * to the appropriate type using the {@link TypeFactory} class.
 * Otherwise, for the type Foo, this searches for a FooDefault
 * annotation. If such an annotation is present then its value is
 * returned.
 *
 * @param configInterface The configuration interface.
 * @param method          The method.
 * @return The default value, or null.
 */
public static String getDefaultValue(Class<?> configInterface, Method method) {
    try {
        Default defaultValue = method.getAnnotation(Default.class);
        if (defaultValue != null) {
            return defaultValue.value();
        } else {
            Class<?> type = method.getReturnType();
            if (EnumClass.class.isAssignableFrom(type)) {
                @SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) type;
                EnumStore mode = getAnnotation(configInterface, method, EnumStore.class, true);
                if (mode != null && EnumStoreMode.ID == mode.value()) {
                    Class<?> idType = getEnumIdType(enumeration);
                    String name = "Default" + StringUtils.capitalize(ClassUtils.getShortClassName(idType));
                    Object value = getAnnotationValue(method, name);
                    if (value != null) {
                        Method fromId = enumeration.getDeclaredMethod("fromId", idType);
                        TypeStringify stringConverter = TypeStringify.getInstance(configInterface, method);
                        return stringConverter.stringify(fromId.invoke(null, value));
                    }
                    return NO_DEFAULT;
                }
            }
            String name = "Default" + StringUtils.capitalize(ClassUtils.getShortClassName(type));
            Object value = getAnnotationValue(method, name);
            if (value != null) {
                TypeStringify stringConverter = TypeStringify.getInstance(configInterface, method);
                return stringConverter.stringify(value);
            }
        }
        return NO_DEFAULT;
    } catch (Exception ex) {
        throw new RuntimeException("Default value error", ex);
    }
}
Also used : Method(java.lang.reflect.Method) Default(com.haulmont.cuba.core.config.defaults.Default) TypeStringify(com.haulmont.cuba.core.config.type.TypeStringify) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 4 with EnumClass

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

the class DataContextImpl method setPropertyValue.

protected void setPropertyValue(Object entity, MetaProperty property, @Nullable Object value, boolean checkEquals) {
    EntityPreconditions.checkEntityType(entity);
    if (!property.isReadOnly()) {
        EntityValues.setValue(entity, property.getName(), value, checkEquals);
    } else {
        AnnotatedElement annotatedElement = property.getAnnotatedElement();
        if (annotatedElement instanceof Field) {
            Field field = (Field) annotatedElement;
            field.setAccessible(true);
            if (value instanceof EnumClass) {
                value = ((EnumClass<?>) value).getId();
            }
            try {
                field.set(entity, value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Unable to set property value", e);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) AnnotatedElement(java.lang.reflect.AnnotatedElement) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 5 with EnumClass

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

the class JmixEclipseLinkQuery method internalSetParameter.

private TypedQuery<E> internalSetParameter(int position, Object value) {
    checkState();
    DbmsFeatures dbmsFeatures = dbmsSpecifics.getDbmsFeatures();
    if (isNative && (value instanceof UUID) && (dbmsFeatures.getUuidTypeClassName() != null)) {
        Class<?> c = ReflectionHelper.getClass(dbmsFeatures.getUuidTypeClassName());
        try {
            value = ReflectionHelper.newInstance(c, value);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Error setting parameter value", e);
        }
    } else if (value instanceof EnumClass) {
        value = ((EnumClass) value).getId();
    } else if (isCollectionOfEntitiesOrEnums(value)) {
        value = convertToCollectionOfIds(value);
    }
    params.add(new Param(position, value));
    return this;
}
Also used : DbmsFeatures(io.jmix.data.persistence.DbmsFeatures) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

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