Project

General

Profile

1
var isManagerControllers = angular.module('isManagerControllers', ['LocalStorageModule', 'ui.codemirror']);
2

    
3
function common_init($scope, $http, $sce, $location) {
4
	initSpinner();
5
	$scope.showError        = function(error)   { show_notification("error", error); }
6
	$scope.showNotification = function(message) { show_notification("info", message); }
7
	$scope.showSpinner      = function()        { showSpinner(); }
8
	$scope.hideSpinner      = function()        { hideSpinner(); }
9
	$scope.to_trusted       = function(html)    { return $sce.trustAsHtml(html); }
10
	$scope.go               = function(path)    { $location.path(path); }
11
	$scope.encodeValue      = function(val)     { return val; }
12
}
13

    
14
isManagerControllers.controller('typesCtrl', [
15
	'$scope', '$http', '$sce', '$location', '$routeParams',
16
	function ($scope, $http, $sce, $location, $routeParams) {
17
		common_init($scope, $http, $sce, $location);
18
		$scope.kind = $routeParams.kind;
19
		$scope.types = [];
20
		$scope.showSpinner();
21
		$http.get('ajax_is/listResourceTypes.do?kind=' + $scope.kind).success(function(data) {
22
			$scope.hideSpinner();
23
			$scope.types = data;
24
		}).error(function() {
25
			$scope.showError('Error listing resource types');
26
			$scope.hideSpinner();
27
	    });
28
	}
29
]);
30

    
31

    
32
isManagerControllers.controller('dsListCtrl', [
33
	'$scope', '$http', '$sce', '$location', '$routeParams',
34
	function ($scope, $http, $sce, $location, $routeParams) {
35
		common_init($scope, $http, $sce, $location);
36
		$scope.list = [];
37
		$scope.kind = $routeParams.kind;
38
		$scope.type = $routeParams.type;
39
		$scope.format = $routeParams.format;
40
		$scope.showSpinner();
41
		$http.get('ajax_is/listDatastructures.do?type=' + $scope.type).success(function(data) {
42
			$scope.hideSpinner();
43
			$scope.list = data;
44
		}).error(function() {
45
			$scope.showError('Error listing datastructures');
46
			$scope.hideSpinner();
47
	    });
48
	}
49
]);
50

    
51

    
52
isManagerControllers.controller('dsCtrl', [
53
	'$scope', '$http', '$sce', '$location', '$routeParams', '$timeout',
54
	function ($scope, $http, $sce, $location, $routeParams, $timeout) {
55
		common_init($scope, $http, $sce, $location);
56

    
57
		$scope.resource = {};
58
		$scope.format = $routeParams.format;
59
		$scope.parsedContent = '';
60
		
61
		$scope.editorOptions = {
62
			lineWrapping : true,
63
			lineNumbers: true,
64
			mode: "application/json"
65
		};
66
		
67
		$scope.showSpinner();
68
		$scope.getResource = function(id) {
69
			if (id) {
70
				$scope.showSpinner();
71
				$http.get('ajax_is/getDs.do?id=' + id).success(function(data) {
72
					$scope.hideSpinner();
73
					$scope.resource = data;
74
					$scope.parsedContent = '';
75
				}).error(function() {
76
					$scope.showError('Error retrieving ds');
77
					$scope.hideSpinner();
78
				});
79
			}
80
		}
81
		
82
		$scope.setupEditContent = function () {
83
			$timeout(function() {
84
				try {
85
					if ($scope.format == 'JSON') {
86
						$scope.editorOptions.mode = "application/json";
87
						$scope.parsedContent = JSON.stringify(JSON.parse($scope.resource.content),null,2);
88
					} else if ($scope.format == 'XML') {
89
						$scope.editorOptions.mode = 'application/xml';
90
						$scope.parsedContent = $scope.resource.content;
91
					} else {
92
						$scope.editorOptions.mode = null;
93
						$scope.parsedContent = $scope.resource.content;
94
					}
95
				} catch(err) {
96
					$scope.showError('Error parsing content (' + $scope.format + ')');
97
					$scope.parsedContent = $scope.resource.content;
98
				}
99
	        }, 1000);
100
		}
101
		
102
		$scope.deleteResource = function() {
103
			if ($scope.resource && $scope.resource.id && confirm("Are you sure ?")) {
104
				$scope.showSpinner();
105
				$http.get('ajax_is/deleteDs.do?id=' + $scope.resource.id).success(function(data) {
106
					$scope.resource = {};
107
					$scope.parsedContent = '';
108
					$scope.hideSpinner();
109
					$scope.showNotification('Resource deleted !');
110
				}).error(function() {
111
					$scope.showError('Error deleting resource');
112
					$scope.hideSpinner();
113
				});
114
			}
115
		}
116

    
117
		$scope.saveContent = function() {
118
			try {
119
				if ($scope.format == 'JSON') {
120
					$scope.saveDs({
121
						'id'      : $scope.resource.id,
122
						'content' : JSON.stringify(JSON.parse($scope.parsedContent))
123
					});
124
				} else {
125
					$scope.saveDs({
126
						'id'      : $scope.resource.id,
127
						'content' : $scope.parsedContent
128
					});
129
				}
130
			} catch(err) {
131
				$scope.showError('ERROR: Invalid content (' + $scope.format + ')');
132
			}
133
		}
134
		
135
		$scope.saveInfo = function() {
136
			$scope.saveDs({
137
				'id'          : $scope.resource.id,
138
				'name'        : $scope.resource.name,
139
				'description' : $scope.resource.description
140
			});
141
		}
142
		
143
		$scope.saveDs = function(map) {
144
			$scope.showSpinner();
145
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
146
			$http.post('ajax_is/updateDs.do', $.param(map)).success(function(data) {
147
				$scope.hideSpinner();
148
				$scope.showNotification('Resource updated !');
149
				if (map.content) {
150
					$('#contentModal').modal('hide');
151
				}
152
				$scope.getResource($scope.resource.id);
153
			}).error(function(err) {
154
				$scope.showError(err.message);
155
				$scope.hideSpinner();
156
			});
157
		}
158
				
159
		$scope.getResource($routeParams.id);
160
	}
161
]);
162

    
163
isManagerControllers.controller('servicesCtrl', [
164
	'$scope', '$http', '$sce', '$location', '$routeParams',
165
    function ($scope, $http, $sce, $location, $routeParams) {
166
		common_init($scope, $http, $sce, $location);
167
		$scope.groups = [];
168
		
169
		$scope.showSpinner();
170
		$http.get('ajax_is/listServices.do').success(function(data) {
171
			$scope.hideSpinner();
172
			$scope.groups = data;
173
			angular.forEach($scope.groups, function(group) {
174
				angular.forEach(group.services, function(service) {
175
					service.status = 'UNKNOWN';
176
				});
177
			});
178
		}).error(function() {
179
			$scope.showError('Error obtaining services');
180
			$scope.hideSpinner();
181
		});
182
		
183
		$scope.pingServices = function() {
184
			var list = [];
185
			angular.forEach($scope.groups, function(group) {
186
				angular.forEach(group.services, function(service) {
187
					if (service.id && service.protocols && service.protocols.SOAP) {
188
						service.status = 'UNKNOWN';
189
						list.push(service);
190
					}
191
				});
192
			});
193
			$scope.ping(list, 0);
194
		}
195
		
196
		$scope.ping = function(list, pos) {
197
			if (list.length == 0)   { return; }
198
			if (pos >= list.length) { $scope.hideSpinner();	return;	}
199
			if (pos == 0)           { $scope.showSpinner(); }
200
			
201
			$http.get('ajax_is/ping.do?timeout=1500&url='+encodeURIComponent(list[pos].protocols.SOAP + '?wsdl')).success(function(data) {
202
				list[pos].status = 'ACTIVE';
203
				$scope.ping(list, pos + 1);
204
			}).error(function() {
205
				list[pos].status = 'NOT_RESPONDING';
206
				$scope.ping(list, pos + 1);
207
			});
208
		}
209
	}
210
]);
211

    
212
isManagerControllers.controller('serviceCtrl', [
213
	'$scope', '$http', '$sce', '$location', '$routeParams',
214
    function ($scope, $http, $sce, $location, $routeParams) {
215
		common_init($scope, $http, $sce, $location);
216
		
217
		$scope.service = {};
218
		
219
		$scope.showSpinner();
220
		$http.get('ajax_is/getService.do?id=' + $routeParams.id).success(function(data) {
221
			$scope.hideSpinner();
222
			$scope.service = data;
223
		}).error(function() {
224
			$scope.showError('Error obtaining service');
225
			$scope.hideSpinner();
226
		});
227
		
228
	}
229
]);
230

    
231
isManagerControllers.controller('isQueryCtrl', [
232
	'$scope', '$http', '$sce', '$location', '$routeParams', 'localStorageService',
233
	function ($scope, $http, $sce, $location, $routeParams, localStorageService) {
234
		common_init($scope, $http, $sce, $location);
235
		
236
		$scope.history = localStorageService.get('sql_query_history');
237
		if (!$scope.history) {
238
			$scope.history = [];
239
		}
240
		
241
		$scope.sqlEditorOptions = {
242
			lineWrapping : true,
243
			lineNumbers: true,
244
			mode: 'text/x-sql',
245
			matchBrackets : true,
246
		    extraKeys: {"Ctrl-Space": "autocomplete"}
247
		};
248
			
249
		$scope.sqlResultOptions = {
250
			lineWrapping : true,
251
			lineNumbers: true,
252
			readOnly: 'nocursor',
253
			mode: "application/json"
254
		};
255
		
256
		$scope.query = "select * from resource_types";
257
		$scope.error = null;
258
		$scope.results = [];
259

    
260
        $scope.search = function() {
261
			$scope.results = [];
262
			$scope.error = null;
263
			$scope.showSpinner();
264
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
265
			$http.post('ajax_is/query.do', $.param({
266
				'query' : $scope.query
267
			})).success(function(data) {
268
				$scope.hideSpinner();
269
				$scope.results = [];
270
				angular.forEach(data, function(d) {
271
					$scope.results.push(JSON.stringify(JSON.parse(d),null,2));
272
				});
273
				$scope.history.push({ 'date' : new Date(), 'query' : $scope.query });
274
				if ($scope.history.length > 100) {
275
					$scope.history = $scope.history.splice($scope.history.length - 100);
276
				}
277
				localStorageService.set('sql_query_history', $scope.history);
278
			}).error(function(err) {
279
				$scope.error = err;
280
				$scope.hideSpinner();
281
			});
282
		}
283
        
284
        $scope.updateSqlQuery = function(q) {
285
        	$scope.query = q;
286
        	$scope.search(); 
287
        }
288
        
289
        $scope.clearHistory = function() {
290
        	$scope.history = [];
291
        	localStorageService.set('sql_query_history', []);
292
        }
293
	}
294
]);
295

    
296
isManagerControllers.controller('bulkImporterCtrl', [
297
	'$scope', '$http', '$sce', '$location', '$routeParams',
298
	function ($scope, $http, $sce, $location, $routeParams) {
299
		common_init($scope, $http, $sce, $location);
300

    
301
		$scope.path = 'classpath*:/eu/dnetlib/enabling/is/setup/ds/**/*.xml';
302
		$scope.pkg = 'eu.dnetlib.enabling.datastructures';
303
		$scope.types = null;
304
		$scope.resources = null;
305
				
306
		$scope.importResources = function() {
307
			$scope.showSpinner();
308
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
309
			$http.post('ajax_is/importResources.do', $.param({
310
				'path' : $scope.path
311
			})).success(function(data) {
312
				$scope.hideSpinner();
313
				$scope.resources = data;
314
			}).error(function() {
315
				$scope.showError('Error registering resources');
316
				$scope.hideSpinner();
317
		    });
318
		}
319
		
320
		$scope.importTypes = function() {
321
			$scope.showSpinner();
322
			$http.get('ajax_is/importTypes.do?package=' + $scope.pkg).success(function(data) {
323
				$scope.hideSpinner();
324
				$scope.types = data;
325
			}).error(function() {
326
				$scope.showError('Error registering annotated types');
327
				$scope.hideSpinner();
328
		    });
329
		}	
330

    
331
	}
332
]);
333

    
334
isManagerControllers.controller('blackboardCtrl', [
335
	'$scope', '$http', '$sce', '$location', '$routeParams',
336
	function ($scope, $http, $sce, $location, $routeParams) {
337
		common_init($scope, $http, $sce, $location);
338
		
339
		$scope.refresh = function() {
340
			$scope.blackboards = [];
341
			$scope.showSpinner();
342
			$http.get('ajax_is/listBlackboards.do').success(function(data) {
343
				$scope.hideSpinner();
344
				$scope.blackboards = data;
345
			}).error(function() {
346
				$scope.showError('Error retreiving schemas');
347
				$scope.hideSpinner();
348
		    });
349
		}
350
		$scope.refresh();
351
	}
352
]);
(2-2/2)