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