Project

General

Profile

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 SubCategoryController {
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 CategoryDAO categoryDAO;
36

    
37
    @Autowired
38
    private SubCategoryDAO subCategoryDAO;
39

    
40
    @Autowired
41
    private SectionDAO sectionDAO;
42

    
43
    @Autowired
44
    private IndicatorDAO indicatorDAO;
45

    
46
    @Autowired
47
    private SectionController sectionController;
48

    
49
    public SubCategory<Section<Indicator>> buildSubCategory(SubCategory<Section<Indicator>> subcategoryFull) {
50
        SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
51

    
52
        List<String> sectionCharts = new ArrayList<>();
53
        List<Section<Indicator>> sectionChartsFull = new ArrayList<>();
54

    
55
        for(Section section : subcategoryFull.getCharts()) {
56
            Section<Indicator> sectionFull = sectionController.buildSection(section);
57
            sectionChartsFull.add(sectionFull);
58
            sectionCharts.add(sectionFull.getId());
59
        }
60
        subcategoryFull.setCharts(sectionChartsFull);
61
        subCategory.setCharts(sectionCharts);
62

    
63
        List<String> sectionNumbers = new ArrayList<>();
64
        List<Section<Indicator>> sectionNumbersFull = new ArrayList<>();
65

    
66
        for(Section section : subcategoryFull.getNumbers()) {
67
            Section<Indicator> sectionFull = sectionController.buildSection(section);
68
            sectionNumbersFull.add(sectionFull);
69
            sectionNumbers.add(sectionFull.getId());
70
        }
71
        subcategoryFull.setNumbers(sectionNumbersFull);
72
        subCategory.setNumbers(sectionNumbers);
73

    
74
        Date date = new Date();
75
        subCategory.setCreationDate(date);
76
        subCategory.setUpdateDate(date);
77

    
78
        subcategoryFull.setCreationDate(date);
79
        subcategoryFull.setUpdateDate(date);
80

    
81

    
82
        subCategoryDAO.save(subCategory);
83

    
84
        subcategoryFull.setId(subCategory.getId());
85
        return subcategoryFull;
86
    }
87

    
88
    @PreAuthorize("isAuthenticated()")
89
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
90
    public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
91
                                                           @PathVariable("topicId") String topicId,
92
                                                           @PathVariable("categoryId") String categoryId,
93
                                                           @RequestBody SubCategory<Section<Indicator>> subcategoryFull) {
94
        log.debug("save subcategory");
95
        log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
96

    
97
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
98

    
99
        SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
100

    
101
        Date date = new Date();
102
        subCategory.setUpdateDate(date);
103
        subcategoryFull.setUpdateDate(date);
104

    
105
        List<String> chartSections = new ArrayList<>();
106
        List<String> numberSections = new ArrayList<>();
107

    
108
        SubCategory<String> oldSubcategory = null;
109
        if(subcategoryFull.getId() != null) {
110
            oldSubcategory = subCategoryDAO.findById(subcategoryFull.getId());
111
            if(oldSubcategory == null) {
112
                // EXCEPTION - SubCategory not found
113
                throw new EntityNotFoundException("save subcategory: SubCategory with id: " + subcategoryFull.getId() + " not found");
114
            }
115

    
116
            for(String chartSectionId : oldSubcategory.getCharts()) {
117
                Section section = sectionDAO.findById(chartSectionId);
118
                if (section == null) {
119
                    // EXCEPTION - Section not found
120
                    throw new EntityNotFoundException("Save subcategory: Chart section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
121
                }
122
                chartSections.add(section.getId());
123
            }
124

    
125
            for(String numberSectionId : oldSubcategory.getNumbers()) {
126
                Section section = sectionDAO.findById(numberSectionId);
127
                if (section == null) {
128
                    // EXCEPTION - Section not found
129
                    throw new EntityNotFoundException("Save subcategory: Number section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
130
                }
131
                numberSections.add(section.getId());
132
            }
133
        } else { // subcategory does not exist in DB
134
            subCategory.setCreationDate(date);
135
            subcategoryFull.setCreationDate(date);
136

    
137
            for(Section chartSection : subcategoryFull.getCharts()) {
138
                chartSections.add(chartSection.getId());
139
            }
140

    
141
            for(Section numberSection : subcategoryFull.getNumbers()) {
142
                numberSections.add(numberSection.getId());
143
            }
144
        }
145

    
146
//        List<String> charts = new ArrayList<>();
147
//        for(Indicator chart : subcategoryFull.getCharts()) {
148
//            charts.add(chart.getId());
149
//        }
150
//        subCategory.setCharts(charts);
151
//
152
//        List<String> numbers = new ArrayList<>();
153
//        for(Indicator numbr : subcategoryFull.getNumbers()) {
154
//            numbers.add(numbr.getId());
155
//        }
156
//        subCategory.setNumbers(numbers);
157

    
158

    
159
        subCategory.setCharts(chartSections);
160
        subCategory.setNumbers(numberSections);
161

    
162
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
163
        if(stakeholder.getDefaultId() == null) {
164
            if(subcategoryFull.getId() == null) {
165
                subCategoryDAO.save(subCategory);
166
                onSaveDefaultSubCategory(subCategory, categoryId);
167
            } else {
168
                onUpdateDefaultSubCategory(subCategory, oldSubcategory);
169
                subCategoryDAO.save(subCategory);
170
            }
171
        } else {
172
            subCategoryDAO.save(subCategory);
173
        }
174

    
175
        List<String> subcategories = category.getSubCategories();
176
        int index = subcategories.indexOf(subCategory.getId());
177
        if(index == -1) {
178
            subcategories.add(subCategory.getId());
179
            categoryDAO.save(category);
180
            log.debug("Subcategory saved!");
181

    
182
            subcategoryFull.setId(subCategory.getId());
183
        }
184

    
185
        chartSections = null;
186
        numberSections = null;
187
        subCategory = null;
188

    
189
        return subcategoryFull;
190
    }
191

    
192
    public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) {
193
        log.debug("On save default subCategory");
194

    
195
        List<Category> categories = categoryDAO.findByDefaultId(categoryId);
196
        for(Category category : categories) {
197
            SubCategory subCategoryNew = new SubCategory();
198
            subCategoryNew.copyFromDefault(subCategory);
199

    
200
            subCategoryDAO.save(subCategoryNew);
201

    
202
            List<String> subCategories = category.getSubCategories();
203
            subCategories.add(subCategoryNew.getId());
204

    
205
            categoryDAO.save(category);
206
        }
207
    }
208

    
209
    public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) {
210
        log.debug("On update default subCategory");
211

    
212
        List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(subCategory.getId());
213
        boolean changed = false;
214
        for(SubCategory subCategoryBasedOnDefault : subCategories) {
215
            if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName())
216
                    && (oldSubcategory.getName() == null || oldSubcategory.getName().equals(subCategoryBasedOnDefault.getName()))) {
217

    
218
                subCategoryBasedOnDefault.setName(subCategory.getName());
219
                subCategoryBasedOnDefault.setAlias(subCategory.getAlias());
220
                changed = true;
221
            }
