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.uoamonitorservice.handlers.PathNotValidException;
7
import org.apache.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.web.bind.annotation.*;
10

    
11
import java.util.ArrayList;
12
import java.util.Iterator;
13
import java.util.List;
14

    
15
@RestController
16
@CrossOrigin(origins = "*")
17
public class CategoryController {
18
    private final Logger log = Logger.getLogger(this.getClass());
19

    
20
    @Autowired
21
    private StakeholderDAO stakeholderDAO;
22

    
23
    @Autowired
24
    private TopicDAO topicDAO;
25

    
26
    @Autowired
27
    private CategoryDAO categoryDAO;
28

    
29
    @Autowired
30
    private SubCategoryDAO subCategoryDAO;
31

    
32
    @Autowired
33
    private SectionDAO sectionDAO;
34

    
35
    @Autowired
36
    private IndicatorDAO indicatorDAO;
37

    
38
    @Autowired
39
    private SubCategoryController subCategoryController;
40

    
41
    public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
42
        Category<String> category = new Category<>(categoryFull);
43

    
44
        List<String> subCategories = new ArrayList<>();
45
        List<SubCategory> subCategoriesFull = new ArrayList<>();
46
        for(SubCategory<Section<Indicator>> subCategory : categoryFull.getSubCategories()) {
47
            SubCategory<Section<Indicator>> subcategoryFull = subCategoryController.buildSubCategory(subCategory);
48
            subCategoriesFull.add(subcategoryFull);
49
            subCategories.add(subcategoryFull.getId());
50
        }
51
        categoryFull.setSubCategories(subCategoriesFull);
52
        category.setSubCategories(subCategories);
53

    
54
        categoryDAO.save(category);
55

    
56
        categoryFull.setId(category.getId());
57
        return categoryFull;
58
    }
59

    
60
    @RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
61
    public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
62
                                              @PathVariable("topicId") String topicId,
63
                                              @RequestBody Category<SubCategory> categoryFull) {
64
        log.debug("save category");
65
        log.debug("Alias: "+categoryFull.getAlias() + " - Id: "+categoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
66

    
67
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
68

    
69
        if(stakeholder != null) {
70
            Category<String> oldCategory = null;
71
            if(categoryFull.getId() != null) {
72
                oldCategory = categoryDAO.findById(categoryFull.getId());
73
            }
74

    
75
            Topic<String> topic = topicDAO.findById(topicId);
76
            if(topic != null) {
77
                if(stakeholder.getTopics().contains(topicId)) {
78
                    // if category not exists (no id), create a new default subcategory, identical to category
79
                    if(categoryFull.getId() == null) {
80
                        SubCategory<String> subCategory = new SubCategory<>();
81
                        subCategory.createOverviewSubCategory(categoryFull);
82

    
83
                        subCategoryDAO.save(subCategory);
84
                        List<SubCategory> subCategories = categoryFull.getSubCategories();
85
                        subCategories.add(subCategory);
86
                    }
87

    
88

    
89
                    Category<String> category = new Category<>(categoryFull);
90

    
91
                    List<String> subCategories = new ArrayList<>();
92
                    for(SubCategory subCategory : categoryFull.getSubCategories()) {
93
                        subCategories.add(subCategory.getId());
94
                    }
95
                    category.setSubCategories(subCategories);
96

    
97
                    categoryDAO.save(category);
98

    
99
                    if(stakeholder.getDefaultId() == null) {
100
                        if(categoryFull.getId() == null) {
101
                            onSaveDefaultCategory(category, topicId);
102
                        } else {
103
                            onUpdateDefaultCategory(category, oldCategory);
104
                        }
105
                    }
106

    
107
                    List<String> categories = topic.getCategories();
108
                    int index = categories.indexOf(category.getId());
109
                    if(index == -1) {
110
                        categories.add(category.getId());
111
                        topicDAO.save(topic);
112
                        log.debug("Category saved!");
113

    
114
                        categoryFull.setId(category.getId());
115
                    }
116

    
117
                    subCategories = null;
118
                    category = null;
119
                } else {
120
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
121
                    throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
122
                }
123
            } else {
124
                // EXCEPTION - Topic not found
125
                throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
126
            }
127
        } else {
128
            // EXCEPTION - Stakeholder not found
129
            throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
130
        }
131
        return categoryFull;
132
    }
133

    
134
    public void onSaveDefaultCategory(Category<String> category, String topicId) {
135
        log.debug("On save default category");
136

    
137
        List<Topic> topics = topicDAO.findByDefaultId(topicId);
138
        for(Topic topic : topics) {
139
            Category categoryNew = new Category();
140
            categoryNew.copyFromDefault(category);
141

    
142
            categoryDAO.save(categoryNew);
143

    
144
            List<String> categories = topic.getCategories();
145
            categories.add(categoryNew.getId());
146

    
147
            topicDAO.save(topic);
148
        }
149
        String subCategoryOverviewId = category.getSubCategories().get(0);
150
        SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId);
151
        subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
152
    }
