Project

General

Profile

1 57964 konstantin
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 58978 konstantin
import java.util.Iterator;
13 57964 konstantin
import java.util.List;
14
15
@RestController
16
@CrossOrigin(origins = "*")
17
public class SectionController {
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 58978 konstantin
    @Autowired
39
    private IndicatorController indicatorController;
40
41 57964 konstantin
    public Section<Indicator> buildSection(Section<Indicator> sectionFull) {
42
        Section<String> section = new Section<>(sectionFull);
43
44
        List<String> indicators = new ArrayList<>();
45
        List<Indicator> indicatorsFull = new ArrayList<>();
46
        for(Indicator chart : sectionFull.getIndicators()) {
47
            Indicator chartSaved = indicatorDAO.save(chart);
48
            chart.setId(chartSaved.getId());
49
            indicatorsFull.add(chart);
50
            indicators.add(chartSaved.getId());
51
        }
52
        sectionFull.setIndicators(indicatorsFull);
53
        section.setIndicators(indicators);
54
55
        sectionDAO.save(section);
56
57
        sectionFull.setId(section.getId());
58
        return sectionFull;
59
    }
60
61 57987 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
62 57964 konstantin
    public Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
63 57987 konstantin
                               @PathVariable("topicId") String topicId,
64
                               @PathVariable("categoryId") String categoryId,
65
                               @PathVariable("subcategoryId") String subcategoryId,
66
                               @PathVariable("index") String index,
67
                               @RequestBody Section<Indicator> sectionFull) {
68 57964 konstantin
        log.debug("save section");
69
        log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
70
71
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
72 58708 konstantin
73
        Section<String> oldSection = null;
74
        if(sectionFull.getId() != null) {
75
            oldSection = sectionDAO.findById(sectionFull.getId());
76
        }
77
78 57964 konstantin
        Section<String> section = new Section<>(sectionFull);
79
80
        String sectionId = sectionFull.getId();
81
        List<String> indicators = new ArrayList<>();
82
        for(Indicator indicator : sectionFull.getIndicators()) {
83
            indicators.add(indicator.getId());
84
        }
85
        section.setIndicators(indicators);
86
        sectionDAO.save(section);
87
88
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
89
        // this section belongs in default profile and it is new or it is updated
90
        if(stakeholder.getDefaultId() == null) {
91
            if(sectionId == null) {
92 58693 konstantin
                onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
93 57964 konstantin
            }
94
            else {
95 58708 konstantin
                onUpdateDefaultSection(section, stakeholder, oldSection);
96 57964 konstantin
            }
97
        }
98
99
        List<String> sections = null;
100
        if(sectionFull.getType().equals("chart")) {
101
            sections = subCategory.getCharts();
102
        } else if(sectionFull.getType().equals("number")) {
103
            sections = subCategory.getNumbers();
104
        }
105
106 58693 konstantin
        int existing_index = sections.indexOf(section.getId());
107 57987 konstantin
        if (existing_index == -1) {
108 58693 konstantin
            if(Integer.parseInt(index) != -1) {
109
                sections.add(Integer.parseInt(index), section.getId());
110 57987 konstantin
            } else {
111 58693 konstantin
                sections.add(section.getId());
112 57987 konstantin
            }
113 57964 konstantin
            subCategoryDAO.save(subCategory);
114
            log.debug("Section saved!");
115
116
            sectionFull.setId(section.getId());
117
        }
118
119
        return sectionFull;
120
    }
121
122
    public void onSaveDefaultSection(Section section,
123
                                     String defaultTopicId, String defaultCategoryId,
124
                                     String defaultSubcategoryId, Stakeholder defaultStakeholder) {
125
        log.debug("On save default section");
126
127
128
        // new section in default profile - add it on profiles of the same type
129
        List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
130
131
        for (SubCategory subCategory : subCategories) {
132 58708 konstantin
            Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId());
133
            Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId());
134
            Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId());
135
136 57964 konstantin
            Section sectionNew = new Section();
137
            sectionNew.copyFromDefault(section);
138
139 58708 konstantin
            sectionNew.setStakeholderAlias(parentStakeholder.getAlias());
140
141 57964 konstantin
            sectionDAO.save(sectionNew);
142
143
            List<String> sections = null;
144
            if (section.getType().equals("chart")) {
145
                sections = subCategory.getCharts();
146
            } else if (section.getType().equals("number")) {
147
                sections = subCategory.getNumbers();
148
            }
149
            sections.add(sectionNew.getId());
150
151
            subCategoryDAO.save(subCategory);
152
        }
153
    }
154
155 58708 konstantin
    public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) {
156 57964 konstantin
        log.debug("On update default section");
157
158
        // section already exists - check if changed and update all sections based on it
159
160
        boolean changed = false;
161
        List<Section> sections = sectionDAO.findByDefaultId(section.getId());
162
163
        for(Section sectionBasedOnDefault : sections) {
164 58708 konstantin
            if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())
165
                    && (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) {
166
167
                sectionBasedOnDefault.setTitle(section.getTitle());
168 57964 konstantin
                changed = true;
169
            }
170
171
            if(!changed) {
172 58708 konstantin
//                break;
173
                continue;
174 57964 konstantin
            }
175
176 58708 konstantin
//            sectionBasedOnDefault.setTitle(section.getTitle());
177 57964 konstantin
            sectionDAO.save(sectionBasedOnDefault);
178
        }
