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