aboutsummaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/dbca-overlay/src/main/webapp/app/dbcapp/dmaapaccess/dmaap-access-popup-controller.js
blob: 4fe3805eda80cd877097d79ef8db590032c1b770 (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
appDS2.controller('dmaapAccessPopupCtrl', function($scope, $log, $modalInstance, modalService, message, DmaapAccessService) {
	'use strict';
	
	// this object holds all app data and functions
	$scope.dbcapp = {};
	
	// Set the label variable for the template
	if (message.dmaapAccess == null)
		$scope.dbcapp.label = 'Add Access Profile';
	else
		$scope.dbcapp.label = 'Edit Access Profile';
	$scope.dbcapp.editDmaapAccess = message.dmaapAccess;

	/**
	 * Validates content of user-editable fields.
	 * Returns null if all is well, 
	 * a descriptive error message otherwise.
	 */
	$scope.dbcapp.validateProfile = function(dmaapAccess) {
		if (dmaapAccess == null)
			return "No data found.\nPlease enter some values.";
		if (dmaapAccess.name == null || dmaapAccess.name.trim() == '')
			return "Name is required.\nPlease enter a value.";
		// User cannot edit URL
		//if (dmaapAccess.dmaapUrl == null || dmaapAccess.dmaapUrl.trim() == '')
		//	return "URL is required.\nPlease enter a value.";
		//if (dmaapAccess.dmaapUrl.toLowerCase().indexOf('http') != 0)
		//	return "Unexpected URL prefix.\nPlease enter a URL starting with 'http'.";
		for (var x in message.dmaapAccessList) {
			// $log.debug('saveDmaapAccess: checking item >' + message.dmaapAccessList[x].name + '<');
			// Ignore the name in the one being edited.
			if (message.dmaapAccessList[x].id == dmaapAccess.id)
				continue;
			if (message.dmaapAccessList[x].name == dmaapAccess.name)
				return "Name " + dmaapAccess.name + " exists.\nPlease enter a different name.";
		}
		return null;
	}
	
	/**
	 * Tests the URL for validity.  Shows a modal dialog to display test result.
	 * Returns nothing.
	 */
	$scope.dbcapp.testDmaapAccess = function(dmaapAccess) {
		if (dmaapAccess == null || dmaapAccess.dmaapUrl == null || dmaapAccess.dmaapUrl.trim() == '') {
			alert('No URL found.\nPlease enter a URL.');
			return;
		}
		// result should have a data aggregate that's a serialized DMaaP object 
		DmaapAccessService.testDmaapAccess(dmaapAccess)
			.then(function(response) {
				if (response.error != null)
					alert('Invalid Content:\n' + response.error);
				else
					alert('DMaaP name is ' + response.data.dmaapName)
			},
			function (error) {
				alert('Test failed:\n' + error);				
			}
		);
	}

	/**
	 * Validates the content; on success, calls service to save it.
	 */
	$scope.dbcapp.saveDmaapAccess = function(dmaapAccess) {
		var validateMsg = $scope.dbcapp.validateProfile(dmaapAccess);
		if (validateMsg != null) {
			alert('Invalid Content:\n' + validateMsg);
			return;			
		}

		// Set the selected flag if this is the only access profile.
		if (dmaapAccess.id == null && message.dmaapAccessList.length == 0)
			dmaapAccess.selected = true;
		
		if (dmaapAccess.id == null) {
			// No id, so create a new one
			DmaapAccessService.addDmaapAccess(dmaapAccess)
				.then(function(dmaapAccessList) {

					$modalInstance.close(dmaapAccessList);
				},
				function (error) {
					$log.error('dmaapAccessPopupCtrl: error while adding: ' + error);
				}
			);
		}
		else {
			// Has id, so update an existing one
			DmaapAccessService.updateDmaapAccess(dmaapAccess)
				.then(function(dmaapAccessList) {
					$modalInstance.close(dmaapAccessList);
				},
				function (error) {
					$log.error('dmaapAccessPopupCtrl: error while updating: ' + error);
				}
			);
		}

	};

	$scope.dbcapp.close = function() {
		$modalInstance.close();
	};
});