2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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 import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";
28 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
30 * This service is a singleton that holds a map of all DataTypes, recieved from server on load.
31 * It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
35 export class DataTypeService {
36 public dataTypes: DataTypesMap;
38 constructor(private dataTypeService: DataTypesService) {
39 this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
42 public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
43 this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
44 let dataTypeFound = this.dataTypes[typeName];
46 console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
51 public getDataTypeByTypeName(typeName: string): DataTypeModel {
53 this.dataTypes = this.dataTypeService.getAllDataTypes();
55 if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
56 return this.dataTypes[typeName];
59 public getDataTypeByModel(modelName: string): DataTypesMap {
60 return this.dataTypeService.getAllDataTypesFromModel(modelName);
63 public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName){
64 // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
65 // property.name == propertyName);
66 // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
70 public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
71 //push all child properties to array
72 if (!dataTypeObj) return;
73 if (dataTypeObj.properties) {
74 dataTypeObj.properties.forEach((derivedProperty) => {
75 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
76 propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
78 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
79 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
82 //recurse parent (derivedFrom), in case one of parents contains properties
83 if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
84 this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
88 public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
89 //push all child properties to array
90 if (!dataTypeObj) return;
91 if (dataTypeObj.attributes) {
92 dataTypeObj.attributes.forEach((derivedAttribute) => {
93 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
94 attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
96 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
97 this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
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.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
107 * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
108 * Additional custom behavior can be added by adding a function with the given dataType name
110 public checkForCustomBehavior = (property:PropertyFEModel) => {
111 let shortTypeName:string = property.type.split('.').pop();
112 if (this[shortTypeName]) {
113 this[shortTypeName](property); //execute function for given type, pass property as param
117 public Naming = (property: PropertyFEModel) => {
118 let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
119 property.flattenedChildren.forEach((prop) => {
120 if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
121 if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;