Sync Integ to Master
[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         return tree[0].childrens; // Return the childrens without the root.
48     }
49
50     public createSimpleFlatProperty = (property: PropertyFEModel | DerivedFEProperty, instanceName:string): SimpleFlatProperty => {
51         if (property instanceof PropertyFEModel) {
52             return new SimpleFlatProperty(property.uniqueId, property.name, property.name, '', instanceName);
53         } else {
54             let propName: string = (property.isChildOfListOrMap) ? property.mapKey : property.name;
55             return new SimpleFlatProperty(property.uniqueId, property.propertiesName, propName, property.parentName, instanceName);
56         }
57         
58     }
59
60     /**
61      * Unflatten Array<SimpleFlatProperty> and build hirarchy.
62      * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
63      */
64     private unflatten(array: Array<SimpleFlatProperty>, parent: any, tree?: any): any {
65         tree = typeof tree !== 'undefined' ? tree : [];
66         parent = typeof parent !== 'undefined' && parent !== '' ? parent : { path: '' };
67
68         var childrens = _.filter(array, (child: SimpleFlatProperty): boolean => {
69             return child.parentName == parent.path;
70         });
71
72         if (!_.isEmpty(childrens)) {
73             if (parent.path == '') {
74                 tree = childrens;
75             } else {
76                 parent['childrens'] = childrens;
77             }
78             _.each(childrens, (child): void => {
79                 this.unflatten(array, child);
80             });
81         }
82         return tree;
83     }
84 }