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