222
            if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription())
223
                    && (oldSubcategory.getDescription() == null || oldSubcategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()))) {
224

    
225
                subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
226
                changed = true;
227
            }
228

    
229
            if(!changed) {
230
//                break;
231
                continue;
232
            }
233

    
234
//            subCategoryBasedOnDefault.setName(subCategory.getName());
235
//            subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
236
            subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
237
            subCategoryDAO.save(subCategoryBasedOnDefault);
238
        }
239
    }
240

    
241
    @PreAuthorize("isAuthenticated()")
242
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
243
    public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
244
                                     @PathVariable("topicId") String topicId,
245
                                     @PathVariable("categoryId") String categoryId,
246
                                     @PathVariable("subcategoryId") String subcategoryId,
247
                                     @RequestParam(required = false) String children) {
248
        log.debug("delete subcategory");
249
        log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
250

    
251
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
252

    
253
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
254
        if(subcategory != null) {
255

    
256
            Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
257
            List<String> roles = rolesUtils.getRoles();
258
            if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
259
                // EXCEPTION - Access denied
260
                throw new ForbiddenException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId);
261
            }
262

    
263
            List<String> subcategories = category.getSubCategories();
264
            int index = subcategories.indexOf(subcategoryId);
265
            if(index != -1) {
266
                // this subCategory belongs in default profile
267
                if(category.getDefaultId() == null && children != null) {
268
                    onDeleteDefaultSubCategory(subcategoryId, categoryId, children);
269
                }
270

    
271
//                for(String chartSectionId : subcategory.getCharts()) {
272
//                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
273
//                    if (chartSection == null) {
274
//                        // EXCEPTION - Section not found
275
//                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
276
//                    }
277
//
278
//                    for (String chartId : chartSection.getIndicators()) {
279
//                        indicatorDAO.delete(chartId);
280
//                    }
281
//                    subcategory.setCharts(null);
282
//                    sectionDAO.delete(chartSectionId);
283
//                }
284
//
285
//                for(String numberSectionId : subcategory.getNumbers()) {
286
//                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
287
//                    if (numberSection == null) {
288
//                        // EXCEPTION - Section not found
289
//                        throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
290
//                    }
291
//
292
//                    for (String numberId : numberSection.getIndicators()) {
293
//                        indicatorDAO.delete(numberId);
294
//                    }
295
//                    subcategory.setNumbers(null);
296
//                    sectionDAO.delete(numberSectionId);
297
//                }
298

    
299
                sectionController.deleteTree(subcategory);
300

    
301
                subcategory.setCharts(null);
302
                subcategory.setNumbers(null);
303

    
304
                subcategories.remove(index);
305
                categoryDAO.save(category);
306

    
307
                subCategoryDAO.delete(subcategoryId);
308
                log.debug("Subcategory deleted!");
309
            } else {
310
                // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
311
                throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
312
            }
