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