Add support for delete property from non-normative data type
[sdc.git] / catalog-ui / src / app / ng2 / services / data-type.service.ts
index be23881..636217f 100644 (file)
@@ -1,7 +1,40 @@
-import { Injectable } from '@angular/core';
-import { DataTypeModel, DataTypesMap, PropertyBEModel, PropertyFEModel, DerivedFEProperty, DerivedFEPropertyMap } from "app/models";
-import { DataTypesService } from "app/services/data-types-service";
-import { PROPERTY_DATA, PROPERTY_TYPES } from "app/utils";
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+import * as _ from "lodash";
+import {Inject, Injectable} from '@angular/core';
+import {
+    DataTypeModel,
+    DataTypesMap,
+    DerivedFEProperty,
+    PropertyBEModel,
+    PropertyFEModel
+} from "app/models";
+import {DataTypesService} from "app/services/data-types-service";
+import {PROPERTY_DATA} from "app/utils";
+import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";
+import {ISdcConfig} from "../config/sdc-config.config.factory";
+import {SdcConfigToken} from "../config/sdc-config.config";
+import {HttpBackend, HttpClient, HttpHeaders} from "@angular/common/http";
+import {Observable} from "rxjs/Observable";
+import {AuthenticationService} from "./authentication.service";
 
 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
  *
