Search in sources :

Example 1 with FormDescriptorGenerationException

use of alien4cloud.ui.form.exception.FormDescriptorGenerationException in project alien4cloud by alien4cloud.

the class PropertyDefinitionConverter method convert.

public PropertyDefinition convert(FormPropertyDefinition definitionAnnotation) {
    if (definitionAnnotation == null) {
        return null;
    }
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(definitionAnnotation.type());
    // FIXME ? can be other than a scalar here ?
    propertyDefinition.setDefault(new ScalarPropertyValue(definitionAnnotation.defaultValue()));
    propertyDefinition.setDescription(definitionAnnotation.description());
    propertyDefinition.setPassword(definitionAnnotation.isPassword());
    propertyDefinition.setRequired(definitionAnnotation.isRequired());
    List<PropertyConstraint> constraints = Lists.newArrayList();
    if (!definitionAnnotation.constraints().equal().isEmpty()) {
        EqualConstraint equalConstraint = new EqualConstraint();
        equalConstraint.setEqual(definitionAnnotation.constraints().equal());
        constraints.add(equalConstraint);
    }
    if (!definitionAnnotation.constraints().greaterOrEqual().isEmpty()) {
        GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
        greaterOrEqualConstraint.setGreaterOrEqual(definitionAnnotation.constraints().greaterOrEqual());
        constraints.add(greaterOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().greaterThan().isEmpty()) {
        GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
        greaterThanConstraint.setGreaterThan(definitionAnnotation.constraints().greaterThan());
        constraints.add(greaterThanConstraint);
    }
    if (!definitionAnnotation.constraints().inRange().isEmpty()) {
        String inRangeText = definitionAnnotation.constraints().inRange();
        Matcher matcher = IN_RANGE_REGEXP.matcher(inRangeText);
        if (matcher.matches()) {
            InRangeConstraint inRangeConstraint = new InRangeConstraint();
            inRangeConstraint.setRangeMinValue(matcher.group(1).trim());
            inRangeConstraint.setRangeMaxValue(matcher.group(2).trim());
            constraints.add(inRangeConstraint);
        } else {
            throw new FormDescriptorGenerationException("In range constraint definition must be in this format '[ $min - $max ]'");
        }
    }
    if (definitionAnnotation.constraints().length() >= 0) {
        LengthConstraint lengthConstraint = new LengthConstraint();
        lengthConstraint.setLength(definitionAnnotation.constraints().length());
        constraints.add(lengthConstraint);
    }
    if (!definitionAnnotation.constraints().lessOrEqual().isEmpty()) {
        LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
        lessOrEqualConstraint.setLessOrEqual(definitionAnnotation.constraints().lessOrEqual());
        constraints.add(lessOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().lessThan().isEmpty()) {
        LessThanConstraint lessThanConstraint = new LessThanConstraint();
        lessThanConstraint.setLessThan(definitionAnnotation.constraints().lessThan());
        constraints.add(lessThanConstraint);
    }
    if (definitionAnnotation.constraints().maxLength() >= 0) {
        MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
        maxLengthConstraint.setMaxLength(definitionAnnotation.constraints().maxLength());
        constraints.add(maxLengthConstraint);
    }
    if (definitionAnnotation.constraints().minLength() >= 0) {
        MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
        minLengthConstraint.setMinLength(definitionAnnotation.constraints().minLength());
        constraints.add(minLengthConstraint);
    }
    if (!definitionAnnotation.constraints().pattern().isEmpty()) {
        PatternConstraint patternConstraint = new PatternConstraint();
        patternConstraint.setPattern(definitionAnnotation.constraints().pattern());
        constraints.add(patternConstraint);
    }
    if (definitionAnnotation.constraints().validValues().length > 0) {
        ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
        validValuesConstraint.setValidValues(Lists.newArrayList(definitionAnnotation.constraints().validValues()));
        constraints.add(validValuesConstraint);
    }
    if (!constraints.isEmpty()) {
        propertyDefinition.setConstraints(constraints);
    }
    return propertyDefinition;
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) Matcher(java.util.regex.Matcher) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) FormPropertyDefinition(alien4cloud.ui.form.annotation.FormPropertyDefinition) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)

Example 2 with FormDescriptorGenerationException

use of alien4cloud.ui.form.exception.FormDescriptorGenerationException in project alien4cloud by alien4cloud.

the class PojoFormDescriptorGenerator method buildSimpleTypeDescriptor.

private Map<String, Object> buildSimpleTypeDescriptor(Class<?> clazz) {
    Map<String, Object> type = Maps.newHashMap();
    String typeName;
    if (clazz.isPrimitive()) {
        if (char.class == clazz) {
            throw new FormDescriptorGenerationException("Cannot handle 'char' type");
        } else if (boolean.class == clazz) {
            typeName = BOOLEAN_TYPE;
        } else {
            typeName = NUMBER_TYPE;
        }
    } else if (Number.class.isAssignableFrom(clazz)) {
        typeName = NUMBER_TYPE;
    } else if (Boolean.class == clazz) {
        typeName = BOOLEAN_TYPE;
    } else if (Character.class == clazz) {
        throw new FormDescriptorGenerationException("Cannot handle 'Character' type");
    } else if (String.class == clazz) {
        typeName = STRING_TYPE;
    } else if (Date.class.isAssignableFrom(clazz)) {
        typeName = DATE_TYPE;
    } else {
        throw new FormDescriptorGenerationException("Unmanaged primitive type [" + clazz.getName() + "]");
    }
    type.put(TYPE_KEY, typeName);
    return type;
}
Also used : FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) Date(java.util.Date)

Example 3 with FormDescriptorGenerationException

use of alien4cloud.ui.form.exception.FormDescriptorGenerationException 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)

Example 4 with FormDescriptorGenerationException

use of alien4cloud.ui.form.exception.FormDescriptorGenerationException in project alien4cloud by alien4cloud.

the class PojoFormDescriptorGenerator method buildSequenceTypeDescriptor.

private Map<String, Object> buildSequenceTypeDescriptor(String sequenceTypeName, Class<?> contentType) {
    Map<String, Object> type = Maps.newHashMap();
    type.put(TYPE_KEY, sequenceTypeName);
    if (isPrimitive(contentType)) {
        type.put(CONTENT_TYPE_KEY, buildSimpleTypeDescriptor(contentType));
    } else if (Map.class.isAssignableFrom(contentType)) {
        throw new FormDescriptorGenerationException("Cannot generate meta data for field of type set of set, set of array, array of array, map of set ... ");
    } else if (List.class.isAssignableFrom(contentType) || Set.class.isAssignableFrom(contentType)) {
        throw new FormDescriptorGenerationException("Cannot generate meta data for field of type set of set, set of array, array of array, map of set ... ");
    } else if (contentType.isArray()) {
        throw new FormDescriptorGenerationException("Cannot generate meta data for field of type set of set, set of array, array of array, map of set ... ");
    } else {
        type.put(CONTENT_TYPE_KEY, buildComplexTypeDescriptor(contentType));
    }
    return type;
}
Also used : Set(java.util.Set) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) List(java.util.List) Map(java.util.Map)

Aggregations

FormDescriptorGenerationException (alien4cloud.ui.form.exception.FormDescriptorGenerationException)4 FormPropertyDefinition (alien4cloud.ui.form.annotation.FormPropertyDefinition)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 FormCustomType (alien4cloud.ui.form.annotation.FormCustomType)1 FormProperties (alien4cloud.ui.form.annotation.FormProperties)1 FormType (alien4cloud.ui.form.annotation.FormType)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)1 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)1 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)1 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)1 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)1