Project

General

Profile

1
var orgsModule = angular.module('orgs', ['ngRoute', 'checklist-model']);
2

    
3
orgsModule.config(function($routeProvider) {
4
	$routeProvider
5
		.when('/new',                              { templateUrl: 'resources/html/edit.html',         controller: 'newOrgCtrl' })
6
		.when('/search',                           { templateUrl: 'resources/html/search.html',       controller: 'searchCtrl' })
7
		.when('/searchResults/:page/:size/:text*', { templateUrl: 'resources/html/list.html',         controller: 'searchResultsCtrl' })
8
		.when('/countries',                        { templateUrl: 'resources/html/browse.html',       controller: 'countriesCtrl' })
9
		.when('/byCountry/:page/:size/:code*',     { templateUrl: 'resources/html/list.html',         controller: 'byCountryCtrl' })
10
		.when('/types',                            { templateUrl: 'resources/html/browse.html',       controller: 'typesCtrl' })
11
		.when('/byType/:page/:size/:type*',        { templateUrl: 'resources/html/list.html',         controller: 'byTypeCtrl' })
12
		.when('/metadata/:msg/:id',                { templateUrl: 'resources/html/edit.html',         controller: 'showMetadataCtrl' })
13
		.when('/relations/:id',                    { templateUrl: 'resources/html/relations.html',    controller: 'showRelationsCtrl' })
14
		.when('/enrichments/:id',                  { templateUrl: 'resources/html/enrichments.html',  controller: 'showEnrichmentsCtrl' })
15
		.when('/conflicts/:id',                    { templateUrl: 'resources/html/conflicts.html',    controller: 'showConflictsCtrl' })
16
		.when('/allConflicts',                     { templateUrl: 'resources/html/allConflicts.html', controller: 'showAllConflictsCtrl' })
17
		.when('/users',                            { templateUrl: 'resources/html/users.html',        controller: 'usersCtrl' })
18
		.otherwise({ redirectTo: '/search' });
19
});
20

    
21
orgsModule.filter('escape', function() {
22
	return function(input) {
23
		return encodeURIComponent(encodeURIComponent(input));
24
	}; 
25
});
26

    
27
orgsModule.filter('unescape', function() {
28
	return function(input) {
29
		return decodeURIComponent(input);
30
	};
31
});
32

    
33
orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $location) {
34
	$scope.org = {
35
			"id": "",
36
			"name": "",
37
			"type": null,
38
			"lat": 0.0,
39
			"lng": 0.0,
40
			"city": "",
41
			"country": "",
42
			"source": "user",
43
			"otherIdentifiers": [],
44
			"otherNames": [],
45
			"relations": [],
46
			"acronyms": [],
47
			"urls": []
48
		};
49
	$scope.vocabularies = {};
50
	
51
	$http.get('api/vocabularies').then(function successCallback(res) {
52
		$scope.vocabularies = res.data;
53
	}, function errorCallback(res) {
54
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
55
	});
56
	
57
	$scope.save = function() {
58
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
59
		$http.post('api/organizations/save', $scope.org).then(function successCallback(res) {
60
//			alert('Organization saved !!!');
61
			$location.url('/metadata/1/' + res.data[0]);
62
		}, function errorCallback(res) {
63
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
64
		});
65
	}
66
	
67
});
68

    
69
orgsModule.controller('searchCtrl', function ($scope, $location) {
70
	$scope.textSearch = '';
71
	$scope.search = function() {
72
		$location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.textSearch)));
73
	}
74
});
75

    
76
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams) {
77
	$scope.basepath = '/searchResults';
78
	$scope.fieldValue = decodeURIComponent($routeParams.text);
79
	
80
	$scope.orgs = {};
81
	
82
	$http.get('api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.fieldValue).then(function successCallback(res) {
83
		$scope.orgs = res.data;
84
	}, function errorCallback(res) {
85
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
86
	});