313

    
314
        } else {
315
            // EXCEPTION - SubCategory not found
316
            throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
317
        }
318
        return true;
319
    }
320

    
321
    public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) {
322
        if(children.equals("delete")) {
323
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
324
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
325

    
326
            for(Category category : categories) {
327
                Iterator<SubCategory> subCategoriesIterator = subCategories.iterator();
328
                while(subCategoriesIterator.hasNext()) {
329
                    SubCategory subCategory = subCategoriesIterator.next();
330

    
331
                    String subCategoryId = subCategory.getId();
332

    
333
                    if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) {
334
                        subCategoriesIterator.remove();
335

    
336
                        category.getSubCategories().remove(subCategoryId);
337
                        categoryDAO.save(category);
338

    
339
                        sectionController.deleteTree(subCategory);
340

    
341
                        subCategoryDAO.delete(subCategoryId);
342
                        log.debug("SubCategory with id: "+subCategoryId+" deleted!");
343

    
344
                        break;
345
                    }
346
                }
347
            }
348
        } else if(children.equals("disconnect")) {
349
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
350
            for(SubCategory subCategory : subCategories) {
351
                sectionController.disConnectTree(subCategory);
352

    
353
                subCategory.setDefaultId(null);
354
                subCategoryDAO.save(subCategory);
355

    
356
                log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!");
357
            }
358
        }
359
        return true;
360
    }
361

    
362
    @PreAuthorize("isAuthenticated()")
363
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
364
    public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
365
                                            @PathVariable("topicId") String topicId,
366
                                            @PathVariable("categoryId") String categoryId,
367
                                            @RequestBody List<String> subCategories) {
368
        log.debug("reorder subCategories");
369
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
370

    
371
        Category<String> category = checkForExceptions(stakeholderId, topicId, categoryId);
372

    
373
        List<String> oldSubcategories = category.getSubCategories();
374
        for (String subcategoryId : oldSubcategories) {
375
            if (!subCategories.contains(subcategoryId)) {
376
                subCategories.add(subcategoryId);
377
            }
378
        }
379
        category.setSubCategories(subCategories);
380

    
381
        List<SubCategory> subCategoriesFull = new ArrayList<>();
382
        for(String subCategoryId : subCategories) {
383
            SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
384
            if(subCategory == null) {
385
                // EXCEPTION - SubCategory not found
386
                throw new EntityNotFoundException("Reorder subCategories: subCategory with id: " + subCategoryId + " not found");
387
            }
388
            subCategoriesFull.add(subCategory);
389
        }
390

    
391
        categoryDAO.save(category);
392
        log.debug("SubCategories reordered!");
393

    
394
        return subCategoriesFull;
395
    }
