[AAI] Remove Robby Maharajh & Harish Kajur as committers
[aai/sparky-fe.git] / src / utils / GeneralCommonFunctions.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2021 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 {GlobalExtConstants} from 'utils/GlobalExtConstants.js';
22
23 let OXM = GlobalExtConstants.OXM;
24
25 var scrollTo = function(id) {
26     setTimeout(function () {
27         document.getElementById("main-app-container").scrollTop = document.getElementById(id).offsetTop;
28         }, 100);
29 }
30 var populatePathDSL = function(tree, isInit, onLoad, enableRealTime){
31     console.log('populatePathDSL>>>>>>> pathFIlter****',tree);
32         var DSL = '';
33     var treeArray = '';
34         var treeArrayLength = 0;
35         if(isInit){
36             treeArray = tree;
37             treeArrayLength = 1;
38         }else{
39             treeArray = tree.children;
40             treeArrayLength = tree.children.length;
41         }
42         for(var k = 0; treeArray && k < treeArrayLength; k++){
43             if(k === 0 && treeArrayLength > 1){
44                 DSL += '[';
45             }
46             var node = '';
47             if(isInit){
48                 node = tree;
49             }else{
50                 node = treeArray[k];
51             }
52             var nodeData=(onLoad)?node:node.data;
53             if(nodeData){
54                 console.log('***************',JSON.stringify(nodeData));
55                 if(isInit){
56                     DSL += '(>'+nodeData.name;
57                 }else{
58                     DSL += nodeData.name;
59                 }
60                 
61                 let propState=false;
62                 if(nodeData.details){
63                     var tempAttributeString = '';
64                     var selectedAttributeCount = 0;                    
65                     for (var key in nodeData.details.attrDetails){
66                         if(nodeData.details.attrDetails[key].filterValue && nodeData.details.attrDetails[key].filterValue[0] !==''){
67                             DSL += '(\'' + nodeData.details.attrDetails[key].attributeName + '\',';
68                             let dslValues='';
69                             for(var indx=0; indx<nodeData.details.attrDetails[key].filterValue.length; indx++){                            
70                                 dslValues=(indx>0) ? dslValues+',':dslValues;
71                                 if(enableRealTime && nodeData.details.attrDetails[key].filterType && nodeData.details.attrDetails[key].filterType[indx]){
72                                     dslValues += nodeData.details.attrDetails[key].filterType[indx]+'(\''+ nodeData.details.attrDetails[key].filterValue[indx] + '\')';                                    
73                                 }else{
74                                     dslValues +='\''+nodeData.details.attrDetails[key].filterValue[indx] + '\'';                                    
75                                 }                      
76                                 if(nodeData.details.attrDetails[key].filterValue.length-1 ===  indx){
77                                     dslValues +=')';
78                                 }
79                             }
80                             DSL += dslValues;
81                         }
82                     }
83                 }
84             }
85             if(node.children && node.children.length>0){
86                 DSL+= '>' + populatePathDSL(node,false,onLoad,enableRealTime);
87             }
88             if(k !==  treeArrayLength - 1){
89                 DSL += ',';
90             }
91             if(k === treeArrayLength - 1 && treeArrayLength > 1){
92                 DSL += ']';
93             }
94         }
95         if(isInit){
96             DSL+=')';
97         }     
98         return DSL;
99     }
100     var getFilteringOptions = function(nodeType){
101         var propertiesDsl = [];
102         var result = JSON.parse(OXM);
103         var arrayOfTypes = result['xml-bindings']['java-types'][0]['java-type'];
104         //console.dir(arrayOfTypes);
105         var foundIndex = -1;
106         var searchParam = nodeType;
107     
108         if (nodeType.toUpperCase() === 'LINESOFBUSINESS') {
109             searchParam = 'lineOfBusiness';
110         }
111         //console.log('searchParam:' + searchParam);
112         for (var i = 0; i < arrayOfTypes.length && foundIndex === -1; i++) {
113             if (arrayOfTypes[i]['xml-root-element'][0]['$']['name'] === camelToDash(searchParam)) {
114             foundIndex = i;
115             }
116         }
117         if(foundIndex && arrayOfTypes[foundIndex]){
118               //build the filter list
119             for (var j = 0; j < arrayOfTypes[foundIndex]['java-attributes'][0]['xml-element'].length; j++) {
120                 let property =  arrayOfTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['name'];            
121                 let type = arrayOfTypes[foundIndex]['java-attributes'][0]['xml-element'][j]['$']['type'];
122                 if(property !== 'length'){
123                     if (type === 'java.lang.String' || type === 'java.lang.Boolean' || type === 'java.lang.Long' || type === 'java.lang.Integer') {
124                         propertiesDsl[property] = {};
125                         propertiesDsl[property].isSelected = false;
126                         propertiesDsl[property].attributeName = property;
127                         propertiesDsl[property].filterValue = [''];
128                         propertiesDsl[property].filterType = [''];
129                         propertiesDsl[property].dslPath = [];
130                         propertiesDsl[property].dslPathTree = [];
131                     }
132                 }
133             }
134             let sortedPropertiesDsl = propertiesDsl.sort(function (filter1, filter2) {
135                 if (filter1.attributeName < filter2.attributeName) {
136                     return -1;
137                 } else if (filter1.attributeName > filter2.attributeName) {
138                     return 1;
139                 } else {
140                     return 0;
141                 }
142             });
143             //console.log('FilterList' + JSON.stringify(sortedPropertiesDsl));
144             return sortedPropertiesDsl;
145         }else{
146             return [];
147         }
148     }
149     var getNodeTypes = function(){
150         var result = JSON.parse(OXM);
151         var arrayOfTypes = result['xml-bindings']['java-types'][0]['java-type'];
152         var nodeTypeArray = [];
153         for(var j = 0; j < arrayOfTypes.length; j++){
154             if(arrayOfTypes[j]['xml-root-element'] && arrayOfTypes[j]['xml-root-element'][0]
155                 && arrayOfTypes[j]['xml-root-element'][0]['$'] && arrayOfTypes[j]['xml-root-element'][0]['$']['name']){
156                 nodeTypeArray.push((arrayOfTypes[j]['xml-root-element'][0]['$']['name']).toLowerCase());
157             }
158         }
159         return nodeTypeArray;
160     }
161     var populateContainer = function(nodeType){
162         var result = JSON.parse(OXM);
163         var arrayOfTypes = result['xml-bindings']['java-types'][0]['java-type'];
164         var isFound = false;
165         var container = '';
166         for (var i = 0; i < arrayOfTypes.length && !isFound; i++) {
167              if(arrayOfTypes[i]['xml-root-element'][0]['$']['name'] === nodeType
168                 && arrayOfTypes[i]['xml-properties']
169                 && arrayOfTypes[i]['xml-properties'][0]
170                 && arrayOfTypes[i]['xml-properties'][0]['xml-property']){
171                         isFound = true;
172                         for(var k = 0; k < arrayOfTypes[i]['xml-properties'][0]['xml-property'].length; k++){
173                             if(arrayOfTypes[i]['xml-properties'][0]['xml-property'][k]['$']['name'] === 'container'){
174                                 container = arrayOfTypes[i]['xml-properties'][0]['xml-property'][k]['$']['value'];
175                             }
176                         }
177              }
178         }
179         return container;
180     }
181     var getEditableAttributes = function(nodeType){
182         //get this from the oxm field
183         var result = JSON.parse(OXM);
184         var arrayOfTypes = result['xml-bindings']['java-types'][0]['java-type'];
185         var isFound = false;
186         var uiEditableProps = [];
187         for (var i = 0; i < arrayOfTypes.length && !isFound; i++) {
188              if(arrayOfTypes[i]['xml-root-element'][0]['$']['name'] === nodeType
189                 && arrayOfTypes[i]['xml-properties']
190                 && arrayOfTypes[i]['xml-properties'][0]
191                 && arrayOfTypes[i]['xml-properties'][0]['xml-property']){
192                         isFound = true;
193                         for(var k = 0; k < arrayOfTypes[i]['xml-properties'][0]['xml-property'].length; k++){
194                             if(arrayOfTypes[i]['xml-properties'][0]['xml-property'][k]['$']['name'] === 'uiEditableProps'){
195                                 uiEditableProps = ((arrayOfTypes[i]['xml-properties'][0]['xml-property'][k]['$']['value']).replace(/\s/g,"")).split(',');
196                             }
197                         }
198              }
199         }
200         return uiEditableProps;
201     }
202     var populateEdgeRules = function(nodeType,edgeRules){
203         var nodeDetails = [];
204         var node = null;
205         console.log('populateEdgeRules.nodeType: ' + nodeType);
206         var id = generateID();
207         for (var i = 0; i < edgeRules.length; i++) {
208             var ruleObj = edgeRules[i];
209             if (ruleObj.from === nodeType && !nodeDetails[ruleObj.to + id]) {
210                 node = ruleObj.to + id;
211                 if(!nodeDetails[node]){
212                   nodeDetails[node] = {};
213                   nodeDetails[node].nodeType = ruleObj.to;
214                   nodeDetails[node].isSelected = false;
215                   nodeDetails[node].attrDetails = GeneralCommonFunctions.getFilteringOptions(ruleObj.to);
216                   nodeDetails[node].parentContainer = GeneralCommonFunctions.populateContainer(ruleObj.to);
217               }
218             }
219             if (ruleObj.to === nodeType && !nodeDetails[ruleObj.from + id]) {
220                 node = ruleObj.from + id;
221                 if(!nodeDetails[node]){
222                   nodeDetails[node] = {};
223                   nodeDetails[node].nodeType = ruleObj.from;
224                   nodeDetails[node].isSelected = false;
225                   nodeDetails[node].attrDetails = GeneralCommonFunctions.getFilteringOptions(ruleObj.from);
226                   nodeDetails[node].parentContainer = GeneralCommonFunctions.populateContainer(ruleObj.from);
227               }
228             }
229         }
230         let nodesSorted = nodeDetails.sort(function (filter1, filter2) {
231             if (filter1.nodeType < filter2.nodeType) {
232                 return -1;
233             } else if (filter1.nodeType > filter2.nodeType) {
234                 return 1;
235             } else {
236                 return 0;
237             }
238         });
239         console.log('EdgeRulesList' + JSON.stringify(nodesSorted));
240         nodeDetails = nodesSorted;
241         return nodeDetails;
242     }
243 var camelToDash = function(str){
244     return (str.replace(/\W+/g, '-')
245     .replace(/([a-z\d])([A-Z])/g, '$1-$2')).toLowerCase();
246 }
247 var  generateID = function(){
248     return Math.random().toString().replace('0.', '');
249 }
250 var extractNodeDetails = function(node, isRoot, errorFunction, isAperture){
251   let nodeType =  node['node-type'];
252   let nodeData = {
253       "name": nodeType,
254       "id": nodeType,
255       "children": [],
256       "details":{}
257   }
258   nodeData.details.includeInOutput = node.store;
259   nodeData.details.isSelected = true;
260   nodeData.details.isRootNode = isRoot;
261   nodeData.details.nodeType = nodeType;
262   var attributes = GeneralCommonFunctions.getFilteringOptions(nodeType);
263   nodeData.details.attrDetails = attributes;
264   nodeData.details.parentContainer = GeneralCommonFunctions.populateContainer(nodeType);
265   if(node.store && !node['requested-props']){
266       for(var key in nodeData.details.attrDetails){
267           nodeData.details.attrDetails[key].isSelected = true;
268       }
269   }else if (node.store && node['requested-props']){
270        for(var key in node['requested-props']){
271            nodeData.details.attrDetails[key].isSelected = true;
272        }
273   }
274   var isValid = true;
275   for (var x in node['node-filter']){
276       if(isValid){
277           for (var y in node['node-filter'][x]) {
278               if(isValid){
279                   var attrKey = node['node-filter'][x][y]['key'];
280                   var filter = node['node-filter'][x][y]['filter'];
281                   //If aperture is not turned on and query loaded uses anything besides EQ throw error
282                   //when merged with calls from builder use condition to enable this
283                   /*if(!isAperture && filter !== 'EQ'){
284                       errorFunction(null, "invalidQuery");
285                       isValid = false;
286                   }*/
287                   if(!nodeData.details.attrDetails[attrKey]){
288                       nodeData.details.attrDetails[attrKey] = {};
289                   }
290                   if(nodeData.details.attrDetails[attrKey].filterType.length > 0 && nodeData.details.attrDetails[attrKey].filterType[0] === ''){
291                       nodeData.details.attrDetails[attrKey].filterType = [];
292                   }
293                   if(nodeData.details.attrDetails[attrKey].filterValue.length > 0 && nodeData.details.attrDetails[attrKey].filterValue[0] === ''){
294                       nodeData.details.attrDetails[attrKey].filterValue = [];
295                   }
296
297                   if(node['node-filter'][x][y]['value'][0]){
298                       for (var i in node['node-filter'][x][y]['value']){
299                           nodeData.details.attrDetails[attrKey].filterType.push(filter);
300                           nodeData.details.attrDetails[attrKey].filterValue.push(node['node-filter'][x][y]['value'][i]);
301                       }
302                       if(!nodeData.details.attrDetails[attrKey].attributeName){
303                           nodeData.details.attrDetails[attrKey].attributeName = attrKey;
304                       }
305                   }else{
306                       //if a filter had no values associated to it do nothing
307                       //when merged with calls from builder use condition to enable this
308                       /* errorFunction(null, "invalidQuery");
309                       isValid = false; */
310                   }
311               }
312           }
313       }
314   }
315   var initWhereNode = null;
316   if(node['where-filter'].length > 0){
317       for(var index in node['where-filter']){
318           initWhereNode = GeneralCommonFunctions.extractNodeDetails(node['where-filter'][index].children[0], true);
319       }
320   }
321   if(initWhereNode){
322       nodeData.details.dslPath=[];
323       nodeData.details.dslPathTree=[];
324       nodeData.details.dslPathTree.push(initWhereNode);
325   }
326   if(node.children.length > 0){
327       for(var i = 0; i < node.children.length; i++){
328           nodeData.children[i] = GeneralCommonFunctions.extractNodeDetails(node.children[i], false);
329       }
330   }
331   return nodeData;
332 }
333 export const GeneralCommonFunctions = {
334     scrollTo: scrollTo,
335     populatePathDSL: populatePathDSL,
336     getFilteringOptions: getFilteringOptions,
337     getNodeTypes: getNodeTypes,
338     populateContainer: populateContainer,
339     populateEdgeRules: populateEdgeRules,
340     camelToDash: camelToDash,
341     generateID: generateID,
342     extractNodeDetails: extractNodeDetails,
343     getEditableAttributes: getEditableAttributes
344 };