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