396

    
397
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status", method = RequestMethod.POST)
398
//    public Boolean toggleSubCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
399
//                                           @PathVariable("topicId") String topicId,
400
//                                           @PathVariable("categoryId") String categoryId,
401
//                                           @PathVariable("subcategoryId") String subcategoryId) {
402
//        log.debug("toggle subCategory status (isActive)");
403
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
404
//
405
//        SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
406
//        if (subCategory == null) {
407
//            // EXCEPTION - SubCategory not found
408
//            throw new EntityNotFoundException("Toggle subCategory status: SubCategory with id: "+subcategoryId+" not found");
409
//        }
410
//        subCategory.setIsActive(!subCategory.getIsActive());
411
//
412
//        this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
413
//
414
//        return subCategory.getIsActive();
415
//    }
416
//
417
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-access", method = RequestMethod.POST)
418
//    public Boolean toggleSubCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
419
//                                           @PathVariable("topicId") String topicId,
420
//                                           @PathVariable("categoryId") String categoryId,
421
//                                           @PathVariable("subcategoryId") String subcategoryId) {
422
//        log.debug("toggle subCategory access (isPublic)");
423
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
424
//
425
//        SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
426
//        if (subCategory == null) {
427
//            // EXCEPTION - SubCategory not found
428
//            throw new EntityNotFoundException("Toggle subCategory access: SubCategory with id: "+subcategoryId+" not found");
429
//        }
430
//        subCategory.setIsPublic(!subCategory.getIsPublic());
431
//
432
//        this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
433
//
434
//        return subCategory.getIsPublic();
435
//    }
436

    
437
    @PreAuthorize("isAuthenticated()")
438
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
439
    public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
440
                                            @PathVariable("topicId") String topicId,
441
                                            @PathVariable("categoryId") String categoryId,
442
                                            @PathVariable("subcategoryId") String subcategoryId,
443
                                            @RequestParam("visibility") Visibility visibility) {
444
        log.debug("change subCategory visibility: "+visibility);
445
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
446

    
447
        SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
448
        if (subCategory == null) {
449
            // EXCEPTION - SubCategory not found
450
            throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: "+subcategoryId+" not found");
451
        }
452
        subCategory.setVisibility(visibility);
453

    
454
        this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
455

    
456
        return subCategory.getVisibility();
457
    }
458

    
459
    public void toggleSubCategory(String stakeholderId, String topicId, String categoryId, SubCategory subcategory) {
460
        Category category = checkForExceptions(stakeholderId, topicId, categoryId);
461

    
462
        if (category.getSubCategories().contains(subcategory.getId())) {
463
            subCategoryDAO.save(subcategory);
464
            log.debug("SubCategory toggled!");
465
        } else {
466
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
467
            throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategory.getId()+" not found in Category: "+categoryId);
468
        }
469
    }
470

    
471

    
472
    private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) {
473

    
474
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
475

    
476
        if(stakeholder == null) {
477
            // EXCEPTION - Stakeholder not found
478
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
479
        }
480

    
481
        List<String> roles = rolesUtils.getRoles();
482
        if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
483
            // EXCEPTION - Access denied
484
            throw new ForbiddenException("CheckForExceptions SubCategory: You are not authorized to update stakeholder with id: "+stakeholderId);
485
        }
486

    
487
        Topic<String> topic = topicDAO.findById(topicId);
488
        if(topic == null) {
489
            // EXCEPTION - Topic not found
490
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
491
        }
492

    
493
        if(!stakeholder.getTopics().contains(topicId)) {
494
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
495
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
496
        }
497

    
498
        Category<String> category = categoryDAO.findById(categoryId);
499
        if(category == null) {
500
            // EXCEPTION - Category not found
501
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
502
        }
503

    
504
        if(!topic.getCategories().contains(categoryId)) {
505
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
506
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
507
        }
508

    
509
//        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
510
//        if(subcategory == null) {
511
//            // EXCEPTION - SubCategory not found
512
//            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
513
//        }
514
//
515
//        if (!category.getSubCategories().contains(subcategoryId)) {
516
//            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
517
//            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
518
//        }
519

    
520
        return  category;
521
    }
522

    
523
    public void deleteTree(Category category) {
524
        List<String> subCategories = category.getSubCategories();
525
        for(String subCategoryId : subCategories) {
526
            SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
527
            if (subCategory == null) {
528
                // EXCEPTION - SubCategory not found
529
                throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
530
            }
531

    
532
            sectionController.deleteTree(subCategory);
533

    
534
            subCategoryDAO.delete(subCategoryId);
535
        }
536
    }
537

    
538
    public void disConnectTree(Category category) {
539
        List<String> subCategories = category.getSubCategories();
540
        for(String subCategoryId : subCategories) {
541
            SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
542
            if (subCategory == null) {
543
                // EXCEPTION - SubCategory not found
544
                throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
545
            }
546

    
547
            sectionController.disConnectTree(subCategory);
548

    
549
            subCategory.setDefaultId(null);
550
            subCategoryDAO.save(subCategory);
551
        }
552
    }
553
}
(7-7/9)