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('/similarities/:id',                 { templateUrl: 'resources/html/similarities.html', controller: 'showSimilaritiesCtrl' })
15
		.when('/warnings',                         { templateUrl: 'resources/html/warnings.html',     controller: 'showWarningsCtrl' })
16
		.when('/users',                            { templateUrl: 'resources/html/users.html',        controller: 'usersCtrl' })
17
		.otherwise({ redirectTo: '/search' });
18
});
19

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

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

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

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

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

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

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

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

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

    
140

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

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

    
180

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

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

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

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

    
272

    
273
orgsModule.controller('showWarningsCtrl', function ($scope, $http, $routeParams) {
274
	$scope.orgId = $routeParams.id;
275
	$scope.warnings = [];
276
	
277
	$http.get('api/organizations/warnings?id=' + $routeParams.id).then(function successCallback(res) {
278
		$scope.warnings = res.data;
279
	}, function errorCallback(res) {
280
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
281
	});
282

    
283
});
284

    
285

    
286
orgsModule.controller('usersCtrl', function ($scope, $http, $timeout, $route) {
287
	$scope.users = [];
288
	$scope.vocs = {};
289
	$scope.currentUser = {};
290
	
291
	$http.get('api/vocabularies').then(function successCallback(res) {
292
		$scope.vocs = res.data;
293

    
294
		$http.get('api/users').then(function successCallback(res) {
295
			$scope.users = res.data;
296
		}, function errorCallback(res) {
297
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
298
		});
299
	}, function errorCallback(res) {
300
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
301
	});
302
	
303
	$scope.setCurrentUser = function(user) {
304
		angular.copy(user, $scope.currentUser);
305
	}
306
	
307
	$scope.saveUser = function(user) {
308
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
309
		$http.post('api/users', user).then(function successCallback(res) {
310
			$scope.users = res.data;
311
		}, function errorCallback(res) {
312
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
313
		});
314
	}
315
	
316
	$scope.deleteUser = function(email) {
317
		if (confirm("Are you sure ?")) {
318
			$http.delete('api/users?email=' + email).then(function successCallback(res) {
319
				$scope.users = res.data;
320
			}, function errorCallback(res) {
321
				alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
322
			});
323
		}
324
	}
325
});
(10-10/12)