Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-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
21 /**
22  */
23 angular.module('abs').controller('dcaeMicroServiceController', function ($scope, $window, $compile, PapUrlService, PolicyDictionaryService, modalService, $modal, Notification) {
24     $("#dialog").hide();
25
26         $scope.temp.policy.ttlDate = new Date($scope.temp.policy.ttlDate);
27         var papUrl;
28         PapUrlService.getPapUrl().then(function(data) {
29                 var config = data;
30                 papUrl = config.PAP_URL;
31                 console.log(papUrl);
32                 
33             PolicyDictionaryService.getEcompDictionaryData(papUrl).then(function (data) {
34                 var j = data;
35                 $scope.data = JSON.parse(j.data);
36                 console.log($scope.data);
37                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
38                 console.log($scope.ecompNameDictionaryDatas);
39             }, function (error) {
40                 console.log("failed");
41             });
42             
43             PolicyDictionaryService.getPriorityValueData().then(function (data) {
44                 var j = data;
45                 $scope.data = JSON.parse(j.data);
46                 console.log($scope.data);
47                 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
48                 console.log($scope.priorityDatas);
49             }, function (error) {
50                 console.log("failed");
51             });
52
53             PolicyDictionaryService.getPolicyScopeData(papUrl).then(function (data) {
54                 var j = data;
55                 $scope.data = JSON.parse(j.data);
56                 console.log($scope.data);
57                 $scope.groupPolicyScopeListDatas = JSON.parse($scope.data.groupPolicyScopeListDatas);
58                 console.log($scope.groupPolicyScopeListDatas);
59             }, function (error) {
60                 console.log("failed");
61             });
62
63             PolicyDictionaryService.getMSConfigDataByName(papUrl).then(function (data) {
64                 var j = data;
65                 $scope.data = JSON.parse(j.data);
66                 console.log($scope.data);
67                 $scope.microServiceCongigNameDictionaryDatas = JSON.parse($scope.data.microServiceCongigNameDictionaryDatas);
68                 console.log($scope.microServiceCongigNameDictionaryDatas);
69             }, function (error) {
70                 console.log("failed");
71             });
72
73             PolicyDictionaryService.getMSLocationDataByName(papUrl).then(function (data) {
74                 var j = data;
75                 $scope.data = JSON.parse(j.data);
76                 console.log($scope.data);
77                 $scope.microServiceLocationDictionaryDatas = JSON.parse($scope.data.microServiceLocationDictionaryDatas);
78                 console.log($scope.microServiceLocationDictionaryDatas);
79             }, function (error) {
80                 console.log("failed");
81             });
82
83             PolicyDictionaryService.getMSServiceModelsDataByName(papUrl).then(function (data) {
84                 var j = data;
85                 $scope.data = JSON.parse(j.data);
86                 console.log($scope.data);
87                 $scope.microServiceModelsDictionaryDatas = JSON.parse($scope.data.microServiceModelsDictionaryDatas);
88                 console.log($scope.microServiceModelsDictionaryDatas);
89             }, function (error) {
90                 console.log("failed");
91             });
92
93             PolicyDictionaryService.getDCAEuuidDataByName(papUrl).then(function (data) {
94                 var j = data;
95                 $scope.data = JSON.parse(j.data);
96                 console.log($scope.data);
97                 $scope.dcaeUUIDDictionaryDatas = JSON.parse($scope.data.dcaeUUIDDictionaryDatas);
98                 console.log($scope.dcaeUUIDDictionaryDatas);
99             }, function (error) {
100                 console.log("failed");
101             });
102             
103             PolicyDictionaryService.getRiskTypeDictionaryData(papUrl).then(function (data) {
104                 var j = data;
105                 $scope.data = JSON.parse(j.data);
106                 console.log($scope.data);
107                 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
108                 console.log($scope.riskTypeDictionaryDatas);
109             }, function (error) {
110                 console.log("failed");
111             });
112             
113             PolicyDictionaryService.getRiskLevelValueData().then(function (data) {
114                 var j = data;
115                 $scope.data = JSON.parse(j.data);
116                 console.log($scope.data);
117                 $scope.riskLevelDatas = JSON.parse($scope.data.riskLevelDatas);
118                 console.log($scope.riskLevelDatas);
119             }, function (error) {
120                 console.log("failed");
121             });
122             
123             PolicyDictionaryService.getGuardValueData().then(function (data) {
124                 var j = data;
125                 $scope.data = JSON.parse(j.data);
126                 console.log($scope.data);
127                 $scope.guardDatas = JSON.parse($scope.data.guardDatas);
128                 console.log($scope.guardDatas);
129             }, function (error) {
130                 console.log("failed");
131             });
132         });
133
134          $scope.choices = [];
135          $scope.attributeDatas = [{"attributes" : $scope.choices}];
136          
137          $scope.addNewChoice = function(value) {
138                  console.log(value);
139                  if(value != undefined){
140                         var parentElement = document.getElementById("div."+value);
141                         var div = document.getElementById(value+"@0");
142                         if(div != null){
143                                 var clone = div.cloneNode(true); 
144                                 var addElement = parentElement.childElementCount + 1;
145                                 clone.id = ''+value+'@'+addElement;
146                                 clone.value = '';
147                                 document.getElementById("div."+value).appendChild(clone);
148                                 plainAttributeKeys.push(''+value+'@'+addElement);
149                         }else{
150                                 div = document.getElementById("div."+value+"@0");
151                                 var childElement = parentElement.firstElementChild;
152                                 var countParent = parentElement.childElementCount;
153                                 var childElementString = childElement.innerHTML;
154                                 var find = value+"@0";
155                                 var re = new RegExp(find, 'g');
156                                 childElementString = childElementString.replace(re,value+'@' + countParent);
157                                 var clone = childElement.cloneNode(true);
158                                 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
159                             var childId = parentElement.childNodes[ii].id;
160                             if(ii = parentElement.childNodes.length){
161                                 var childnewId = childId.slice(0, -1);
162                                 var count = childId.slice(-1);
163                             }
164                         }
165                                 var countvalue = parseInt(count) + 1;
166                                 clone.id = childnewId+countvalue;
167                                 clone.value = '';
168                                 clone.innerHTML=childElementString;
169                                 document.getElementById("div."+value).appendChild(clone);
170                                 var inputs = clone.getElementsByTagName("input");
171                                 for(var i=0; i<inputs.length; i++){
172                                         if($scope.temp.policy.ruleData != null){
173                                                 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
174                                         }
175                                         plainAttributeKeys.push(inputs[i].id);
176                                 }
177                                 var ele = angular.element(document.getElementById("div."+value));
178                         $compile(ele.contents())($scope);
179                     $scope.$apply();
180                         }
181                  }
182          };
183          
184          $scope.removeChoice = function(value) {
185                  console.log(value);
186                  if(value != undefined){
187                          document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);     
188                  }
189          };
190             
191     var splitDash = '-';
192         var splitEqual = '=';
193         var splitComma = ',';
194         var splitcolon = ':';
195         var plainAttributeKeys = [];
196     $scope.addDataToFields = function(serviceName){
197         if(serviceName != null){
198                 var myNode = document.getElementById("DynamicTemplate");
199                 myNode.innerHTML = '';
200             var uuu = "policyController/getDCAEMSTemplateData.htm";
201             var postData={policyData: serviceName};
202             $.ajax({
203                 type : 'POST',
204                 url : uuu,
205                 dataType: 'json',
206                 contentType: 'application/json',
207                 data: JSON.stringify(postData),
208                 success : function(data){
209                     $scope.$apply(function(){
210                         $scope.addNewChoice();
211                         var plainAttributeKeys = [];
212                         $scope.dcaeModelData = data[0].dcaeModelData;
213                         var attributes = $scope.dcaeModelData.attributes;
214                         var refAttributes = $scope.dcaeModelData.ref_attributes;
215                         var subAttributes =     $scope.dcaeModelData.sub_attributes;
216             
217                                 $scope.temp.policy.ruleGridData = [];
218                         if(attributes != null && attributes.length>0){  
219                                 if(attributes.indexOf(splitComma) >= 0){
220                                         var attributesAfterCommaSplit = attributes.split(splitComma);
221                                         for(i = 0; i < attributesAfterCommaSplit.length; i++){
222                                                 if(attributesAfterCommaSplit[i].indexOf(splitcolon) > 0){
223                                                         var splitAttribute = attributesAfterCommaSplit[i].split(splitcolon);
224                                                         $scope.attributeLayout(splitAttribute);
225                                                 }
226                                         }
227                                 }else{
228                                         //Implementation for single Attribute
229                                         var attributesAfterCommaSplit = attributes;
230                                         var splitAttribute = attributesAfterCommaSplit.split(splitcolon);
231                                         $scope.attributeLayout(splitAttribute);
232    
233                                 }       
234                         }
235                     if(refAttributes != null){
236                         if(refAttributes.indexOf(splitComma) >= 0){
237                                         var refattributesAfterCommaSplit = refAttributes.split(splitComma);
238                                         for(j = 0; j < refattributesAfterCommaSplit.length; j++){
239                                                 if(refattributesAfterCommaSplit[j].indexOf(splitcolon) > 0){
240                                                         var splitRefAttribute = refattributesAfterCommaSplit[j].split(splitcolon);
241                                                         $scope.refAttributeLayout(splitRefAttribute, subAttributes);
242                                                 }
243                                         }
244                         }else{
245                                 //Implementation for single RefAttribute
246                                 if(refAttributes != ""){
247                                         var refattributesAfterCommaSplit = refAttributes;
248                                         var splitRefAttribute = refattributesAfterCommaSplit.split(splitcolon);
249                                         $scope.refAttributeLayout(splitRefAttribute, subAttributes);    
250                                 }       
251                         }
252                     }   
253                     });
254                     if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
255                         var checkData = [];
256                         var data = [];
257                         if($scope.temp.policy.ruleData != null){
258                                 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
259                                 propNames.forEach(function(name) {
260                                         data.push(name);
261                                 });
262                                 for(a = 0; a < data.length; a++){
263                                         var splitPlainAttributeKey1 = data[a].split(".");
264                                         if(splitPlainAttributeKey1[0].indexOf("@") != -1){
265                                                 var key = splitPlainAttributeKey1[0];
266                                                 checkData.push(key);
267                                         }
268                                 }
269                                 var unique = checkData.filter(onlyUnique);
270                                 for(i =0; i < unique.length; i++){
271                                         if(unique[i].indexOf("@0") == -1){
272                                                 var finalkey = unique[i].split("@")[0];
273                                                 $scope.addNewChoice(finalkey);
274                                         }       
275                                 }
276                         }
277                     }
278                     var ele = angular.element(document.getElementById("DynamicTemplate"));
279                         $compile(ele.contents())($scope);
280                     $scope.$apply();
281                 },
282                 error : function(data){
283                     alert("Error While Retriving the Template Layout Pattren.");
284                 }
285             });
286         }
287     };
288
289     function onlyUnique(value, index, self) { 
290         return self.indexOf(value) === index;
291     };
292     
293     $scope.attributeLayout = function(splitAttribute){
294                 var attributeValue = splitAttribute[0];
295                 var splitAttributeValue = attributeValue.split(splitEqual);
296                 var attibuteKey = splitAttributeValue[0].trim();
297                 var attributeType = splitAttributeValue[1];
298                 var attributedefaultValue = splitAttribute[1];
299                 var attributeRequiredValue = splitAttribute[2];
300                 var attributeManyValue = splitAttribute[3];
301                 var splitAttributeMany = attributeManyValue.split(splitDash);
302                 var attributeManyKey = splitAttributeMany[1];
303                 $scope.temp.policy.ruleGridData.push(attibuteKey);
304                 var br = document.createElement("BR");
305                 var label = document.createElement("Label");
306                 var labeltext = document.createTextNode(attibuteKey);
307                 label.appendChild(labeltext); 
308                 var element = document.getElementById("DynamicTemplate");
309                 var textField = document.createElement("INPUT");
310                 textField.setAttribute("class" , "form-control");
311                 textField.setAttribute("type" , "text");
312                 textField.setAttribute("style" , "width:300px;");
313                 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
314                 var checkKey;
315                 if(attributeManyKey == 'true'){
316                         checkKey = attibuteKey+'@0';
317                         textField.setAttribute("id" , ''+attibuteKey+'@0'+'');
318                         var divTag = document.createElement("div");
319                         divTag.setAttribute("id", "div."+attibuteKey);
320                         var addButton = document.createElement("BUTTON");
321                         var buttonaddLabel = document.createTextNode("+");       
322                         addButton.appendChild(buttonaddLabel); 
323                         addButton.setAttribute("id", attibuteKey);
324                         addButton.setAttribute("class", "btn btn-default");
325                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+attibuteKey+'");');
326                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
327                         var removeButton = document.createElement("BUTTON");
328                         var buttonremoveLabel = document.createTextNode("-");       
329                         removeButton.appendChild(buttonremoveLabel); 
330                         removeButton.setAttribute("class", "btn btn-default");
331                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+attibuteKey+'");');
332                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
333                         document.getElementById("DynamicTemplate").appendChild(addButton); 
334                         document.getElementById("DynamicTemplate").appendChild(removeButton); 
335                         document.getElementById("DynamicTemplate").appendChild(label);  
336                         document.getElementById("DynamicTemplate").appendChild(textField); 
337                         document.getElementById("DynamicTemplate").appendChild(divTag); 
338                         document.getElementById("DynamicTemplate").appendChild(br); 
339                 }else{
340                         checkKey = attibuteKey;
341                         textField.setAttribute("id" , ''+attibuteKey+'');
342                         document.getElementById("DynamicTemplate").appendChild(label);   
343                         document.getElementById("DynamicTemplate").appendChild(textField);  
344                         document.getElementById("DynamicTemplate").appendChild(br);
345                 }
346                 if($scope.temp.policy.ruleData != null){
347                         document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
348                 }
349                 plainAttributeKeys.push(attibuteKey+'*'+attributeManyKey);
350     };
351     
352     $scope.firstlayer = null;
353     $scope.secondlayer = null;
354     $scope.refAttributeLayout = function(splitRefAttribute, subAttributes){
355         var attibuteKey = splitRefAttribute[0];
356                 var splitRefAttributeValue = attibuteKey.split(splitEqual);
357                 var refattributeLabel = splitRefAttributeValue[0].trim();
358                 var refattributeValue = splitRefAttributeValue[1].trim();
359                 
360                 var refattributeManyValue = splitRefAttribute[1];
361                 var splitRefAttributeMany = refattributeManyValue.split(splitDash);
362                 var refattributeManyKey = splitRefAttributeMany[1];
363                 var subAttributeJSON = JSON.parse(subAttributes);
364                 var resultValue = subAttributeJSON[refattributeValue];
365                 var br = document.createElement("BR");
366                 var label = document.createElement("Label");
367                 var labeltext = document.createTextNode(refattributeLabel);
368                 label.appendChild(labeltext);
369                    
370                 if(refattributeManyKey == 'true'){
371  
372                         var divTag = document.createElement("div");
373                         divTag.setAttribute("id", "div."+refattributeLabel);
374                         var addButton = document.createElement("BUTTON");
375                         var buttonLabel = document.createTextNode("+");       
376                         addButton.appendChild(buttonLabel); 
377                         addButton.setAttribute("id", refattributeLabel);
378                         addButton.setAttribute("class", "btn btn-default");
379                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+refattributeLabel+'");');
380                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
381                         var removeButton = document.createElement("BUTTON");
382                         var buttonremoveLabel = document.createTextNode("-");       
383                         removeButton.appendChild(buttonremoveLabel); 
384                         removeButton.setAttribute("class", "btn btn-default");
385                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+refattributeLabel+'");');
386                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly"); 
387                         document.getElementById("DynamicTemplate").appendChild(addButton); 
388                         document.getElementById("DynamicTemplate").appendChild(removeButton);
389                         document.getElementById("DynamicTemplate").appendChild(label);
390                         document.getElementById("DynamicTemplate").appendChild(br); 
391                         document.getElementById("DynamicTemplate").appendChild(divTag);
392                         var divTag = document.createElement("div");
393                         divTag.setAttribute("id", "div."+refattributeLabel+"@0");
394                         document.getElementById("div."+refattributeLabel).appendChild(divTag);
395                         $scope.firstlayer = refattributeLabel+"@0";
396                         $scope.subAttributeLayout(resultValue, subAttributes, refattributeLabel);
397                         document.getElementById("DynamicTemplate").appendChild(br); 
398                 }else{
399                         document.getElementById("DynamicTemplate").appendChild(label);  
400                         document.getElementById("DynamicTemplate").appendChild(br);  
401                         $scope.firstlayer = refattributeLabel;
402                         $scope.subAttributeLayout(resultValue, subAttributes, refattributeLabel);
403                 }
404     };
405     
406     $scope.subAttributeLayout = function(resultValue, subAttributes, refattributeLabel){
407         for(var key in resultValue){
408                 key = key.trim();
409                 if(key == 'logicalConnector'){
410                         $scope.secondlayer = null;
411                 }
412                         var splitSubAttribute = resultValue[key].split(splitcolon);
413                         var br = document.createElement("BR");
414                         var label = document.createElement("Label");
415                         var labeltext = document.createTextNode(key);
416                         label.appendChild(labeltext);
417                         var textField = document.createElement("INPUT");
418                         textField.setAttribute("class" , "form-control");
419                         textField.setAttribute("type" , "text");
420                         textField.setAttribute("style" , "width:300px;");
421                         textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
422         
423                         if(splitSubAttribute.length >= 3){
424                                 var subattributeType = splitSubAttribute[0];
425                                 var subattributedefaultValue = splitSubAttribute[1];
426                                 var subattributeRequiredValue = splitSubAttribute[2];
427                                 var subattributeManyValue = splitSubAttribute[3];
428                                 var splitSubAttributeMany = subattributeManyValue.split(splitDash);
429                                 var subattributeManyKey = splitSubAttributeMany[1];
430                                 if(subattributeManyKey == 'true'){      
431                                         if($scope.firstlayer == null && $scope.secondlayer == null){
432                                                 textField.setAttribute("id" , ''+key+'@0'+'');
433                                         }
434                                         if($scope.firstlayer != null && $scope.secondlayer == null){
435                                                 textField.setAttribute("id" , ''+$scope.firstlayer+'.'+key+'@0'+'');
436                                         }
437                                         if($scope.firstlayer != null && $scope.secondlayer != null){
438                                                 textField.setAttribute("id" , ''+$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0'+'');
439                                         }       
440                                         var divTag = document.createElement("div");
441                                         var addButton = document.createElement("BUTTON");
442                                         var buttonLabel = document.createTextNode("+");       
443                                         addButton.appendChild(buttonLabel); 
444                                         addButton.setAttribute("class", "btn btn-default");
445                                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
446                                         var removeButton = document.createElement("BUTTON");
447                                         var buttonremoveLabel = document.createTextNode("-");       
448                                         removeButton.appendChild(buttonremoveLabel); 
449                                         removeButton.setAttribute("class", "btn btn-default");
450                                         if($scope.secondlayer == null){
451                                                 addButton.setAttribute("ng-click" ,  'addNewChoice("'+$scope.firstlayer+'.'+key+'");');
452                                                 removeButton.setAttribute("ng-click" ,  'removeChoice("'+$scope.firstlayer+'.'+key+'");');
453                                                 divTag.setAttribute("id", "div."+$scope.firstlayer+'.'+key);
454                                         }else{
455                                                 addButton.setAttribute("ng-click" ,  'addNewChoice("'+$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'");');
456                                                 removeButton.setAttribute("ng-click" ,  'removeChoice("'+$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'");');
457                                                 divTag.setAttribute("id", "div."+$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key);
458                                         }       
459                                         
460                                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
461                                         if(!refattributeLabel.startsWith('div.')){
462                                                 refattributeLabel = 'div.'+refattributeLabel+"@0";
463                                         }
464                                         document.getElementById(refattributeLabel).appendChild(addButton); 
465                                         document.getElementById(refattributeLabel).appendChild(removeButton); 
466                                         document.getElementById(refattributeLabel).appendChild(label);
467                                         document.getElementById(refattributeLabel).appendChild(textField);  
468                                         document.getElementById(refattributeLabel).appendChild(divTag);
469                                         document.getElementById(refattributeLabel).appendChild(br);
470                                 }else{
471                                         if($scope.firstlayer == null && $scope.secondlayer == null){
472                                                 textField.setAttribute("id" , ''+key+'');
473                                         }
474                                         if($scope.firstlayer != null && $scope.secondlayer == null){
475                                                 textField.setAttribute("id" , ''+$scope.firstlayer+'.'+key+'');
476                                         }
477                                         if($scope.firstlayer != null && $scope.secondlayer != null){
478                                                 textField.setAttribute("id" , ''+$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'');
479                                         }
480                                         if(!refattributeLabel.startsWith('div.')){
481                                                 refattributeLabel = 'div.'+refattributeLabel+"@0";
482                                         }
483                                         document.getElementById(refattributeLabel).appendChild(label); 
484                                         document.getElementById(refattributeLabel).appendChild(textField); 
485                                         document.getElementById(refattributeLabel).appendChild(br); 
486                                 }
487                                 if($scope.secondlayer == null){
488                                         plainAttributeKeys.push($scope.firstlayer+'.'+key+'*'+subattributeManyKey);
489                                         if($scope.temp.policy.ruleData != null){
490                                                 if(document.getElementById($scope.firstlayer+'.'+key) != null){
491                                                         document.getElementById($scope.firstlayer+'.'+key).value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+key];
492                                                 }else{
493                                                         document.getElementById($scope.firstlayer+'.'+key+'@0').value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+key+'@0'];
494                                                         var i =1 ;
495                                                         while(true){
496                                                                 var newValue = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+key+'@'+i];
497                                                                 if(newValue==null){
498                                                                         break;
499                                                                 }else{
500                                                                         $scope.addNewChoice($scope.firstlayer+'.'+key);
501                                                                         document.getElementById($scope.firstlayer+'.'+key+'@'+i).value = newValue;
502                                                                         i = i+1;
503                                                                 }
504                                                         }
505                                                 }
506                                                 
507                                         }
508                                 }else{
509                                         plainAttributeKeys.push($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'*'+subattributeManyKey);
510                                         if($scope.temp.policy.ruleData != null){
511                                                 if(document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key) != null){
512                                                         document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key).value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key];
513                                                 }else{
514                                                         document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0').value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0'];
515                                                         var i =1 ;
516                                                         while(true){
517                                                                 var newValue = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@'+i];
518                                                                 if(newValue==null){
519                                                                         break;
520                                                                 }else{
521                                                                         $scope.addNewChoice($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key);
522                                                                         document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@'+i).value = newValue;
523                                                                         i = i+1;
524                                                                 }
525                                                         }
526                                                 }
527                                                 
528                                         }
529                                 }
530                         }else{
531                                 //Add Recursive Function
532                                 var saveResultValue = resultValue;
533                                 var subAttributeJSON = JSON.parse(subAttributes);
534                                 var recursiveKey = splitSubAttribute[0].trim();
535                                 var resultValue = subAttributeJSON[recursiveKey];
536                                 var recursiveMany = splitSubAttribute[1];
537                                 var br = document.createElement("BR");
538                                 var label = document.createElement("Label");
539                                 var labeltext = document.createTextNode(key);
540                                 label.appendChild(labeltext);
541
542                                 if(recursiveMany == 'MANY-true'){
543                                         var addButton = document.createElement("BUTTON");
544                                         var buttonLabel = document.createTextNode("+");
545                                         addButton.appendChild(buttonLabel); 
546                                         addButton.setAttribute("class", "btn btn-default");
547                                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
548                                         var removeButton = document.createElement("BUTTON");
549                                         var buttonremoveLabel = document.createTextNode("-");       
550                                         removeButton.appendChild(buttonremoveLabel); 
551                                         removeButton.setAttribute("class", "btn btn-default");
552                                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+$scope.firstlayer+'.'+key+'");');
553                                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+$scope.firstlayer+'.'+key+'");');
554                                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
555                                         var idf = "div."+$scope.firstlayer;
556                                         document.getElementById(idf).appendChild(addButton); 
557                                         document.getElementById(idf).appendChild(removeButton);
558                                         document.getElementById(idf).appendChild(label);
559                                         document.getElementById(idf).appendChild(br); 
560                                         var id = "div."+$scope.firstlayer+'.'+key;
561                                         var divTag = document.createElement("div");
562                                         divTag.setAttribute("id",id); 
563                                         document.getElementById("div."+$scope.firstlayer).appendChild(divTag);
564                                         $scope.secondlayer = key;
565                                         var idc = "div."+$scope.firstlayer+key+'@0';
566                                         var divTag = document.createElement("div");
567                                         divTag.setAttribute("id", idc);
568                                         document.getElementById(id).appendChild(divTag);
569                                         $scope.subAttributeLayout(resultValue, subAttributes, idc);
570                                         document.getElementById(id).appendChild(br); 
571                                 }else{
572                                         var id = "div."+$scope.firstlayer+'.'+key;
573                                         var divTag = document.createElement("div");
574                                         divTag.setAttribute("id",id); 
575                                         document.getElementById("div."+$scope.firstlayer).appendChild(divTag);
576                                         document.getElementById(id).appendChild(br); 
577                                         document.getElementById(id).appendChild(label); 
578                                         document.getElementById(id).appendChild(br);
579                                         $scope.secondlayer = key;
580                                         $scope.subAttributeLayout(resultValue, subAttributes, id);
581                                 }
582                                 resultValue = saveResultValue;
583                                 //$scope.secondlayer == null;
584                                 if($scope.secondlayer == null){
585                                         plainAttributeKeys.push($scope.firstlayer+'.'+key+'*'+recursiveMany);
586                                         if($scope.temp.policy.ruleData != null){
587                                                 if(document.getElementById($scope.firstlayer+'.'+key) != null){
588                                                         document.getElementById($scope.firstlayer+'.'+key).value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+key];
589                                                 }else{
590                                                         document.getElementById($scope.firstlayer+'.'+key+'@0').value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+key+'@0'];
591                                                 }
592                                         }
593                                 }else{
594                                         plainAttributeKeys.push($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'*'+recursiveMany);
595                                         if($scope.temp.policy.ruleData != null){
596                                                 if(document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key) != null){
597                                                         document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key).value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key];
598                                                 }else{
599                                                         if(document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0') != null){
600                                                                 document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0').value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0'];
601                                                         }else if(document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key) != null){
602                                                                 document.getElementById($scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key).value = $scope.temp.policy.ruleData[$scope.firstlayer+'.'+$scope.secondlayer+'@0.'+key+'@0'];
603                                                         }                                               
604                                                 }                                       
605                                         }
606                                 }       
607                         }
608                 }
609     };
610     
611     $scope.savePolicy = function(policy){
612         var splitAt = '*';
613         var dot ='.';
614         var jsonPolicy = {};
615         if(plainAttributeKeys != null){
616                 for(a = 0; a < plainAttributeKeys.length; a++){
617                         var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
618                         console.log(splitPlainAttributeKey[1]); 
619                         var searchElement = document.getElementById(splitPlainAttributeKey[0]);
620                         var key = splitPlainAttributeKey[0];
621                         if(searchElement == null){
622                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
623                                 key = splitPlainAttributeKey[0]+'@0';
624                         }
625                         if(searchElement != null){
626                                 if(searchElement.value != null){
627                                         jsonPolicy[key]= searchElement.value;
628                                 }
629                         }
630                 }
631         }
632         var uuu = "policyController/save_DCAEMSPolicy.htm";
633         var postData={policyData: policy, policyJSON : jsonPolicy};
634         $.ajax({
635             type : 'POST',
636             url : uuu,
637             dataType: 'json',
638             contentType: 'application/json',
639             data: JSON.stringify(postData),
640             success : function(data){
641                 $scope.$apply(function(){
642                     $scope.data=data.data;
643                         $scope.temp.policy.readOnly = 'true';
644                     Notification.success("Policy Saved Successfully.");
645                 });
646                 console.log($scope.data);
647                 $modalInstance.close();
648
649             },
650             error : function(data){
651                 Notification.error("Error Occured while saving Policy.");
652             }
653         });
654     };
655     
656     $scope.validatePolicy = function(policy){
657         var uuu = "policyController/validate_policy.htm";
658                 var postData={policyData: policy};
659                 $.ajax({
660                         type : 'POST',
661                         url : uuu,
662                         dataType: 'json',
663                         contentType: 'application/json',
664                         data: JSON.stringify(postData),
665                         success : function(data){
666                                 $scope.$apply(function(){
667                                         $scope.data=data.data.substring(1,8);
668                                                 var size = data.data.length;
669                                                 if($scope.data == 'success'){
670                                                         Notification.success("Validation Success.");
671                                                         if (size > 18){
672                                                                 var displayWarning = data.data.substring(19,size);
673                                                                 window.alert(displayWarning);
674                                                         }
675                                                 }else{
676                                                         Notification.error("Validation Failed.");
677                                                 }
678                                                 
679                                 });
680                                 console.log($scope.data);       
681                         },
682                         error : function(data){
683                                 Notification.error("Validation Failed.");
684                         }
685                 });
686     };
687
688     function extend(obj, src) {
689         for (var key in src) {
690             if (src.hasOwnProperty(key)) obj[key] = src[key];
691         }
692         return obj;
693     }
694 });