Add support for delete property from non-normative data type
[sdc.git] / catalog-ui / src / app / ng2 / services / data-type.service.ts
index 298ba90..636217f 100644 (file)
 
 import * as _ from "lodash";
 import {Inject, Injectable} from '@angular/core';
-import {DataTypeModel, DataTypesMap, PropertyFEModel, DerivedFEProperty, PropertyBEModel} from "app/models";
-import { DataTypesService } from "app/services/data-types-service";
-import { PROPERTY_DATA } from "app/utils";
+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 {HttpClient} from "@angular/common/http";
+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
  *
@@ -40,14 +47,17 @@ export class DataTypeService {
     public dataTypes: DataTypesMap;
     private readonly baseUrl: string;
     private readonly dataTypeUrl: string;
+    private readonly dataTypeUploadUrl: string;
 
-    constructor(private dataTypeService: DataTypesService, private httpClient: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
+
+    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.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];
@@ -58,7 +68,7 @@ export class DataTypeService {
     }
 
     public getDataTypeByTypeName(typeName: string): DataTypeModel {
-        if(!this.dataTypes){
+        if (!this.dataTypes) {
             this.dataTypes = this.dataTypeService.getAllDataTypes();
         }
         if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
@@ -84,11 +94,37 @@ export class DataTypeService {
     }
 
     public createProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
-        const url = `${this.dataTypeUrl}/${id}/properties`
+        const url = `${this.dataTypeUrl}/${id}/properties`;
         return this.httpClient.post<PropertyBEModel>(url, property);
     }
 
-    public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName){
+    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;
@@ -100,7 +136,7 @@ export class DataTypeService {
         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);
@@ -118,7 +154,7 @@ export class DataTypeService {
         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
+                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);
@@ -135,8 +171,8 @@ export class DataTypeService {
      * 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();
+    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
         }
@@ -149,6 +185,5 @@ export class DataTypeService {
             if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;
         });
     }
-
 }