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

    
245
			$scope.tempMdIdPath = '';
246
			angular.forEach($scope.currentRepoApi.otherParams, function(param, pos){
247
				if (param.name == 'metadata_identifier_path') {
248
					$scope.tempMdIdPath = param.value;
249
				}
250
			});
251
		}
252

    
253

    
254
		$scope.updateRepoApi = function() {
255
			$scope.showSpinner();
256

    
257
			var map = {};
258
			angular.forEach($scope.tempAccessParams, function(param, pos){
259
				if (param.name) {
260
					map[param.name] = param.value
261
				}
262
			});
263

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

    
279

    
280
		$scope.updateSets = function(selection, accessParams) {
281
			var s = selection.join();
282
			angular.forEach(accessParams, function(param, pos){
283
				if (param.name == 'set') {
284
					param.value = s;
285
				}
286
			});
287
		}
288

    
289
		$scope.listSets = function(accessParams) {
290
			var baseUrl = '';
291

    
292
			$scope.selectedSets = [];
293
			$scope.currentSets = [];
294

    
295
			angular.forEach(accessParams, function(param, pos){
296
				if (param.name == 'baseUrl') {
297
					baseUrl = param.value
298
				}
299
				if (param.name == 'set') {
300
					$scope.selectedSets = param.value.split(",");
301
				}
302
			});
303

    
304
			if (!baseUrl) {
305
				$scope.showError("baseUrl param is missing");
306
				return;
307
			}
308

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

    
330
		};
331

    
332

    
333
		$scope.verifyMetaWf = function(ds, wf) {
334
			var compliance = '';
335
			var type = '';
336
			angular.forEach(ds.commonParams, function(p,pos) {
337
				if (p.id == 'compliance') {
338
					compliance = p.value;
339
				}
340
				if (p.id == 'typology') {
341
					type = p.value;
342
				}
343
			});
344

    
345
			return (compliance && type && $scope.isValidTerm(compliance, wf.compliances) && $scope.isValidTerm(type, wf.ifaceTypes)); 
346
		}
347

    
348
		$scope.isValidTerm = function(term, list) {
349

    
350
			if (list.length == 0) {
351
				return true;
352
			}
353
			var res = false;
354
			angular.forEach(list, function(curr,pos) {
355
				if (term.lastIndexOf(curr, 0) === 0) { // implementation of term.startsWith(curr)
356
					res = true;
357
				}
358
			});
359
			return res;
360
		}
361

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

    
379
		$scope.resetApiCompliance = function() {
380
			$scope.showSpinner();
381
			$http.get('repoApiCompliance.reset?id=' + $scope.currentRepoApi.repoId + '&iface=' + $scope.currentRepoApi.id)
382
			.success(
383
					function(data) {
384
						$scope.hideSpinner();
385
						$scope.showNotification('Api correctly updated !');
386
						$scope.loadApi(); 
387
					}
388
			).error(
389
					function() {
390
						$scope.showError('Something really bad must have happened to our fellow hamster..');
391
						$scope.hideSpinner();
392
					}
393
			);
394
		}
395

    
396
		$scope.showCreateRemoteSyncApi = function(mongoUrl, mongoDB, mdId, apiSuffix) {
397

    
398
			var path = "//*[local-name()='header']/*[local-name()='identifier']";
399
			angular.forEach($scope.currentRepoApi.otherParams, function(p) {
400
				if (p.id=='metadata_identifier_path') {
401
					path = p.value;
402
				}
403
			});
404

    
405
			var compliance = "UNKNOWN";
406
			angular.forEach($scope.currentRepoApi.commonParams, function(p) {
407
				if (p.id=='compliance') {
408
					compliance = p.value;
409
				}
410
			});
411
			
412
			var typology = "UNKNOWN";
413
			angular.forEach($scope.currentRepoApi.commonParams, function(p) {
414
				if (p.id=='typology') {
415
					typology = p.value;
416
				}
417
			});
418

    
419
			$scope.showSpinner();
420
			$http.get('remoteDatasource/apis?repo=' + $routeParams.repoId)
421
			.success(
422
					function(data) {
423
						$scope.hideSpinner();
424

    
425
						var alreadyPresent = false;
426
						var apiId = 'api_________::' + $routeParams.repoId + "::" + apiSuffix;
427

    
428
						if(data.api) {
429
							angular.forEach(data.api, function(api) {
430
								if (api.id == apiId) {
431
									alreadyPresent = true;
432
									$scope.syncApi = api;
433
								}
434
							});
435
						}
436

    
437
						if (alreadyPresent) {
438
							$scope.syncShowRemoteMessage = "The synchronization API has been already registered on";
439
							$('#showRemoteSyncApiModal').modal('show');
440
						} else {
441
							$scope.syncApi = {
442
									"datasource": $routeParams.repoId,
443
									"id": apiId,
444
									"protocol": "remoteMdstore",
445
									"contentdescription": "metadata",
446
									"typology": typology,
447
									"compatibility": compliance,
448
									"baseurl": mongoUrl,
449
									"removable": true,
450
									"metadataIdentifierPath": path,
451
									"apiParams": [
452
										{
453
											"param": "remote_database",
454
											"value": mongoDB
455
										},
456
										{
457
											"param": "remote_mdstore_id",
458
											"value": mdId
459
										}
460
									]
461
							};
462
							$('#newRemoteSyncApiModal').modal('show');	        				
463
						}
464
					}
465
			).error(
466
					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
		}
473
		
474
		$scope.createRemoteSyncApi = function(api) {
475
			$scope.showSpinner();
476

    
477
			$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
478
			$http.post('remoteDatasource/api', api).success(function(data) {
479
				$scope.hideSpinner();
480
				$scope.syncShowRemoteMessage = "The synchronization API has been created on";
481
				$('#showRemoteSyncApiModal').modal('show');
482
			}).error(function(err) {
483
				$scope.showError("Error registering the API: probably the datasource has not been registered on the remote infrastructure.");
484
				$scope.hideSpinner();
485
			});
486
		}
487

    
488
		$scope.loadApi();
489
	}
490

    
491
	]);
492

    
(3-3/7)