@@ -11,50 +44,99 @@ import { PROPERTY_DATA, PROPERTY_TYPES } from "app/utils";
 
 @Injectable()
 export class DataTypeService {
-    private dataTypes: DataTypesMap;
+    public dataTypes: DataTypesMap;
+    private readonly baseUrl: string;
+    private readonly dataTypeUrl: string;
+    private readonly dataTypeUploadUrl: string;
+
 
-    constructor(private dataTypeService: DataTypesService) {
+    constructor(private dataTypeService: DataTypesService, private authService: AuthenticationService, private handler: HttpBackend, private httpClient: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
         this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
+        this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
+        this.dataTypeUrl = `${this.baseUrl}data-types`;
+        this.dataTypeUploadUrl = `${this.baseUrl}uploadType`;
+        this.httpClient = new HttpClient(handler);
+    }
+
+    public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
+        this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
+        let dataTypeFound = this.dataTypes[typeName];
+        if (!dataTypeFound) {
+            console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
+        }
+        return dataTypeFound;
     }
 
     public getDataTypeByTypeName(typeName: string): DataTypeModel {
+        if (!this.dataTypes) {
+            this.dataTypes = this.dataTypeService.getAllDataTypes();
+        }
+        if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
         return this.dataTypes[typeName];
     }
-/*
-    //if the dt derived from simple- return the first parent type, else- return null
-    public getTypeForDataTypeDerivedFromSimple = (dataTypeName:string):string => {
-        /////////temporary hack for tosca primitives///////////////////////
-        if (!this.dataTypes[dataTypeName]) {
-            return PROPERTY_TYPES.STRING;
-        }
-        ///////////////////////////////////////////////////////////////////
-        if (this.dataTypes[dataTypeName].derivedFromName == PROPERTY_DATA.ROOT_DATA_TYPE || this.dataTypes[dataTypeName].properties) {
-            return null;
-        }
-        if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.dataTypes[dataTypeName].derivedFromName) > -1) {
-            return this.dataTypes[dataTypeName].derivedFromName
-        }
-        return this.getTypeForDataTypeDerivedFromSimple(this.dataTypes[dataTypeName].derivedFromName);
-    };
 
-    /**
-     * The function returns all properties for the DataType passed in, and recurses through parent dataTypes (derivedFrom) to retrieve their properties as well
-     * @param dataTypeObj
-     *
-    public getDataTypePropertiesRecursively(dataTypeObj: DataTypeModel): Array<PropertyBEModel> {
-        let propertiesArray: Array<PropertyBEModel> = dataTypeObj.properties || [];
-        if (PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFromName) {
-            propertiesArray = propertiesArray.concat(this.getDataTypePropertiesRecursively(dataTypeObj.derivedFrom));
-        }
-        return propertiesArray;
+    public getDataTypeByModel(modelName: string): DataTypesMap {
+        return this.dataTypeService.getAllDataTypesFromModel(modelName);
+    }
+
+    public findAllDataTypesByModel(modelName: string): Promise<Map<string, DataTypeModel>> {
+        return this.dataTypeService.findAllDataTypesByModel(modelName);
+    }
+
+    public findById(id: string): Observable<DataTypeModel> {
+        const url = `${this.dataTypeUrl}/${id}`
+        return this.httpClient.get<DataTypeModel>(url);
+    }
+
+    public findAllProperties(id: string): Observable<Array<PropertyBEModel>> {
+        const url = `${this.dataTypeUrl}/${id}/properties`
+        return this.httpClient.get<Array<PropertyBEModel>>(url);
+    }
+
+    public createProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
+        const url = `${this.dataTypeUrl}/${id}/properties`;
+        return this.httpClient.post<PropertyBEModel>(url, property);
+    }
+
+    public updateProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
+        const url = `${this.dataTypeUrl}/${id}/properties`;
+        return this.httpClient.put<PropertyBEModel>(url, property);
+    }
+
+    public deleteProperty(dataTypeId: string, propertyId: string): Observable<Object> {
+        const url = `${this.dataTypeUrl}/${dataTypeId}/${propertyId}`;
+        let headers = new HttpHeaders({'USER_ID': this.authService.getLoggedinUser().userId});
+        let options = {headers: headers};
+        return this.httpClient.delete(url, options).map((res: Response) => {
+            return propertyId;
+        });
+    }
+
+    public createImportedType(model: string, importingFile: File): Observable<any> {
+        const url = `${this.dataTypeUploadUrl}/datatypesyaml`;
+        const formData = new FormData();
+        formData.append('dataTypesYaml', importingFile);
+        formData.append('model', model != 'SDC AID' ? model : "")
+        formData.append('includeToModelImport', "true");
+        let headers = new HttpHeaders({'USER_ID': this.authService.getLoggedinUser().userId});
+        let options = {headers: headers};
+
+        return this.httpClient.post<any>(url, formData, options);
+    }
+
+    public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName) {
+        // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
+        //     property.name == propertyName);
+        // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
+        return null;
     }
-*/
 
     public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
         //push all child properties to array
+        if (!dataTypeObj) return;
         if (dataTypeObj.properties) {
             dataTypeObj.properties.forEach((derivedProperty) => {
-                if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
+                if (dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA) {//The requirement is to not display the property supplemental_data
                     propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
                 }
                 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
@@ -62,10 +144,46 @@ export class DataTypeService {
             });
         }
         //recurse parent (derivedFrom), in case one of parents contains properties
-        if (PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
+        if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
             this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
         }
     }
 
+    public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
+        //push all child properties to array
+        if (!dataTypeObj) return;
+        if (dataTypeObj.attributes) {
+            dataTypeObj.attributes.forEach((derivedAttribute) => {
+                if (dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA) {//The requirement is to not display the property supplemental_data
+                    attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
+                }
+                let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
+                this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
+            });
+        }
+        //recurse parent (derivedFrom), in case one of parents contains properties
+        if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
+            this.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
+        }
+    }
+
+    /**
+     * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
+     * Additional custom behavior can be added by adding a function with the given dataType name
+     */
+    public checkForCustomBehavior = (property: PropertyFEModel) => {
+        let shortTypeName: string = property.type.split('.').pop();
+        if (this[shortTypeName]) {
+            this[shortTypeName](property); //execute function for given type, pass property as param
+        }
+    }
+
+    public Naming = (property: PropertyFEModel) => {
+        let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
+        property.flattenedChildren.forEach((prop) => {
+            if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
+            if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
+        });
+    }
 }