153

    
154
    public void onUpdateDefaultCategory(Category category, Category oldCategory) {
155
        log.debug("On update default category");
156

    
157
        List<Category> categories = categoryDAO.findByDefaultId(category.getId());
158
        boolean changed = false;
159
        for(Category categoryBasedOnDefault : categories) {
160
            if(category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName())
161
                    && (oldCategory.getName() == null || oldCategory.getName().equals(categoryBasedOnDefault.getName()))) {
162

    
163
                categoryBasedOnDefault.setName(category.getName());
164
                changed = true;
165
            }
166
            if(category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription())
167
                    && (oldCategory.getDescription() == null || oldCategory.getDescription().equals(categoryBasedOnDefault.getDescription()))) {
168

    
169
                categoryBasedOnDefault.setDescription(category.getDescription());
170
                changed = true;
171
            }
172

    
173
            if(!changed) {
174
//                break;
175
                continue;
176
            }
177

    
178
//            categoryBasedOnDefault.setName(category.getName());
179
//            categoryBasedOnDefault.setDescription(category.getDescription());
180
            categoryDAO.save(categoryBasedOnDefault);
181
        }
182
    }
183

    
184
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
185
    public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
186
                                  @PathVariable("topicId") String topicId,
187
                                  @PathVariable("categoryId") String categoryId,
188
                                  @RequestParam(required = false) String children) {
189
        log.debug("delete category");
190
        log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
191

    
192
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
193

    
194
        if(stakeholder != null) {
195

    
196
            Topic<String> topic = topicDAO.findById(topicId);
197
            if(topic != null) {
198
                if(stakeholder.getTopics().contains(topicId)) {
199

    
200
                    Category<String> category = categoryDAO.findById(categoryId);
201
                    if(category != null) {
202

    
203
                        List<String> categories = topic.getCategories();
204
                        int index = categories.indexOf(categoryId);
205
                        if(index != -1) {
206
                            // this category belongs in default profile
207
                            if(topic.getDefaultId() == null && children != null) {
208
                                onDeleteDefaultCategory(categoryId, topicId, children);
209
                            }
210

    
211
//                            for(String subCategoryId : category.getSubCategories()) {
212
//                                SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
213
//                                if(subcategory == null) {
214
//                                    // EXCEPTION - SubCategory not found
215
//                                    throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
216
//                                }
217
//
218
//                                for(String chartSectionId : subcategory.getCharts()) {
219
//                                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
220
//                                    if (chartSection == null) {
221
//                                        // EXCEPTION - Section not found
222
//                                        throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
223
//                                    }
224
//
225
//                                    for (String chartId : chartSection.getIndicators()) {
226
//                                        indicatorDAO.delete(chartId);
227
//                                    }
228
//                                    subcategory.setCharts(null);
229
//                                    sectionDAO.delete(chartSectionId);
230
//                                }
231
//
232
//                                for(String numberSectionId : subcategory.getNumbers()) {
233
//                                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
234
//                                    if (numberSection == null) {
235
//                                        // EXCEPTION - Section not found
236
//                                        throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
237
//                                    }
238
//
239
//                                    for (String numberId : numberSection.getIndicators()) {
240
//                                        indicatorDAO.delete(numberId);
241
//                                    }
242
//                                    subcategory.setNumbers(null);
243
//                                    sectionDAO.delete(numberSectionId);
244
//                                }
245
//
246
//                                subCategoryDAO.delete(subCategoryId);
247
//                            }
248
                            subCategoryController.deleteTree(category);
249

    
250
                            category.setSubCategories(null);
251

    
252
                            categories.remove(index);
253
                            topicDAO.save(topic);
254

    
255
                            categoryDAO.delete(categoryId);
256
                            log.debug("Category deleted!");
257
                        } else {
258
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
259
                            throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
260
                        }
261

    
262
                    } else {
263
                        // EXCEPTION - Category not found
264
                        throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
265
                    }
266
                } else {
267
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
268
                    throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
269
                }
270
            } else {
271
                // EXCEPTION - Topic not found
272
                throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
273
            }
274
        } else {
275
            // EXCEPTION - Stakeholder not found
276
            throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
277
        }
278
        return true;
279
    }
