Support TOSCA functions in sub properties
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / services / hierarchy-nav.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 import * as _ from "lodash";
22 import { Injectable } from '@angular/core';
23 import { SimpleFlatProperty, PropertyFEModel, DerivedFEProperty } from 'app/models';
24
25
26 @Injectable()
27 export class HierarchyNavService {
28     /**
29      * Build hirarchy structure for the tree when user selects on table row.
30      * First create Array<SimpleFlatProperty> and insert also the parent (PropertyFEModel) to this array.
31      * The Array is flat and contains SimpleFlatProperty that has parentName and uniqueId.
32      * Now we build hirarchy from this Array (that includes childrens) and return it for the tree
33      *
34      * @argument property: PropertyFEModel - property contains flattenedChildren array of DerivedFEProperty
35      * @returns  Array<SimpleFlatProperty> - containing childrens Array<SimpleFlatProperty>, augmantin childrens to SimpleFlatProperty.
36      */
37     public getSimplePropertiesTree(property: PropertyFEModel, instanceName: string): Array<SimpleFlatProperty> {
38         // Build Array of SimpleFlatProperty before unflatten function
39         let flattenProperties: Array<SimpleFlatProperty> = [];
40         flattenProperties.push(this.createSimpleFlatProperty(property, instanceName)); // Push the root property
41         _.each(property.flattenedChildren, (child: DerivedFEProperty): void => {
42             if (child.isChildOfListOrMap && child.schema.property.isSimpleType) return; //do not display non-complex children of list or map
43             flattenProperties.push(this.createSimpleFlatProperty(child, instanceName));
44         });
45
46         let tree = this.unflatten(flattenProperties, '', []);
47
48         return tree[0].childrens; // Return the childrens without the root.
49     }
50
51     public createSimpleFlatProperty = (property: PropertyFEModel | DerivedFEProperty, instanceName:string): SimpleFlatProperty => {
52         if (property instanceof PropertyFEModel) {
53             return new SimpleFlatProperty(property.uniqueId, property.name, property.name, '', instanceName);
54         } else {
55             let propName: string = (property.isChildOfListOrMap) ? property.mapKey : property.name;
56             return new SimpleFlatProperty(property.uniqueId, property.propertiesName, propName, property.parentName, instanceName);
57         }
58         
59     }
60
61     /**
62      * Unflatten Array<SimpleFlatProperty> and build hirarchy.
63      * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
64      */
65     private unflatten(array: Array<SimpleFlatProperty>, parent: any, tree?: any): any {
66         tree = typeof tree !== 'undefined' ? tree : [];
67         parent = typeof parent !== 'undefined' && parent !== '' ? parent : { path: '' };
68
69         var childrens = _.filter(array, (child: SimpleFlatProperty): boolean => {
70             return child.parentName == parent.path;
71         });
72
73         if (!_.isEmpty(childrens)) {
74             if (parent.path == '') {
75                 tree = childrens;
76             } else {
77                 parent['childrens'] = childrens;
78             }
79             _.each(childrens, (child): void => {
80                 this.unflatten(array, child);
81             });
82         }
83         return tree;
84     }
85 }