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