179
    }
180
181
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
182
    public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,
183 58978 konstantin
                                 @PathVariable("topicId") String topicId,
184
                                 @PathVariable("categoryId") String categoryId,
185
                                 @PathVariable("subcategoryId") String subcategoryId,
186
                                 @PathVariable("sectionId") String sectionId,
187
                                 @RequestParam(required = false) String children) {
188 57964 konstantin
        log.debug("delete section");
189
        log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
190
191
        Section section = sectionDAO.findById(sectionId);
192
        if(section != null) {
193
            SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
194
195 58978 konstantin
            String type = "";
196 57964 konstantin
            List<String> sections = null;
197
            if (section.getType().equals("chart")) {
198
                sections = subCategory.getCharts();
199 58978 konstantin
                type = "chart";
200 57964 konstantin
            } else if (section.getType().equals("number")) {
201
                sections = subCategory.getNumbers();
202 58978 konstantin
                type = "number";
203 57964 konstantin
            }
204
205
            int index = sections.indexOf(sectionId);
206
            if (index != -1) {
207 58978 konstantin
                // this section belongs in default profile
208
                if(subCategory.getDefaultId() == null && children != null) {
209
                    onDeleteDefaultSection(sectionId, subcategoryId, children, type);
210
                }
211
212
                indicatorController.deleteTree(section);
213
214 57964 konstantin
                sections.remove(index);
215
                subCategoryDAO.save(subCategory);
216
217
                sectionDAO.delete(sectionId);
218
                log.debug("Section deleted!");
219
            } else {
220
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
221
                throw new PathNotValidException("Delete section: Section with id: "+sectionId+" not found in SubCategory: "+subcategoryId);
222
            }
223
        } else {
224
            // EXCEPTION - Section not found
225
            throw new EntityNotFoundException("Delete section: Section with id: "+sectionId+" not found");
226
        }
227
        return true;
228
    }
229
230 58978 konstantin
    public boolean onDeleteDefaultSection(String defaultSectionId, String defaultSubCategoryId, String children, String type) {
231
        if(children.equals("delete")) {
232
            List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
233
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
234
235
            for(SubCategory subCategory : subCategories) {
236
                Iterator<Section> sectionsIterator = sections.iterator();
237
                while(sectionsIterator.hasNext()) {
238
                    Section section = sectionsIterator.next();
239
240
                    String sectionId = section.getId();
241
                    List<String> subCategorySections = null;
242
                    if(type.equals("chart")) {
243
                        subCategorySections = subCategory.getCharts();
244
                    } else if(type.equals("number")) {
245
                        subCategorySections = subCategory.getNumbers();
246
                    }
247
                    if(subCategorySections != null && subCategorySections.contains(sectionId)) {
248
                        sectionsIterator.remove();
249
250
                        subCategorySections.remove(sectionId);
251
                        subCategoryDAO.save(subCategory);
252
253
                        indicatorController.deleteTree(section);
254
255
                        sectionDAO.delete(sectionId);
256
                        log.debug("Section with id: "+sectionId+" deleted!");
257
258
                        break;
259
                    }
260
                }
261
            }
262
        } else if(children.equals("disconnect")) {
263
            List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
264
            for(Section section : sections) {
265
                indicatorController.disConnectTree(section);
266
267
                section.setDefaultId(null);
268
                sectionDAO.save(section);
269
270
                log.debug("DefaultId for Section with id: "+section.getId()+" empty!");
271
            }
272
        }
273
        return true;
274
    }
275
276 57964 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
277
    public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
278
                                         @PathVariable("topicId") String topicId,
279
                                         @PathVariable("categoryId") String categoryId,
280
                                         @PathVariable("subcategoryId") String subcategoryId,
281
                                         @PathVariable("type") String type,
282
                                         @RequestBody List<String> sections) {
283
        log.debug("reorder sections of type: "+type);
284
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
285
286
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
287
288
        if (type.equals("chart")) {
289
            subCategory.setCharts(sections);
290
        } else if (type.equals("number")) {
291
            subCategory.setNumbers(sections);
292
        }
293
294
        subCategoryDAO.save(subCategory);
295
        log.debug("Sections reordered!");
296
297
        List<Section> sectionsFull = new ArrayList<>();
298
        for(String sectionId : sections) {
299
            sectionsFull.add(sectionDAO.findById(sectionId));
300
        }
301
        return sectionsFull;
302
    }
