Policy 1707 Second commit
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / DCAEMicroServicePolicyController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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 angular.module('abs').controller('dcaeMicroServiceController', ['$scope', '$window', '$compile', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, $window, $compile, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
21     $("#dialog").hide();
22     
23     $scope.policyNavigator;
24     $scope.isCheck = false;
25     $scope.savebutton = true;
26     $scope.refreshCheck = false;
27     
28     $scope.refresh = function(){
29         if($scope.refreshCheck){
30                 $scope.policyNavigator.refresh();
31         }
32         $scope.modal('createNewPolicy', true);
33     };
34     
35     $scope.modal = function(id, hide) {
36         return $('#' + id).modal(hide ? 'hide' : 'show');
37     };
38     
39         if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly  != undefined){
40                 if ($scope.temp.policy.configName == undefined){
41                         $scope.isCheck = false;
42                 }else{
43                         $scope.isCheck = true;
44                 }
45         }else {
46                 $scope.isCheck = false;
47         }
48                 
49         PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
50                 var j = data;
51                 $scope.data = JSON.parse(j.data);
52                 console.log($scope.data);
53                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
54                 console.log($scope.ecompNameDictionaryDatas);
55         }, function (error) {
56                 console.log("failed");
57         });
58
59         PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
60                 var j = data;
61                 $scope.data = JSON.parse(j.data);
62                 console.log($scope.data);
63                 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
64                 console.log($scope.priorityDatas);
65         }, function (error) {
66                 console.log("failed");
67         });
68
69         PolicyAppService.getData('getDictionary/get_GroupPolicyScopeDataByName').then(function (data) {
70                 var j = data;
71                 $scope.data = JSON.parse(j.data);
72                 console.log($scope.data);
73                 $scope.groupPolicyScopeListDatas = JSON.parse($scope.data.groupPolicyScopeListDatas);
74                 console.log($scope.groupPolicyScopeListDatas);
75         }, function (error) {
76                 console.log("failed");
77         });
78
79         PolicyAppService.getData('getDictionary/get_MicroServiceConfigNameDataByName').then(function (data) {
80                 var j = data;
81                 $scope.data = JSON.parse(j.data);
82                 console.log($scope.data);
83                 $scope.microServiceCongigNameDictionaryDatas = JSON.parse($scope.data.microServiceCongigNameDictionaryDatas);
84                 console.log($scope.microServiceCongigNameDictionaryDatas);
85         }, function (error) {
86                 console.log("failed");
87         });
88
89         PolicyAppService.getData('getDictionary/get_MicroServiceLocationDataByName').then(function (data) {
90                 var j = data;
91                 $scope.data = JSON.parse(j.data);
92                 console.log($scope.data);
93                 $scope.microServiceLocationDictionaryDatas = JSON.parse($scope.data.microServiceLocationDictionaryDatas);
94                 console.log($scope.microServiceLocationDictionaryDatas);
95         }, function (error) {
96                 console.log("failed");
97         });
98
99         PolicyAppService.getData('getDictionary/get_MicroServiceModelsDataByName').then(function (data) {
100                 var j = data;
101                 $scope.data = JSON.parse(j.data);
102                 console.log($scope.data);
103                 var inputModelList = JSON.parse($scope.data.microServiceModelsDictionaryDatas);
104                 var unique = {};
105                 var uniqueList = [];
106                 for(var i = 0; i < inputModelList.length; i++){
107                         if(typeof unique[inputModelList[i]] == "undefined"){
108                                 unique[inputModelList[i]] = "";
109                                 uniqueList.push(inputModelList[i]);
110                         }
111                 }
112                 $scope.microServiceModelsDictionaryDatas = uniqueList;
113                 console.log($scope.microServiceModelsDictionaryDatas);
114         }, function (error) {
115                 console.log("failed");
116         });
117
118         PolicyAppService.getData('getDictionary/get_DCAEUUIDDataByName').then(function (data) {
119                 var j = data;
120                 $scope.data = JSON.parse(j.data);
121                 console.log($scope.data);
122                 $scope.dcaeUUIDDictionaryDatas = JSON.parse($scope.data.dcaeUUIDDictionaryDatas);
123                 console.log($scope.dcaeUUIDDictionaryDatas);
124         }, function (error) {
125                 console.log("failed");
126         });
127
128         PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
129                 var j = data;
130                 $scope.data = JSON.parse(j.data);
131                 console.log($scope.data);
132                 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
133                 console.log($scope.riskTypeDictionaryDatas);
134         }, function (error) {
135                 console.log("failed");
136         });
137
138         PolicyAppService.getData('getDictionary/get_MicroServiceAttributeData').then(function (data) {
139                 var j = data;
140                 $scope.data = JSON.parse(j.data);
141                 console.log($scope.data);
142                 $scope.microServiceAttributeDictionaryDatas = JSON.parse($scope.data.microServiceAttributeDictionaryDatas);
143                 console.log($scope.microServiceAttributeDictionaryDatas);
144         }, function (error) {
145                 console.log("failed");
146         });
147             
148
149          $scope.choices = [];
150          $scope.attributeDatas = [{"attributes" : $scope.choices}];
151          
152          $scope.addNewChoice = function(value) {
153                  console.log(value);
154                  if(value != undefined){
155                         if (value.startsWith('div.')){
156                                 value = value.replace('div.','');
157                         }
158                         var parentElement = document.getElementById("div."+value);
159                         var div = document.getElementById(value+"@0");
160                         if(div != null){
161                                 var clone = div.cloneNode(true); 
162                                 var addElement = parentElement.childElementCount + 1;
163                                 clone.id = ''+value+'@'+addElement;
164                                 clone.value = '';
165                                 document.getElementById("div."+value).appendChild(clone);
166                                 plainAttributeKeys.push(''+value+'@'+addElement);
167                         }else{
168                                 div = document.getElementById("div."+value+"@0");
169                                 var childElement = parentElement.firstElementChild;
170                                 var countParent = parentElement.childElementCount;
171                                 var childElementString = childElement.innerHTML;
172                                 var find = value+"@0";
173                                 var re = new RegExp(find, 'g');
174                                 childElementString = childElementString.replace(re,value+'@' + countParent);
175                                 var clone = childElement.cloneNode(true);
176                                 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
177                             var childId = parentElement.childNodes[ii].id;
178                             if(ii = parentElement.childNodes.length){
179                                 var childnewId = childId.slice(0, -1);
180                                 var count = childId.slice(-1);
181                             }
182                         }
183                                 var countvalue = parseInt(count) + 1;
184                                 clone.id = childnewId+countvalue;
185                                 clone.value = '';
186                                 clone.innerHTML=childElementString;
187                                 document.getElementById("div."+value).appendChild(clone);
188                                 var selects = clone.getElementsByTagName("select");
189                                 var inputs = clone.getElementsByTagName("input");
190                                 var removeValues = [];
191                                 for(var i=0; i<inputs.length; i++){
192                                         if ($scope.temp.policy.ruleData!=undefined){
193                                                 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
194                                                 if (checkValue!=undefined){
195                                                         if($scope.temp.policy.ruleData != null){
196                                                                 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
197                                                                 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
198                                                                 plainAttributeKeys.push(inputs[i].id);
199                                                         }
200                                                 } else {
201 //                                                      removeValues.push(inputs[i].id);
202                                                         plainAttributeKeys.push(inputs[i].id);
203                                                 }
204                                         }else {
205                                                 plainAttributeKeys.push(inputs[i].id);
206                                         }
207                                 }
208                                 
209                                 for(var i=0; i<selects.length; i++){
210                                         if ($scope.temp.policy.ruleData!=undefined){
211                                                 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
212                                                 if (checkValue!=undefined){
213                                                         if($scope.temp.policy.ruleData != null){
214                                                                 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
215                                                                 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
216                                                                 plainAttributeKeys.push(selects[i].id);
217                                                         }
218                                                 } else {
219                                                         plainAttributeKeys.push(selects[i].id);
220                                                 }
221                                         }else {
222                                                 plainAttributeKeys.push(selects[i].id);
223                                         }
224                                 }
225                                 
226                                 for (var k=0; k<removeValues.length; k++){
227                                         var elem = document.getElementById(removeValues[k]);
228                                         elem.parentNode.removeChild(elem);
229                                 }
230                                 var ele = angular.element(document.getElementById("div."+value));
231                         $compile(ele.contents())($scope);
232                     $scope.$apply();
233                         }
234                  }
235          };
236          
237          $scope.removeChoice = function(value) {
238                  console.log(value);
239                  if(value != undefined){
240                          document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);     
241                  }
242          };
243          
244          $scope.pullVersion = function(serviceName) {
245                  console.log(serviceName);
246                  if(serviceName != undefined){
247                          var uuu = "policyController/getModelServiceVersioneData.htm";
248                          var postData={policyData: serviceName};
249                          $.ajax({
250                                  type : 'POST',
251                                  url : uuu,
252                                  dataType: 'json',
253                                  contentType: 'application/json',
254                                  data: JSON.stringify(postData),
255                                  success : function(data){
256                                          $scope.$apply(function(){
257                                                  $scope.microServiceModelsDictionaryVersionDatas = data[0].dcaeModelVersionData;
258                                          });
259                                  },
260                                  error : function(data){
261                                          alert("Error While Retriving the Template Layout Pattren.");
262                                  }
263                          });             
264                  }
265          };
266     
267     var splitDash = '-';
268         var splitEqual = '=';
269         var splitComma = ',';
270         var splitcolon = ':';
271         var splitsemicolon = ";";
272         var splitEnum = "],";
273         var plainAttributeKeys = [];
274         var matching = [];
275         var enumKeyList = [];
276         var dictionaryList = [];
277         var dictionaryNameList = [];
278     $scope.addDataToFields = function(serviceName, version){
279         if(serviceName != null && version !=null){
280                 var service=serviceName+"-v"+version;
281                 var myNode = document.getElementById("DynamicTemplate");
282                 myNode.innerHTML = '';
283             var uuu = "policyController/getDCAEMSTemplateData.htm";
284             var postData={policyData: service};
285             $.ajax({
286                 type : 'POST',
287                 url : uuu,
288                 dataType: 'json',
289                 contentType: 'application/json',
290                 data: JSON.stringify(postData),
291                 success : function(data){
292                     $scope.$apply(function(){
293                         $scope.addNewChoice();
294                         var plainAttributeKeys = [];
295                         $scope.dcaeModelData = data[0].dcaeModelData;
296                         $scope.dcaeJsonDate = data[0].jsonValue;
297                         var attributes = $scope.dcaeModelData.attributes;
298                         var refAttributes = $scope.dcaeModelData.ref_attributes;
299                         var subAttributes =     $scope.dcaeModelData.sub_attributes;
300                         var enumAttributes = $scope.dcaeModelData.enumValues;
301                         var annotation = $scope.dcaeModelData.annotation;
302                         var dictionary = $scope.microServiceAttributeDictionaryDatas;
303
304                         if (annotation == null || annotation.length<1){
305                                 $scope.isCheck = true;
306                         }else {
307                                 $scope.isCheck = false;
308                                 var annoationList = annotation.split(splitComma);
309                                 for (k = 0; k < annoationList.length; k++){
310                                         var splitAnnotation = annoationList[k].split(splitEqual);
311                                         if (splitAnnotation[1].includes("matching-true")){
312                                                 matching.push(splitAnnotation[0].trim());
313                                         }
314                                 }
315
316                         }
317
318                         if (dictionary!= null || dictionary>1){
319                                 if (dictionary!= null || dictionary.length>1){
320                                         for (m=0; m < dictionary.length; m += 1){
321                                                 var keyCompare = dictionary[m].name;
322                                                 var valueCompare = dictionary[m].value;
323                                                 var valueModel = dictionary[m].modelName;
324                                                 var conpairService = serviceName;
325                                                 if (valueModel.includes('-v')){
326                                                         conpairService = service;
327                                                 }
328                                                 if(valueModel.localeCompare(conpairService) == 0){
329                                                         console.log(valueCompare);      
330                                                         dictionaryList.push(dictionary[m]);
331                                                         if (!dictionaryNameList.includes(dictionary[m].name)){
332                                                                 dictionaryNameList.push(dictionary[m].name)
333                                                         }
334                                                 }
335                                         }
336                                 }
337                         }
338
339                                 $scope.temp.policy.ruleGridData = [];
340                                 
341                                 $scope.jsonLayout($scope.dcaeJsonDate);
342                                 
343                     });
344                     if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
345                         var checkData = [];
346                         var data = [];
347                         if($scope.temp.policy.ruleData != null){
348                                 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
349                                 propNames.forEach(function(name) {
350                                         data.push(name);
351                                 });
352                                 for(a = 0; a < data.length; a++){
353                                         var splitPlainAttributeKey1 = data[a].split(".");
354                                         if(splitPlainAttributeKey1[0].indexOf("@") != -1){
355                                                 var key = splitPlainAttributeKey1[0];
356                                                 checkData.push(key);
357                                         }
358                                 }
359                                 var unique = checkData.filter(onlyUnique);
360                                 for(i =0; i < unique.length; i++){
361                                         if(unique[i].indexOf("@0") == -1){
362                                                 var finalkey = unique[i].split("@")[0];
363                                                 $scope.addNewChoice(finalkey);
364                                         }       
365                                 }
366                         }
367                     }
368                     var ele = angular.element(document.getElementById("DynamicTemplate"));
369                         $compile(ele.contents())($scope);
370                     $scope.$apply();
371                 },
372                 error : function(data){
373                     alert("Error While Retriving the Template Layout Pattren.");
374                 }
375             });
376         }
377     };
378     
379     function getDictionary(attribute){
380         var dicName = attribute;
381         if(attribute){
382            if(attribute.includes(":")){
383                    dicName = attribute.split(":")[0];
384            }
385         }
386             var dictionaryRegExp = new RegExp(dicName);
387             listemunerateValues = [];
388             if (dictionaryRegExp.test(dictionaryNameList)) {
389                         for (p=0; p < dictionaryList.length; p += 1) {
390                                 if (dicName == dictionaryList[p].name) {
391                                         listemunerateValues.push(dictionaryList[p].value);
392                                 }
393                         }
394             }
395             return listemunerateValues;
396     }
397     
398     function getList(attribute) {
399                 var enumName = attribute;
400                 console.log("In getList: attribute => " + attribute);
401                 if(attribute){
402                    if(attribute.includes(":")){
403                            enumName = attribute.split(":")[0];
404                    }
405                 }       
406             var baseEnum = $scope.dcaeModelData.enumValues;
407                 var enumList = baseEnum.split(splitEnum);
408                 var enumAttributes;
409                 var patternTest = new RegExp(enumName);
410                 for (k=0; k < enumList.length; k += 1){
411                         if(patternTest.test(enumList[k]) == true){
412                                 enumAttributes = enumList[k].trim();
413                 }
414         }
415
416          if(enumAttributes){
417                 enumAttributes = enumAttributes.replace("[", "");
418                 enumAttributes = enumAttributes.replace("]", "");
419                 enumAttributes = enumAttributes.replace(/ /g, '');
420                         var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
421                         listemunerateValues  = dropListAfterCommaSplit[1].split(splitComma);
422                         //enumKeyList.push(attribute); 
423                 return listemunerateValues;
424          }
425          
426             return [];
427             }
428             
429             function getBooleanList(){
430                 var booleanList = [];
431                 booleanList.push(true);
432                 booleanList.push(false);
433                 return booleanList;
434             }
435             
436
437             function isArray(arrayTest) {
438                 return Object.prototype.toString.call(arrayTest) === '[object Array]';
439             }
440             var lableList = [];
441             
442                 function deconstructJSON(dataTest, level , name) {
443                         var array = false;
444                         var label = level;
445                          var stringValue = "java.lang.String";
446                          var intValue = "int";
447                          var double = "double";
448                          var boolean = "boolean";
449                          var baseLevel = level;
450                          
451                         if (name.length > 1){
452                                 label = label + name + '.';
453                         }
454                         
455                     for (key in dataTest) {
456                         array = isArray(dataTest[key]);
457                         console.log(key , dataTest[key]);
458                         if (!!dataTest[key] && typeof(dataTest[key])=="object") {
459                                 if (array==false && key!=="0"){
460                                         $scope.labelLayout(label, key, array );                         
461                                 }
462                                 
463                                 if (array == true && key!=0){
464                                         lableList.push(key);
465                                 }
466                                 
467                                 if (lableList.length > 0){
468                                         array = true;
469                                 }
470                                 if ( key=="0"){
471                                         var newKey = lableList.pop();
472                                         $scope.labelLayout(baseLevel, newKey, array );
473                                         if (array){
474                                                 label = baseLevel + newKey + '@0.';
475                                         } else {
476                                                 label = baseLevel + newKey + '.';
477                                         }
478                                 }
479                                 deconstructJSON(dataTest[key] , label, key);
480                         } else {
481                                 var attirbuteLabel = label;
482                                 var defaultValue='';
483                                 var isRequired = false;
484                                 if (dataTest[key].includes('defaultValue-')){
485                                         defaultValue = dataTest[key].split('defaultValue-')[1];
486                                 }
487
488                                 if (key==="0"){
489                                         array = true;
490                                         attributekey = lableList.pop();
491                                         attirbuteLabel = baseLevel;
492                                 } else {
493                                         attributekey = key.split();
494                                 }
495                                 
496                                 if (dataTest[key].includes('required-true')){
497                                         isRequired = true;
498                                 }
499                                 console.log("attirbuteLabel = " + attirbuteLabel);
500                                 switch (dataTest[key].split(splitcolon)[0]){
501                                         case stringValue:
502                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
503                                                 break;
504                                         case intValue: 
505                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
506                                                 break;
507                                         case double:
508                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
509                                                 break;
510                                         case boolean:
511                                                 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], getBooleanList());
512                                                 break;
513                                         default:
514                                                 if (dataTest[key].includes('dictionary-')){
515                                                         var list = getDictionary(dataTest[key].split('dictionary-')[1]);
516                                                 }else{
517                                                         var list = getList(dataTest[key]);
518                                                 }
519                                                 if (list.length===0){
520                                                         $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
521                                                 }else{
522                                                         $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], list);
523                                                 }
524                                                 break;
525                                 }
526                         }
527                     }
528                 }  
529
530             $scope.jsonLayout = function(dataTest){
531                 deconstructJSON(dataTest , "", "");
532             }
533             
534             
535             $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired){
536                         $scope.temp.policy.ruleGridData.push(attibuteKey);                      
537                 var br = document.createElement("BR");
538                 
539                 var label = document.createElement("Label");
540                 var labeltext = null;
541                 var requiredLabName = "";
542                 if (matching.includes(attibuteKey)){
543                         labeltext = document.createTextNode(attibuteKey + "*!");                
544                 }else {
545                         //var labeltext = document.createTextNode(refattributeLabel);
546                         if(isRequired){
547                                 requiredLabName = attibuteKey + " *";
548                                 labeltext = document.createTextNode(requiredLabName);
549                         }else{
550                             labeltext = document.createTextNode(attibuteKey);   
551                         }
552                 }
553
554                 
555                 var divID = labelValue;
556                 
557                 if (labelValue.length  < 1){
558                         divID = "DynamicTemplate";
559                 }else if (labelValue.endsWith('.')){
560                         var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
561                 }
562                 
563                 label.appendChild(labeltext);
564                 
565                 var textField = document.createElement("INPUT");
566                 
567                 textField.setAttribute("class" , "form-control");
568                 textField.setAttribute("type" , "text");
569                 textField.setAttribute("style" , "width:300px;");
570                 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
571                 var checkKey;
572                 if(attributeManyKey){
573                         checkKey = labelValue + attibuteKey+'@0';
574                         textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
575                         var divTag = document.createElement("div");
576                         divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
577                         var addButton = document.createElement("BUTTON");
578                         var buttonaddLabel = document.createTextNode("+");       
579                         addButton.appendChild(buttonaddLabel); 
580                         addButton.setAttribute("id", labelValue + attibuteKey);
581                         addButton.setAttribute("class", "btn btn-default");
582                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+labelValue + attibuteKey+'");');
583                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
584                         var removeButton = document.createElement("BUTTON");
585                         var buttonremoveLabel = document.createTextNode("-");       
586                         removeButton.appendChild(buttonremoveLabel); 
587                         removeButton.setAttribute("class", "btn btn-default");
588                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+labelValue + attibuteKey+'");');
589                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
590                         document.getElementById(divID).appendChild(addButton); 
591                         document.getElementById(divID).appendChild(removeButton); 
592                         document.getElementById(divID).appendChild(label);  
593                         document.getElementById(divID).appendChild(textField); 
594                         document.getElementById(divID).appendChild(br); 
595                         document.getElementById(divID).appendChild(divTag); 
596                 }else{
597                         checkKey = labelValue + attibuteKey;
598                         textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
599                         if(requiredLabName.includes("*")){
600                                 textField.setAttribute("required", "true");
601                         }
602                         document.getElementById(divID).appendChild(label);  
603                         document.getElementById(divID).appendChild(textField);  
604                         document.getElementById(divID).appendChild(br); 
605
606                 }
607                 
608                 if (defaultValue.length > 0){   
609                         if(defaultValue.includes(":")){
610                                 defaultValue = defaultValue.split(":")[0];
611                                 if(defaultValue === "NA") {
612                                         defaultValue = "";
613                                 }                               
614                         }
615                         document.getElementById(checkKey).value = defaultValue;
616                 }
617                 
618                 if($scope.temp.policy.ruleData != null){
619                         //document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
620                         if (attributeManyKey){
621                                 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
622                                 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
623                                 var i =1 ;
624                                 while(true){
625                                         var newValue = $scope.temp.policy.ruleData[newCheckKey+'@'+i];
626                                         if(newValue==null){
627                                                 break;
628                                         }else{
629                                                 $scope.addNewChoice(newCheckKey);
630                                                 document.getElementById(newCheckKey+'@'+i).value = newValue;
631                                                 i = i+1;
632                                         }
633                                 }               
634                 
635                         }else{
636                                 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
637                         }
638                 } 
639                 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey); 
640     };
641   
642     $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
643                 var label = document.createElement("Label")
644                 var divID = labelValue;
645                 if (labelValue.endsWith('.')){
646                         var workingLabel = labelValue.substring(0, labelValue.length-1);
647                 }else {
648                         var workingLabel = labelValue;
649                 }
650                 
651                 if (labelValue.length  < 1){
652                         divID = "DynamicTemplate";
653                 } else if (labelValue.endsWith('.')){
654                         var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
655                 }
656                 
657                 var labeltext = document.createTextNode(lableName);
658         
659                 label.appendChild(labeltext);
660
661                 if(labelManyKey){
662                         var addButton = document.createElement("BUTTON");
663                         var buttonLabel = document.createTextNode("+");       
664                         addButton.appendChild(buttonLabel); 
665                         addButton.setAttribute("class", "btn btn-default");
666                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+labelValue + lableName+'");');
667                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
668                         var removeButton = document.createElement("BUTTON");
669                         var buttonremoveLabel = document.createTextNode("-");       
670                         removeButton.appendChild(buttonremoveLabel); 
671                         removeButton.setAttribute("class", "btn btn-default");
672                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+labelValue +lableName+'");');
673                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly"); 
674                         document.getElementById(divID).appendChild(addButton); 
675                         document.getElementById(divID).appendChild(removeButton);
676                         document.getElementById(divID).appendChild(label);
677                         var id = "div."+labelValue+lableName;
678                         var divTag = document.createElement("div");
679                         divTag.setAttribute("id", id); 
680                         document.getElementById(divID).appendChild(divTag);
681                         
682                         var divTag = document.createElement("div");
683                         divTag.setAttribute("id", id +'@0');
684                         document.getElementById(id).appendChild(divTag);
685                 }else{
686                         var divTag = document.createElement("div");
687                         divTag.setAttribute("id", "div."+labelValue+lableName);
688                         document.getElementById(divID).appendChild(label);  
689                         document.getElementById(divID).appendChild(divTag);
690                 }
691     };
692
693     $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues){
694                 var br = document.createElement("BR");
695
696                 if (labelLevel.length  < 1){
697                         var divID = "DynamicTemplate";
698                 } else if (labelLevel.endsWith('.')){
699                         var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
700                 }       
701
702
703         var label = document.createElement("Label")
704         if (matching.includes(attributeName)){
705                 var labeltext = document.createTextNode(attributeName + "*!");
706         }else {
707                 var labeltext = document.createTextNode(attributeName);
708         }
709         label.appendChild(labeltext);
710
711         var listField = document.createElement("SELECT");
712         listField.setAttribute("class" , "form-control");
713         listField.setAttribute("style" , "width:300px;");
714         listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
715         
716         for (i=0; i < listemunerateValues.length; i += 1) {
717             option = document.createElement('option');
718             option.setAttribute('value', listemunerateValues[i]);
719             option.appendChild(document.createTextNode(listemunerateValues[i]));
720             listField.appendChild(option);
721         }
722         listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
723         
724         enumKeyList.push(attributeName);
725         
726         document.getElementById(divID).appendChild(label);  
727         document.getElementById(divID).appendChild(br); 
728                         
729         if(many == true){
730                 document.getElementById(divID).appendChild(listField).multiple = true;
731                 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
732         }else {
733                 document.getElementById(divID).appendChild(listField).multiple = false;
734                 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
735         }
736
737         if($scope.temp.policy.ruleData != null){
738                 if (many == true){
739                         document.getElementById(labelLevel +attributeName).options[0].selected = false;
740                         for (i=0; i < listemunerateValues.length; i += 1) {
741                                 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
742                                 if (testValue === undefined){
743                                         testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
744                                         }
745                                 var location = listemunerateValues.indexOf(testValue);
746                                 if (location!=-1){
747                                         document.getElementById(labelLevel +attributeName).options[location].selected = true;
748                                         }
749                                 }                       
750                         }else {
751                         document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];    
752                                 
753                         }
754                 }
755     };
756     
757     function onlyUnique(value, index, self) { 
758         return self.indexOf(value) === index;
759     };
760     
761     
762     function checkDictionary(value){
763         for (i = 0; i < $scope.microServiceAttributeDictionaryDatas.length; i++) {
764                 if ($scope.microServiceAttributeDictionaryDatas[i].name.localeCompare(value)){
765                         return true;
766                 }
767         }
768         
769     }
770     $scope.savePolicy = function(policy){
771         if(policy.itemContent != undefined){
772                 $scope.refreshCheck = true; 
773                 $scope.policyNavigator = policy.itemContent;
774                 policy.itemContent = "";
775         }
776         $scope.savebutton = false;
777         var splitAt = '*';
778         var dot ='.';
779         var jsonPolicy = {};
780         if(plainAttributeKeys != null){
781                 for(a = 0; a < plainAttributeKeys.length; a++){
782                         var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
783                         console.log(splitPlainAttributeKey[1]); 
784                         var searchElement = document.getElementById(splitPlainAttributeKey[0]);
785                         var key = splitPlainAttributeKey[0];
786                 if(searchElement == null){
787                     searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
788                     key = splitPlainAttributeKey[0]+'@0';
789                 }else if (searchElement.nodeName == 'BUTTON'){
790                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
791                                 key = splitPlainAttributeKey[0]+'@0';
792                         }
793                         if(searchElement != null){
794                                 var keySplit = key.split(dot);
795                                 var elumentLocation = keySplit.length;
796                                 var enumKey = key;
797                                 if (elumentLocation > 1){
798                                         enumKey = keySplit[keySplit.length - 1];
799                                 }
800                                 var aWhiteSpace = " ";
801                                 if (enumKeyList.indexOf(enumKey) != -1){
802                                                 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
803                                                         var multiSlect = [];
804                                                         for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
805                                                                 multiSlect.push(searchElement.selectedOptions[i].value);
806                                                                 }
807                                                         jsonPolicy[key]= multiSlect;
808                                                 }else{
809                                                         //set a space due to empty value caused JSON format error in PolicyRestAdapter and remove it in back-end.
810                                                         if(searchElement.value == ""){
811                                                                 searchElement.value = aWhiteSpace;
812                                                         }
813                                                         console.log(" searchElement.value = > " + searchElement.value);
814                                                         jsonPolicy[key]= searchElement.value;
815                                                 }
816                                 } else {
817                                         if(searchElement.value != null){
818                                                         //set a default value due to empty value caused JSON format error in PolicyRestAdapter
819                                                         if(searchElement.value == ""){
820                                                                 searchElement.value = aWhiteSpace;
821                                                         }
822                                                         console.log(" searchElement.value = > " + searchElement.value);
823                                                 jsonPolicy[key]= searchElement.value;
824                                         }
825                                 }
826                         }
827                 }
828         }
829         var uuu = "policycreation/save_policy";
830         var postData={policyData: policy, policyJSON : jsonPolicy};
831         $.ajax({
832             type : 'POST',
833             url : uuu,
834             dataType: 'json',
835             contentType: 'application/json',
836             data: JSON.stringify(postData),
837             success : function(data){
838                 $scope.$apply(function(){
839                         $scope.data=data.policyData;
840                         if($scope.data == 'success'){
841                                 $scope.temp.policy.readOnly = 'true';
842                                 $scope.safetyChecker = data.policyData.split("#")[2];
843                                 if ($scope.safetyChecker!=undefined) {
844                                         Notification.success($scope.safetyChecker);
845                                 }
846                                 $scope.pushStatus=data.policyData.split("&")[1];
847                                 if($scope.pushStatus=="successPush"){
848                                         Notification.success("Policy pushed successfully");
849                                 }
850                                 Notification.success("Policy Saved Successfully.");     
851                         }else if ($scope.data == 'PolicyExists'){
852                                                 $scope.savebutton = true;
853                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
854                                         }
855                 });
856                 console.log($scope.data);
857             },
858             error : function(data){
859                 Notification.error("Error Occured while saving Policy.");
860             }
861         });
862     };
863     
864     $scope.validatePolicy = function(policy){
865         document.getElementById("validate").innerHTML = "";
866         var splitAt = '*';
867         var dot ='.';
868         var jsonPolicy = {};
869         if(plainAttributeKeys != null){
870                 for(a = 0; a < plainAttributeKeys.length; a++){
871                         var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
872                         console.log(splitPlainAttributeKey[1]); 
873                         var searchElement = document.getElementById(splitPlainAttributeKey[0]);
874                         var key = splitPlainAttributeKey[0];
875                         if(searchElement == null){
876                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
877                                 key = splitPlainAttributeKey[0]+'@0';
878                         }else if (searchElement.nodeName == 'BUTTON'){
879                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
880                                 key = splitPlainAttributeKey[0]+'@0';
881                         }
882                         if(searchElement != null){
883                                 if (enumKeyList.indexOf(key) != -1){
884                                                 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
885                                                         var multiSlect = [];
886                                                         for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
887                                                                 multiSlect.push(searchElement.selectedOptions[i].value);
888                                                                 }
889                                                         jsonPolicy[key]= multiSlect;
890                                                 }else{
891                                                         jsonPolicy[key]= searchElement.value;
892                                                 }
893                                 } else {
894                                         if(searchElement.value != null){
895                                                 jsonPolicy[key]= searchElement.value;
896                                                 if(searchElement.getAttribute("required")){
897                                                         if(!searchElement.value){
898                                                                 return;
899                                                         }
900                                                 }                                               
901                                         }
902                                 }
903                         }
904                 }
905         }
906         var uuu = "policyController/validate_policy.htm";
907         var postData={policyData: policy, policyJSON : jsonPolicy};
908                 $.ajax({
909                         type : 'POST',
910                         url : uuu,
911                         dataType: 'json',
912                         contentType: 'application/json',
913                         data: JSON.stringify(postData),
914                         success : function(data){
915                                 $scope.$apply(function(){
916                                         $scope.validateData = data.data.replace(/\"/g, "");
917                                         $scope.data=data.data.substring(1,8);
918                                                 var size = data.data.length;
919                                                 if($scope.data == 'success'){
920                                                         Notification.success("Validation Success.");
921                                                         $scope.savebutton = false;
922                                                         if (size > 18){
923                                                                 var displayWarning = data.data.substring(19,size  - 1);
924                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
925                                                                 document.getElementById("validate").style.color = "white";
926                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
927                                                         }
928                                                 }else{
929                                                         Notification.error("Validation Failed.");
930                                                         document.getElementById("validate").innerHTML = $scope.validateData;
931                                                         document.getElementById("validate").style.color = "white";
932                                                         document.getElementById("validate").style.backgroundColor = "red";
933                                                         $scope.savebutton = true;
934                                                 }
935                                                 
936                                 });
937                                 console.log($scope.data);       
938                         },
939                         error : function(data){
940                                 Notification.error("Validation Failed.");
941                                 $scope.savebutton = true;
942                         }
943                 });
944     };
945
946     function extend(obj, src) {
947         for (var key in src) {
948             if (src.hasOwnProperty(key)) obj[key] = src[key];
949         }
950         return obj;
951     }
952 }]);