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=========================================================
22 import { DataTypePropertyModel } from "../models/data-type-properties";
30 SchemaProperty, DataTypeModel
32 import {PROPERTY_DATA} from "../utils/constants";
34 export interface IDataTypesService {
36 dataTypes:DataTypesMap; //Data type map
37 selectedPropertiesName:string;
38 selectedInput:PropertyModel;
39 alreadySelectedProperties:Array<InputPropertyBase>;
40 selectedInstance:ComponentInstance;
41 selectedComponentInputs:Array<InputModel>;
43 loadDataTypesCache(modelName:string):void;
44 findAllDataTypesByModel(modelName: string): void;
45 getAllDataTypes():DataTypesMap;
46 getFirsLevelOfDataTypeProperties(dataTypeName:string):Array<DataTypePropertyModel>;
47 isDataTypeForSchemaType(property:SchemaProperty):boolean;
48 isDataTypeForPropertyType(property:PropertyModel):boolean;
49 isDataTypeForDataTypePropertyType(property:DataTypePropertyModel):boolean;
52 export class DataTypesService implements IDataTypesService {
60 constructor(private sdcConfig:IAppConfigurtaion,
61 private $q:ng.IQService,
62 private $http:ng.IHttpService) {
65 private baseUrl = this.sdcConfig.api.root + this.sdcConfig.api.component_api_root;
67 dataTypes:DataTypesMap; //Data type map
68 selectedPropertiesName:string;
69 selectedInput:PropertyModel;
70 alreadySelectedProperties:Array<InputPropertyBase>;
71 selectedInstance:ComponentInstance;
72 selectedComponentInputs:Array<InputModel>;
74 public loadDataTypesCache = (modelName: string): void => {
77 model = {'model': modelName}
79 this.$http.get(this.baseUrl+"dataTypes", {params: model})
80 .then((response:any) => {
81 this.dataTypes = response.data;
82 delete this.dataTypes['tosca.datatypes.Root'];
86 public fetchDataTypesByModel = (modelName: string): angular.IHttpPromise<any> => {
89 model = {'model': modelName}
91 return this.$http.get(this.baseUrl + "dataTypes", {params: model});
94 public getAllDataTypesFromModel = (modelName: string): DataTypesMap => {
95 this.loadDataTypesCache(modelName);
96 return this.dataTypes;
99 public findAllDataTypesByModel = (modelName: string): Promise<Map<string, DataTypeModel>> => {
100 return new Promise<Map<string, DataTypeModel>>((resolve, reject) => {
101 this.fetchDataTypesByModel(modelName).then(response => {
102 const dataTypes = response.data;
103 delete dataTypes[PROPERTY_DATA.ROOT_DATA_TYPE];
104 const dataTypeMap = new Map<string, DataTypeModel>();
105 for(const dataTypeKey of Object.keys(dataTypes)) {
106 dataTypeMap.set(dataTypeKey, new DataTypeModel(dataTypes[dataTypeKey]))
108 resolve(dataTypeMap);
115 public getAllDataTypes = ():DataTypesMap => {
116 return this.dataTypes;
119 //if the dt derived from simple- return the first parent type, else- return null
120 private getTypeForDataTypeDerivedFromSimple = (dataTypeName:string):string => {
121 /////////temporary hack for tosca primitives///////////////////////
122 if (!this.dataTypes[dataTypeName]) {
125 ///////////////////////////////////////////////////////////////////
126 if (this.dataTypes[dataTypeName].derivedFromName == "tosca.datatypes.Root" || this.dataTypes[dataTypeName].properties) {
129 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.dataTypes[dataTypeName].derivedFromName) > -1) {
130 return this.dataTypes[dataTypeName].derivedFromName
132 return this.getTypeForDataTypeDerivedFromSimple(this.dataTypes[dataTypeName].derivedFromName);
136 //return list of data type properties and all its parents properties
137 //(not include the properties of its properties, in case this data type has not primitive properties)
138 public getFirsLevelOfDataTypeProperties = (dataTypeName:string):Array<DataTypePropertyModel> => {
139 let properties = this.dataTypes[dataTypeName].properties || [];
140 if (this.dataTypes[dataTypeName].derivedFromName != "tosca.datatypes.Root") {
141 properties = this.getFirsLevelOfDataTypeProperties(this.dataTypes[dataTypeName].derivedFromName).concat(properties);
146 //return false when type= data type (=not simple type) that not derived from simple type
147 public isDataTypeForSchemaType = (property:SchemaProperty):boolean=> {
148 property.simpleType = "";
149 if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
152 let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
154 property.simpleType = simpleType;
160 public isDataTypeForPropertyType = (property:PropertyModel):boolean=> {
161 property.simpleType = "";
162 if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
165 let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
167 property.simpleType = simpleType;
174 public isDataTypeForDataTypePropertyType = (property:DataTypePropertyModel):boolean=> {
175 property.simpleType = "";
176 let isScalarForNFoD:boolean = property.type === 'scalar-unit.size';
177 if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1 || isScalarForNFoD) {
180 let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
182 property.simpleType = simpleType;