Project

General

Profile

1
var repoControllers = angular.module('repoControllers', []);
2

    
3
repoControllers.controller('repoBrowseCtrl', [
4
	'$scope', '$http', '$timeout', '$sce', '$location',
5
	function ($scope, $http, $timeout, $sce, $location) {
6
		common_init($scope, $http, $sce, $location);
7
		$scope.repoBrowseFields = getBrowseFields();
8

    
9
		$scope.sortType     = 'term';
10
		$scope.sortReverse  = false;
11

    
12
		$scope.repoBrowseData = { 'data' : [] };
13

    
14
		$scope.searchApis = function (text) {
15
			$scope.go('/list/__SEARCH__/' + text); 
16
		} 
17

    
18
		$scope.browseApis = function (field, value) {
19
			$('#showRepoBrowseData').modal('hide');
20
			$scope.showSpinner();
21
			$timeout(function() {
22
				$scope.go('/list/' + field + '/' + value);
23
			}, 1500);
24
		}
25

    
26
		$scope.browseRepoField = function(field) {
27
			$scope.repoBrowseData = {
28
					'label' : field.label,
29
					'id' :    field.id,
30
					'data'  : []
31
			};
32
			$scope.showSpinner();
33
			$http.get('browseRepoField.do?field=' + field.id).success(
34
					function(data) {
35
						$scope.hideSpinner();
36
						$scope.repoBrowseData.data = data;
37
						$('#showRepoBrowseData').modal('show');
38
					}
39
			).error(
40
					function() {
41
						$scope.showError('Something really bad must have happened to our fellow hamster..');
42
						$scope.hideSpinner();
43
					}
44
			);
45
		};
46
	}
47
	]);
48

    
49
repoControllers.controller('repoListCtrl', [
50
	'$scope', '$http', '$timeout', '$sce', '$routeParams', '$location',
51
	function ($scope, $http, $timeout, $sce, $routeParams, $location) {
52
		common_init($scope, $http, $sce, $location);
53

    
54
		$scope.sortType     = 'repoName';
55
		$scope.sortReverse  = false;
56
		$scope.apis = [];		
57
		$scope.currentApiSelectionParam = $routeParams.param;
58
		$scope.currentApiSelectionValue = $routeParams.value;
59

    
60
		$scope.updateApis = function(refresh) {
61
			$scope.showSpinner();
62

    
63
			var params = {
64
					'param' : $scope.currentApiSelectionParam,
65
					'value' : $scope.currentApiSelectionValue
66
			};
67
			if (refresh) {
68
				params.refresh = 1;
69
			}
70

    
71
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
72
			$http.post('listApis.do', $.param(params)).success(
73
					function(data) {
74
						$scope.hideSpinner();
75
						$scope.apis = data;
76
					}
77
			).error(
78
					function() {
79
						$scope.showError('Something really bad must have happened to our fellow hamster..');
80
						$scope.hideSpinner();
81
					}
82
			);
83
		};
84

    
85
		$scope.updateApis(false);
86
	}
87
	]);
