Search in sources :

Example 1 with FieldRulePropertyBuilder

use of com.thinkbiganalytics.policy.rest.model.FieldRulePropertyBuilder in project kylo by Teradata.

the class BasePolicyAnnotationTransformer method getUiProperties.

/**
 * For a given domain policy class extract the user interface {@link FieldRuleProperty} classes parsing the fields annotated with the {@link PolicyProperty}
 *
 * @param policyClass the domain policy class to parse
 * @return a list of user interface fields annotated with the {@link PolicyProperty}
 */
public List<FieldRuleProperty> getUiProperties(Class<P> policyClass) {
    AnnotationFieldNameResolver annotationFieldNameResolver = new AnnotationFieldNameResolver(PolicyProperty.class);
    List<AnnotatedFieldProperty> list = annotationFieldNameResolver.getProperties(policyClass);
    List<FieldRuleProperty> properties = new ArrayList<>();
    Map<String, List<FieldRuleProperty>> groupedProperties = new HashMap<>();
    if (hasConstructor(policyClass)) {
        Map<String, Integer> groupOrder = new HashMap<>();
        for (AnnotatedFieldProperty<PolicyProperty> annotatedFieldProperty : list) {
            PolicyProperty prop = annotatedFieldProperty.getAnnotation();
            String value = StringUtils.isBlank(prop.value()) ? null : prop.value();
            String group = prop.group();
            Integer order = 0;
            if (!groupOrder.containsKey(group)) {
                groupOrder.put(group, order);
            }
            order = groupOrder.get(group);
            order++;
            groupOrder.put(group, order);
            FieldRuleProperty rule = new FieldRulePropertyBuilder(prop.name()).displayName(StringUtils.isNotBlank(prop.displayName()) ? prop.displayName() : prop.name()).hint(prop.hint()).type(PolicyPropertyTypes.PROPERTY_TYPE.valueOf(prop.type().name())).objectProperty(annotatedFieldProperty.getName()).placeholder(prop.placeholder()).value(value).required(prop.required()).group(group).groupOrder(order).hidden(prop.hidden()).pattern(prop.pattern()).patternInvalidMessage(prop.patternInvalidMessage()).addSelectableValues(convertToLabelValue(prop.selectableValues())).addSelectableValues(convertToLabelValue(prop.labelValues())).build();
            properties.add(rule);
            if (!group.equals("")) {
                if (!groupedProperties.containsKey(group)) {
                    groupedProperties.put(group, new ArrayList<FieldRuleProperty>());
                }
                groupedProperties.get(group).add(rule);
            }
        }
        // update layout property
        for (Collection<FieldRuleProperty> groupProps : groupedProperties.values()) {
            for (FieldRuleProperty property : groupProps) {
                property.setLayout("row");
            }
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) FieldRulePropertyBuilder(com.thinkbiganalytics.policy.rest.model.FieldRulePropertyBuilder) ArrayList(java.util.ArrayList) AnnotationFieldNameResolver(com.thinkbiganalytics.annotations.AnnotationFieldNameResolver) FieldRuleProperty(com.thinkbiganalytics.policy.rest.model.FieldRuleProperty) AnnotatedFieldProperty(com.thinkbiganalytics.annotations.AnnotatedFieldProperty) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with FieldRulePropertyBuilder

use of com.thinkbiganalytics.policy.rest.model.FieldRulePropertyBuilder in project kylo by Teradata.

the class BasePolicyAnnotationTransformer method getUiProperties.

/**
 * For a given domain policy class extract the user interface {@link FieldRuleProperty} classes parsing the fields annotated with the {@link PolicyProperty}
 *
 * @param policy the domain policy object to parse
 * @return a list of user interface fields annotated with the {@link PolicyProperty}
 */
private List<FieldRuleProperty> getUiProperties(P policy) {
    AnnotationFieldNameResolver annotationFieldNameResolver = new AnnotationFieldNameResolver(PolicyProperty.class);
    List<AnnotatedFieldProperty> list = annotationFieldNameResolver.getProperties(policy.getClass());
    List<FieldRuleProperty> properties = new ArrayList<>();
    Map<String, Integer> groupOrder = new HashMap<>();
    Map<String, List<FieldRuleProperty>> groupedProperties = new HashMap<>();
    if (hasConstructor(policy.getClass())) {
        for (AnnotatedFieldProperty<PolicyProperty> annotatedFieldProperty : list) {
            PolicyProperty prop = annotatedFieldProperty.getAnnotation();
            String value = null;
            try {
                Object fieldValue = FieldUtils.readField(annotatedFieldProperty.getField(), policy, true);
                if (fieldValue != null) {
                    value = fieldValue.toString();
                }
            } catch (IllegalAccessException e) {
            }
            String group = prop.group();
            Integer order = 0;
            if (!groupOrder.containsKey(group)) {
                groupOrder.put(group, order);
            }
            order = groupOrder.get(group);
            order++;
            groupOrder.put(group, order);
            FieldRuleProperty rule = new FieldRulePropertyBuilder(prop.name()).displayName(StringUtils.isNotBlank(prop.displayName()) ? prop.displayName() : prop.name()).hint(prop.hint()).type(PolicyPropertyTypes.PROPERTY_TYPE.valueOf(prop.type().name())).objectProperty(annotatedFieldProperty.getName()).placeholder(prop.placeholder()).value(value).required(prop.required()).group(group).groupOrder(order).pattern(prop.pattern()).patternInvalidMessage(prop.patternInvalidMessage()).hidden(prop.hidden()).addSelectableValues(convertToLabelValue(prop.selectableValues())).addSelectableValues(convertToLabelValue(prop.labelValues())).build();
            properties.add(rule);
            if (!group.equals("")) {
                if (!groupedProperties.containsKey(group)) {
                    groupedProperties.put(group, new ArrayList<FieldRuleProperty>());
                }
                groupedProperties.get(group).add(rule);
            }
        }
        // update layout property
        for (Collection<FieldRuleProperty> groupProps : groupedProperties.values()) {
            for (FieldRuleProperty property : groupProps) {
                property.setLayout("row");
            }
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) FieldRulePropertyBuilder(com.thinkbiganalytics.policy.rest.model.FieldRulePropertyBuilder) ArrayList(java.util.ArrayList) AnnotationFieldNameResolver(com.thinkbiganalytics.annotations.AnnotationFieldNameResolver) FieldRuleProperty(com.thinkbiganalytics.policy.rest.model.FieldRuleProperty) AnnotatedFieldProperty(com.thinkbiganalytics.annotations.AnnotatedFieldProperty) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

AnnotatedFieldProperty (com.thinkbiganalytics.annotations.AnnotatedFieldProperty)2 AnnotationFieldNameResolver (com.thinkbiganalytics.annotations.AnnotationFieldNameResolver)2 FieldRuleProperty (com.thinkbiganalytics.policy.rest.model.FieldRuleProperty)2 FieldRulePropertyBuilder (com.thinkbiganalytics.policy.rest.model.FieldRulePropertyBuilder)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2