08b49ae422001959f13fab97e29a761fd8ac2c1d
[sdc.git] / catalog-ui / src / app / services / data-types-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 'use strict';
22 import { DataTypePropertyModel } from "../models/data-type-properties";
23 import {
24     ComponentInstance,
25     InputModel,
26     DataTypesMap,
27     PropertyModel,
28     InputPropertyBase,
29     IAppConfigurtaion,
30     SchemaProperty
31 } from "../models";
32 import {PROPERTY_DATA} from "../utils/constants";
33
34 export interface IDataTypesService {
35
36     dataTypes:DataTypesMap; //Data type map
37     selectedPropertiesName:string;
38     selectedInput:PropertyModel;
39     alreadySelectedProperties:Array<InputPropertyBase>;
40     selectedInstance:ComponentInstance;
41     selectedComponentInputs:Array<InputModel>;
42     //declare methods
43     fetchDataTypesByModel(modelName:string):void;
44     getAllDataTypes():DataTypesMap;
45     getFirsLevelOfDataTypeProperties(dataTypeName:string):Array<DataTypePropertyModel>;
46     isDataTypeForSchemaType(property:SchemaProperty):boolean;
47     isDataTypeForPropertyType(property:PropertyModel):boolean;
48     isDataTypeForDataTypePropertyType(property:DataTypePropertyModel):boolean;
49 }
50
51 export class DataTypesService implements IDataTypesService {
52
53     static '$inject' = [
54         'sdcConfig',
55         '$q',
56         '$http'
57     ];
58
59     constructor(private sdcConfig:IAppConfigurtaion,
60                 private $q:ng.IQService,
61                 private $http:ng.IHttpService) {
62     }
63
64     private baseUrl = this.sdcConfig.api.root + this.sdcConfig.api.component_api_root;
65
66     dataTypes:DataTypesMap; //Data type map
67     selectedPropertiesName:string;
68     selectedInput:PropertyModel;
69     alreadySelectedProperties:Array<InputPropertyBase>;
70     selectedInstance:ComponentInstance;
71     selectedComponentInputs:Array<InputModel>;
72
73     public fetchDataTypesByModel = (modelName: string):void => {
74         let model;
75         if (modelName) {
76             model = {'model': modelName}
77         }
78         this.$http.get(this.baseUrl+"dataTypes", {params: model})
79         .then((response:any) => {
80             this.dataTypes = response.data;
81             delete this.dataTypes['tosca.datatypes.Root'];
82         });
83     };
84
85     public getAllDataTypesFromModel = (modelName: string): DataTypesMap => {
86         this.fetchDataTypesByModel(modelName);
87         return this.dataTypes;
88     }
89
90     public getAllDataTypes = ():DataTypesMap => {
91         return this.dataTypes;
92     };
93
94     //if the dt derived from simple- return the first parent type, else- return null
95     private getTypeForDataTypeDerivedFromSimple = (dataTypeName:string):string => {
96         /////////temporary hack for tosca primitives///////////////////////
97         if (!this.dataTypes[dataTypeName]) {
98             return 'string';
99         }
100         ///////////////////////////////////////////////////////////////////
101         if (this.dataTypes[dataTypeName].derivedFromName == "tosca.datatypes.Root" || this.dataTypes[dataTypeName].properties) {
102             return null;
103         }
104         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.dataTypes[dataTypeName].derivedFromName) > -1) {
105             return this.dataTypes[dataTypeName].derivedFromName
106         }
107         return this.getTypeForDataTypeDerivedFromSimple(this.dataTypes[dataTypeName].derivedFromName);
108     };
109
110
111     //return list of data type properties and all its parents properties
112     //(not include the properties of its properties, in case this data type has not primitive properties)
113     public getFirsLevelOfDataTypeProperties = (dataTypeName:string):Array<DataTypePropertyModel> => {
114         let properties = this.dataTypes[dataTypeName].properties || [];
115         if (this.dataTypes[dataTypeName].derivedFromName != "tosca.datatypes.Root") {
116             properties = this.getFirsLevelOfDataTypeProperties(this.dataTypes[dataTypeName].derivedFromName).concat(properties);
117         }
118         return properties;
119     };
120
121     //return false when type= data type (=not simple type) that not derived from simple type
122     public isDataTypeForSchemaType = (property:SchemaProperty):boolean=> {
123         property.simpleType = "";
124         if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
125             return false;
126         }
127         let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
128         if (simpleType) {
129             property.simpleType = simpleType;
130             return false;
131         }
132         return true;
133     };
134
135     public isDataTypeForPropertyType = (property:PropertyModel):boolean=> {
136         property.simpleType = "";
137         if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
138             return false;
139         }
140         let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
141         if (simpleType) {
142             property.simpleType = simpleType;
143             return false;
144         }
145         return true;
146     };
147
148
149     public isDataTypeForDataTypePropertyType = (property:DataTypePropertyModel):boolean=> {
150         property.simpleType = "";
151         let isScalarForNFoD:boolean = property.type === 'scalar-unit.size';
152         if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1 || isScalarForNFoD) {
153             return false;
154         }
155         let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
156         if (simpleType) {
157             property.simpleType = simpleType;
158             return false;
159         }
160         return true;
161     };
162 }