2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 angular.module('abs').controller('optimizationController',
21 ['$scope', '$window', '$compile', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification',
22 function ($scope, $window, $compile, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
25 $scope.policyNavigator;
26 $scope.isCheck = false;
27 $scope.savebutton = true;
28 $scope.refreshCheck = false;
30 if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
31 $scope.temp.policy = {
32 policyType : "Config",
33 configPolicyType : "Optimization"
37 $scope.refresh = function(){
38 if($scope.refreshCheck){
39 $scope.policyNavigator.refresh();
41 $scope.modal('createNewPolicy', true);
42 $scope.temp.policy = "";
45 $scope.modal = function(id, hide) {
46 return $('#' + id).modal(hide ? 'hide' : 'show');
49 $('#ttlDate').datepicker({
50 dateFormat: 'dd/mm/yy',
53 onSelect: function(date) {
54 angular.element($('#ttlDate')).triggerHandler('input');
58 if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly != undefined){
59 if ($scope.temp.policy.configName == undefined){
60 $scope.isCheck = false;
62 $scope.isCheck = true;
65 $scope.isCheck = false;
68 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function (data) {
70 $scope.data = JSON.parse(j.data);
71 console.log($scope.data);
72 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
73 console.log($scope.onapNameDictionaryDatas);
75 console.log("failed");
78 PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
80 $scope.data = JSON.parse(j.data);
81 console.log($scope.data);
82 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
83 console.log($scope.priorityDatas);
85 console.log("failed");
88 PolicyAppService.getData('getDictionary/get_OptimizationModelsDataByName').then(function (data) {
90 $scope.data = JSON.parse(j.data);
91 console.log($scope.data);
92 var inputModelList = JSON.parse($scope.data.optimizationModelsDictionaryDatas);
95 for(var i = 0; i < inputModelList.length; i++){
96 if(typeof unique[inputModelList[i]] == "undefined"){
97 unique[inputModelList[i]] = "";
98 uniqueList.push(inputModelList[i]);
101 $scope.optimizationModelsDictionaryDatas = uniqueList;
102 console.log($scope.optimizationModelsDictionaryDatas);
103 }, function (error) {
104 console.log("failed");
107 PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
109 $scope.data = JSON.parse(j.data);
110 console.log($scope.data);
111 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
112 console.log($scope.riskTypeDictionaryDatas);
113 }, function (error) {
114 console.log("failed");
118 $scope.attributeDatas = [{"attributes" : $scope.choices}];
119 $scope.isInitEditTemplate = true; //just initially create the edit template, didn't click add button yet.
120 $scope.addNewChoice = function(value) {
121 console.log("input value : " + value);
122 if(value != undefined){
123 if (value.startsWith('div.')){
124 value = value.replace('div.','');
126 var parentElement = document.getElementById("div."+value);
127 var div = document.getElementById(value+"@0");
129 var clone = div.cloneNode(true);
130 var addElement = parentElement.childElementCount;
131 clone.id = ''+value+'@'+addElement;
133 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ //if it's view or edit
134 if($scope.temp.policy.ruleData[clone.id] || ($scope.temp.policy.editPolicy && !$scope.isInitEditTemplate)){ // Only append child if its value found in ruleData or edit mode
135 if($scope.temp.policy.ruleData[clone.id]){
136 clone.value = $scope.temp.policy.ruleData[clone.id];
138 if(!clone.className.includes("child_single")){
139 clone.className += ' child_single'; //single element clone
141 document.getElementById("div."+value).appendChild(clone);
142 plainAttributeKeys.push(''+value+'@'+addElement);
144 }else{ //not view or edit
145 if(!clone.className.includes("child_single")){
146 clone.className += ' child_single'; //single element clone
148 document.getElementById("div."+value).appendChild(clone);
149 plainAttributeKeys.push(''+value+'@'+addElement);
152 div = document.getElementById("div."+value+"@0");
155 div.className += ' children_group'; //div with a group of children.
157 var childElement = parentElement.firstElementChild;
158 var countParent = parentElement.childElementCount;
159 var childElementString = childElement.innerHTML;
160 var find = value+"@0";
161 var re = new RegExp(find, 'g');
162 childElementString = childElementString.replace(re,value+'@' + countParent);
163 var clone = childElement.cloneNode(true);
164 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
165 var childId = parentElement.childNodes[ii].id;
166 if(ii = parentElement.childNodes.length){
167 var childnewId = childId.slice(0, -1);
168 var count = childId.slice(-1);
171 var countvalue = parseInt(count) + 1;
172 clone.id = childnewId+countvalue;
174 clone.innerHTML=childElementString;
175 document.getElementById("div."+value).appendChild(clone);
176 var selects = clone.getElementsByTagName("select");
177 var inputs = clone.getElementsByTagName("input");
178 var removeValues = [];
179 for(var i=0; i<inputs.length; i++){
180 if ($scope.temp.policy.ruleData!=undefined){
181 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
182 if (checkValue!=undefined && checkValue != "undefined"){
183 if($scope.temp.policy.ruleData != null){
184 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
185 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
186 plainAttributeKeys.push(inputs[i].id);
189 plainAttributeKeys.push(inputs[i].id);
192 plainAttributeKeys.push(inputs[i].id);
196 for(var i=0; i<selects.length; i++){
197 if ($scope.temp.policy.ruleData!=undefined){
198 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
199 if (checkValue!=undefined && checkValue!="undefined"){
200 if($scope.temp.policy.ruleData != null){
201 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
202 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
203 plainAttributeKeys.push(selects[i].id);
206 plainAttributeKeys.push(selects[i].id);
209 plainAttributeKeys.push(selects[i].id);
213 for (var k=0; k<removeValues.length; k++){
214 var elem = document.getElementById(removeValues[k]);
215 elem.parentNode.removeChild(elem);
217 var ele = angular.element(document.getElementById("div."+value));
218 $compile(ele.contents())($scope);
224 function findVal(object, key) {
226 Object.keys(object).some(function(k) {
231 if (object[k] && typeof object[k] === 'object') {
232 value = findVal(object[k], key);
233 return value !== undefined;
239 $scope.removeChoice = function(value) {
241 if(value != undefined){
242 var c = document.getElementById("div."+value).childElementCount;
245 Notification.error("The original one is not removable.");
248 document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);
252 $scope.pullVersion = function(serviceName) {
253 console.log(serviceName);
254 if(serviceName != undefined){
255 var uuu = "policyController/getModelServiceVersionData.htm";
256 var postData={policyData: serviceName};
261 contentType: 'application/json',
262 data: JSON.stringify(postData),
263 success : function(data){
264 $scope.$apply(function(){
265 $scope.optimizationModelsDictionaryVersionDatas = data[0].optimizationModelVersionData;
268 error : function(data){
269 Notification.error("Error While Retrieving the Template Layout Pattern.");
275 var splitEqual = '=';
276 var splitComma = ',';
277 var splitcolon = ':';
278 var splitEnum = "],";
279 var plainAttributeKeys = [];
281 var enumKeyList = [];
282 var dictionaryList = [];
283 var dictionaryNameList = [];
284 $scope.addDataToFields = function(serviceName, version){
285 if(serviceName != null && version !=null){
286 var service=serviceName+"-v"+version;
287 var myNode = document.getElementById("DynamicTemplate");
288 myNode.innerHTML = '';
289 var uuu = "policyController/getOptimizationTemplateData.htm";
290 var postData={policyData: service};
291 var dataOrderInfo = "";
297 contentType: 'application/json',
298 data: JSON.stringify(postData),
299 success : function(data){
300 $scope.$apply(function(){
301 $scope.optimizationModelData = data[0].optimizationModelData;
302 $scope.optimizationJsonDate = data[0].jsonValue;
303 $scope.dataOrderInfo = null;
304 $scope.dataOrderInfo = data[0].dataOrderInfo;
305 console.log("data[0].dataOrderInfo: " + data[0].dataOrderInfo);
306 console.log("$scope.dataOrderInfo: " + $scope.dataOrderInfo);
307 if(data[0].allManyTrueKeys){
308 console.log("$scope.allManyTrueKeys: " + $scope.allManyTrueKeys);
310 console.log("$scope.optimizationJsonDate: " + $scope.optimizationJsonDate);
311 var attributes = $scope.optimizationModelData.attributes;
312 var refAttributes = $scope.optimizationModelData.ref_attributes;
313 var subAttributes = $scope.optimizationModelData.sub_attributes;
314 console.log("attributes: " +attributes);
315 console.log("subAttributes: " + subAttributes);
316 console.log("refAttributes: " + refAttributes);
318 var headDefautlsData = data[0].headDefautlsData;
319 if(headDefautlsData != null){
320 $scope.temp.policy.onapName = headDefautlsData.onapName;
321 $scope.temp.policy.guard = headDefautlsData.guard;
322 $scope.temp.policy.riskType = headDefautlsData.riskType;
323 $scope.temp.policy.riskLevel = headDefautlsData.riskLevel;
324 $scope.temp.policy.priority = headDefautlsData.priority;
326 }else if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
327 $scope.temp.policy.onapName = "";
328 $scope.temp.policy.guard = "";
329 $scope.temp.policy.riskType = "";
330 $scope.temp.policy.riskLevel = "";
331 $scope.temp.policy.priority = "";
334 var annotation = $scope.optimizationModelData.annotation;
335 var dictionary = $scope.microServiceAttributeDictionaryDatas;
337 if (annotation == null || annotation.length<1){
338 $scope.isCheck = true;
340 $scope.isCheck = false;
341 var annoationList = annotation.split(splitComma);
342 for (k = 0; k < annoationList.length; k++){
343 var splitAnnotation = annoationList[k].split(splitEqual);
344 if (splitAnnotation[1].includes("matching-true")){
345 matching.push(splitAnnotation[0].trim());
351 if (dictionary!= null && dictionary.length>1){
352 for (m=0; m < dictionary.length; m += 1){
353 var valueCompare = dictionary[m].value;
354 var valueModel = dictionary[m].modelName;
355 var conpairService = serviceName;
356 if (valueModel.includes('-v')){
357 conpairService = service;
359 if(valueModel.localeCompare(conpairService) == 0){
360 console.log(valueCompare);
361 dictionaryList.push(dictionary[m]);
362 if (!dictionaryNameList.includes(dictionary[m].name)){
363 dictionaryNameList.push(dictionary[m].name)
369 $scope.temp.policy.ruleGridData = [];
371 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
372 dataOrderInfo = $scope.dataOrderInfo;
375 $scope.jsonLayout($scope.optimizationJsonDate);
379 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ // If it's veiw or edit
381 if($scope.temp.policy.editPolicy){
382 $scope.isInitEditTemplate = true;
387 // If ruleData contains extra elements created by clicked add button
388 if($scope.temp.policy.ruleData != null){
389 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
390 propNames.forEach(function(name) {
394 var extraElements = data;
396 if(plainAttributeKeys != null){
397 for(b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
398 var newValue = plainAttributeKeys[b].split("*");
399 for(a = 0; a < data.length; a++){
400 if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
401 extraElements.splice(a, 1);
407 //--- Populate these extra elements created by clicked add button
408 for(a = 0; a < extraElements.length; a++){
409 if(extraElements[a].includes("@")){
410 var index = extraElements[a].lastIndexOf("@");
412 // Get the number after @
413 var n = getNumOfDigits(extraElements[a], index+1);
415 var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
416 console.log("key: " + key);
421 var unique = checkData.filter(onlyUnique);
422 //if no layout order info, keep the process as before
424 for(i =0; i < unique.length; i++){
425 //remove @x and let addNewChoice add @1 or @2...
426 //var newKey = unique[i].substring(0, unique[i].length-2);
427 var index = unique[i].lastIndexOf("@");
428 var newKey = unique[i].substring(0, index);
429 console.log("newKey: " + newKey);
430 $scope.addNewChoice(newKey);
434 for (i = 0; i < $scope.labelManyKeys.length; i++) {
435 console.log("dataOrderInfo["+i+"]"+ dataOrderInfo[i]);
436 var label = $scope.labelManyKeys[i];
437 // first add parent/label level
438 for (k = 0; k < unique.length; k++){
439 var newKey = unique[k].substring(0, index);
441 //Check this label has bee created or not
442 if(!document.getElementById(unique[k])){
443 $scope.addNewChoice(newKey);
445 unique[k] = "*processed*";
451 //---reset to default
453 $scope.labelManyKeys = [];
455 //---process none labels
456 for (j = 0; j < unique.length; j++){
457 if(unique[j] != "*processed*"){
458 // if not created yet
459 if(!document.getElementById(unique[j])){
460 var index = unique[j].lastIndexOf("@");
461 var newKey = unique[j].substring(0, index);
462 $scope.addNewChoice(newKey);
470 if($scope.temp.policy.editPolicy){
471 //clean all the events of addNewChoice
472 $scope.$on('$destroy', addNewChoice);
476 var ele = angular.element(document.getElementById("DynamicTemplate"));
477 $compile(ele.contents())($scope);
480 error : function(data){
481 Notification.error("Error While Retrieving the Template Layout Pattern.");
487 function getNumOfDigits(str_value, index){
488 // Get the number after @
489 var str = str_value.substring(index, str_value.length);
492 for (var x = 0; x < str.length; x++){
503 function getDictionary(attribute){
504 var dicName = attribute;
506 if(attribute.includes(":")){
507 dicName = attribute.split(":")[0];
510 var dictionaryRegExp = new RegExp(dicName);
511 listemunerateValues = [];
512 if (dictionaryRegExp.test(dictionaryNameList)) {
513 for (p=0; p < dictionaryList.length; p += 1) {
514 if (dicName == dictionaryList[p].name) {
515 listemunerateValues.push(dictionaryList[p].value);
519 return listemunerateValues;
522 function getList(attribute) {
523 var enumName = attribute;
524 console.log("In getList: attribute => " + attribute);
526 if(attribute.includes(":")){
527 enumName = attribute.split(":")[0];
530 var baseEnum = $scope.optimizationModelData.enumValues;
532 if(baseEnum != null){
533 enumList = baseEnum.split(splitEnum);
536 var patternTest = new RegExp(enumName);
537 for (k=0; k < enumList.length; k += 1){
538 if(patternTest.test(enumList[k]) == true){
539 enumAttributes = enumList[k].trim();
544 enumAttributes = enumAttributes.replace("[", "");
545 enumAttributes = enumAttributes.replace("]", "");
546 enumAttributes = enumAttributes.replace(/ /g, '');
547 var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
548 listemunerateValues = dropListAfterCommaSplit[1].split(splitComma);
549 return listemunerateValues;
555 function getBooleanList(){
556 var booleanList = [];
557 booleanList.push(true);
558 booleanList.push(false);
562 function isArray(arrayTest) {
563 return Object.prototype.toString.call(arrayTest) === '[object Array]';
568 $scope.layOutElementList = [];
569 $scope.layOutOnlyLableList = [];
571 var elementOrderNum = 0;
573 function deconstructJSON(layOutData, level , name) {
577 var stringValue = "java.lang.String";
578 var string = "string";
579 var intValue = "int";
580 var integerValue = "integer";
581 var double = "double";
582 var boolean = "boolean";
583 var baseLevel = level;
585 var String = "String";
587 var attributekey = "";
589 if (name.length > 1){
590 label = label + name + '.';
593 for (key in layOutData) {
594 array = isArray(layOutData[key]);
595 console.log("key: " + key , "value: " + layOutData[key]);
597 if (!!layOutData[key] && typeof(layOutData[key])=="object") {
599 if (array==false && key!=="0"){
601 if($scope.dataOrderInfo){
602 var labelObject = {"label" : key, "level" : label, "array" : array};
603 //save it to the list
604 $scope.layOutOnlyLableList.push(labelObject);
608 $scope.labelLayout(label, key, array );
613 if (array == true && key!=0){
617 if (lableList.length > 0){
621 var newKey = lableList.pop();
623 if($scope.dataOrderInfo){
625 var labelObject = {"label" : newKey, "level" : baseLevel, "array" : array};
626 //save it to the list
627 $scope.layOutOnlyLableList.push(labelObject);
631 $scope.labelLayout(baseLevel, newKey, array );
635 label = baseLevel + newKey + '@0.';
637 label = baseLevel + newKey + '.';
640 deconstructJSON(layOutData[key] , label, key);
642 var attirbuteLabel = label;
644 var isRequired = false;
645 if (layOutData[key].includes('defaultValue-')){
646 defaultValue = layOutData[key].split('defaultValue-')[1];
651 attributekey = lableList.pop();
652 attirbuteLabel = baseLevel;
654 attributekey = key.split();
657 if (layOutData[key].includes('required-true')){
661 var subAttributes = $scope.optimizationModelData.sub_attributes;
664 var jsonObject = JSON.parse(subAttributes);
665 var allkeys = Object.keys(jsonObject);
667 for (var k = 0; k < allkeys.length; k++) {
668 var keyValue = allkeys[k];
669 console.log(" keyValue:jsonObject["+keyValue+ "]: " + jsonObject[keyValue]);
670 if(jsonObject[keyValue]){
671 var tempObject = jsonObject[keyValue];
672 if(tempObject && tempObject[key]){
673 if (tempObject[key].includes('required-true')){
682 var elementObject = {};
683 switch (layOutData[key].split(splitcolon)[0]){
687 if($scope.dataOrderInfo){
689 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue, "isRequired": isRequired, "type":"text"};
690 $scope.layOutElementList.push(elementObject);
692 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
697 if($scope.dataOrderInfo){
699 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"number"};
700 $scope.layOutElementList.push(elementObject);
702 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
706 if($scope.dataOrderInfo){
708 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"double"};
709 $scope.layOutElementList.push(elementObject);
711 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "double");
715 if($scope.dataOrderInfo){
717 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key], "list": getBooleanList, "isRequired": isRequired, "type":"dropBox"};
718 $scope.layOutElementList.push(elementObject);
720 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], getBooleanList());
724 if (layOutData[key].includes('dictionary-')){
725 var list = getDictionary(layOutData[key].split('dictionary-')[1]);
727 //--- get dropdown values from enumValues
728 var list = getList(layOutData[key]);
730 if (list.length===0){ //not dropdown element
731 if($scope.dataOrderInfo){
733 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"text"};
734 $scope.layOutElementList.push(elementObject);
737 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
740 if($scope.dataOrderInfo){
742 elementObject = {"id": elementOrderNum, "attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key],"isRequired": isRequired, "list":list, "type":"dropBox"};
743 $scope.layOutElementList.push(elementObject);
745 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], list, isRequired);
754 $scope.validContionalRequired = function(parentId) {
755 console.log("ng-blur event: parentId : " + parentId);
756 var c = document.getElementById(parentId).children;
758 var hasValue = false;
759 for (i = 0; i < c.length; i++) {
760 if(c[i].getAttribute("data-conditional")){
761 console.log(c[i].getAttribute("data-conditional"));
762 console.log(c[i].value);
763 if(c[i].value != null && c[i].value.trim() != ""){
769 for (i = 0; i < c.length; i++) {
770 if(c[i].getAttribute("data-conditional")){
772 c[i].setAttribute("required", true);
774 c[i].removeAttribute("required");
780 $scope.jsonLayout = function(layOutData){
782 deconstructJSON(layOutData , "", "");
784 var orderValue = $scope.dataOrderInfo;
785 var layOutElementList = $scope.layOutElementList;
786 var labelList = $scope.layOutOnlyLableList;
790 $scope.layOutElementList = [];
791 $scope.layOutOnlyLableList = [];
793 // Only layout in order if order info provided
796 if(orderValue.includes("[")){
797 orderValue = orderValue.replace("[", "") ;
798 orderValue = orderValue.replace("]", "") ;
801 orderValue = orderValue.split(',') ;
803 for (i = 0; i < orderValue.length; i++) {
804 console.log("orderValue["+i+"]"+ orderValue[i]);
805 var key = orderValue[i].trim();
807 //--- Create labels first {"label" : newKey, "level" : baseLevel, "array" : array};
809 for (k = 0; k < labelList.length; k++){
811 var label = labelList[k].label.toString().trim();
812 var level = labelList[k].level.toString().trim();
813 var array = labelList[k].array;
816 $scope.labelLayout(level, label, array);
817 //in case to have duplicate label names
818 labelList[k].label = "*processed*";
823 //--- then layout each element based on its order defined in YAML file
824 for (j = 0; j < layOutElementList.length; j++) {
826 var attributekey = layOutElementList[j].attributekey.toString().trim();
828 if(key == attributekey){
830 var attirbuteLabel = layOutElementList[j].attirbuteLabel.toString().trim();
831 var defaultValue = layOutElementList[j].defaultValue.toString().trim();
832 var isRequired = layOutElementList[j].isRequired;
834 console.log("layOutElementList[" +j+ "]: id:" + layOutElementList[j].id + ", attributekey:"+ layOutElementList[j].attributekey + ", attirbuteLabel:" + layOutElementList[j].attirbuteLabel);
836 if (layOutElementList[j].type == "dropBox"){
837 $scope.dropBoxLayout(attirbuteLabel, attributekey, layOutElementList[j].array, defaultValue, layOutElementList[j].list, isRequired);
840 $scope.attributeBox(attributekey, layOutElementList[j].array, attirbuteLabel, defaultValue, isRequired, layOutElementList[j].type);
844 //in case to have duplicate attribute names
845 layOutElementList[j].attributekey = "*processed*";
856 $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired, dataType ){
857 $scope.temp.policy.ruleGridData.push(attibuteKey);
858 var br = document.createElement("BR");
860 var label = document.createElement("Label");
861 var labeltext = null;
862 var requiredLabName = "";
863 if (matching.includes(attibuteKey)){
864 labeltext = document.createTextNode(attibuteKey + "*!");
865 isRequired = true; //set required as true for matching element
868 requiredLabName = attibuteKey + " * ";
869 labeltext = document.createTextNode(requiredLabName);
871 labeltext = document.createTextNode(attibuteKey);
876 var divID = labelValue;
878 if (labelValue.length < 1){
879 divID = "DynamicTemplate";
880 }else if (labelValue.endsWith('.')){
881 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
884 label.appendChild(labeltext);
886 var textField = document.createElement("INPUT");
888 textField.setAttribute("class" , "form-control");
890 if(dataType == "double"){
891 textField.setAttribute("type" , "number");
892 textField.setAttribute("step" , "any");
894 textField.setAttribute("type" , dataType);
897 textField.setAttribute("style" , "width:300px;");
898 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
901 if(attributeManyKey){
902 checkKey = labelValue + attibuteKey+'@0';
903 textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
904 var divTag = document.createElement("div");
905 divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
906 var addButton = document.createElement("BUTTON");
907 var buttonaddLabel = document.createTextNode("+");
908 addButton.appendChild(buttonaddLabel);
909 addButton.setAttribute("id", labelValue + attibuteKey);
910 addButton.setAttribute("class", "btn btn-add-remove");
911 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + attibuteKey+'");');
912 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
913 var removeButton = document.createElement("BUTTON");
914 var buttonremoveLabel = document.createTextNode("-");
915 removeButton.appendChild(buttonremoveLabel);
916 removeButton.setAttribute("class", "btn btn-add-remove");
917 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue + attibuteKey+'");');
918 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
919 document.getElementById(divID).appendChild(addButton);
920 document.getElementById(divID).appendChild(removeButton);
921 document.getElementById(divID).appendChild(label);
922 id = "div."+labelValue+attibuteKey;
923 divTag.setAttribute("id", id);
924 document.getElementById(divID).appendChild(divTag);
925 textField.className += ' first_child';
927 textField.setAttribute("required", "true");
929 divTag.appendChild(textField);
930 document.getElementById(divID).appendChild(divTag);
933 checkKey = labelValue + attibuteKey;
934 textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
935 if(document.getElementById(divID).hasAttribute('required') || !document.getElementById(divID).hasAttribute('data-conditional')){
936 if(requiredLabName.includes("*") || isRequired){
937 textField.setAttribute("required", "true");
939 }else if (document.getElementById(divID).hasAttribute('data-conditional')){
940 if(requiredLabName.includes("*")){
941 var requiredNode = document.createElement('span');
942 requiredNode.setAttribute("class", "mstooltip");
943 requiredNode.textContent = "?";
944 label.appendChild(requiredNode);
946 var requiredNodeToolTip = document.createElement('span');
947 requiredNodeToolTip.setAttribute("class", "tooltiptext");
948 requiredNodeToolTip.textContent = "Conditional Required";
949 requiredNode.appendChild(requiredNodeToolTip);
951 textField.setAttribute("data-conditional", divID);
952 textField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
956 document.getElementById(divID).appendChild(label);
957 document.getElementById(divID).appendChild(textField);
958 document.getElementById(divID).appendChild(br);
962 if(divID.includes("@0") && divID.includes("div.")){
963 var firstChild_Id = divID.split("@0")[0];
964 var firstChild_element = document.getElementById(firstChild_Id);
965 if(firstChild_element){
966 firstChild_element.className += ' children_group'; //here is a div with a group of children.
969 console.log('firstChild_Id: ' + firstChild_Id);
970 console.log('divID: ' + divID);
972 if (defaultValue.length > 0){
973 if(defaultValue.includes(":")){
974 defaultValue = defaultValue.split(":")[0];
975 if(defaultValue === "NA") {
979 if(defaultValue != "undefined" && defaultValue != undefined && defaultValue != "null"){
980 document.getElementById(checkKey).value = defaultValue;
984 if($scope.temp.policy.ruleData != null){
985 if (attributeManyKey){
986 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
987 if($scope.temp.policy.ruleData[newCheckKey +'@0'] != undefined && $scope.temp.policy.ruleData[newCheckKey +'@0'] != "undefined"){
988 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
991 if($scope.temp.policy.ruleData[checkKey] != undefined && $scope.temp.policy.ruleData[checkKey] != "undefined"){
992 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
996 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey);
1000 $scope.labelManyKeys = [];
1001 $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
1002 var label = document.createElement("Label")
1003 var divID = labelValue;
1004 if (labelValue.endsWith('.')){
1005 var workingLabel = labelValue.substring(0, labelValue.length-1);
1007 var workingLabel = labelValue;
1010 if (labelValue.length < 1){
1011 divID = "DynamicTemplate";
1012 } else if (labelValue.endsWith('.')){
1013 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
1016 var subAttributes = $scope.optimizationModelData.subattributes;
1017 var jsonObject = JSON.parse(subAttributes);
1018 var lablInfo = findVal(jsonObject, lableName);
1019 console.log("findValue : " + lableName +": "+ lablInfo);
1021 var required = null;
1023 if(lablInfo.includes("required-true")){
1026 }else if (lablInfo.includes("required-false")){
1031 var labeltext = document.createTextNode(lableName + star);
1033 label.appendChild(labeltext);
1037 var addButton = document.createElement("BUTTON");
1038 var buttonLabel = document.createTextNode("+");
1039 addButton.appendChild(buttonLabel);
1040 addButton.setAttribute("class", "btn btn-add-remove");
1041 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + lableName+'");');
1042 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1043 var removeButton = document.createElement("BUTTON");
1044 var buttonremoveLabel = document.createTextNode("-");
1045 removeButton.appendChild(buttonremoveLabel);
1046 removeButton.setAttribute("class", "btn btn-add-remove");
1047 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue +lableName+'");');
1048 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1049 document.getElementById(divID).appendChild(addButton);
1050 document.getElementById(divID).appendChild(removeButton);
1051 document.getElementById(divID).appendChild(label);
1052 var id = "div."+labelValue+lableName;
1053 var divTag = document.createElement("div");
1054 divTag.setAttribute("id", id);
1055 document.getElementById(divID).appendChild(divTag);
1057 var divTag = document.createElement("div");
1058 divTag.setAttribute("id", id +'@0');
1060 divTag.className += ' children_group'; //here is div with a group of children.
1063 divTag.setAttribute("required", required);
1064 }else if(required == false){
1065 divTag.setAttribute("data-conditional", "yes");
1068 document.getElementById(id).appendChild(divTag);
1070 $scope.labelManyKeys.push(lableName);
1073 var divTag = document.createElement("div");
1074 divTag.setAttribute("id", "div."+labelValue+lableName);
1075 divTag.className += ' children_group'; //here is div with a group of children.
1077 divTag.setAttribute("required", required);
1078 }else if(required == false){
1079 divTag.setAttribute("data-conditional", "yes");
1081 document.getElementById(divID).appendChild(label);
1082 document.getElementById(divID).appendChild(divTag);
1086 $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues, isRequired){
1087 var br = document.createElement("BR");
1089 if (labelLevel.length < 1){
1090 var divID = "DynamicTemplate";
1091 } else if (labelLevel.endsWith('.')){
1092 var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
1096 var label = document.createElement("Label")
1098 var refAttributes = $scope.optimizationModelData.ref_attributes;
1099 if(isRequired != true && refAttributes){ //check refAttributes also
1100 var refAttributesList = refAttributes.split(splitComma);
1101 for (k = 0; k < refAttributesList.length; k++){
1102 var refAttribute = refAttributesList[k].split(splitEqual);
1103 if (attributeName == refAttribute[0].trim() && refAttribute[1].includes("required-true")){
1109 if (matching.includes(attributeName)){
1110 var labeltext = document.createTextNode(attributeName + "*!");
1111 label.appendChild(labeltext);
1112 isRequired = true; //set required as true for matching element
1114 var labeltext = document.createTextNode(attributeName);
1116 var requiredLabName = attributeName+ " * ";
1117 labeltext = document.createTextNode(requiredLabName);
1119 labeltext = document.createTextNode(attributeName);
1122 label.appendChild(labeltext);
1124 label.appendChild(labeltext);
1125 // if this field is required, but its parent is not required
1126 if(isRequired && document.getElementById(divID).hasAttribute('data-conditional')){
1127 var requiredNode = document.createElement('span');
1128 requiredNode.setAttribute("class", "mstooltip");
1129 requiredNode.textContent = "?";
1130 label.appendChild(requiredNode);
1132 var requiredNodeToolTip = document.createElement('span');
1133 requiredNodeToolTip.setAttribute("class", "tooltiptext");
1134 requiredNodeToolTip.textContent = "Conditional Required";
1135 requiredNode.appendChild(requiredNodeToolTip);
1139 var listField = document.createElement("SELECT");
1140 listField.setAttribute("class" , "form-control");
1141 listField.setAttribute("style" , "width:300px;");
1142 listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
1145 if(document.getElementById(divID).hasAttribute('data-conditional')){
1146 listField.setAttribute("data-conditional", divID);
1147 listField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
1149 listField.setAttribute("required", true);
1152 if( many != true || isRequired != true){ // add an empty option for not required or not multiple select element
1153 var optionFirst = document.createElement('option');
1154 optionFirst.setAttribute('value', "");
1155 listField.appendChild(optionFirst);
1158 for (var i=0; i < listemunerateValues.length; i += 1) {
1159 if(typeof listemunerateValues[i] == "string" && listemunerateValues[i].includes("equal-sign")){
1160 listemunerateValues[i] = listemunerateValues[i].replace('equal-sign','=');
1162 var option = document.createElement('option');
1163 option.setAttribute('value', listemunerateValues[i]);
1164 option.appendChild(document.createTextNode(listemunerateValues[i]));
1165 option.setAttribute('value', listemunerateValues[i]);
1166 listField.appendChild(option);
1168 listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
1170 enumKeyList.push(attributeName);
1172 document.getElementById(divID).appendChild(label);
1173 document.getElementById(divID).appendChild(br);
1176 document.getElementById(divID).appendChild(listField).multiple = true;
1177 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
1179 document.getElementById(divID).appendChild(listField).multiple = false;
1180 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
1183 if($scope.temp.policy.ruleData != null){
1185 document.getElementById(labelLevel +attributeName).options[0].selected = false;
1186 for (i=0; i < listemunerateValues.length; i += 1) {
1187 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
1188 if (testValue === undefined){
1189 testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
1191 var location = listemunerateValues.indexOf(testValue);
1193 document.getElementById(labelLevel +attributeName).options[location].selected = true;
1197 if($scope.temp.policy.ruleData[labelLevel + attributeName] != undefined && $scope.temp.policy.ruleData[labelLevel + attributeName] != "undefined"){
1198 document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];
1204 function onlyUnique(value, index, self) {
1205 return self.indexOf(value) === index;
1208 $scope.savePolicy = function(policy){
1209 if(policy.itemContent != undefined){
1210 $scope.refreshCheck = true;
1211 $scope.policyNavigator = policy.itemContent;
1212 policy.itemContent = "";
1214 $scope.savebutton = false;
1217 var jsonPolicy = {};
1218 if(plainAttributeKeys != null){
1219 for(a = 0; a < plainAttributeKeys.length; a++){
1220 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1221 console.log("splitPlainAttributeKey: " + splitPlainAttributeKey);
1222 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1223 var key = splitPlainAttributeKey[0];
1225 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1226 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1227 key = splitPlainAttributeKey[0]+'@0';
1230 if(searchElement != null){
1231 var keySplit = key.split(dot);
1232 var elumentLocation = keySplit.length;
1234 if (elumentLocation > 1){
1235 enumKey = keySplit[keySplit.length - 1];
1237 //check it is undefined or not
1238 if (enumKeyList != undefined && enumKeyList.indexOf(enumKey) != -1){
1239 if (splitPlainAttributeKey[1]!= undefined && splitPlainAttributeKey[1].indexOf("true") !== -1){
1240 var multiSlect = [];
1241 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1242 multiSlect.push(searchElement.selectedOptions[i].value);
1244 jsonPolicy[key]= multiSlect;
1246 console.log(" searchElement.value = > " + searchElement.value);
1247 jsonPolicy[key]= searchElement.value;
1250 if(searchElement.value != null){
1251 console.log(" searchElement.value = > " + searchElement.value);
1252 jsonPolicy[key]= searchElement.value;
1258 var uuu = "policycreation/save_policy";
1259 var postData={policyData: policy, policyJSON : jsonPolicy};
1264 contentType: 'application/json',
1265 data: JSON.stringify(postData),
1266 success : function(data){
1267 $scope.$apply(function(){
1268 $scope.data=data.policyData;
1269 if($scope.data == 'success'){
1270 $scope.temp.policy.readOnly = 'true';
1271 $scope.safetyChecker = data.policyData.split("#")[2];
1272 if ($scope.safetyChecker!=undefined) {
1273 Notification.success($scope.safetyChecker);
1275 $scope.pushStatus=data.policyData.split("&")[1];
1276 if($scope.pushStatus=="successPush"){
1277 Notification.success("Policy pushed successfully");
1279 Notification.success("Policy Saved Successfully.");
1280 }else if ($scope.data == 'PolicyExists'){
1281 $scope.savebutton = true;
1282 Notification.error("Policy Already Exists with Same Name in Scope.");
1285 console.log($scope.data);
1287 error : function(data){
1288 Notification.error("Error Occured while saving Policy.");
1293 $scope.validatePolicy = function(policy){
1294 document.getElementById("validate").innerHTML = "";
1297 var jsonPolicy = {};
1298 if(plainAttributeKeys != null){
1299 for(a = 0; a < plainAttributeKeys.length; a++){
1300 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1301 console.log(splitPlainAttributeKey[1]);
1302 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1303 var key = splitPlainAttributeKey[0];
1304 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1305 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1306 key = splitPlainAttributeKey[0]+'@0';
1308 if(searchElement != null){
1309 if (enumKeyList.indexOf(key) != -1){
1310 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
1311 var multiSlect = [];
1312 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1313 multiSlect.push(searchElement.selectedOptions[i].value);
1315 jsonPolicy[key]= multiSlect;
1317 jsonPolicy[key]= searchElement.value;
1319 if(searchElement.getAttribute("required")){
1320 if(!searchElement.value){
1325 if(searchElement.value != null){
1326 jsonPolicy[key]= searchElement.value;
1327 if(searchElement.getAttribute("required")){
1328 if(!searchElement.value){
1337 var uuu = "policyController/validate_policy.htm";
1338 var postData={policyData: policy, policyJSON : jsonPolicy};
1343 contentType: 'application/json',
1344 data: JSON.stringify(postData),
1345 success : function(data){
1346 $scope.$apply(function(){
1347 $scope.validateData = data.data.replace(/\"/g, "");
1348 $scope.data=data.data.substring(1,8);
1349 var size = data.data.length;
1350 if($scope.data == 'success'){
1351 Notification.success("Validation Success.");
1352 $scope.savebutton = false;
1354 var displayWarning = data.data.substring(19,size - 1);
1355 document.getElementById("validate").innerHTML = "Safe Policy Warning Message : "+displayWarning;
1356 document.getElementById("validate").style.color = "white";
1357 document.getElementById("validate").style.backgroundColor = "skyblue";
1360 Notification.error("Validation Failed.");
1361 document.getElementById("validate").innerHTML = $scope.validateData;
1362 document.getElementById("validate").style.color = "white";
1363 document.getElementById("validate").style.backgroundColor = "red";
1364 $scope.savebutton = true;
1368 console.log($scope.data);
1370 error : function(data){
1371 Notification.error("Validation Failed.");
1372 $scope.savebutton = true;
1377 function extend(obj, src) {
1378 for (var key in src) {
1379 if (src.hasOwnProperty(key)) obj[key] = src[key];