87
});
88

    
89
orgsModule.controller('countriesCtrl', function ($scope, $http, $routeParams) {
90
	$scope.field = 'Country';
91
	$scope.resultsBasePath = '/byCountry'
92
	$scope.entries = [];
93
	
94
	$http.get('api/organizations/browse/countries').then(function successCallback(res) {
95
		$scope.entries = res.data;
96
	}, function errorCallback(res) {
97
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
98
	});
99
	
100
});
101

    
102
orgsModule.controller('byCountryCtrl', function ($scope, $http, $routeParams) {
103
	$scope.basepath = '/byCountry';
104
	$scope.fieldValue = $routeParams.code;
105
	
106
	$scope.orgs = [];
107
	
108
	$http.get('api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
109
		$scope.orgs = res.data;
110
	}, function errorCallback(res) {
111
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
112
	});
113
});
114

    
115
orgsModule.controller('typesCtrl', function ($scope, $http, $routeParams) {
116
	$scope.field = 'Organization type';
117
	$scope.resultsBasePath = '/byType'
118
	$scope.entries = [];
119
	
120
	$http.get('api/organizations/browse/types').then(function successCallback(res) {
121
		$scope.entries = res.data;
122
	}, function errorCallback(res) {
123
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
124
	});
125
	
126
});
127

    
128
orgsModule.controller('byTypeCtrl', function ($scope, $http, $routeParams) {
129
	$scope.basepath = '/byType';
130
	$scope.fieldValue = $routeParams.type;
131
	
132
	$scope.orgs = [];
133
	
134
	$http.get('api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
135
		$scope.orgs = res.data;
136
	}, function errorCallback(res) {
137
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
138
	});
139
});
140

    
141

    
142
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams, $route, $location, $timeout) {
143
	$scope.orgId = $routeParams.id;
144
	$scope.org = {};
145
	$scope.vocabularies = {};
146
	
147
	if ($routeParams.msg == 1) {
148
		$scope.message = 'New organization registered !!!';
149
	} else if ($routeParams.msg == 2) {
150
		$scope.message = 'Organization updated !!!';
151
	} else {
152
		$scope.message = '';
153
	}	
154
	
155
	$timeout(function() { $scope.message = ''; }, 3000)
156
		
157
	$http.get('api/vocabularies').then(function successCallback(res) {
158
		$scope.vocabularies = res.data;
159

    
160
		$http.get('api/organizations/get?id=' + $routeParams.id).then(function successCallback(res) {
161
			$scope.org = res.data;
162
		}, function errorCallback(res) {
163
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
164
		});
165
	}, function errorCallback(res) {
166
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
167
	});
168
	
169
	$scope.save = function() {
170
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
171
		$http.post('api/organizations/save', $scope.org).then(function successCallback(res) {
172
			if ($routeParams.msg == 2) { $route.reload(); }
173
			else                       { $location.url('/metadata/2/' + res.data[0]); }
174
		}, function errorCallback(res) {
175
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
176
		});
177
	}
178
	
179
});
180

    
181

    
182
orgsModule.controller('showRelationsCtrl', function ($scope, $http, $routeParams) {
183
	$scope.orgId = $routeParams.id;
184
	$scope.rels = [];
185
	$scope.vocabularies = {};
186
	
187
	$scope.searchValue = '';
188
	$scope.searchOrgs = [];
189
	$scope.newRelType='';
190
	$scope.newRelation = {};
191
	
192
	$http.get('api/vocabularies').then(function successCallback(res) {
193
		$scope.vocabularies = res.data;
194

    
195
		$http.get('api/organizations/relations?id=' + $routeParams.id).then(function successCallback(res) {
196
			$scope.rels = res.data;
197
		}, function errorCallback(res) {
198
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
199
		});
200
	}, function errorCallback(res) {
201
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
202
	});
203
	
204
	$scope.prepareNewRelation = function(o) {
205
		$scope.newRelation = o;
206
	}
207
		
208
	$scope.addRelation = function() {
209
		$http.put('api/organizations/relations', null, { 
210
			'params': { 'from': $scope.orgId, 'to': $scope.newRelation.id, 'type': $scope.newRelType }
211
		}).then(function successCallback(res) {
212
			$scope.rels = res.data;
213
			$scope.newRelType = '';
214
			$scope.newRelation = {};
215
//			alert("Relation added !!!")
216
		}, function errorCallback(res) {
217
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
218
		});
219
	}
220
	
221
	$scope.deleteRelation = function(to, type) {
222
		$http.delete('api/organizations/relations', { 
223
			'params': { 'from': $scope.orgId, 'to': to, 'type': type }
224
		}).then(function successCallback(res) {
225
			$scope.rels = res.data;
226
//			alert("Relation deleted !!!")
227
		}, function errorCallback(res) {
228
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
229
		});
230
	}
231
	
232
	$scope.search = function(text, page, size) {
233
		$scope.orgs = [];
234
		$http.get('api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) {
235
			$scope.searchValue = text;
236
			$scope.searchOrgs = res.data;
237
		}, function errorCallback(res) {
238
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
239
		});
240
	}
241
	
242
});
243

    
244
orgsModule.controller('showEnrichmentsCtrl', function ($scope, $http, $routeParams) {
245
	$scope.orgId = $routeParams.id;
246
	$scope.enrichments = [];
247
	$scope.vocabularies = {};
248
	
249
	$http.get('api/vocabularies').then(function successCallback(res) {
250
		$scope.vocabularies = res.data;
251

    
252
		$http.get('api/organizations/enrichments?id=' + $routeParams.id).then(function successCallback(res) {
253
			$scope.enrichments = res.data;
254
		}, function errorCallback(res) {
255
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
256
		});
257
	}, function errorCallback(res) {
258
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
259
	});
260
	
261
	
262
	$scope.saveEnrichments = function() {
263
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
264
		$http.post('api/organizations/enrichments', $scope.enrichments).then(function successCallback(res) {
265
			alert('Enrichments updated !!!');
266
			$scope.enrichments = res.data;
267
		}, function errorCallback(res) {
268
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
269
		});
270
	}
271
});
272

    
273
orgsModule.controller('showConflictsCtrl', function ($scope, $http, $routeParams) {
274
	$scope.orgId = $routeParams.id;
275
	$scope.conflicts = [];
276
	$scope.vocabularies = {};
277
	
278
	$http.get('api/vocabularies').then(function successCallback(res) {
279
		$scope.vocabularies = res.data;
280

    
281
		$http.get('api/organizations/conflicts?id=' + $routeParams.id).then(function successCallback(res) {
282
			$scope.conflicts = res.data;
283
		}, function errorCallback(res) {
284
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
285
		});
286
	}, function errorCallback(res) {
287
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
288
	});
289
});
290

    
291
orgsModule.controller('showAllConflictsCtrl', function ($scope, $http) {
292
	$scope.conflicts = [];
293
	$http.get('api/organizations/conflicts/all').then(function successCallback(res) {
294
		$scope.conflicts = res.data;
295
	}, function errorCallback(res) {
296
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
297
	});
298
});
299

    
300
orgsModule.controller('usersCtrl', function ($scope, $http, $timeout, $route) {
301
	$scope.users = [];
302
	$scope.vocs = {};
303
	$scope.currentUser = {};
304
	$scope.superAdminMode = superAdminMode();
305
	
306
	$http.get('api/vocabularies').then(function successCallback(res) {
307
		$scope.vocs = res.data;
308

    
309
		$http.get('api/users').then(function successCallback(res) {
310
			$scope.users = res.data;
311
		}, function errorCallback(res) {
312
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
313
		});
314
	}, function errorCallback(res) {
315
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
316
	});
317
	
318
	$scope.setCurrentUser = function(user) {
319
		angular.copy(user, $scope.currentUser);
320
		$scope.currentUser.role = 'USER';
321
	}
322
	
323
	$scope.saveUser = function(user) {
324
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
325
		$http.post('api/users', user).then(function successCallback(res) {
326
			$scope.users = res.data;
327
		}, function errorCallback(res) {
328
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
329
		});
330
	}
331
	
332
	$scope.deleteUser = function(email) {
333
		if (confirm("Are you sure ?")) {
334
			$http.delete('api/users?email=' + email).then(function successCallback(res) {
335
				$scope.users = res.data;
336
			}, function errorCallback(res) {
337
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
338
			});
339
		}
340
	}
341
});
(10-10/12)