[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / DescriptiveSearchDictController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 app.controller('editDescriptiveScopeController' , function ($scope, $modalInstance, message, UserInfoServiceDS2, Notification){
21     if(message.descriptiveScopeDictionaryData==null)
22         $scope.label='Add Descriptive Scope',
23         $scope.choices = [];
24     else{
25         $scope.label='Edit Descriptive Scope'
26         $scope.disableCd=true;
27         $scope.choices = [];
28         var headers = message.descriptiveScopeDictionaryData.search;
29         var SplitChars = 'AND';
30         if (headers.indexOf(SplitChars) >= 0) {
31             var splitHeader = headers.split(SplitChars);
32             var singleHeader  = splitHeader;
33             var splitEqual = ':';
34             for(i = 0; i < singleHeader.length; i++){
35                  if (singleHeader[i].indexOf(splitEqual) >= 0) {
36                         var splitValue = singleHeader[i].split(splitEqual);
37                         var key  = splitValue[0];
38                         var value = splitValue[1];
39                         var newItemNo = $scope.choices.length+1;
40                         $scope.choices.push({'id':'choice'+newItemNo, 'option': key , 'number' : value });
41                  }
42             }
43         }else{
44                  var splitEqual = ':';
45              if (headers.indexOf(splitEqual) >= 0) {
46                  var splitValue = headers.split(splitEqual);
47                  var key  = splitValue[0];
48                  var value = splitValue[1];
49                  $scope.choices.push({'id':'choice'+1, 'option': key , 'number' : value });
50              }
51         }
52     }
53         
54         /*getting user info from session*/
55         var userid = null;
56         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
57                 .then(function (response) {                     
58                         userid = response.userid;               
59          });
60         
61     $scope.editDescriptiveScope = message.descriptiveScopeDictionaryData;
62
63     $scope.saveDescriptiveScope = function(descriptiveScopeDictionaryData) {
64         var regex = new RegExp("^[a-zA-Z0-9_]*$");
65         if(!regex.test(descriptiveScopeDictionaryData.scopeName)) {
66                 Notification.error("Enter Valid Descriptive Scope Name without spaces or special characters");
67         }else{
68                 var finalData = extend(descriptiveScopeDictionaryData, $scope.actions[0]);
69                 var uuu = "saveDictionary/descriptive_dictionary/save_descriptive";
70                 var postData={descriptiveScopeDictionaryData: finalData, userid: userid};
71                 $.ajax({
72                         type : 'POST',
73                         url : uuu,
74                         dataType: 'json',
75                         contentType: 'application/json',
76                         data: JSON.stringify(postData),
77                         success : function(data){
78                                 $scope.$apply(function(){
79                                         $scope.descriptiveScopeDictionaryDatas=data.descriptiveScopeDictionaryDatas;});
80                                 if($scope.descriptiveScopeDictionaryDatas == "Duplicate"){
81                                         Notification.error("Descriptive Scope Dictionary exists with Same Scope Name.")
82                                 }else{      
83                                         console.log($scope.descriptiveScopeDictionaryDatas);
84                                         $modalInstance.close({descriptiveScopeDictionaryDatas:$scope.descriptiveScopeDictionaryDatas});
85                                 }       
86
87                         },
88                         error : function(data){
89                                 alert("Error while saving.");
90                         }
91                 });
92         }
93     };
94
95     $scope.close = function() {
96         $modalInstance.close();
97     };
98     
99     function extend(obj, src) {
100         for (var key in src) {
101             if (src.hasOwnProperty(key)) obj[key] = src[key];
102         }
103         return obj;
104     }
105     
106     $scope.actions = [{"attributes" : $scope.choices}];
107     $scope.addNewChoice = function() {
108         var newItemNo = $scope.choices.length+1;
109         $scope.choices.push({'id':'choice'+newItemNo});
110     };
111     $scope.removeChoice = function() {
112         var lastItem = $scope.choices.length-1;
113         $scope.choices.splice(lastItem);
114     };
115
116 });