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', ['$scope', '$window', '$compile', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, $window, $compile, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
23 $scope.policyNavigator;
24 $scope.isCheck = false;
25 $scope.savebutton = true;
26 $scope.refreshCheck = false;
28 if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
29 $scope.temp.policy = {
30 policyType : "Config",
31 configPolicyType : "Optimization"
35 $scope.refresh = function(){
36 if($scope.refreshCheck){
37 $scope.policyNavigator.refresh();
39 $scope.modal('createNewPolicy', true);
40 $scope.temp.policy = "";
43 $scope.modal = function(id, hide) {
44 return $('#' + id).modal(hide ? 'hide' : 'show');
47 $('#ttlDate').datepicker({
48 dateFormat: 'dd/mm/yy',
51 onSelect: function(date) {
52 angular.element($('#ttlDate')).triggerHandler('input');
56 if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly != undefined){
57 if ($scope.temp.policy.configName == undefined){
58 $scope.isCheck = false;
60 $scope.isCheck = true;
63 $scope.isCheck = false;
66 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function (data) {
68 $scope.data = JSON.parse(j.data);
69 console.log($scope.data);
70 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
71 console.log($scope.onapNameDictionaryDatas);
73 console.log("failed");
76 PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
78 $scope.data = JSON.parse(j.data);
79 console.log($scope.data);
80 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
81 console.log($scope.priorityDatas);
83 console.log("failed");
86 PolicyAppService.getData('getDictionary/get_OptimizationModelsDataByName').then(function (data) {
88 $scope.data = JSON.parse(j.data);
89 console.log($scope.data);
90 var inputModelList = JSON.parse($scope.data.optimizationModelsDictionaryDatas);
93 for(var i = 0; i < inputModelList.length; i++){
94 if(typeof unique[inputModelList[i]] == "undefined"){
95 unique[inputModelList[i]] = "";
96 uniqueList.push(inputModelList[i]);
99 $scope.optimizationModelsDictionaryDatas = uniqueList;
100 console.log($scope.optimizationModelsDictionaryDatas);
101 }, function (error) {
102 console.log("failed");
105 PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
107 $scope.data = JSON.parse(j.data);
108 console.log($scope.data);
109 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
110 console.log($scope.riskTypeDictionaryDatas);
111 }, function (error) {
112 console.log("failed");
116 $scope.attributeDatas = [{"attributes" : $scope.choices}];
117 $scope.isInitEditTemplate = true; //just initially create the edit template, didn't click add button yet.
118 $scope.addNewChoice = function(value) {
119 console.log("input value : " + value);
120 if(value != undefined){
121 if (value.startsWith('div.')){
122 value = value.replace('div.','');
124 var parentElement = document.getElementById("div."+value);
125 var div = document.getElementById(value+"@0");
127 var clone = div.cloneNode(true);
128 var addElement = parentElement.childElementCount;
129 clone.id = ''+value+'@'+addElement;
131 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ //if it's view or edit
132 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
133 if($scope.temp.policy.ruleData[clone.id]){
134 clone.value = $scope.temp.policy.ruleData[clone.id];
136 if(!clone.className.includes("child_single")){
137 clone.className += ' child_single'; //single element clone
139 document.getElementById("div."+value).appendChild(clone);
140 plainAttributeKeys.push(''+value+'@'+addElement);
142 }else{ //not view or edit
143 if(!clone.className.includes("child_single")){
144 clone.className += ' child_single'; //single element clone
146 document.getElementById("div."+value).appendChild(clone);
147 plainAttributeKeys.push(''+value+'@'+addElement);
150 div = document.getElementById("div."+value+"@0");
153 div.className += ' children_group'; //div with a group of children.
155 var childElement = parentElement.firstElementChild;
156 var countParent = parentElement.childElementCount;
157 var childElementString = childElement.innerHTML;
158 var find = value+"@0";
159 var re = new RegExp(find, 'g');
160 childElementString = childElementString.replace(re,value+'@' + countParent);
161 var clone = childElement.cloneNode(true);
162 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
163 var childId = parentElement.childNodes[ii].id;
164 if(ii = parentElement.childNodes.length){
165 var childnewId = childId.slice(0, -1);
166 var count = childId.slice(-1);
169 var countvalue = parseInt(count) + 1;
170 clone.id = childnewId+countvalue;
172 clone.innerHTML=childElementString;
173 document.getElementById("div."+value).appendChild(clone);
174 var selects = clone.getElementsByTagName("select");
175 var inputs = clone.getElementsByTagName("input");
176 var removeValues = [];
177 for(var i=0; i<inputs.length; i++){
178 if ($scope.temp.policy.ruleData!=undefined){
179 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
180 if (checkValue!=undefined && checkValue != "undefined"){
181 if($scope.temp.policy.ruleData != null){
182 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
183 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
184 plainAttributeKeys.push(inputs[i].id);
187 plainAttributeKeys.push(inputs[i].id);
190 plainAttributeKeys.push(inputs[i].id);
194 for(var i=0; i<selects.length; i++){
195 if ($scope.temp.policy.ruleData!=undefined){
196 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
197 if (checkValue!=undefined && checkValue!="undefined"){
198 if($scope.temp.policy.ruleData != null){
199 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
200 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
201 plainAttributeKeys.push(selects[i].id);
204 plainAttributeKeys.push(selects[i].id);
207 plainAttributeKeys.push(selects[i].id);
211 for (var k=0; k<removeValues.length; k++){
212 var elem = document.getElementById(removeValues[k]);
213 elem.parentNode.removeChild(elem);
215 var ele = angular.element(document.getElementById("div."+value));
216 $compile(ele.contents())($scope);
222 function findVal(object, key) {
224 Object.keys(object).some(function(k) {
229 if (object[k] && typeof object[k] === 'object') {
230 value = findVal(object[k], key);
231 return value !== undefined;
237 $scope.removeChoice = function(value) {
239 if(value != undefined){
240 var c = document.getElementById("div."+value).childElementCount;
243 Notification.error("The original one is not removable.");
246 document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);
250 $scope.pullVersion = function(serviceName) {
251 console.log(serviceName);
252 if(serviceName != undefined){
253 var uuu = "policyController/getModelServiceVersionData.htm";
254 var postData={policyData: serviceName};
259 contentType: 'application/json',
260 data: JSON.stringify(postData),
261 success : function(data){
262 $scope.$apply(function(){
263 $scope.optimizationModelsDictionaryVersionDatas = data[0].optimizationModelVersionData;
266 error : function(data){
267 alert("Error While Retrieving the Template Layout Pattern.");
273 var splitEqual = '=';
274 var splitComma = ',';
275 var splitcolon = ':';
276 var splitEnum = "],";
277 var plainAttributeKeys = [];
279 var enumKeyList = [];
280 var dictionaryList = [];
281 var dictionaryNameList = [];
282 $scope.addDataToFields = function(serviceName, version){
283 if(serviceName != null && version !=null){
284 var service=serviceName+"-v"+version;
285 var myNode = document.getElementById("DynamicTemplate");
286 myNode.innerHTML = '';
287 var uuu = "policyController/getOptimizationTemplateData.htm";
288 var postData={policyData: service};
289 var dataOrderInfo = "";
295 contentType: 'application/json',
296 data: JSON.stringify(postData),
297 success : function(data){
298 $scope.$apply(function(){
299 $scope.optimizationModelData = data[0].optimizationModelData;
300 $scope.optimizationJsonDate = data[0].jsonValue;
301 $scope.dataOrderInfo = null;
302 $scope.dataOrderInfo = data[0].dataOrderInfo;
303 console.log("data[0].dataOrderInfo: " + data[0].dataOrderInfo);
304 console.log("$scope.dataOrderInfo: " + $scope.dataOrderInfo);
305 if(data[0].allManyTrueKeys){
306 console.log("$scope.allManyTrueKeys: " + $scope.allManyTrueKeys);
308 console.log("$scope.optimizationJsonDate: " + $scope.optimizationJsonDate);
309 var attributes = $scope.optimizationModelData.attributes;
310 var refAttributes = $scope.optimizationModelData.ref_attributes;
311 var subAttributes = $scope.optimizationModelData.sub_attributes;
312 console.log("attributes: " +attributes);
313 console.log("subAttributes: " + subAttributes);
314 console.log("refAttributes: " + refAttributes);
316 var headDefautlsData = data[0].headDefautlsData;
317 if(headDefautlsData != null){
318 $scope.temp.policy.onapName = headDefautlsData.onapName;
319 $scope.temp.policy.guard = headDefautlsData.guard;
320 $scope.temp.policy.riskType = headDefautlsData.riskType;
321 $scope.temp.policy.riskLevel = headDefautlsData.riskLevel;
322 $scope.temp.policy.priority = headDefautlsData.priority;
324 }else if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
325 $scope.temp.policy.onapName = "";
326 $scope.temp.policy.guard = "";
327 $scope.temp.policy.riskType = "";
328 $scope.temp.policy.riskLevel = "";
329 $scope.temp.policy.priority = "";
332 var annotation = $scope.optimizationModelData.annotation;
333 var dictionary = $scope.microServiceAttributeDictionaryDatas;
335 if (annotation == null || annotation.length<1){
336 $scope.isCheck = true;
338 $scope.isCheck = false;
339 var annoationList = annotation.split(splitComma);
340 for (k = 0; k < annoationList.length; k++){
341 var splitAnnotation = annoationList[k].split(splitEqual);
342 if (splitAnnotation[1].includes("matching-true")){
343 matching.push(splitAnnotation[0].trim());
349 if (dictionary!= null && dictionary.length>1){
350 for (m=0; m < dictionary.length; m += 1){
351 var valueCompare = dictionary[m].value;
352 var valueModel = dictionary[m].modelName;
353 var conpairService = serviceName;
354 if (valueModel.includes('-v')){
355 conpairService = service;
357 if(valueModel.localeCompare(conpairService) == 0){
358 console.log(valueCompare);
359 dictionaryList.push(dictionary[m]);
360 if (!dictionaryNameList.includes(dictionary[m].name)){
361 dictionaryNameList.push(dictionary[m].name)
367 $scope.temp.policy.ruleGridData = [];
369 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
370 dataOrderInfo = $scope.dataOrderInfo;
373 $scope.jsonLayout($scope.optimizationJsonDate);
377 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ // If it's veiw or edit
379 if($scope.temp.policy.editPolicy){
380 $scope.isInitEditTemplate = true;
385 // If ruleData contains extra elements created by clicked add button
386 if($scope.temp.policy.ruleData != null){
387 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
388 propNames.forEach(function(name) {
392 var extraElements = data;
394 if(plainAttributeKeys != null){
395 for(b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
396 var newValue = plainAttributeKeys[b].split("*");
397 for(a = 0; a < data.length; a++){
398 if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
399 extraElements.splice(a, 1);
405 //--- Populate these extra elements created by clicked add button
406 for(a = 0; a < extraElements.length; a++){
407 if(extraElements[a].includes("@")){
408 var index = extraElements[a].lastIndexOf("@");
410 // Get the number after @
411 var n = getNumOfDigits(extraElements[a], index+1);
413 var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
414 console.log("key: " + key);
419 var unique = checkData.filter(onlyUnique);
420 //if no layout order info, keep the process as before
422 for(i =0; i < unique.length; i++){
423 //remove @x and let addNewChoice add @1 or @2...
424 //var newKey = unique[i].substring(0, unique[i].length-2);
425 var index = unique[i].lastIndexOf("@");
426 var newKey = unique[i].substring(0, index);
427 console.log("newKey: " + newKey);
428 $scope.addNewChoice(newKey);
432 for (i = 0; i < $scope.labelManyKeys.length; i++) {
433 console.log("dataOrderInfo["+i+"]"+ dataOrderInfo[i]);
434 var label = $scope.labelManyKeys[i];
435 // first add parent/label level
436 for (k = 0; k < unique.length; k++){
437 var newKey = unique[k].substring(0, index);
439 //Check this label has bee created or not
440 if(!document.getElementById(unique[k])){
441 $scope.addNewChoice(newKey);
443 unique[k] = "*processed*";
449 //---reset to default
451 $scope.labelManyKeys = [];
453 //---process none labels
454 for (j = 0; j < unique.length; j++){
455 if(unique[j] != "*processed*"){
456 // if not created yet
457 if(!document.getElementById(unique[j])){
458 var index = unique[j].lastIndexOf("@");
459 var newKey = unique[j].substring(0, index);
460 $scope.addNewChoice(newKey);
468 if($scope.temp.policy.editPolicy){
469 //clean all the events of addNewChoice
470 $scope.$on('$destroy', addNewChoice);
474 var ele = angular.element(document.getElementById("DynamicTemplate"));
475 $compile(ele.contents())($scope);
478 error : function(data){
479 alert("Error While Retrieving the Template Layout Pattern.");
485 function getNumOfDigits(str_value, index){
486 // Get the number after @
487 var str = str_value.substring(index, str_value.length);
490 for (var x = 0; x < str.length; x++){
501 function getDictionary(attribute){
502 var dicName = attribute;
504 if(attribute.includes(":")){
505 dicName = attribute.split(":")[0];
508 var dictionaryRegExp = new RegExp(dicName);
509 listemunerateValues = [];
510 if (dictionaryRegExp.test(dictionaryNameList)) {
511 for (p=0; p < dictionaryList.length; p += 1) {
512 if (dicName == dictionaryList[p].name) {
513 listemunerateValues.push(dictionaryList[p].value);
517 return listemunerateValues;
520 function getList(attribute) {
521 var enumName = attribute;
522 console.log("In getList: attribute => " + attribute);
524 if(attribute.includes(":")){
525 enumName = attribute.split(":")[0];
528 var baseEnum = $scope.optimizationModelData.enumValues;
530 if(baseEnum != null){
531 enumList = baseEnum.split(splitEnum);
534 var patternTest = new RegExp(enumName);
535 for (k=0; k < enumList.length; k += 1){
536 if(patternTest.test(enumList[k]) == true){
537 enumAttributes = enumList[k].trim();
542 enumAttributes = enumAttributes.replace("[", "");
543 enumAttributes = enumAttributes.replace("]", "");
544 enumAttributes = enumAttributes.replace(/ /g, '');
545 var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
546 listemunerateValues = dropListAfterCommaSplit[1].split(splitComma);
547 return listemunerateValues;
553 function getBooleanList(){
554 var booleanList = [];
555 booleanList.push(true);
556 booleanList.push(false);
560 function isArray(arrayTest) {
561 return Object.prototype.toString.call(arrayTest) === '[object Array]';
566 $scope.layOutElementList = [];
567 $scope.layOutOnlyLableList = [];
569 var elementOrderNum = 0;
571 function deconstructJSON(layOutData, level , name) {
575 var stringValue = "java.lang.String";
576 var string = "string";
577 var intValue = "int";
578 var integerValue = "integer";
579 var double = "double";
580 var boolean = "boolean";
581 var baseLevel = level;
583 var String = "String";
585 var attributekey = "";
587 if (name.length > 1){
588 label = label + name + '.';
591 for (key in layOutData) {
592 array = isArray(layOutData[key]);
593 console.log("key: " + key , "value: " + layOutData[key]);
595 if (!!layOutData[key] && typeof(layOutData[key])=="object") {
597 if (array==false && key!=="0"){
599 if($scope.dataOrderInfo){
600 var labelObject = {"label" : key, "level" : label, "array" : array};
601 //save it to the list
602 $scope.layOutOnlyLableList.push(labelObject);
606 $scope.labelLayout(label, key, array );
611 if (array == true && key!=0){
615 if (lableList.length > 0){
619 var newKey = lableList.pop();
621 if($scope.dataOrderInfo){
623 var labelObject = {"label" : newKey, "level" : baseLevel, "array" : array};
624 //save it to the list
625 $scope.layOutOnlyLableList.push(labelObject);
629 $scope.labelLayout(baseLevel, newKey, array );
633 label = baseLevel + newKey + '@0.';
635 label = baseLevel + newKey + '.';
638 deconstructJSON(layOutData[key] , label, key);
640 var attirbuteLabel = label;
642 var isRequired = false;
643 if (layOutData[key].includes('defaultValue-')){
644 defaultValue = layOutData[key].split('defaultValue-')[1];
649 attributekey = lableList.pop();
650 attirbuteLabel = baseLevel;
652 attributekey = key.split();
655 if (layOutData[key].includes('required-true')){
659 var subAttributes = $scope.optimizationModelData.sub_attributes;
662 var jsonObject = JSON.parse(subAttributes);
663 var allkeys = Object.keys(jsonObject);
665 for (var k = 0; k < allkeys.length; k++) {
666 var keyValue = allkeys[k];
667 console.log(" keyValue:jsonObject["+keyValue+ "]: " + jsonObject[keyValue]);
668 if(jsonObject[keyValue]){
669 var tempObject = jsonObject[keyValue];
670 if(tempObject && tempObject[key]){
671 if (tempObject[key].includes('required-true')){
680 var elementObject = {};
681 switch (layOutData[key].split(splitcolon)[0]){
685 if($scope.dataOrderInfo){
687 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue, "isRequired": isRequired, "type":"text"};
688 $scope.layOutElementList.push(elementObject);
690 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
695 if($scope.dataOrderInfo){
697 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"number"};
698 $scope.layOutElementList.push(elementObject);
700 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
704 if($scope.dataOrderInfo){
706 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"double"};
707 $scope.layOutElementList.push(elementObject);
709 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "double");
713 if($scope.dataOrderInfo){
715 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key], "list": getBooleanList, "isRequired": isRequired, "type":"dropBox"};
716 $scope.layOutElementList.push(elementObject);
718 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], getBooleanList());
722 if (layOutData[key].includes('dictionary-')){
723 var list = getDictionary(layOutData[key].split('dictionary-')[1]);
725 //--- get dropdown values from enumValues
726 var list = getList(layOutData[key]);
728 if (list.length===0){ //not dropdown element
729 if($scope.dataOrderInfo){
731 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"text"};
732 $scope.layOutElementList.push(elementObject);
735 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
738 if($scope.dataOrderInfo){
740 elementObject = {"id": elementOrderNum, "attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key],"isRequired": isRequired, "list":list, "type":"dropBox"};
741 $scope.layOutElementList.push(elementObject);
743 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], list, isRequired);
752 $scope.validContionalRequired = function(parentId) {
753 console.log("ng-blur event: parentId : " + parentId);
754 var c = document.getElementById(parentId).children;
756 var hasValue = false;
757 for (i = 0; i < c.length; i++) {
758 if(c[i].getAttribute("data-conditional")){
759 console.log(c[i].getAttribute("data-conditional"));
760 console.log(c[i].value);
761 if(c[i].value != null && c[i].value.trim() != ""){
767 for (i = 0; i < c.length; i++) {
768 if(c[i].getAttribute("data-conditional")){
770 c[i].setAttribute("required", true);
772 c[i].removeAttribute("required");
778 $scope.jsonLayout = function(layOutData){
780 deconstructJSON(layOutData , "", "");
782 var orderValue = $scope.dataOrderInfo;
783 var layOutElementList = $scope.layOutElementList;
784 var labelList = $scope.layOutOnlyLableList;
788 $scope.layOutElementList = [];
789 $scope.layOutOnlyLableList = [];
791 // Only layout in order if order info provided
794 if(orderValue.includes("[")){
795 orderValue = orderValue.replace("[", "") ;
796 orderValue = orderValue.replace("]", "") ;
799 orderValue = orderValue.split(',') ;
801 for (i = 0; i < orderValue.length; i++) {
802 console.log("orderValue["+i+"]"+ orderValue[i]);
803 var key = orderValue[i].trim();
805 //--- Create labels first {"label" : newKey, "level" : baseLevel, "array" : array};
807 for (k = 0; k < labelList.length; k++){
809 var label = labelList[k].label.toString().trim();
810 var level = labelList[k].level.toString().trim();
811 var array = labelList[k].array;
814 $scope.labelLayout(level, label, array);
815 //in case to have duplicate label names
816 labelList[k].label = "*processed*";
821 //--- then layout each element based on its order defined in YAML file
822 for (j = 0; j < layOutElementList.length; j++) {
824 var attributekey = layOutElementList[j].attributekey.toString().trim();
826 if(key == attributekey){
828 var attirbuteLabel = layOutElementList[j].attirbuteLabel.toString().trim();
829 var defaultValue = layOutElementList[j].defaultValue.toString().trim();
830 var isRequired = layOutElementList[j].isRequired;
832 console.log("layOutElementList[" +j+ "]: id:" + layOutElementList[j].id + ", attributekey:"+ layOutElementList[j].attributekey + ", attirbuteLabel:" + layOutElementList[j].attirbuteLabel);
834 if (layOutElementList[j].type == "dropBox"){
835 $scope.dropBoxLayout(attirbuteLabel, attributekey, layOutElementList[j].array, defaultValue, layOutElementList[j].list, isRequired);
838 $scope.attributeBox(attributekey, layOutElementList[j].array, attirbuteLabel, defaultValue, isRequired, layOutElementList[j].type);
842 //in case to have duplicate attribute names
843 layOutElementList[j].attributekey = "*processed*";
854 $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired, dataType ){
855 $scope.temp.policy.ruleGridData.push(attibuteKey);
856 var br = document.createElement("BR");
858 var label = document.createElement("Label");
859 var labeltext = null;
860 var requiredLabName = "";
861 if (matching.includes(attibuteKey)){
862 labeltext = document.createTextNode(attibuteKey + "*!");
863 isRequired = true; //set required as true for matching element
866 requiredLabName = attibuteKey + " * ";
867 labeltext = document.createTextNode(requiredLabName);
869 labeltext = document.createTextNode(attibuteKey);
874 var divID = labelValue;
876 if (labelValue.length < 1){
877 divID = "DynamicTemplate";
878 }else if (labelValue.endsWith('.')){
879 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
882 label.appendChild(labeltext);
884 var textField = document.createElement("INPUT");
886 textField.setAttribute("class" , "form-control");
888 if(dataType == "double"){
889 textField.setAttribute("type" , "number");
890 textField.setAttribute("step" , "any");
892 textField.setAttribute("type" , dataType);
895 textField.setAttribute("style" , "width:300px;");
896 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
899 if(attributeManyKey){
900 checkKey = labelValue + attibuteKey+'@0';
901 textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
902 var divTag = document.createElement("div");
903 divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
904 var addButton = document.createElement("BUTTON");
905 var buttonaddLabel = document.createTextNode("+");
906 addButton.appendChild(buttonaddLabel);
907 addButton.setAttribute("id", labelValue + attibuteKey);
908 addButton.setAttribute("class", "btn btn-add-remove");
909 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + attibuteKey+'");');
910 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
911 var removeButton = document.createElement("BUTTON");
912 var buttonremoveLabel = document.createTextNode("-");
913 removeButton.appendChild(buttonremoveLabel);
914 removeButton.setAttribute("class", "btn btn-add-remove");
915 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue + attibuteKey+'");');
916 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
917 document.getElementById(divID).appendChild(addButton);
918 document.getElementById(divID).appendChild(removeButton);
919 document.getElementById(divID).appendChild(label);
920 id = "div."+labelValue+attibuteKey;
921 divTag.setAttribute("id", id);
922 document.getElementById(divID).appendChild(divTag);
923 textField.className += ' first_child';
925 textField.setAttribute("required", "true");
927 divTag.appendChild(textField);
928 document.getElementById(divID).appendChild(divTag);
931 checkKey = labelValue + attibuteKey;
932 textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
933 if(document.getElementById(divID).hasAttribute('required') || !document.getElementById(divID).hasAttribute('data-conditional')){
934 if(requiredLabName.includes("*") || isRequired){
935 textField.setAttribute("required", "true");
937 }else if (document.getElementById(divID).hasAttribute('data-conditional')){
938 if(requiredLabName.includes("*")){
939 var requiredNode = document.createElement('span');
940 requiredNode.setAttribute("class", "mstooltip");
941 requiredNode.textContent = "?";
942 label.appendChild(requiredNode);
944 var requiredNodeToolTip = document.createElement('span');
945 requiredNodeToolTip.setAttribute("class", "tooltiptext");
946 requiredNodeToolTip.textContent = "Conditional Required";
947 requiredNode.appendChild(requiredNodeToolTip);
949 textField.setAttribute("data-conditional", divID);
950 textField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
954 document.getElementById(divID).appendChild(label);
955 document.getElementById(divID).appendChild(textField);
956 document.getElementById(divID).appendChild(br);
960 if(divID.includes("@0") && divID.includes("div.")){
961 var firstChild_Id = divID.split("@0")[0];
962 var firstChild_element = document.getElementById(firstChild_Id);
963 if(firstChild_element){
964 firstChild_element.className += ' children_group'; //here is a div with a group of children.
967 console.log('firstChild_Id: ' + firstChild_Id);
968 console.log('divID: ' + divID);
970 if (defaultValue.length > 0){
971 if(defaultValue.includes(":")){
972 defaultValue = defaultValue.split(":")[0];
973 if(defaultValue === "NA") {
977 if(defaultValue != "undefined" && defaultValue != undefined && defaultValue != "null"){
978 document.getElementById(checkKey).value = defaultValue;
982 if($scope.temp.policy.ruleData != null){
983 if (attributeManyKey){
984 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
985 if($scope.temp.policy.ruleData[newCheckKey +'@0'] != undefined && $scope.temp.policy.ruleData[newCheckKey +'@0'] != "undefined"){
986 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
989 if($scope.temp.policy.ruleData[checkKey] != undefined && $scope.temp.policy.ruleData[checkKey] != "undefined"){
990 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
994 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey);
998 $scope.labelManyKeys = [];
999 $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
1000 var label = document.createElement("Label")
1001 var divID = labelValue;
1002 if (labelValue.endsWith('.')){
1003 var workingLabel = labelValue.substring(0, labelValue.length-1);
1005 var workingLabel = labelValue;
1008 if (labelValue.length < 1){
1009 divID = "DynamicTemplate";
1010 } else if (labelValue.endsWith('.')){
1011 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
1014 var subAttributes = $scope.optimizationModelData.subattributes;
1015 var jsonObject = JSON.parse(subAttributes);
1016 var lablInfo = findVal(jsonObject, lableName);
1017 console.log("findValue : " + lableName +": "+ lablInfo);
1019 var required = null;
1021 if(lablInfo.includes("required-true")){
1024 }else if (lablInfo.includes("required-false")){
1029 var labeltext = document.createTextNode(lableName + star);
1031 label.appendChild(labeltext);
1035 var addButton = document.createElement("BUTTON");
1036 var buttonLabel = document.createTextNode("+");
1037 addButton.appendChild(buttonLabel);
1038 addButton.setAttribute("class", "btn btn-add-remove");
1039 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + lableName+'");');
1040 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1041 var removeButton = document.createElement("BUTTON");
1042 var buttonremoveLabel = document.createTextNode("-");
1043 removeButton.appendChild(buttonremoveLabel);
1044 removeButton.setAttribute("class", "btn btn-add-remove");
1045 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue +lableName+'");');
1046 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1047 document.getElementById(divID).appendChild(addButton);
1048 document.getElementById(divID).appendChild(removeButton);
1049 document.getElementById(divID).appendChild(label);
1050 var id = "div."+labelValue+lableName;
1051 var divTag = document.createElement("div");
1052 divTag.setAttribute("id", id);
1053 document.getElementById(divID).appendChild(divTag);
1055 var divTag = document.createElement("div");
1056 divTag.setAttribute("id", id +'@0');
1058 divTag.className += ' children_group'; //here is div with a group of children.
1061 divTag.setAttribute("required", required);
1062 }else if(required == false){
1063 divTag.setAttribute("data-conditional", "yes");
1066 document.getElementById(id).appendChild(divTag);
1068 $scope.labelManyKeys.push(lableName);
1071 var divTag = document.createElement("div");
1072 divTag.setAttribute("id", "div."+labelValue+lableName);
1073 divTag.className += ' children_group'; //here is div with a group of children.
1075 divTag.setAttribute("required", required);
1076 }else if(required == false){
1077 divTag.setAttribute("data-conditional", "yes");
1079 document.getElementById(divID).appendChild(label);
1080 document.getElementById(divID).appendChild(divTag);
1084 $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues, isRequired){
1085 var br = document.createElement("BR");
1087 if (labelLevel.length < 1){
1088 var divID = "DynamicTemplate";
1089 } else if (labelLevel.endsWith('.')){
1090 var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
1094 var label = document.createElement("Label")
1096 var refAttributes = $scope.optimizationModelData.ref_attributes;
1097 if(isRequired != true && refAttributes){ //check refAttributes also
1098 var refAttributesList = refAttributes.split(splitComma);
1099 for (k = 0; k < refAttributesList.length; k++){
1100 var refAttribute = refAttributesList[k].split(splitEqual);
1101 if (attributeName == refAttribute[0].trim() && refAttribute[1].includes("required-true")){
1107 if (matching.includes(attributeName)){
1108 var labeltext = document.createTextNode(attributeName + "*!");
1109 label.appendChild(labeltext);
1110 isRequired = true; //set required as true for matching element
1112 var labeltext = document.createTextNode(attributeName);
1114 var requiredLabName = attributeName+ " * ";
1115 labeltext = document.createTextNode(requiredLabName);
1117 labeltext = document.createTextNode(attributeName);
1120 label.appendChild(labeltext);
1122 label.appendChild(labeltext);
1123 // if this field is required, but its parent is not required
1124 if(isRequired && document.getElementById(divID).hasAttribute('data-conditional')){
1125 var requiredNode = document.createElement('span');
1126 requiredNode.setAttribute("class", "mstooltip");
1127 requiredNode.textContent = "?";
1128 label.appendChild(requiredNode);
1130 var requiredNodeToolTip = document.createElement('span');
1131 requiredNodeToolTip.setAttribute("class", "tooltiptext");
1132 requiredNodeToolTip.textContent = "Conditional Required";
1133 requiredNode.appendChild(requiredNodeToolTip);
1137 var listField = document.createElement("SELECT");
1138 listField.setAttribute("class" , "form-control");
1139 listField.setAttribute("style" , "width:300px;");
1140 listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
1143 if(document.getElementById(divID).hasAttribute('data-conditional')){
1144 listField.setAttribute("data-conditional", divID);
1145 listField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
1147 listField.setAttribute("required", true);
1150 if( many != true || isRequired != true){ // add an empty option for not required or not multiple select element
1151 var optionFirst = document.createElement('option');
1152 optionFirst.setAttribute('value', "");
1153 listField.appendChild(optionFirst);
1156 for (var i=0; i < listemunerateValues.length; i += 1) {
1157 if(typeof listemunerateValues[i] == "string" && listemunerateValues[i].includes("equal-sign")){
1158 listemunerateValues[i] = listemunerateValues[i].replace('equal-sign','=');
1160 var option = document.createElement('option');
1161 option.setAttribute('value', listemunerateValues[i]);
1162 option.appendChild(document.createTextNode(listemunerateValues[i]));
1163 option.setAttribute('value', listemunerateValues[i]);
1164 listField.appendChild(option);
1166 listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
1168 enumKeyList.push(attributeName);
1170 document.getElementById(divID).appendChild(label);
1171 document.getElementById(divID).appendChild(br);
1174 document.getElementById(divID).appendChild(listField).multiple = true;
1175 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
1177 document.getElementById(divID).appendChild(listField).multiple = false;
1178 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
1181 if($scope.temp.policy.ruleData != null){
1183 document.getElementById(labelLevel +attributeName).options[0].selected = false;
1184 for (i=0; i < listemunerateValues.length; i += 1) {
1185 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
1186 if (testValue === undefined){
1187 testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
1189 var location = listemunerateValues.indexOf(testValue);
1191 document.getElementById(labelLevel +attributeName).options[location].selected = true;
1195 if($scope.temp.policy.ruleData[labelLevel + attributeName] != undefined && $scope.temp.policy.ruleData[labelLevel + attributeName] != "undefined"){
1196 document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];
1202 function onlyUnique(value, index, self) {
1203 return self.indexOf(value) === index;
1206 $scope.savePolicy = function(policy){
1207 if(policy.itemContent != undefined){
1208 $scope.refreshCheck = true;
1209 $scope.policyNavigator = policy.itemContent;
1210 policy.itemContent = "";
1212 $scope.savebutton = false;
1215 var jsonPolicy = {};
1216 if(plainAttributeKeys != null){
1217 for(a = 0; a < plainAttributeKeys.length; a++){
1218 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1219 console.log("splitPlainAttributeKey: " + splitPlainAttributeKey);
1220 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1221 var key = splitPlainAttributeKey[0];
1223 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1224 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1225 key = splitPlainAttributeKey[0]+'@0';
1228 if(searchElement != null){
1229 var keySplit = key.split(dot);
1230 var elumentLocation = keySplit.length;
1232 if (elumentLocation > 1){
1233 enumKey = keySplit[keySplit.length - 1];
1235 //check it is undefined or not
1236 if (enumKeyList != undefined && enumKeyList.indexOf(enumKey) != -1){
1237 if (splitPlainAttributeKey[1]!= undefined && splitPlainAttributeKey[1].indexOf("true") !== -1){
1238 var multiSlect = [];
1239 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1240 multiSlect.push(searchElement.selectedOptions[i].value);
1242 jsonPolicy[key]= multiSlect;
1244 console.log(" searchElement.value = > " + searchElement.value);
1245 jsonPolicy[key]= searchElement.value;
1248 if(searchElement.value != null){
1249 console.log(" searchElement.value = > " + searchElement.value);
1250 jsonPolicy[key]= searchElement.value;
1256 var uuu = "policycreation/save_policy";
1257 var postData={policyData: policy, policyJSON : jsonPolicy};
1262 contentType: 'application/json',
1263 data: JSON.stringify(postData),
1264 success : function(data){
1265 $scope.$apply(function(){
1266 $scope.data=data.policyData;
1267 if($scope.data == 'success'){
1268 $scope.temp.policy.readOnly = 'true';
1269 $scope.safetyChecker = data.policyData.split("#")[2];
1270 if ($scope.safetyChecker!=undefined) {
1271 Notification.success($scope.safetyChecker);
1273 $scope.pushStatus=data.policyData.split("&")[1];
1274 if($scope.pushStatus=="successPush"){
1275 Notification.success("Policy pushed successfully");
1277 Notification.success("Policy Saved Successfully.");
1278 }else if ($scope.data == 'PolicyExists'){
1279 $scope.savebutton = true;
1280 Notification.error("Policy Already Exists with Same Name in Scope.");
1283 console.log($scope.data);
1285 error : function(data){
1286 Notification.error("Error Occured while saving Policy.");
1291 $scope.validatePolicy = function(policy){
1292 document.getElementById("validate").innerHTML = "";
1295 var jsonPolicy = {};
1296 if(plainAttributeKeys != null){
1297 for(a = 0; a < plainAttributeKeys.length; a++){
1298 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1299 console.log(splitPlainAttributeKey[1]);
1300 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1301 var key = splitPlainAttributeKey[0];
1302 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1303 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1304 key = splitPlainAttributeKey[0]+'@0';
1306 if(searchElement != null){
1307 if (enumKeyList.indexOf(key) != -1){
1308 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
1309 var multiSlect = [];
1310 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1311 multiSlect.push(searchElement.selectedOptions[i].value);
1313 jsonPolicy[key]= multiSlect;
1315 jsonPolicy[key]= searchElement.value;
1317 if(searchElement.getAttribute("required")){
1318 if(!searchElement.value){
1323 if(searchElement.value != null){
1324 jsonPolicy[key]= searchElement.value;
1325 if(searchElement.getAttribute("required")){
1326 if(!searchElement.value){
1335 var uuu = "policyController/validate_policy.htm";
1336 var postData={policyData: policy, policyJSON : jsonPolicy};
1341 contentType: 'application/json',
1342 data: JSON.stringify(postData),
1343 success : function(data){
1344 $scope.$apply(function(){
1345 $scope.validateData = data.data.replace(/\"/g, "");
1346 $scope.data=data.data.substring(1,8);
1347 var size = data.data.length;
1348 if($scope.data == 'success'){
1349 Notification.success("Validation Success.");
1350 $scope.savebutton = false;
1352 var displayWarning = data.data.substring(19,size - 1);
1353 document.getElementById("validate").innerHTML = "Safe Policy Warning Message : "+displayWarning;
1354 document.getElementById("validate").style.color = "white";
1355 document.getElementById("validate").style.backgroundColor = "skyblue";
1358 Notification.error("Validation Failed.");
1359 document.getElementById("validate").innerHTML = $scope.validateData;
1360 document.getElementById("validate").style.color = "white";
1361 document.getElementById("validate").style.backgroundColor = "red";
1362 $scope.savebutton = true;
1366 console.log($scope.data);
1368 error : function(data){
1369 Notification.error("Validation Failed.");
1370 $scope.savebutton = true;
1375 function extend(obj, src) {
1376 for (var key in src) {
1377 if (src.hasOwnProperty(key)) obj[key] = src[key];