aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/data-type.service.ts
blob: 70555a5efde4b341b6f529b5b1586f7b089ef500 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import * as _ from "lodash";
import { Injectable } from '@angular/core';
import { DataTypeModel, DataTypesMap, PropertyFEModel, DerivedFEProperty} from "app/models";
import { DataTypesService } from "app/services/data-types-service";
import { PROPERTY_DATA } from "app/utils";
import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";

/** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
 *
 *  This service is a singleton that holds a map of all DataTypes, recieved from server on load.
 *  It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
 */

@Injectable()
export class DataTypeService {
    public dataTypes: DataTypesMap;

    constructor(private dataTypeService: DataTypesService) {
        this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
    }

    public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
        this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
        let dataTypeFound = this.dataTypes[typeName];
        if (!dataTypeFound) {
            console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
        }
        return dataTypeFound;
    }

    public getDataTypeByTypeName(typeName: string): DataTypeModel {
        if(!this.dataTypes){
            this.dataTypes = this.dataTypeService.getAllDataTypes();
        }
        if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
        return this.dataTypes[typeName];
    }

    public getDataTypeByModel(modelName: string): DataTypesMap {
        return this.dataTypeService.getAllDataTypesFromModel(modelName);
    }

    public findAllDataTypesByModel(modelName: string): Promise<Map<string, DataTypeModel>> {
        return this.dataTypeService.findAllDataTypesByModel(modelName);
    }

    public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName){
        // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
        //     property.name == propertyName);
        // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
        return null;
    }

    public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
        //push all child properties to array
        if (!dataTypeObj) return;
        if (dataTypeObj.properties) {
            dataTypeObj.properties.forEach((derivedProperty) => {
                if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
                    propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
                }
                let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
                this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
            });
        }
        //recurse parent (derivedFrom), in case one of parents contains properties
        if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
            this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
        }
    }

    public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
        //push all child properties to array
        if (!dataTypeObj) return;
        if (dataTypeObj.attributes) {
            dataTypeObj.attributes.forEach((derivedAttribute) => {
                if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
                    attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
                }
                let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
                this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
            });
        }
        //recurse parent (derivedFrom), in case one of parents contains properties
        if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
            this.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
        }
    }

    /**
     * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
     * Additional custom behavior can be added by adding a function with the given dataType name
     */
    public checkForCustomBehavior = (property:PropertyFEModel) => {
        let shortTypeName:string = property.type.split('.').pop();
        if (this[shortTypeName]) {
            this[shortTypeName](property); //execute function for given type, pass property as param
        }
    }

    public Naming = (property: PropertyFEModel) => {
        let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
        property.flattenedChildren.forEach((prop) => {
            if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
            if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
        });
    }

}