303
304
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-status", method = RequestMethod.POST)
305
//    public Boolean toggleSectionStatus(@PathVariable("stakeholderId") String stakeholderId,
306
//                                       @PathVariable("topicId") String topicId,
307
//                                       @PathVariable("categoryId") String categoryId,
308
//                                       @PathVariable("subcategoryId") String subcategoryId,
309
//                                       @PathVariable("sectionId") String sectionId) {
310
//        log.debug("toggle section status (isActive)");
311
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Section: "+sectionId);
312
//
313
//        Section section = sectionDAO.findById(sectionId);
314
//        if (section == null) {
315
//            // EXCEPTION - Section not found
316
//            throw new EntityNotFoundException("Toggle section status: Section with id: "+sectionId+" not found");
317
//        }
318
//        section.setIsActive(!section.getIsActive());
319
//
320
//        this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId, section);
321
//
322
//        return section.getIsActive();
323
//    }
324
325
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-access", method = RequestMethod.POST)
326
//    public Boolean toggleSectionAccess(@PathVariable("stakeholderId") String stakeholderId,
327
//                                         @PathVariable("topicId") String topicId,
328
//                                         @PathVariable("categoryId") String categoryId,
329
//                                         @PathVariable("subcategoryId") String subcategoryId,
330
//                                         @PathVariable("sectionId") String sectionId) {
331
//        log.debug("toggle section access (isPublic)");
332
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
333
//
334
//        Section section = sectionDAO.findById(sectionId);
335
//        if (section == null) {
336
//            // EXCEPTION - Section not found
337
//            throw new EntityNotFoundException("Toggle section access: Section with id: "+sectionId+" not found");
338
//        }
339
//        section.setIsPublic(!section.getIsPublic());
340
//
341
//        this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId);
342
//
343
//        return section.getIsPublic();
344
//    }
345
346
    public void toggleSection(String stakeholderId, String topicId, String categoryId, String subcategoryId, Section section) {
347
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
348
349
        List<String> sections = null;
350
        if (section.getType().equals("chart")) {
351
            sections = subCategory.getCharts();
352
        } else if (section.getType().equals("number")) {
353
            sections = subCategory.getNumbers();
354
        }
355
356
        if(sections.contains(section.getId())) {
357
            sectionDAO.save(section);
358
            log.debug("Section toggled!");
359
        } else {
360
            // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias();
361
            throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId);
362
        }
363
364
    }
365
366
    private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) {
367
368
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
369
370
        if(stakeholder == null) {
371
            // EXCEPTION - Stakeholder not found
372
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
373
        }
374
375
        Topic<String> topic = topicDAO.findById(topicId);
376
        if(topic == null) {
377
            // EXCEPTION - Topic not found
378
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
379
        }
380
381
        if(!stakeholder.getTopics().contains(topicId)) {
382
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
383
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
384
        }
385
386
        Category<String> category = categoryDAO.findById(categoryId);
387
        if(category == null) {
388
            // EXCEPTION - Category not found
389
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
390
        }
391
392
        if(!topic.getCategories().contains(categoryId)) {
393
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
394
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
395
        }
396
397
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
398
        if(subcategory == null) {
399
            // EXCEPTION - SubCategory not found
400
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
401
        }
402
403
        if (!category.getSubCategories().contains(subcategoryId)) {
404
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
405
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
406
        }
407
408
        return  subcategory;
409
    }
410 58978 konstantin
411
    public void deleteTree(SubCategory subCategory) {
412
        List<String> sections = subCategory.getCharts();
413
        for(String sectionId : sections) {
414
            Section section = sectionDAO.findById(sectionId);
415
            if (section == null) {
416
                // EXCEPTION - Section not found
417
                throw new EntityNotFoundException("Section delete tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
418
            }
419
420
            indicatorController.deleteTree(section);
421
422
            sectionDAO.delete(sectionId);
423
        }
424
425
        sections = subCategory.getNumbers();
426
        for(String sectionId : sections) {
427
            Section section = sectionDAO.findById(sectionId);
428
            if (section == null) {
429
                // EXCEPTION - Section not found
430
                throw new EntityNotFoundException("Section delete tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
431
            }
432
433
            indicatorController.deleteTree(section);
434
435
            sectionDAO.delete(sectionId);
436
        }
437
    }
438
439
    public void disConnectTree(SubCategory subCategory) {
440
        List<String> sections = subCategory.getCharts();
441
        for(String sectionId : sections) {
442
            Section section = sectionDAO.findById(sectionId);
443
            if (section == null) {
444
                // EXCEPTION - Section not found
445
                throw new EntityNotFoundException("Section disconnect tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
446
            }
447
448
            indicatorController.disConnectTree(section);
449
450
            section.setDefaultId(null);
451
            sectionDAO.save(section);
452
        }
453
454
        sections = subCategory.getNumbers();
455
        for(String sectionId : sections) {
456
            Section section = sectionDAO.findById(sectionId);
457
            if (section == null) {
458
                // EXCEPTION - Section not found
459
                throw new EntityNotFoundException("Section disconnect tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")");
460
            }
461
462
            indicatorController.disConnectTree(section);
463
464
            section.setDefaultId(null);
465
            sectionDAO.save(section);
466
        }
467
    }
468 57964 konstantin
}