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 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
74 PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
76 $scope.data = JSON.parse(j.data);
77 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
80 PolicyAppService.getData('getDictionary/get_OptimizationModelsDataByName').then(function (data) {
82 $scope.data = JSON.parse(j.data);
83 var inputModelList = JSON.parse($scope.data.optimizationModelsDictionaryDatas);
86 for(var i = 0; i < inputModelList.length; i++){
87 if(typeof unique[inputModelList[i]] == "undefined"){
88 unique[inputModelList[i]] = "";
89 uniqueList.push(inputModelList[i]);
92 $scope.optimizationModelsDictionaryDatas = uniqueList;
95 PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
97 $scope.data = JSON.parse(j.data);
98 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
102 $scope.attributeDatas = [{"attributes" : $scope.choices}];
103 $scope.isInitEditTemplate = true; //just initially create the edit template, didn't click add button yet.
104 $scope.addNewChoice = function(value) {
105 if(value != undefined){
106 if (value.startsWith('div.')){
107 value = value.replace('div.','');
109 var parentElement = document.getElementById("div."+value);
110 var div = document.getElementById(value+"@0");
112 var clone = div.cloneNode(true);
113 var addElement = parentElement.childElementCount;
114 clone.id = ''+value+'@'+addElement;
116 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ //if it's view or edit
117 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
118 if($scope.temp.policy.ruleData[clone.id]){
119 clone.value = $scope.temp.policy.ruleData[clone.id];
121 if(!clone.className.includes("child_single")){
122 clone.className += ' child_single'; //single element clone
124 document.getElementById("div."+value).appendChild(clone);
125 plainAttributeKeys.push(''+value+'@'+addElement);
127 }else{ //not view or edit
128 if(!clone.className.includes("child_single")){
129 clone.className += ' child_single'; //single element clone
131 document.getElementById("div."+value).appendChild(clone);
132 plainAttributeKeys.push(''+value+'@'+addElement);
135 div = document.getElementById("div."+value+"@0");
138 div.className += ' children_group'; //div with a group of children.
140 var childElement = parentElement.firstElementChild;
141 var countParent = parentElement.childElementCount;
142 var childElementString = childElement.innerHTML;
143 var find = value+"@0";
144 var re = new RegExp(find, 'g');
145 childElementString = childElementString.replace(re,value+'@' + countParent);
146 var clone = childElement.cloneNode(true);
147 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
148 var childId = parentElement.childNodes[ii].id;
149 if(ii = parentElement.childNodes.length){
150 var childnewId = childId.slice(0, -1);
151 var count = childId.slice(-1);
154 var countvalue = parseInt(count) + 1;
155 clone.id = childnewId+countvalue;
157 clone.innerHTML=childElementString;
158 document.getElementById("div."+value).appendChild(clone);
159 var selects = clone.getElementsByTagName("select");
160 var inputs = clone.getElementsByTagName("input");
161 var removeValues = [];
162 for(var i=0; i<inputs.length; i++){
163 if ($scope.temp.policy.ruleData!=undefined){
164 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
165 if (checkValue!=undefined && checkValue != "undefined"){
166 if($scope.temp.policy.ruleData != null){
167 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
168 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
169 plainAttributeKeys.push(inputs[i].id);
172 plainAttributeKeys.push(inputs[i].id);
175 plainAttributeKeys.push(inputs[i].id);
179 for(var i=0; i<selects.length; i++){
180 if ($scope.temp.policy.ruleData!=undefined){
181 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
182 if (checkValue!=undefined && checkValue!="undefined"){
183 if($scope.temp.policy.ruleData != null){
184 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
185 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
186 plainAttributeKeys.push(selects[i].id);
189 plainAttributeKeys.push(selects[i].id);
192 plainAttributeKeys.push(selects[i].id);
196 for (var k=0; k<removeValues.length; k++){
197 var elem = document.getElementById(removeValues[k]);
198 elem.parentNode.removeChild(elem);
200 var ele = angular.element(document.getElementById("div."+value));
201 $compile(ele.contents())($scope);
207 function findVal(object, key) {
209 Object.keys(object).some(function(k) {
214 if (object[k] && typeof object[k] === 'object') {
215 value = findVal(object[k], key);
216 return value !== undefined;
222 $scope.removeChoice = function(value) {
223 if(value != undefined){
224 var c = document.getElementById("div."+value).childElementCount;
227 Notification.error("The original one is not removable.");
230 document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);
234 $scope.pullVersion = function(serviceName) {
235 if(serviceName != undefined){
236 var uuu = "policyController/getModelServiceVersionData.htm";
237 var postData={policyData: serviceName};
242 contentType: 'application/json',
243 data: JSON.stringify(postData),
244 success : function(data){
245 $scope.$apply(function(){
246 $scope.optimizationModelsDictionaryVersionDatas = data[0].optimizationModelVersionData;
249 error : function(data){
250 Notification.error("Error While Retrieving the Template Layout Pattern.");
256 var splitEqual = '=';
257 var splitComma = ',';
258 var splitcolon = ':';
259 var splitEnum = "],";
260 var plainAttributeKeys = [];
262 var enumKeyList = [];
263 var dictionaryList = [];
264 var dictionaryNameList = [];
265 $scope.addDataToFields = function(serviceName, version){
266 if(serviceName != null && version !=null){
267 var service=serviceName+"-v"+version;
268 var myNode = document.getElementById("DynamicTemplate");
269 myNode.innerHTML = '';
270 var uuu = "policyController/getOptimizationTemplateData.htm";
271 var postData={policyData: service};
272 var dataOrderInfo = "";
278 contentType: 'application/json',
279 data: JSON.stringify(postData),
280 success : function(data){
281 $scope.$apply(function(){
282 $scope.optimizationModelData = data[0].optimizationModelData;
283 $scope.optimizationJsonDate = data[0].jsonValue;
284 $scope.dataOrderInfo = null;
285 var headDefautlsData = data[0].headDefautlsData;
286 if(headDefautlsData != null){
287 $scope.temp.policy.onapName = headDefautlsData.onapName;
288 $scope.temp.policy.guard = headDefautlsData.guard;
289 $scope.temp.policy.riskType = headDefautlsData.riskType;
290 $scope.temp.policy.riskLevel = headDefautlsData.riskLevel;
291 $scope.temp.policy.priority = headDefautlsData.priority;
293 }else if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
294 $scope.temp.policy.onapName = "";
295 $scope.temp.policy.guard = "";
296 $scope.temp.policy.riskType = "";
297 $scope.temp.policy.riskLevel = "";
298 $scope.temp.policy.priority = "";
301 var annotation = $scope.optimizationModelData.annotation;
302 var dictionary = $scope.microServiceAttributeDictionaryDatas;
304 if (annotation == null || annotation.length<1){
305 $scope.isCheck = true;
307 $scope.isCheck = false;
308 var annoationList = annotation.split(splitComma);
309 for (k = 0; k < annoationList.length; k++){
310 var splitAnnotation = annoationList[k].split(splitEqual);
311 if (splitAnnotation[1].includes("matching-true")){
312 matching.push(splitAnnotation[0].trim());
318 if (dictionary!= null && dictionary.length>1){
319 for (m=0; m < dictionary.length; m += 1){
320 var valueCompare = dictionary[m].value;
321 var valueModel = dictionary[m].modelName;
322 var conpairService = serviceName;
323 if (valueModel.includes('-v')){
324 conpairService = service;
326 if(valueModel.localeCompare(conpairService) == 0){
327 dictionaryList.push(dictionary[m]);
328 if (!dictionaryNameList.includes(dictionary[m].name)){
329 dictionaryNameList.push(dictionary[m].name)
335 $scope.temp.policy.ruleGridData = [];
337 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
338 dataOrderInfo = $scope.dataOrderInfo;
341 $scope.jsonLayout($scope.optimizationJsonDate);
345 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ // If it's veiw or edit
347 if($scope.temp.policy.editPolicy){
348 $scope.isInitEditTemplate = true;
353 // If ruleData contains extra elements created by clicked add button
354 if($scope.temp.policy.ruleData != null){
355 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
356 propNames.forEach(function(name) {
360 var extraElements = data;
362 if(plainAttributeKeys != null){
363 for(b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
364 var newValue = plainAttributeKeys[b].split("*");
365 for(a = 0; a < data.length; a++){
366 if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
367 extraElements.splice(a, 1);
373 //--- Populate these extra elements created by clicked add button
374 for(a = 0; a < extraElements.length; a++){
375 if(extraElements[a].includes("@")){
376 var index = extraElements[a].lastIndexOf("@");
378 // Get the number after @
379 var n = getNumOfDigits(extraElements[a], index+1);
381 var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
386 var unique = checkData.filter(onlyUnique);
387 //if no layout order info, keep the process as before
389 for(i =0; i < unique.length; i++){
390 //remove @x and let addNewChoice add @1 or @2...
391 //var newKey = unique[i].substring(0, unique[i].length-2);
392 var index = unique[i].lastIndexOf("@");
393 var newKey = unique[i].substring(0, index);
394 $scope.addNewChoice(newKey);
398 for (i = 0; i < $scope.labelManyKeys.length; i++) {
399 var label = $scope.labelManyKeys[i];
400 // first add parent/label level
401 for (k = 0; k < unique.length; k++){
402 var newKey = unique[k].substring(0, index);
404 //Check this label has bee created or not
405 if(!document.getElementById(unique[k])){
406 $scope.addNewChoice(newKey);
408 unique[k] = "*processed*";
414 //---reset to default
416 $scope.labelManyKeys = [];
418 //---process none labels
419 for (j = 0; j < unique.length; j++){
420 if(unique[j] != "*processed*"){
421 // if not created yet
422 if(!document.getElementById(unique[j])){
423 var index = unique[j].lastIndexOf("@");
424 var newKey = unique[j].substring(0, index);
425 $scope.addNewChoice(newKey);
433 if($scope.temp.policy.editPolicy){
434 //clean all the events of addNewChoice
435 $scope.$on('$destroy', addNewChoice);
439 var ele = angular.element(document.getElementById("DynamicTemplate"));
440 $compile(ele.contents())($scope);
443 error : function(data){
444 Notification.error("Error While Retrieving the Template Layout Pattern.");
450 function getNumOfDigits(str_value, index){
451 // Get the number after @
452 var str = str_value.substring(index, str_value.length);
455 for (var x = 0; x < str.length; x++){
466 function getDictionary(attribute){
467 var dicName = attribute;
469 if(attribute.includes(":")){
470 dicName = attribute.split(":")[0];
473 var dictionaryRegExp = new RegExp(dicName);
474 listemunerateValues = [];
475 if (dictionaryRegExp.test(dictionaryNameList)) {
476 for (p=0; p < dictionaryList.length; p += 1) {
477 if (dicName == dictionaryList[p].name) {
478 listemunerateValues.push(dictionaryList[p].value);
482 return listemunerateValues;
485 function getList(attribute) {
486 var enumName = attribute;
488 if(attribute.includes(":")){
489 enumName = attribute.split(":")[0];
492 var baseEnum = $scope.optimizationModelData.enumValues;
494 if(baseEnum != null){
495 enumList = baseEnum.split(splitEnum);
498 var patternTest = new RegExp(enumName);
499 for (k=0; k < enumList.length; k += 1){
500 if(patternTest.test(enumList[k]) == true){
501 enumAttributes = enumList[k].trim();
506 enumAttributes = enumAttributes.replace("[", "");
507 enumAttributes = enumAttributes.replace("]", "");
508 enumAttributes = enumAttributes.replace(/ /g, '');
509 var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
510 listemunerateValues = dropListAfterCommaSplit[1].split(splitComma);
511 return listemunerateValues;
517 function getBooleanList(){
518 var booleanList = [];
519 booleanList.push(true);
520 booleanList.push(false);
524 function isArray(arrayTest) {
525 return Object.prototype.toString.call(arrayTest) === '[object Array]';
530 $scope.layOutElementList = [];
531 $scope.layOutOnlyLableList = [];
533 var elementOrderNum = 0;
535 function deconstructJSON(layOutData, level , name) {
539 var stringValue = "java.lang.String";
540 var string = "string";
541 var intValue = "int";
542 var integerValue = "integer";
543 var double = "double";
544 var boolean = "boolean";
545 var baseLevel = level;
547 var String = "String";
549 var attributekey = "";
551 if (name.length > 1){
552 label = label + name + '.';
555 for (key in layOutData) {
556 array = isArray(layOutData[key]);
558 if (!!layOutData[key] && typeof(layOutData[key])=="object") {
560 if (array==false && key!=="0"){
562 if($scope.dataOrderInfo){
563 var labelObject = {"label" : key, "level" : label, "array" : array};
564 //save it to the list
565 $scope.layOutOnlyLableList.push(labelObject);
569 $scope.labelLayout(label, key, array );
574 if (array == true && key!=0){
578 if (lableList.length > 0){
582 var newKey = lableList.pop();
584 if($scope.dataOrderInfo){
586 var labelObject = {"label" : newKey, "level" : baseLevel, "array" : array};
587 //save it to the list
588 $scope.layOutOnlyLableList.push(labelObject);
592 $scope.labelLayout(baseLevel, newKey, array );
596 label = baseLevel + newKey + '@0.';
598 label = baseLevel + newKey + '.';
601 deconstructJSON(layOutData[key] , label, key);
603 var attirbuteLabel = label;
605 var isRequired = false;
606 if (layOutData[key].includes('defaultValue-')){
607 defaultValue = layOutData[key].split('defaultValue-')[1];
612 attributekey = lableList.pop();
613 attirbuteLabel = baseLevel;
615 attributekey = key.split();
618 if (layOutData[key].includes('required-true')){
622 var subAttributes = $scope.optimizationModelData.sub_attributes;
625 var jsonObject = JSON.parse(subAttributes);
626 var allkeys = Object.keys(jsonObject);
628 for (var k = 0; k < allkeys.length; k++) {
629 var keyValue = allkeys[k];
630 if(jsonObject[keyValue]){
631 var tempObject = jsonObject[keyValue];
632 if(tempObject && tempObject[key]){
633 if (tempObject[key].includes('required-true')){
642 var elementObject = {};
643 switch (layOutData[key].split(splitcolon)[0]){
647 if($scope.dataOrderInfo){
649 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue, "isRequired": isRequired, "type":"text"};
650 $scope.layOutElementList.push(elementObject);
652 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
657 if($scope.dataOrderInfo){
659 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"number"};
660 $scope.layOutElementList.push(elementObject);
662 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
666 if($scope.dataOrderInfo){
668 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"double"};
669 $scope.layOutElementList.push(elementObject);
671 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "double");
675 if($scope.dataOrderInfo){
677 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key], "list": getBooleanList, "isRequired": isRequired, "type":"dropBox"};
678 $scope.layOutElementList.push(elementObject);
680 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], getBooleanList());
684 if (layOutData[key].includes('dictionary-')){
685 var list = getDictionary(layOutData[key].split('dictionary-')[1]);
687 //--- get dropdown values from enumValues
688 var list = getList(layOutData[key]);
690 if (list.length===0){ //not dropdown element
691 if($scope.dataOrderInfo){
693 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"text"};
694 $scope.layOutElementList.push(elementObject);
697 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
700 if($scope.dataOrderInfo){
702 elementObject = {"id": elementOrderNum, "attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key],"isRequired": isRequired, "list":list, "type":"dropBox"};
703 $scope.layOutElementList.push(elementObject);
705 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], list, isRequired);
714 $scope.validContionalRequired = function(parentId) {
715 var c = document.getElementById(parentId).children;
717 var hasValue = false;
718 for (i = 0; i < c.length; i++) {
719 if(c[i].getAttribute("data-conditional")){
720 if(c[i].value != null && c[i].value.trim() != ""){
726 for (i = 0; i < c.length; i++) {
727 if(c[i].getAttribute("data-conditional")){
729 c[i].setAttribute("required", true);
731 c[i].removeAttribute("required");
737 $scope.jsonLayout = function(layOutData){
739 deconstructJSON(layOutData , "", "");
741 var orderValue = $scope.dataOrderInfo;
742 var layOutElementList = $scope.layOutElementList;
743 var labelList = $scope.layOutOnlyLableList;
747 $scope.layOutElementList = [];
748 $scope.layOutOnlyLableList = [];
750 // Only layout in order if order info provided
753 if(orderValue.includes("[")){
754 orderValue = orderValue.replace("[", "") ;
755 orderValue = orderValue.replace("]", "") ;
758 orderValue = orderValue.split(',') ;
760 for (i = 0; i < orderValue.length; i++) {
761 var key = orderValue[i].trim();
763 //--- Create labels first {"label" : newKey, "level" : baseLevel, "array" : array};
765 for (k = 0; k < labelList.length; k++){
767 var label = labelList[k].label.toString().trim();
768 var level = labelList[k].level.toString().trim();
769 var array = labelList[k].array;
772 $scope.labelLayout(level, label, array);
773 //in case to have duplicate label names
774 labelList[k].label = "*processed*";
779 //--- then layout each element based on its order defined in YAML file
780 for (j = 0; j < layOutElementList.length; j++) {
782 var attributekey = layOutElementList[j].attributekey.toString().trim();
784 if(key == attributekey){
786 var attirbuteLabel = layOutElementList[j].attirbuteLabel.toString().trim();
787 var defaultValue = layOutElementList[j].defaultValue.toString().trim();
788 var isRequired = layOutElementList[j].isRequired;
791 if (layOutElementList[j].type == "dropBox"){
792 $scope.dropBoxLayout(attirbuteLabel, attributekey, layOutElementList[j].array, defaultValue, layOutElementList[j].list, isRequired);
795 $scope.attributeBox(attributekey, layOutElementList[j].array, attirbuteLabel, defaultValue, isRequired, layOutElementList[j].type);
799 //in case to have duplicate attribute names
800 layOutElementList[j].attributekey = "*processed*";
811 $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired, dataType ){
812 $scope.temp.policy.ruleGridData.push(attibuteKey);
813 var br = document.createElement("BR");
815 var label = document.createElement("Label");
816 var labeltext = null;
817 var requiredLabName = "";
818 if (matching.includes(attibuteKey)){
819 labeltext = document.createTextNode(attibuteKey + "*!");
820 isRequired = true; //set required as true for matching element
823 requiredLabName = attibuteKey + " * ";
824 labeltext = document.createTextNode(requiredLabName);
826 labeltext = document.createTextNode(attibuteKey);
831 var divID = labelValue;
833 if (labelValue.length < 1){
834 divID = "DynamicTemplate";
835 }else if (labelValue.endsWith('.')){
836 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
839 label.appendChild(labeltext);
841 var textField = document.createElement("INPUT");
843 textField.setAttribute("class" , "form-control");
845 if(dataType == "double"){
846 textField.setAttribute("type" , "number");
847 textField.setAttribute("step" , "any");
849 textField.setAttribute("type" , dataType);
852 textField.setAttribute("style" , "width:300px;");
853 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
856 if(attributeManyKey){
857 checkKey = labelValue + attibuteKey+'@0';
858 textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
859 var divTag = document.createElement("div");
860 divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
861 var addButton = document.createElement("BUTTON");
862 var buttonaddLabel = document.createTextNode("+");
863 addButton.appendChild(buttonaddLabel);
864 addButton.setAttribute("id", labelValue + attibuteKey);
865 addButton.setAttribute("class", "btn btn-add-remove");
866 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + attibuteKey+'");');
867 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
868 var removeButton = document.createElement("BUTTON");
869 var buttonremoveLabel = document.createTextNode("-");
870 removeButton.appendChild(buttonremoveLabel);
871 removeButton.setAttribute("class", "btn btn-add-remove");
872 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue + attibuteKey+'");');
873 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
874 document.getElementById(divID).appendChild(addButton);
875 document.getElementById(divID).appendChild(removeButton);
876 document.getElementById(divID).appendChild(label);
877 id = "div."+labelValue+attibuteKey;
878 divTag.setAttribute("id", id);
879 document.getElementById(divID).appendChild(divTag);
880 textField.className += ' first_child';
882 textField.setAttribute("required", "true");
884 divTag.appendChild(textField);
885 document.getElementById(divID).appendChild(divTag);
888 checkKey = labelValue + attibuteKey;
889 textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
890 if(document.getElementById(divID).hasAttribute('required') || !document.getElementById(divID).hasAttribute('data-conditional')){
891 if(requiredLabName.includes("*") || isRequired){
892 textField.setAttribute("required", "true");
894 }else if (document.getElementById(divID).hasAttribute('data-conditional')){
895 if(requiredLabName.includes("*")){
896 var requiredNode = document.createElement('span');
897 requiredNode.setAttribute("class", "mstooltip");
898 requiredNode.textContent = "?";
899 label.appendChild(requiredNode);
901 var requiredNodeToolTip = document.createElement('span');
902 requiredNodeToolTip.setAttribute("class", "tooltiptext");
903 requiredNodeToolTip.textContent = "Conditional Required";
904 requiredNode.appendChild(requiredNodeToolTip);
906 textField.setAttribute("data-conditional", divID);
907 textField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
911 document.getElementById(divID).appendChild(label);
912 document.getElementById(divID).appendChild(textField);
913 document.getElementById(divID).appendChild(br);
917 if(divID.includes("@0") && divID.includes("div.")){
918 var firstChild_Id = divID.split("@0")[0];
919 var firstChild_element = document.getElementById(firstChild_Id);
920 if(firstChild_element){
921 firstChild_element.className += ' children_group'; //here is a div with a group of children.
925 if (defaultValue.length > 0){
926 if(defaultValue.includes(":")){
927 defaultValue = defaultValue.split(":")[0];
928 if(defaultValue === "NA") {
932 if(defaultValue != "undefined" && defaultValue != undefined && defaultValue != "null"){
933 document.getElementById(checkKey).value = defaultValue;
937 if($scope.temp.policy.ruleData != null){
938 if (attributeManyKey){
939 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
940 if($scope.temp.policy.ruleData[newCheckKey +'@0'] != undefined && $scope.temp.policy.ruleData[newCheckKey +'@0'] != "undefined"){
941 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
944 if($scope.temp.policy.ruleData[checkKey] != undefined && $scope.temp.policy.ruleData[checkKey] != "undefined"){
945 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
949 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey);
953 $scope.labelManyKeys = [];
954 $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
955 var label = document.createElement("Label")
956 var divID = labelValue;
957 if (labelValue.endsWith('.')){
958 var workingLabel = labelValue.substring(0, labelValue.length-1);
960 var workingLabel = labelValue;
963 if (labelValue.length < 1){
964 divID = "DynamicTemplate";
965 } else if (labelValue.endsWith('.')){
966 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
969 var subAttributes = $scope.optimizationModelData.subattributes;
970 var jsonObject = JSON.parse(subAttributes);
971 var lablInfo = findVal(jsonObject, lableName);
975 if(lablInfo.includes("required-true")){
978 }else if (lablInfo.includes("required-false")){
983 var labeltext = document.createTextNode(lableName + star);
985 label.appendChild(labeltext);
989 var addButton = document.createElement("BUTTON");
990 var buttonLabel = document.createTextNode("+");
991 addButton.appendChild(buttonLabel);
992 addButton.setAttribute("class", "btn btn-add-remove");
993 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + lableName+'");');
994 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
995 var removeButton = document.createElement("BUTTON");
996 var buttonremoveLabel = document.createTextNode("-");
997 removeButton.appendChild(buttonremoveLabel);
998 removeButton.setAttribute("class", "btn btn-add-remove");
999 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue +lableName+'");');
1000 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1001 document.getElementById(divID).appendChild(addButton);
1002 document.getElementById(divID).appendChild(removeButton);
1003 document.getElementById(divID).appendChild(label);
1004 var id = "div."+labelValue+lableName;
1005 var divTag = document.createElement("div");
1006 divTag.setAttribute("id", id);
1007 document.getElementById(divID).appendChild(divTag);
1009 var divTag = document.createElement("div");
1010 divTag.setAttribute("id", id +'@0');
1012 divTag.className += ' children_group'; //here is div with a group of children.
1015 divTag.setAttribute("required", required);
1016 }else if(required == false){
1017 divTag.setAttribute("data-conditional", "yes");
1020 document.getElementById(id).appendChild(divTag);
1022 $scope.labelManyKeys.push(lableName);
1025 var divTag = document.createElement("div");
1026 divTag.setAttribute("id", "div."+labelValue+lableName);
1027 divTag.className += ' children_group'; //here is div with a group of children.
1029 divTag.setAttribute("required", required);
1030 }else if(required == false){
1031 divTag.setAttribute("data-conditional", "yes");
1033 document.getElementById(divID).appendChild(label);
1034 document.getElementById(divID).appendChild(divTag);
1038 $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues, isRequired){
1039 var br = document.createElement("BR");
1041 if (labelLevel.length < 1){
1042 var divID = "DynamicTemplate";
1043 } else if (labelLevel.endsWith('.')){
1044 var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
1048 var label = document.createElement("Label")
1050 var refAttributes = $scope.optimizationModelData.ref_attributes;
1051 if(isRequired != true && refAttributes){ //check refAttributes also
1052 var refAttributesList = refAttributes.split(splitComma);
1053 for (k = 0; k < refAttributesList.length; k++){
1054 var refAttribute = refAttributesList[k].split(splitEqual);
1055 if (attributeName == refAttribute[0].trim() && refAttribute[1].includes("required-true")){
1061 if (matching.includes(attributeName)){
1062 var labeltext = document.createTextNode(attributeName + "*!");
1063 label.appendChild(labeltext);
1064 isRequired = true; //set required as true for matching element
1066 var labeltext = document.createTextNode(attributeName);
1068 var requiredLabName = attributeName+ " * ";
1069 labeltext = document.createTextNode(requiredLabName);
1071 labeltext = document.createTextNode(attributeName);
1074 label.appendChild(labeltext);
1076 label.appendChild(labeltext);
1077 // if this field is required, but its parent is not required
1078 if(isRequired && document.getElementById(divID).hasAttribute('data-conditional')){
1079 var requiredNode = document.createElement('span');
1080 requiredNode.setAttribute("class", "mstooltip");
1081 requiredNode.textContent = "?";
1082 label.appendChild(requiredNode);
1084 var requiredNodeToolTip = document.createElement('span');
1085 requiredNodeToolTip.setAttribute("class", "tooltiptext");
1086 requiredNodeToolTip.textContent = "Conditional Required";
1087 requiredNode.appendChild(requiredNodeToolTip);
1091 var listField = document.createElement("SELECT");
1092 listField.setAttribute("class" , "form-control");
1093 listField.setAttribute("style" , "width:300px;");
1094 listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
1097 if(document.getElementById(divID).hasAttribute('data-conditional')){
1098 listField.setAttribute("data-conditional", divID);
1099 listField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
1101 listField.setAttribute("required", true);
1104 if( many != true || isRequired != true){ // add an empty option for not required or not multiple select element
1105 var optionFirst = document.createElement('option');
1106 optionFirst.setAttribute('value', "");
1107 listField.appendChild(optionFirst);
1110 for (var i=0; i < listemunerateValues.length; i += 1) {
1111 if(typeof listemunerateValues[i] == "string" && listemunerateValues[i].includes("equal-sign")){
1112 listemunerateValues[i] = listemunerateValues[i].replace('equal-sign','=');
1114 var option = document.createElement('option');
1115 option.setAttribute('value', listemunerateValues[i]);
1116 option.appendChild(document.createTextNode(listemunerateValues[i]));
1117 option.setAttribute('value', listemunerateValues[i]);
1118 listField.appendChild(option);
1120 listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
1122 enumKeyList.push(attributeName);
1124 document.getElementById(divID).appendChild(label);
1125 document.getElementById(divID).appendChild(br);
1128 document.getElementById(divID).appendChild(listField).multiple = true;
1129 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
1131 document.getElementById(divID).appendChild(listField).multiple = false;
1132 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
1135 if($scope.temp.policy.ruleData != null){
1137 document.getElementById(labelLevel +attributeName).options[0].selected = false;
1138 for (i=0; i < listemunerateValues.length; i += 1) {
1139 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
1140 if (testValue === undefined){
1141 testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
1143 var location = listemunerateValues.indexOf(testValue);
1145 document.getElementById(labelLevel +attributeName).options[location].selected = true;
1149 if($scope.temp.policy.ruleData[labelLevel + attributeName] != undefined && $scope.temp.policy.ruleData[labelLevel + attributeName] != "undefined"){
1150 document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];
1156 function onlyUnique(value, index, self) {
1157 return self.indexOf(value) === index;
1160 $scope.savePolicy = function(policy){
1161 if(policy.itemContent != undefined){
1162 $scope.refreshCheck = true;
1163 $scope.policyNavigator = policy.itemContent;
1164 policy.itemContent = "";
1166 $scope.savebutton = false;
1169 var jsonPolicy = {};
1170 if(plainAttributeKeys != null){
1171 for(a = 0; a < plainAttributeKeys.length; a++){
1172 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1173 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1174 var key = splitPlainAttributeKey[0];
1176 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1177 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1178 key = splitPlainAttributeKey[0]+'@0';
1181 if(searchElement != null){
1182 var keySplit = key.split(dot);
1183 var elumentLocation = keySplit.length;
1185 if (elumentLocation > 1){
1186 enumKey = keySplit[keySplit.length - 1];
1188 //check it is undefined or not
1189 if (enumKeyList != undefined && enumKeyList.indexOf(enumKey) != -1){
1190 if (splitPlainAttributeKey[1]!= undefined && splitPlainAttributeKey[1].indexOf("true") !== -1){
1191 var multiSlect = [];
1192 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1193 multiSlect.push(searchElement.selectedOptions[i].value);
1195 jsonPolicy[key]= multiSlect;
1197 jsonPolicy[key]= searchElement.value;
1200 if(searchElement.value != null){
1201 jsonPolicy[key]= searchElement.value;
1207 var uuu = "policycreation/save_policy";
1208 var postData={policyData: policy, policyJSON : jsonPolicy};
1213 contentType: 'application/json',
1214 data: JSON.stringify(postData),
1215 success : function(data){
1216 $scope.$apply(function(){
1217 $scope.data=data.policyData;
1218 if($scope.data == 'success'){
1219 $scope.temp.policy.readOnly = 'true';
1220 $scope.safetyChecker = data.policyData.split("#")[2];
1221 if ($scope.safetyChecker!=undefined) {
1222 Notification.success($scope.safetyChecker);
1224 $scope.pushStatus=data.policyData.split("&")[1];
1225 if($scope.pushStatus=="successPush"){
1226 Notification.success("Policy pushed successfully");
1228 Notification.success("Policy Saved Successfully.");
1229 }else if ($scope.data == 'PolicyExists'){
1230 $scope.savebutton = true;
1231 Notification.error("Policy Already Exists with Same Name in Scope.");
1235 error : function(data){
1236 Notification.error("Error Occured while saving Policy.");
1241 $scope.validatePolicy = function(policy){
1242 document.getElementById("validate").innerHTML = "";
1245 var jsonPolicy = {};
1246 if(plainAttributeKeys != null){
1247 for(a = 0; a < plainAttributeKeys.length; a++){
1248 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1249 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1250 var key = splitPlainAttributeKey[0];
1251 if(searchElement == null || searchElement.nodeName == 'BUTTON'){
1252 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1253 key = splitPlainAttributeKey[0]+'@0';
1255 if(searchElement != null){
1256 if (enumKeyList.indexOf(key) != -1){
1257 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
1258 var multiSlect = [];
1259 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1260 multiSlect.push(searchElement.selectedOptions[i].value);
1262 jsonPolicy[key]= multiSlect;
1264 jsonPolicy[key]= searchElement.value;
1266 if(searchElement.getAttribute("required")){
1267 if(!searchElement.value){
1272 if(searchElement.value != null){
1273 jsonPolicy[key]= searchElement.value;
1274 if(searchElement.getAttribute("required")){
1275 if(!searchElement.value){
1284 var uuu = "policyController/validate_policy.htm";
1285 var postData={policyData: policy, policyJSON : jsonPolicy};
1290 contentType: 'application/json',
1291 data: JSON.stringify(postData),
1292 success : function(data){
1293 $scope.$apply(function(){
1294 $scope.validateData = data.data.replace(/\"/g, "");
1295 $scope.data=data.data.substring(1,8);
1296 var size = data.data.length;
1297 if($scope.data == 'success'){
1298 Notification.success("Validation Success.");
1299 $scope.savebutton = false;
1301 var displayWarning = data.data.substring(19,size - 1);
1302 document.getElementById("validate").innerHTML = "Safe Policy Warning Message : "+displayWarning;
1303 document.getElementById("validate").style.color = "white";
1304 document.getElementById("validate").style.backgroundColor = "skyblue";
1307 Notification.error("Validation Failed.");
1308 document.getElementById("validate").innerHTML = $scope.validateData;
1309 document.getElementById("validate").style.color = "white";
1310 document.getElementById("validate").style.backgroundColor = "red";
1311 $scope.savebutton = true;
1316 error : function(data){
1317 Notification.error("Validation Failed.");
1318 $scope.savebutton = true;
1323 function extend(obj, src) {
1324 for (var key in src) {
1325 if (src.hasOwnProperty(key)) obj[key] = src[key];