1
|
package eu.dnetlib.uoamonitorservice.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
4
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
5
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
6
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
7
|
import eu.dnetlib.uoamonitorservice.handlers.utils.RolesUtils;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.security.access.AccessDeniedException;
|
11
|
import org.springframework.security.access.prepost.PreAuthorize;
|
12
|
import org.springframework.web.bind.annotation.*;
|
13
|
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.Date;
|
16
|
import java.util.Iterator;
|
17
|
import java.util.List;
|
18
|
|
19
|
@RestController
|
20
|
@CrossOrigin(origins = "*")
|
21
|
public class TopicController {
|
22
|
private final Logger log = Logger.getLogger(this.getClass());
|
23
|
|
24
|
@Autowired
|
25
|
private RolesUtils rolesUtils;
|
26
|
|
27
|
@Autowired
|
28
|
private StakeholderDAO stakeholderDAO;
|
29
|
|
30
|
@Autowired
|
31
|
private TopicDAO topicDAO;
|
32
|
|
33
|
@Autowired
|
34
|
private CategoryController categoryController;
|
35
|
|
36
|
public Topic<Category> buildTopic(Topic<Category> topicFull) {
|
37
|
Topic<String> topic = new Topic<>(topicFull);
|
38
|
|
39
|
List<String> categories = new ArrayList<>();
|
40
|
List<Category> categoriesFull = new ArrayList<>();
|
41
|
for(Category<SubCategory> category : topicFull.getCategories()) {
|
42
|
Category<SubCategory> categoryFull = categoryController.buildCategory(category);
|
43
|
categoriesFull.add(categoryFull);
|
44
|
categories.add(categoryFull.getId());
|
45
|
}
|
46
|
topicFull.setCategories(categoriesFull);
|
47
|
topic.setCategories(categories);
|
48
|
|
49
|
Date date = new Date();
|
50
|
topic.setCreationDate(date);
|
51
|
topic.setUpdateDate(date);
|
52
|
|
53
|
topicFull.setCreationDate(date);
|
54
|
topicFull.setUpdateDate(date);
|
55
|
|
56
|
topicDAO.save(topic);
|
57
|
|
58
|
topicFull.setId(topic.getId());
|
59
|
return topicFull;
|
60
|
}
|
61
|
|
62
|
@PreAuthorize("isAuthenticated()")
|
63
|
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
|
64
|
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
|
65
|
@RequestBody Topic<Category> topicFull) {
|
66
|
log.debug("save topic");
|
67
|
log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId);
|
68
|
|
69
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
70
|
|
71
|
if(stakeholder != null) {
|
72
|
List<String> roles = rolesUtils.getRoles();
|
73
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
74
|
// EXCEPTION - Access denied
|
75
|
throw new AccessDeniedException("Save Topic: You are not authorized to update stakeholder with id: "+stakeholderId);
|
76
|
}
|
77
|
|
78
|
Topic<String> topic = new Topic<>(topicFull);
|
79
|
Date date = new Date();
|
80
|
topic.setUpdateDate(date);
|
81
|
topicFull.setUpdateDate(date);
|
82
|
|
83
|
Topic<String> oldTopic = null;
|
84
|
if(topicFull.getId() != null) {
|
85
|
oldTopic = topicDAO.findById(topicFull.getId());
|
86
|
} else { // topic does not exist in DB
|
87
|
topic.setCreationDate(date);
|
88
|
topicFull.setCreationDate(date);
|
89
|
}
|
90
|
|
91
|
List<String> categories = new ArrayList<>();
|
92
|
for(Category category : topicFull.getCategories()) {
|
93
|
categories.add(category.getId());
|
94
|
}
|
95
|
topic.setCategories(categories);
|
96
|
|
97
|
if(stakeholder.getDefaultId() == null) {
|
98
|
if(topicFull.getId() == null) {
|
99
|
topicDAO.save(topic);
|
100
|
onSaveDefaultTopic(topic, stakeholderId);
|
101
|
} else {
|
102
|
onUpdateDefaultTopic(topic, oldTopic);
|
103
|
topicDAO.save(topic);
|
104
|
}
|
105
|
} else {
|
106
|
topicDAO.save(topic);
|
107
|
}
|
108
|
|
109
|
List<String> topics = stakeholder.getTopics();
|
110
|
int index = topics.indexOf(topic.getId());
|
111
|
if(index == -1) {
|
112
|
topics.add(topic.getId());
|
113
|
stakeholderDAO.save(stakeholder);
|
114
|
log.debug("Topic saved!");
|
115
|
|
116
|
topicFull.setId(topic.getId());
|
117
|
}
|
118
|
|
119
|
categories = null;
|
120
|
topic = null;
|
121
|
} else {
|
122
|
// EXCEPTION - Stakeholder not found
|
123
|
throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
|
124
|
}
|
125
|
return topicFull;
|
126
|
}
|
127
|
|
128
|
public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
|
129
|
log.debug("On save default topic");
|
130
|
|
131
|
List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
|
132
|
for(Stakeholder _stakeholder : stakeholders) {
|
133
|
Topic topicNew = new Topic();
|
134
|
topicNew.copyFromDefault(topic);
|
135
|
|
136
|
topicDAO.save(topicNew);
|
137
|
|
138
|
List<String> topics = _stakeholder.getTopics();
|
139
|
topics.add(topicNew.getId());
|
140
|
|
141
|
stakeholderDAO.save(_stakeholder);
|
142
|
}
|
143
|
}
|
144
|
|
145
|
public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
|
146
|
log.debug("On update default topic");
|
147
|
|
148
|
List<Topic> topics = topicDAO.findByDefaultId(topic.getId());
|
149
|
boolean changed = false;
|
150
|
for(Topic topicBasedOnDefault : topics) {
|
151
|
if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())
|
152
|
&& (oldTopic.getName() == null || oldTopic.getName().equals(topicBasedOnDefault.getName()))) {
|
153
|
|
154
|
topicBasedOnDefault.setName(topic.getName());
|
155
|
topicBasedOnDefault.setAlias(topic.getAlias());
|
156
|
changed = true;
|
157
|
}
|
158
|
if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())
|
159
|
&& (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) {
|
160
|
|
161
|
topicBasedOnDefault.setDescription(topic.getDescription());
|
162
|
changed = true;
|
163
|
}
|
164
|
if(topic.getIcon() != null && !topic.getIcon().equals(topicBasedOnDefault.getIcon())
|
165
|
&& (oldTopic.getIcon() == null || oldTopic.getIcon().equals(topicBasedOnDefault.getIcon()))) {
|
166
|
|
167
|
topicBasedOnDefault.setIcon(topic.getIcon());
|
168
|
changed = true;
|
169
|
}
|
170
|
|
171
|
if(!changed) {
|
172
|
// break;
|
173
|
continue;
|
174
|
}
|
175
|
|
176
|
// topicBasedOnDefault.setName(topic.getName());
|
177
|
// topicBasedOnDefault.setDescription(topic.getDescription());
|
178
|
topicBasedOnDefault.setUpdateDate(topic.getUpdateDate());
|
179
|
topicDAO.save(topicBasedOnDefault);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
@PreAuthorize("isAuthenticated()")
|
184
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
|
185
|
public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
|
186
|
@PathVariable("topicId") String topicId,
|
187
|
@RequestParam(required = false) String children) {
|
188
|
log.debug("delete topic");
|
189
|
log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
|
190
|
|
191
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
192
|
|
193
|
if(stakeholder != null) {
|
194
|
|
195
|
List<String> roles = rolesUtils.getRoles();
|
196
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
197
|
// EXCEPTION - Access denied
|
198
|
throw new AccessDeniedException("Delete topic: You are not authorized to update stakeholder with id: "+stakeholderId);
|
199
|
}
|
200
|
|
201
|
Topic<String> topic = topicDAO.findById(topicId);
|
202
|
if(topic != null) {
|
203
|
|
204
|
if(topic.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
|
205
|
// EXCEPTION - Access denied
|
206
|
throw new AccessDeniedException("Delete topic: You are not authorized to delete a default Topic in stakeholder with id: "+stakeholderId);
|
207
|
}
|
208
|
|
209
|
List<String> topics = stakeholder.getTopics();
|
210
|
int index = topics.indexOf(topicId);
|
211
|
if(index != -1) {
|
212
|
// this topic belongs in default profile
|
213
|
if(stakeholder.getDefaultId() == null && children != null) {
|
214
|
onDeleteDefaultTopic(topicId, stakeholderId, children);
|
215
|
}
|
216
|
|
217
|
// for(String categoryId : topic.getCategories()) {
|
218
|
// Category<String> category = categoryDAO.findById(categoryId);
|
219
|
// if(category == null) {
|
220
|
// // EXCEPTION - Category not found
|
221
|
// throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
222
|
// }
|
223
|
//
|
224
|
// for(String subCategoryId : category.getSubCategories()) {
|
225
|
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
226
|
// if (subcategory == null) {
|
227
|
// // EXCEPTION - SubCategory not found
|
228
|
// throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
229
|
// }
|
230
|
//
|
231
|
// for(String chartSectionId : subcategory.getCharts()) {
|
232
|
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
|
233
|
// if (chartSection == null) {
|
234
|
// // EXCEPTION - Section not found
|
235
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
236
|
// }
|
237
|
//
|
238
|
// for (String chartId : chartSection.getIndicators()) {
|
239
|
// indicatorDAO.delete(chartId);
|
240
|
// }
|
241
|
// subcategory.setCharts(null);
|
242
|
// sectionDAO.delete(chartSectionId);
|
243
|
// }
|
244
|
//
|
245
|
// for(String numberSectionId : subcategory.getNumbers()) {
|
246
|
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
|
247
|
// if (numberSection == null) {
|
248
|
// // EXCEPTION - Section not found
|
249
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
250
|
// }
|
251
|
//
|
252
|
// for (String numberId : numberSection.getIndicators()) {
|
253
|
// indicatorDAO.delete(numberId);
|
254
|
// }
|
255
|
// subcategory.setNumbers(null);
|
256
|
// sectionDAO.delete(numberSectionId);
|
257
|
// }
|
258
|
//
|
259
|
// subCategoryDAO.delete(subCategoryId);
|
260
|
// }
|
261
|
// category.setSubCategories(null);
|
262
|
// categoryDAO.delete(categoryId);
|
263
|
// }
|
264
|
categoryController.deleteTree(topic);
|
265
|
|
266
|
topic.setCategories(null);
|
267
|
|
268
|
topics.remove(index);
|
269
|
stakeholderDAO.save(stakeholder);
|
270
|
|
271
|
topicDAO.delete(topicId);
|
272
|
log.debug("Category deleted!");
|
273
|
} else {
|
274
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
275
|
throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
276
|
}
|
277
|
|
278
|
} else {
|
279
|
// EXCEPTION - Topic not found
|
280
|
throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
|
281
|
}
|
282
|
} else {
|
283
|
// EXCEPTION - Stakeholder not found
|
284
|
throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
|
285
|
}
|
286
|
return true;
|
287
|
}
|
288
|
|
289
|
|
290
|
public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) {
|
291
|
if(children.equals("delete")) {
|
292
|
List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId);
|
293
|
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
|
294
|
|
295
|
for(Stakeholder stakeholder : stakeholders) {
|
296
|
Iterator<Topic> topicsIterator = topics.iterator();
|
297
|
while(topicsIterator.hasNext()) {
|
298
|
Topic topic = topicsIterator.next();
|
299
|
|
300
|
String topicId = topic.getId();
|
301
|
|
302
|
if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) {
|
303
|
topicsIterator.remove();
|
304
|
|
305
|
stakeholder.getTopics().remove(topicId);
|
306
|
stakeholderDAO.save(stakeholder);
|
307
|
|
308
|
categoryController.deleteTree(topic);
|
309
|
|
310
|
topicDAO.delete(topicId);
|
311
|
log.debug("Topic with id: "+topicId+" deleted!");
|
312
|
|
313
|
break;
|
314
|
}
|
315
|
}
|
316
|
}
|
317
|
} else if(children.equals("disconnect")) {
|
318
|
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
|
319
|
for(Topic topic : topics) {
|
320
|
categoryController.disConnectTree(topic);
|
321
|
|
322
|
topic.setDefaultId(null);
|
323
|
topicDAO.save(topic);
|
324
|
|
325
|
log.debug("DefaultId for Topic with id: "+topic.getId()+" empty!");
|
326
|
}
|
327
|
}
|
328
|
return true;
|
329
|
}
|
330
|
|
331
|
@PreAuthorize("isAuthenticated()")
|
332
|
@RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
|
333
|
public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
|
334
|
@RequestBody List<String> topics) {
|
335
|
log.debug("reorder topics");
|
336
|
log.debug("Stakeholder: "+stakeholderId);
|
337
|
|
338
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
339
|
|
340
|
if(stakeholder != null) {
|
341
|
|
342
|
List<String> roles = rolesUtils.getRoles();
|
343
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
344
|
// EXCEPTION - Access denied
|
345
|
throw new AccessDeniedException("Reorder topics: You are not authorized to update stakeholder with id: "+stakeholderId);
|
346
|
}
|
347
|
|
348
|
stakeholder.setTopics(topics);
|
349
|
|
350
|
stakeholderDAO.save(stakeholder);
|
351
|
log.debug("Topics reordered!");
|
352
|
|
353
|
List<Topic> topicsFull = new ArrayList<>();
|
354
|
for (String topicId : topics) {
|
355
|
topicsFull.add(topicDAO.findById(topicId));
|
356
|
}
|
357
|
return topicsFull;
|
358
|
} else {
|
359
|
// EXCEPTION - Stakeholder not found
|
360
|
throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found");
|
361
|
}
|
362
|
}
|
363
|
|
364
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
|
365
|
// public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
|
366
|
// @PathVariable("topicId") String topicId) {
|
367
|
// log.debug("toggle topic status (isActive)");
|
368
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
369
|
//
|
370
|
// Topic topic = topicDAO.findById(topicId);
|
371
|
// if (topic == null) {
|
372
|
// // EXCEPTION - Topic not found
|
373
|
// throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
|
374
|
// }
|
375
|
// topic.setIsActive(!topic.getIsActive());
|
376
|
//
|
377
|
// this.toggleTopic(stakeholderId, topic);
|
378
|
//
|
379
|
// return topic.getIsActive();
|
380
|
// }
|
381
|
//
|
382
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
|
383
|
// public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId,
|
384
|
// @PathVariable("topicId") String topicId) {
|
385
|
// log.debug("toggle topic access (isPublic)");
|
386
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
387
|
//
|
388
|
// Topic topic = topicDAO.findById(topicId);
|
389
|
// if (topic == null) {
|
390
|
// // EXCEPTION - Topic not found
|
391
|
// throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found");
|
392
|
// }
|
393
|
// topic.setIsPublic(!topic.getIsPublic());
|
394
|
//
|
395
|
// this.toggleTopic(stakeholderId, topic);
|
396
|
//
|
397
|
// return topic.getIsPublic();
|
398
|
// }
|
399
|
|
400
|
@PreAuthorize("isAuthenticated()")
|
401
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST)
|
402
|
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
|
403
|
@PathVariable("topicId") String topicId,
|
404
|
@RequestParam("visibility") Visibility visibility) {
|
405
|
log.debug("change topic visibility: "+visibility);
|
406
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
407
|
|
408
|
Topic topic = topicDAO.findById(topicId);
|
409
|
if (topic == null) {
|
410
|
// EXCEPTION - Topic not found
|
411
|
throw new EntityNotFoundException("Change topic visibility: Topic with id: "+topicId+" not found");
|
412
|
}
|
413
|
topic.setVisibility(visibility);
|
414
|
|
415
|
this.toggleTopic(stakeholderId, topic);
|
416
|
|
417
|
return topic.getVisibility();
|
418
|
}
|
419
|
|
420
|
public void toggleTopic(String stakeholderId, Topic topic) {
|
421
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
422
|
|
423
|
if (stakeholder != null) {
|
424
|
|
425
|
List<String> roles = rolesUtils.getRoles();
|
426
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
427
|
// EXCEPTION - Access denied
|
428
|
throw new AccessDeniedException("Toggle topic: You are not authorized to update stakeholder with id: "+stakeholderId);
|
429
|
}
|
430
|
|
431
|
if (stakeholder.getTopics().contains(topic.getId())) {
|
432
|
topicDAO.save(topic);
|
433
|
log.debug("Topic toggled!");
|
434
|
} else {
|
435
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
436
|
throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
|
437
|
}
|
438
|
} else {
|
439
|
// EXCEPTION - Stakeholder not found
|
440
|
throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
|
441
|
}
|
442
|
}
|
443
|
|
444
|
public void deleteTree(Stakeholder stakeholder) {
|
445
|
List<String> topics = stakeholder.getTopics();
|
446
|
for(String topicId : topics) {
|
447
|
Topic topic = topicDAO.findById(topicId);
|
448
|
if (topic == null) {
|
449
|
// EXCEPTION - Topic not found
|
450
|
throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
|
451
|
}
|
452
|
|
453
|
categoryController.deleteTree(topic);
|
454
|
|
455
|
topicDAO.delete(topicId);
|
456
|
}
|
457
|
}
|
458
|
}
|