Merge "[SDC] code rebase for sdc resync to LF"
[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 { Injectable } from '@angular/core';
22 import { DataTypeModel, DataTypesMap, PropertyBEModel, PropertyFEModel, DerivedFEProperty, DerivedFEPropertyMap } from "app/models";
23 import { DataTypesService } from "app/services/data-types-service";
24 import { PROPERTY_DATA, PROPERTY_TYPES } from "app/utils";
25
26 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
27  *
28  *  This service is a singleton that holds a map of all DataTypes, recieved from server on load.
29  *  It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
30  */
31
32 @Injectable()
33 export class DataTypeService {
34     private dataTypes: DataTypesMap;
35
36     constructor(private dataTypeService: DataTypesService) {
37         this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
38     }
39
40     public getDataTypeByTypeName(typeName: string): DataTypeModel {
41         if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
42         return this.dataTypes[typeName];
43     }
44
45
46     public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
47         //push all child properties to array
48         if (!dataTypeObj) return;
49         if (dataTypeObj.properties) {
50             dataTypeObj.properties.forEach((derivedProperty) => {
51                 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
52                     propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
53                 }
54                 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
55                 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
56             });
57         }
58         //recurse parent (derivedFrom), in case one of parents contains properties
59         if (PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
60             this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
61         }
62     }
63
64     /**
65      * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
66      * Additional custom behavior can be added by adding a function with the given dataType name
67      */    
68     public checkForCustomBehavior = (property:PropertyFEModel) => {
69         let shortTypeName:string = property.type.split('.').pop();
70         if (this[shortTypeName]) {
71             this[shortTypeName](property); //execute function for given type, pass property as param
72         }
73     }
74
75     public Naming = (property: PropertyFEModel) => {
76         let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
77         property.flattenedChildren.forEach((prop) => {
78             if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
79             if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
80         });
81     }
82
83 }
84