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;
}
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;
}
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);
}
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;
}
Aggregations