summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/utils/filter-constraint-helper.ts
blob: bdb3f9ec7a007fff1eef686bce200cba3991f433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {FilterConstraint} from "../models/filter-constraint";
import {ToscaFunctionType} from "../models/tosca-function-type.enum";
import {ToscaConcatFunction} from "../models/tosca-concat-function";
import {ToscaGetFunction} from "../models/tosca-get-function";
import {YamlFunction} from "../models/yaml-function";
import {CapabilityFilterConstraint} from "../models/capability-filter-constraint";

export class FilterConstraintHelper {

    public static buildFilterConstraintLabel(constraint: FilterConstraint | CapabilityFilterConstraint): string {
        let value;
        if (this.isValueToscaFunction(constraint.value)) {
            switch (constraint.value.type) {
                case ToscaFunctionType.CONCAT:
                    value = new ToscaConcatFunction(constraint.value).buildValueString();
                    break;
                case ToscaFunctionType.GET_PROPERTY:
                case ToscaFunctionType.GET_INPUT:
                case ToscaFunctionType.GET_ATTRIBUTE:
                    value = new ToscaGetFunction(constraint.value).buildValueString();
                    break;
                case ToscaFunctionType.YAML:
                    value = new YamlFunction(constraint.value).buildValueString();
                    break;
                case ToscaFunctionType.STRING:
                    value = constraint.value.value;
                    break;
                default:
                    value = JSON.stringify(constraint.value, null, 4);
            }
        } else {
            value = JSON.stringify(constraint.value, null, 4);
        }
        if (constraint instanceof CapabilityFilterConstraint) {
            return `${constraint.capabilityName}: ${constraint.servicePropertyName} ${this.convertToSymbol(constraint.constraintOperator)} ${value}`;
        }

        return `${constraint.servicePropertyName} ${this.convertToSymbol(constraint.constraintOperator)} ${value}`;
    }

    public static convertToSymbol(constraintOperator: string) {
        switch (constraintOperator) {
            case OPERATOR_TYPES.LESS_THAN: return '<';
            case OPERATOR_TYPES.EQUAL: return '=';
            case OPERATOR_TYPES.GREATER_THAN: return '>';
            case OPERATOR_TYPES.GREATER_OR_EQUAL: return '>=';
            case OPERATOR_TYPES.LESS_OR_EQUAL: return '<=';
        }
    }

    private static isValueToscaFunction(value: any): boolean {
        return value instanceof Object && 'type' in value && (<any>Object).values(ToscaFunctionType).includes(value.type);
    }

}

export const OPERATOR_TYPES = {
    EQUAL: 'equal',
    GREATER_THAN: 'greater_than',
    LESS_THAN: 'less_than',
    GREATER_OR_EQUAL: 'greater_or_equal',
    LESS_OR_EQUAL: 'less_or_equal'
};