88

    
89
repoControllers.controller('repoApiCtrl', [
90
	'$scope', '$http', '$timeout', '$sce', '$routeParams', '$location',
91
	function ($scope, $http, $timeout, $sce, $routeParams, $location) {
92
		common_init($scope, $http, $sce, $location);
93

    
94
		commonInitialize($scope, $http, $sce);
95
		wf_init($scope, $http, $sce);
96

    
97
		$scope.availableRepohiWfs = getAvailableRepohiWfs();
98
		$scope.compatibilityLevels = getCompatibilityLevels();
99

    
100
		$scope.validatorBlacklistAddress = '';
101
		$scope.validatorBlacklistAddress = '';
102

    
103
		$scope.currentRepoApi = {};
104
		$scope.currentSets = [];
105
		$scope.selectedSets = [];
106

    
107
		$scope.syncApi = {};
108
		$scope.syncShowRemoteMessage = "";
109

    
110
		$scope.loadApi = function() {
111
			$scope.showSpinner();
112
			$(".btnRefreshApi").attr("disabled", "disabled");
113
			$http.get('repoApi.get?repoId=' + $routeParams.repoId + "&ifaceId=" +  $routeParams.ifaceId)
114
			.success(
115
					function(data) {
116
						$scope.currentRepoApi = data;
117
						$scope.metaworkflows = data.metaWFs;
118

    
119
						angular.forEach(data.otherParams, function(term){
120
							if (term.id == 'last_validation_job') {
121
								if (getValidatorAddress()) {
122
									$scope.validatorDetailsAddress = getValidatorAddress() + "/" + term.value;
123
								}
124
								if (getValidatorServiceAddress()) {
125
									$scope.validatorBlacklistAddress = getValidatorServiceAddress() + "/workflows?request=GetBlacklistedRecords&datasourceId=" + $routeParams.repoId;
126
								}
127
							}                                       
128
						});
129

    
130
						if (data.protocol == 'oai') {
131
							var found = false;
132
							angular.forEach(data.accessParams, function(param, pos){
133
								if (param.name == 'set') {
134
									$scope.listSets($scope.currentRepoApi.accessParams);
135
								}
136
							});
137
						}
138
						$scope.hideSpinner(); 
139

    
140
						$(".btnRefreshApi").removeAttr("disabled");
141

    
142
						if ($routeParams.metawf != 'ALL' && $routeParams.wf != 'ALL') {
143
							$scope.getAtomicWorkflowAndMetaWf($routeParams.metawf, $routeParams.wf);
144
							$timeout(function() {
145
								$("#collapse_" + $routeParams.metawf.substring(0, 36)).collapse('show');
146
							}, 2000);
147
						}
148
					}
149
			).error(
150
					function() {
151
						$scope.showError('Something really bad must have happened to our fellow hamster..');
152
						$scope.hideSpinner();
153
					}
154
			);
155
		};
156

    
157

    
158
		$scope.removeApi = function() {
159
			if (confirm("You are deleting the current API.\n\nAre you sure?")) {
160
				$scope.showSpinner();
161
				$http.get('repoApi.delete?repo=' + $routeParams.repoId + "&iface=" +  $routeParams.ifaceId).success(function(data) {
162
					alert("The API has been deleted !!!");
163
					$scope.hideSpinner();
164
					$scope.go("/browse");
165
				}).error(function(err) {
166
					$scope.showError('Error removing API: '  + err.message);
167
					$scope.hideSpinner();
168
				});
169
			}
170
		};
171

    
172

    
173
		$scope.newDataProviderWorkflow = function (wf) {
174

    
175
			if (wf == undefined) {
176
				$scope.showError('Please choose a workflow !');
177
				return;
178
			}
179

    
180
			$scope.showSpinner();
181

    
182
			$scope.metaworkflows.push({"id":"","name":"Waiting...","status":"MISSING","progress":0});
183

    
184
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
185
			$http.post('repoMetaWf.new', $.param({
186
				'wf'    : wf,
187
				'iface' : $scope.currentRepoApi.id,
188
				'id'    : $scope.currentRepoApi.repoId, 
189
				'name'  : $scope.currentRepoApi.repoName
190
			})).success(
191
					function(data) {
192
						$scope.hideSpinner();
193
						$(".btnRefreshApi").attr("disabled", "disabled");
194
						$timeout(function () { $scope.loadApi() }, 7000);
195
					}
196
			).error(
197
					function() {
198
						$scope.showError('Something really bad must have happened to our fellow hamster..');
199
						$scope.hideSpinner();
200
					}
201
			);
202
		};
203

    
204
		$scope.destroyDataProviderWorkflow = function (metaWf) {
205

    
206
			if (metaWf.destroyWorkflow == undefined) {
207
				$scope.showError('Destroy wf is missing !');
208
				return;
209
			}
210

    
211
			if (!confirm("Are you sure ?")) {
212
				return;
213
			}
214

    
215
			$scope.showSpinner();
216

    
217
			metaWf.deleting = true;
218

    
219
			$http.get('repoMetaWf.destroy?destroyWf=' + metaWf.destroyWorkflow)
220
			.success(
221
					function(data) {
222
						$scope.hideSpinner();
223
						$(".btnRefreshApi").attr("disabled", "disabled");
224
						$timeout(function () { $scope.loadApi() }, 7000);
225
					}
226
			).error(
227
					function() {
228
						$scope.showError('Something really bad must have happened to our fellow hamster..');
229
						$scope.hideSpinner();
230
					}
231
			);
232
		};
233

    
234
		$scope.testOAI = function (params) {
235
			angular.forEach(params, function(param, pos){
236
				if (param.name == 'baseUrl') {
237
					location.href = "oaiExplorer.do?oaiBaseUrl=" + param.value;
238
				}
239
			});
240
		}
241

    
242
		$scope.prepareUpdateRepoApi = function() {
243
			$scope.tempAccessParams = angular.copy($scope.currentRepoApi.accessParams); 
244
			$scope.tempMdIdPath = $scope.currentRepoApi.metadataIdentifierPath;
245
		}
246

    
247

    
248
		$scope.updateRepoApi = function() {
249
			$scope.showSpinner();
250

    
251
			var map = {};
252
			angular.forEach($scope.tempAccessParams, function(param, pos){
253
				if (param.name) {
254
					map[param.name] = param.value
255
				}
256
			});
257

    
258
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
259
			$http.post('repoApi.update', $.param({
260
				'id'           : $scope.currentRepoApi.repoId,
261
				'iface'        : $scope.currentRepoApi.id,
262
				'mdIdPath'     : $scope.tempMdIdPath,
263
				'accessParams' : JSON.stringify(map)
264
			})).success(function(data) {
265
				$scope.showNotification('Api correctly saved !');
266
				$scope.loadApi(); 
267
			}).error(function(err) {
268
				$scope.hideSpinner();
269
				$scope.showError('Error updating api: ' + err.message);
270
			});
271
		};
272

    
273

    
274
		$scope.updateSets = function(selection, accessParams) {
275
			var s = selection.join();
276
			angular.forEach(accessParams, function(param, pos){
277
				if (param.name == 'set') {
278
					param.value = s;
279
				}
280
			});
281
		}
282

    
283
		$scope.listSets = function(accessParams) {
284
			var baseUrl = '';
285

    
286
			$scope.selectedSets = [];
287
			$scope.currentSets = [];
288

    
289
			angular.forEach(accessParams, function(param, pos){
290
				if (param.name == 'baseUrl') {
291
					baseUrl = param.value
292
				}
293
				if (param.name == 'set') {
294
					$scope.selectedSets = param.value.split(",");
295
				}
296
			});
297

    
298
			if (!baseUrl) {
299
				$scope.showError("baseUrl param is missing");
300
				return;
301
			}
302

    
303
			$scope.showSpinner();
304
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
305
			$http.post("listValidValuesForParam.do", $.param({
306
				'protocol' : 'oai',
307
				'param'    : 'set',
308
				'baseUrl'  : baseUrl
309
			})).success(function(data) {
310
				$scope.hideSpinner();
311
				$scope.currentSets = data;
312
				angular.forEach($scope.currentSets, function(curr, pos1) {
313
					angular.forEach($scope.selectedSets, function(sel, pos2) {
314
						if (curr.id.trim() == sel.trim()) {
315
							curr.selected = true;
316
						}
317
					});
318
				});
319
			}).error(function(err) {
320
				$scope.hideSpinner();
321
				$scope.showError(err.message);
322
			});
323

    
324
		};
325

    
326

    
327
		$scope.verifyMetaWf = function(ds, wf) {
328
			var compliance = ds.compliance;
329
			var type = ds.repoType;
330
			return (compliance && type && $scope.isValidTerm(compliance, wf.compliances) && $scope.isValidTerm(type, wf.ifaceTypes)); 
331
		}
332

    
333
		$scope.isValidTerm = function(term, list) {
334

    
335
			if (list.length == 0) {
336
				return true;
337
			}
338
			var res = false;
339
			angular.forEach(list, function(curr,pos) {
340
				if (term.lastIndexOf(curr, 0) === 0) { // implementation of term.startsWith(curr)
341
					res = true;
342
				}
343
			});
344
			return res;
345
		}
346

    
347
		$scope.overrideApiCompliance = function(level) {
348
			$scope.showSpinner();
349
			$http.get('repoApiCompliance.update?id=' + $scope.currentRepoApi.repoId + '&iface=' + $scope.currentRepoApi.id + '&compliance=' + level)
350
			.success(
351
					function(data) {
352
						$scope.hideSpinner();
353
						$scope.showNotification('Api correctly updated !');
354
						$scope.loadApi(); 
355
					}
356
			).error(
357
					function() {
358
						$scope.showError('Something really bad must have happened to our fellow hamster..');
359
						$scope.hideSpinner();
360
					}
361
			);
362
		}
363

    
364
		$scope.resetApiCompliance = function() {
365
			$scope.showSpinner();
366
			$http.get('repoApiCompliance.reset?id=' + $scope.currentRepoApi.repoId + '&iface=' + $scope.currentRepoApi.id)
367
			.success(
368
					function(data) {
369
						$scope.hideSpinner();
370
						$scope.showNotification('Api correctly updated !');
371
						$scope.loadApi(); 
372
					}
373
			).error(
374
					function() {
375
						$scope.showError('Something really bad must have happened to our fellow hamster..');
376
						$scope.hideSpinner();
377
					}
378
			);
379
		}
380

    
381
		$scope.showCreateRemoteSyncApi = function(mongoUrl, mongoDB, mdId, apiSuffix) {
382

    
383
			var path = $scope.currentRepoApi.metadataIdentifierPath;
384
			
385
			if (!path) {
386
				path = "//*[local-name()='header']/*[local-name()='identifier']";
387
			}
388

    
389
			var compliance = "UNKNOWN";
390
			angular.forEach($scope.currentRepoApi.commonParams, function(p) {
391
				if (p.id=='compliance') {
392
					compliance = p.value;
393
				}
394
			});
395
			
396
			var typology = "UNKNOWN";
397
			angular.forEach($scope.currentRepoApi.commonParams, function(p) {
398
				if (p.id=='typology') {
399
					typology = p.value;
400
				}
401
			});
402

    
403
			$scope.showSpinner();
404
			$http.get('remoteDatasource/apis?repo=' + $routeParams.repoId)
405
			.success(
406
					function(data) {
407
						$scope.hideSpinner();
408

    
409
						var alreadyPresent = false;
410
						var apiId = 'api_________::' + $routeParams.repoId + "::" + apiSuffix;
411

    
412
						if(data.api) {
413
							angular.forEach(data.api, function(api) {
414
								if (api.id == apiId) {
415
									alreadyPresent = true;
416
									$scope.syncApi = api;
417
								}
418
							});
419
						}
420

    
421
						if (alreadyPresent) {
422
							$scope.syncShowRemoteMessage = "The synchronization API has been already registered on";
423
							$('#showRemoteSyncApiModal').modal('show');
424
						} else {
425
							$scope.syncApi = {
426
									"datasource": $routeParams.repoId,
427
									"id": apiId,
428
									"protocol": "remoteMdstore",
429
									"contentdescription": "metadata",
430
									"typology": typology,
431
									"compatibility": compliance,
432
									"baseurl": mongoUrl,
433
									"removable": true,
434
									"metadataIdentifierPath": path,
435
									"apiParams": [
436
										{
437
											"param": "remote_database",
438
											"value": mongoDB
439
										},
440
										{
441
											"param": "remote_mdstore_id",
442
											"value": mdId
443
										}
444
									]
445
							};
446
							$('#newRemoteSyncApiModal').modal('show');	        				
447
						}
448
					}
449
			).error(
450
					function(err) {
451
						$scope.showError("Error registering the API: probably the datasource has not been registered on the remote infrastructure.");
452
						$scope.hideSpinner();
453
					}
454
			);
455

    
456
		}
457
		
458
		$scope.createRemoteSyncApi = function(api) {
459
			$scope.showSpinner();
460

    
461
			$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
462
			$http.post('remoteDatasource/api', api).success(function(data) {
463
				$scope.hideSpinner();
464
				$scope.syncShowRemoteMessage = "The synchronization API has been created on";
465
				$('#showRemoteSyncApiModal').modal('show');
466
			}).error(function(err) {
467
				$scope.showError("Error registering the API: probably the datasource has not been registered on the remote infrastructure.");
468
				$scope.hideSpinner();
469
			});
470
		}
471

    
472
		$scope.loadApi();
473
	}
474

    
475
	]);
476

    
(3-3/7)