Search in sources :

Example 1 with FormType

use of alien4cloud.ui.form.annotation.FormType in project alien4cloud by alien4cloud.

the class PojoFormDescriptorGenerator method doParseClass.

private void doParseClass(Class<?> clazz, Map<String, Object> descriptors) {
    Map<String, Object> propertyTypes = Maps.newHashMap();
    descriptors.put(PROPERTY_TYPE_KEY, propertyTypes);
    PropertyDescriptor[] properties = ReflectionUtil.getPropertyDescriptors(clazz);
    String[] orderedPropertiesNames;
    Set<String> propertiesNames;
    FormProperties formPropAnnotation = clazz.getAnnotation(FormProperties.class);
    if (formPropAnnotation != null && formPropAnnotation.value() != null) {
        orderedPropertiesNames = formPropAnnotation.value();
        propertiesNames = Sets.newHashSet(orderedPropertiesNames);
    } else {
        propertiesNames = Sets.newLinkedHashSet();
        for (PropertyDescriptor property : properties) {
            if (property.getReadMethod() != null && property.getWriteMethod() != null) {
                propertiesNames.add(property.getName());
            }
        }
        orderedPropertiesNames = propertiesNames.toArray(new String[propertiesNames.size()]);
        if (log.isDebugEnabled()) {
            log.debug("Class " + clazz.getName() + " do not have FormProperties annotation, all properties will be read and order will not be assured");
        }
    }
    for (PropertyDescriptor property : properties) {
        if ((!propertiesNames.contains(property.getName())) || property.getReadMethod() == null || property.getWriteMethod() == null) {
            continue;
        }
        Class<?> propClazz = property.getPropertyType();
        Map<String, FormType> implementations = getImplementations(clazz, property);
        Map<String, FormType> contentImplementations = getContentImplementations(clazz, property);
        PropertyDefinition propertyDefinition = getPropertyDefinition(clazz, property);
        String customFormType = getCustomFormType(clazz, property);
        String[] validValues = getValidValues(clazz, property);
        String label = getLabel(clazz, property);
        Map<String, Object> type = Maps.newHashMap();
        if (customFormType != null) {
            // Custom type that cannot be processed in a generic way on ui side
            type.put(TYPE_KEY, customFormType);
        } else if (propertyDefinition != null) {
            type.put(TYPE_KEY, TOSCA_TYPE);
            type.put(TOSCA_DEFINITION_KEY, propertyDefinition);
            if (propertyDefinition.isRequired()) {
                type.put(NOT_NULL_KEY, true);
            }
        } else if (isPrimitive(propClazz)) {
            // Primitive type
            type = buildSimpleTypeDescriptor(propClazz);
            if (validValues != null && validValues.length > 0) {
                type.put(VALID_VALUES_KEY, validValues);
            }
        } else if (Map.class.isAssignableFrom(propClazz)) {
            // Map type
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(MAP_TYPE, contentImplementations);
            } else {
                ParameterizedType parameterizedType = (ParameterizedType) property.getReadMethod().getGenericReturnType();
                Type[] types = parameterizedType.getActualTypeArguments();
                Class<?> keyClass = (Class<?>) types[0];
                Class<?> valueClass = (Class<?>) types[1];
                if (keyClass != String.class) {
                    throw new FormDescriptorGenerationException("Cannot generate meta data for map with key not of type String");
                }
                type = buildSequenceTypeDescriptor(MAP_TYPE, valueClass);
            }
        } else if (List.class.isAssignableFrom(propClazz) || Set.class.isAssignableFrom(propClazz)) {
            // List or set types
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(ARRAY_TYPE, contentImplementations);
            } else {
                ParameterizedType pt = (ParameterizedType) property.getReadMethod().getGenericReturnType();
                Type[] types = pt.getActualTypeArguments();
                Class<?> valueClass = (Class<?>) types[0];
                type = buildSequenceTypeDescriptor(ARRAY_TYPE, valueClass);
            }
        } else if (propClazz.isArray()) {
            // Array type
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(ARRAY_TYPE, contentImplementations);
            } else {
                Class<?> valueClass = property.getReadMethod().getReturnType().getComponentType();
                type = buildSequenceTypeDescriptor(ARRAY_TYPE, valueClass);
            }
        } else if (implementations != null && !implementations.isEmpty()) {
            type = buildAbstractTypeDescriptor(implementations);
        } else {
            // Complex type
            Class<?> valueClass = property.getReadMethod().getReturnType();
            type = buildComplexTypeDescriptor(valueClass);
        }
        Map<String, Object> suggestion = getSuggestion(clazz, property);
        if (suggestion != null) {
            type.put(SUGGESTION_KEY, suggestion);
        }
        if (isNotNull(clazz, property)) {
            type.put(NOT_NULL_KEY, true);
        }
        if (isPassword(clazz, property)) {
            type.put(IS_PASSWORD_KEY, true);
        }
        if (label != null) {
            type.put(LABEL_KEY, label);
        }
        propertyTypes.put(property.getName(), type);
    }
    descriptors.put(TYPE_KEY, COMPLEX_TYPE);
    descriptors.put(ORDER_KEY, orderedPropertiesNames);
}
Also used : Set(java.util.Set) PropertyDescriptor(java.beans.PropertyDescriptor) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) FormProperties(alien4cloud.ui.form.annotation.FormProperties) FormType(alien4cloud.ui.form.annotation.FormType) FormPropertyDefinition(alien4cloud.ui.form.annotation.FormPropertyDefinition) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ParameterizedType(java.lang.reflect.ParameterizedType) FormType(alien4cloud.ui.form.annotation.FormType) FormCustomType(alien4cloud.ui.form.annotation.FormCustomType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) List(java.util.List) Map(java.util.Map)

Aggregations

FormCustomType (alien4cloud.ui.form.annotation.FormCustomType)1 FormProperties (alien4cloud.ui.form.annotation.FormProperties)1 FormPropertyDefinition (alien4cloud.ui.form.annotation.FormPropertyDefinition)1 FormType (alien4cloud.ui.form.annotation.FormType)1 FormDescriptorGenerationException (alien4cloud.ui.form.exception.FormDescriptorGenerationException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)1