280

    
281

    
282
    public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) {
283
        if(children.equals("delete")) {
284
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
285
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
286

    
287
            for(Topic topic : topics) {
288
                Iterator<Category> categoriesIterator = categories.iterator();
289
                while(categoriesIterator.hasNext()) {
290
                    Category category = categoriesIterator.next();
291

    
292
                    String categoryId = category.getId();
293

    
294
                    if(topic.getCategories() != null && topic.getCategories().contains(categoryId)) {
295
                        categoriesIterator.remove();
296

    
297
                        topic.getCategories().remove(categoryId);
298
                        topicDAO.save(topic);
299

    
300
                        subCategoryController.deleteTree(category);
301

    
302
                        categoryDAO.delete(categoryId);
303
                        log.debug("Category with id: "+categoryId+" deleted!");
304

    
305
                        break;
306
                    }
307
                }
308
            }
309
        } else if(children.equals("disconnect")) {
310
            List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
311
            for(Category category : categories) {
312
                subCategoryController.disConnectTree(category);
313

    
314
                category.setDefaultId(null);
315
                categoryDAO.save(category);
316

    
317
                log.debug("DefaultId for Category with id: "+category.getId()+" empty!");
318
            }
319
        }
320
        return true;
321
    }
322

    
323
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST)
324
    public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
325
                                        @PathVariable("topicId") String topicId,
326
                                        @PathVariable("categoryId") String categoryId) {
327
        log.debug("toggle category status (isActive)");
328
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
329

    
330
        Category category = categoryDAO.findById(categoryId);
331
        if (category == null) {
332
            // EXCEPTION - Category not found
333
            throw new EntityNotFoundException("Toggle category status: Category with id: "+categoryId+" not found");
334
        }
335
        category.setIsActive(!category.getIsActive());
336

    
337
        this.toggleCategory(stakeholderId, topicId, category);
338

    
339
        return category.getIsActive();
340
    }
341

    
342
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-access", method = RequestMethod.POST)
343
    public Boolean toggleCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
344
                                        @PathVariable("topicId") String topicId,
345
                                        @PathVariable("categoryId") String categoryId) {
346
        log.debug("toggle category access (isPublic)");
347
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
348

    
349
        Category category = categoryDAO.findById(categoryId);
350
        if (category == null) {
351
            // EXCEPTION - Category not found
352
            throw new EntityNotFoundException("Toggle category access: Category with id: "+categoryId+" not found");
353
        }
354
        category.setIsPublic(!category.getIsPublic());
355

    
356
        this.toggleCategory(stakeholderId, topicId, category);
357

    
358
        return category.getIsPublic();
359
    }
360

    
361
    public void toggleCategory(String stakeholderId, String topicId, Category category) {
362
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
363

    
364
        if (stakeholder != null) {
365

    
366
            Topic<String> topic = topicDAO.findById(topicId);
367
            if (topic != null) {
368
                if (stakeholder.getTopics().contains(topicId)) {
369
                    if (topic.getCategories().contains(category.getId())) {
370
                        categoryDAO.save(category);
371
                        log.debug("Category toggled!");
372
                    } else {
373
                        // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
374
                        throw new PathNotValidException("Toggle category: Category with id: "+category.getId()+" not found in Topic: "+topicId);
375
                    }
376
                } else {
377
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
378
                    throw new PathNotValidException("Toggle category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
379
                }
380
            } else {
381
                // EXCEPTION - Topic not found
382
                throw new EntityNotFoundException("Toggle category: Topic with id: "+topicId+" not found");
383
            }
384
        } else {
385
            // EXCEPTION - Stakeholder not found
386
            throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found");
387
        }
388
    }
389

    
390
    public void deleteTree(Topic topic) {
391
        List<String> categories = topic.getCategories();
392
        for(String categoryId : categories) {
393
            Category category = categoryDAO.findById(categoryId);
394
            if (category == null) {
395
                // EXCEPTION - Category not found
396
                throw new EntityNotFoundException("Category delete tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")");
397
            }
398

    
399
            subCategoryController.deleteTree(category);
400

    
401
            categoryDAO.delete(categoryId);
402
        }
403
    }
404

    
405
    public void disConnectTree(Topic topic) {
406
        List<String> categories = topic.getCategories();
407
        for(String categoryId : categories) {
408
            Category category = categoryDAO.findById(categoryId);
409
            if (category == null) {
410
                // EXCEPTION - Category not found
411
                throw new EntityNotFoundException("Category disconnect tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")");
412
            }
413

    
414
            subCategoryController.disConnectTree(category);
415

    
416
            category.setDefaultId(null);
417
            categoryDAO.save(category);
418
        }
419
    }
420
}
(1-1/8)