[sdc] update code of sdc
[sdc.git] / catalog-ui / src / app / ng2 / services / data-type.service.ts
1 import { Injectable } from '@angular/core';
2 import { DataTypeModel, DataTypesMap, PropertyBEModel, PropertyFEModel, DerivedFEProperty, DerivedFEPropertyMap } from "app/models";
3 import { DataTypesService } from "app/services/data-types-service";
4 import { PROPERTY_DATA, PROPERTY_TYPES } from "app/utils";
5
6 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
7  *
8  *  This service is a singleton that holds a map of all DataTypes, recieved from server on load.
9  *  It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
10  */
11
12 @Injectable()
13 export class DataTypeService {
14     private dataTypes: DataTypesMap;
15
16     constructor(private dataTypeService: DataTypesService) {
17         this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
18     }
19
20     public getDataTypeByTypeName(typeName: string): DataTypeModel {
21         return this.dataTypes[typeName];
22     }
23
24
25     public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
26         //push all child properties to array
27         if (dataTypeObj.properties) {
28             dataTypeObj.properties.forEach((derivedProperty) => {
29                 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
30                     propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
31                 }
32                 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
33                 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
34             });
35         }
36         //recurse parent (derivedFrom), in case one of parents contains properties
37         if (PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
38             this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
39         }
40     }
41
42     /**
43      * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
44      * Additional custom behavior can be added by adding a function with the given dataType name
45      */    
46     public checkForCustomBehavior = (property:PropertyFEModel) => {
47         let shortTypeName:string = property.type.split('.').pop();
48         if (this[shortTypeName]) {
49             this[shortTypeName](property); //execute function for given type, pass property as param
50         }
51     }
52
53     public Naming = (property: PropertyFEModel) => {
54         let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
55         property.flattenedChildren.forEach((prop) => {
56             if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
57             if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
58         });
59     }
60
61 }
62