aboutsummaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/src/main/webapp/app/dbcapp/dmaapaccess/dmaap-access-list-controller.js
blob: 0c60aeea869ba8cef5db0779acd16a1294131d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*-
 * ================================================================================
 * DCAE DMaaP Bus Controller Web Application
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
app.controller('dmaapAccessListCtrl', function ($scope, $log, $modal, modalService, DmaapAccessService) {
	'use strict';
	
	// this object holds all app data and functions
	$scope.dbcapp = {};
    // Model for radio-button selection group.
	// Uses database row ID as unique value.
    $scope.dbcapp.selectDmaapModel = { id: null };
	$("#dialog").hide();
	$scope.dbcapp.isDataLoading=true;

	/**
	 * Loads the table of DMaaP access profiles.
	 * 
	 * Interprets the remote controller's response and copies to scope
	 * variables. The response is either list to be assigned to tableData, 
	 * or an error to be shown.
	 */
	$scope.dbcapp.loadTable = function() {
		$scope.dbcapp.isDataLoading = true;
		DmaapAccessService.getDmaapAccessList()
			.then(function(jsonObj) {
			if (jsonObj.error) {
				$scope.dbcapp.isRequestFailed = true;
				$scope.dbcapp.errMsg = jsonObj.error;
				$scope.dbcapp.tableData = [];
			} else {
				$scope.dbcapp.isRequestFailed = false;
				$scope.dbcapp.errMsg = null;
				$scope.dbcapp.tableData = jsonObj.data;
				$scope.dbcapp.updateDmaapAccessSelection();
			}			
			$scope.dbcapp.isDataLoading = false;
		}, function(error) {
			// Called with a string, not JSON obj.
			$log.error("dmaapAccessListCtrl.getDmaapAccessList failed: " + error);
			$scope.dbcapp.isRequestFailed = true;
			$scope.dbcapp.errMsg = error;
			$scope.dbcapp.tableData = [];
			$scope.dbcapp.isDataLoading = false;
		});
	};
	
	/**
	 * Sets a value in the model for the radio-button selection group.
	 */
	$scope.dbcapp.updateDmaapAccessSelection = function() {
		for (var i in $scope.dbcapp.tableData) {
			var da = $scope.dbcapp.tableData[i];
			// Set radio button for the selected profile
			// $log.info('dmaapAccessListCtrl: examining ' + JSON.stringify(da));
			if (da.selected) {
				// $log.info('dmaapAccessListCtrl: selecting id ' + da.id);
				$scope.dbcapp.selectDmaapModel.id = da.id;
  			}
		}
	};
	
	/**
	 * Handles a click on radio button to select a profile.
	 */
    $scope.dbcapp.selectDmaapAccess = function(dmaapAccess) {
    	if (dmaapAccess == null || dmaapAccess.id == null)
    		$log.error('selectDmaapAccess invoked with null');
    	else
    		DmaapAccessService.setSelectedDmaapAccess(dmaapAccess.id);
    };
   
    /**
     * Shows a modal pop-up to add a DMaaP access profile.
     * Passes data in via an object named "message".
     * On successful completion, updates the profile list.
     * 
     * After implementing DE238329, this is never called.
     */
	$scope.dbcapp.addDmaapAccessModalPopup = function() {
		$scope.dbcapp.editDmaapAccess = null;
		var modalInstance = $modal.open({
		    templateUrl: 'edit_dmaap_access_popup.html',
		    controller: 'dmaapAccessPopupCtrl',
		    resolve: {
		    	message: function () {
		    		var dataForPopup = {
		    			dmaapAccess     : $scope.dbcapp.editDmaapAccess,
		    			dmaapAccessList : $scope.dbcapp.tableData
                    };
		    		return dataForPopup;
		        }					
		      }
		  }); 
		modalInstance.result.then(function(response) {
			if (response == null) {
				// $log.debug('addDmaapAccessModelPopup: user closed dialog');
			}
			else {
				if (response.error != null)
					modalService.showFailure('Add Profile Failed', 
						'Failed to add access profile:\n' + response.error);
				else
					// success, get the updated list.
					$scope.dbcapp.loadTable();
			}
        });
	};	
		
    /**
     * Shows a modal pop-up to edit a DMaaP access profile.
     * Passes data in via an object named "message".
     * On successful completion, updates the profile list.
     */
	$scope.dbcapp.editDmaapAccessModalPopup = function(dmaapAccess) {
		// edit a copy, not the model for the table row.
		$scope.dbcapp.editDmaapAccess = JSON.parse(JSON.stringify(dmaapAccess));
		var modalInstance = $modal.open({
		    templateUrl: 'edit_dmaap_access_popup.html',
		    controller: 'dmaapAccessPopupCtrl',
		    resolve: {
		    	message: function () {
		    		var dataForPopup = {
		    			dmaapAccess     : $scope.dbcapp.editDmaapAccess,
		    			dmaapAccessList : $scope.dbcapp.tableData 
                   	};
		    		return dataForPopup;
		        }					
		      }
		  }); 
		modalInstance.result.then(function(response) {
			if (response == null) {
				// $log.debug('addDmaapAccessModelPopup: user closed dialog');
			}
			else {
				if (response.error != null)
					modalService.showFailure('Edit Profile Failed', 
						'Failed to edit access profile:\n' + response.error);
				else
					// success, get the updated list.
					$scope.dbcapp.loadTable();
			}
        });
	};
	
    /**
     * Shows a modal pop-up to confirm deletion of a DMaaP access profile.
     * On successful completion, updates the profile list.
     * 
     * After implementing DE238329, this is never called.
     */
	$scope.dbcapp.deleteDmaapAccess = function(dmaapAccess) {
		modalService.popupConfirmWin("Confirm",	"Delete the DMaaP access profile: "
				+ dmaapAccess.name + "\nContinue?",	
			function() {
				// $log.debug('deleteDmaapAccess: deleting id ' + dmaapAccess.id);
				DmaapAccessService.deleteDmaapAccess(dmaapAccess.id).then(
					function(response) {
						if (response.error != null) {
							$log.error('deleteDmaapAccess: failed to delete: ' + response.error);
							modalService.showFailure('Delete Failed', 
									'Failed to delete access profile:\n' + response.error);
						}
						else {
							// success, get the updated list.
							$scope.dbcapp.loadTable()
						}
					},
			function(error) {
				 $log.error('deleteDmaapAccess failed: ' + error);
				 modalService.showFailure("Fail", "dmaapAccessListCtrl failed to delete object");
			 });
		})
	};

	// Populate the table on load.
	$scope.dbcapp.loadTable();
    
});