Add data type view/workspace
[sdc.git] / catalog-ui / src / app / ng2 / services / data-type.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import * as _ from "lodash";
22 import {Inject, Injectable} from '@angular/core';
23 import { DataTypeModel, DataTypesMap, PropertyFEModel, DerivedFEProperty} from "app/models";
24 import { DataTypesService } from "app/services/data-types-service";
25 import { PROPERTY_DATA } from "app/utils";
26 import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";
27 import {ISdcConfig} from "../config/sdc-config.config.factory";
28 import {SdcConfigToken} from "../config/sdc-config.config";
29 import {HttpClient} from "@angular/common/http";
30 import {Observable} from "rxjs/Observable";
31
32 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
33  *
34  *  This service is a singleton that holds a map of all DataTypes, recieved from server on load.
35  *  It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
36  */
37
38 @Injectable()
39 export class DataTypeService {
40     public dataTypes: DataTypesMap;
41     private readonly baseUrl: string;
42     private readonly dataTypeUrl: string;
43
44     constructor(private dataTypeService: DataTypesService, private httpClient: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
45         this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
46         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
47         this.dataTypeUrl = `${this.baseUrl}data-types`
48     }
49
50
51     public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
52         this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
53         let dataTypeFound = this.dataTypes[typeName];
54         if (!dataTypeFound) {
55             console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
56         }
57         return dataTypeFound;
58     }
59
60     public getDataTypeByTypeName(typeName: string): DataTypeModel {
61         if(!this.dataTypes){
62             this.dataTypes = this.dataTypeService.getAllDataTypes();
63         }
64         if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
65         return this.dataTypes[typeName];
66     }
67
68     public getDataTypeByModel(modelName: string): DataTypesMap {
69         return this.dataTypeService.getAllDataTypesFromModel(modelName);
70     }
71
72     public findAllDataTypesByModel(modelName: string): Promise<Map<string, DataTypeModel>> {
73         return this.dataTypeService.findAllDataTypesByModel(modelName);
74     }
75
76     public findById(id: string): Observable<DataTypeModel> {
77         const url = `${this.dataTypeUrl}/${id}`
78         return this.httpClient.get<DataTypeModel>(url);
79     }
80
81     public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName){
82         // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
83         //     property.name == propertyName);
84         // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
85         return null;
86     }
87
88     public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
89         //push all child properties to array
90         if (!dataTypeObj) return;
91         if (dataTypeObj.properties) {
92             dataTypeObj.properties.forEach((derivedProperty) => {
93                 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
94                     propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
95                 }
96                 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
97                 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
98             });
99         }
100         //recurse parent (derivedFrom), in case one of parents contains properties
101         if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
102             this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
103         }
104     }
105
106     public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
107         //push all child properties to array
108         if (!dataTypeObj) return;
109         if (dataTypeObj.attributes) {
110             dataTypeObj.attributes.forEach((derivedAttribute) => {
111                 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
112                     attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
113                 }
114                 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
115                 this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
116             });
117         }
118         //recurse parent (derivedFrom), in case one of parents contains properties
119         if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
120             this.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
121         }
122     }
123
124     /**
125      * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
126      * Additional custom behavior can be added by adding a function with the given dataType name
127      */
128     public checkForCustomBehavior = (property:PropertyFEModel) => {
129         let shortTypeName:string = property.type.split('.').pop();
130         if (this[shortTypeName]) {
131             this[shortTypeName](property); //execute function for given type, pass property as param
132         }
133     }
134
135     public Naming = (property: PropertyFEModel) => {
136         let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
137         property.flattenedChildren.forEach((prop) => {
138             if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
139             if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
140         });
141     }
142
143 }
144