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.List;
13

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

    
19
    @Autowired
20
    private StakeholderDAO stakeholderDAO;
21

    
22
    @Autowired
23
    private TopicDAO topicDAO;
24

    
25
    @Autowired
26
    private CategoryDAO categoryDAO;
27

    
28
    @Autowired
29
    private SubCategoryDAO subCategoryDAO;
30

    
31
    @Autowired
32
    private SectionDAO sectionDAO;
33

    
34
    @Autowired
35
    private IndicatorDAO indicatorDAO;
36

    
37
    @Autowired
38
    private SubCategoryController subCategoryController;
39

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

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

    
53
        categoryDAO.save(category);
54

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

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

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

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

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

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

    
87

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

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

    
96
                    categoryDAO.save(category);
97

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

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

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

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

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

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

    
141
            categoryDAO.save(categoryNew);
142

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

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

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

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

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

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

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

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

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

    
190
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
191

    
192
        if(stakeholder != null) {
193

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

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

    
201
                        List<String> categories = topic.getCategories();
202
                        int index = categories.indexOf(categoryId);
203
                        if(index != -1) {
204
                            for(String subCategoryId : category.getSubCategories()) {
205
                                SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
206
                                if(subcategory == null) {
207
                                    // EXCEPTION - SubCategory not found
208
                                    throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
209
                                }
210

    
211
                                for(String chartSectionId : subcategory.getCharts()) {
212
                                    Section<String> chartSection = sectionDAO.findById(chartSectionId);
213
                                    if (chartSection == null) {
214
                                        // EXCEPTION - Section not found
215
                                        throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
216
                                    }
217

    
218
                                    for (String chartId : chartSection.getIndicators()) {
219
                                        indicatorDAO.delete(chartId);
220
                                    }
221
                                    subcategory.setCharts(null);
222
                                    sectionDAO.delete(chartSectionId);
223
                                }
224

    
225
                                for(String numberSectionId : subcategory.getNumbers()) {
226
                                    Section<String> numberSection = sectionDAO.findById(numberSectionId);
227
                                    if (numberSection == null) {
228
                                        // EXCEPTION - Section not found
229
                                        throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
230
                                    }
231

    
232
                                    for (String numberId : numberSection.getIndicators()) {
233
                                        indicatorDAO.delete(numberId);
234
                                    }
235
                                    subcategory.setNumbers(null);
236
                                    sectionDAO.delete(numberSectionId);
237
                                }
238

    
239
                                subCategoryDAO.delete(subCategoryId);
240
                            }
241
                            category.setSubCategories(null);
242

    
243
                            categories.remove(index);
244
                            topicDAO.save(topic);
245

    
246
                            categoryDAO.delete(categoryId);
247
                            log.debug("Category deleted!");
248
                        } else {
249
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
250
                            throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
251
                        }
252

    
253
                    } else {
254
                        // EXCEPTION - Category not found
255
                        throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
256
                    }
257
                } else {
258
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
259
                    throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
260
                }
261
            } else {
262
                // EXCEPTION - Topic not found
263
                throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
264
            }
265
        } else {
266
            // EXCEPTION - Stakeholder not found
267
            throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
268
        }
269
        return true;
270
    }
271

    
272
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST)
273
    public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
274
                                        @PathVariable("topicId") String topicId,
275
                                        @PathVariable("categoryId") String categoryId) {
276
        log.debug("toggle category status (isActive)");
277
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
278

    
279
        Category category = categoryDAO.findById(categoryId);
280
        if (category == null) {
281
            // EXCEPTION - Category not found
282
            throw new EntityNotFoundException("Toggle category status: Category with id: "+categoryId+" not found");
283
        }
284
        category.setIsActive(!category.getIsActive());
285

    
286
        this.toggleCategory(stakeholderId, topicId, category);
287

    
288
        return category.getIsActive();
289
    }
290

    
291
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-access", method = RequestMethod.POST)
292
    public Boolean toggleCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
293
                                        @PathVariable("topicId") String topicId,
294
                                        @PathVariable("categoryId") String categoryId) {
295
        log.debug("toggle category access (isPublic)");
296
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
297

    
298
        Category category = categoryDAO.findById(categoryId);
299
        if (category == null) {
300
            // EXCEPTION - Category not found
301
            throw new EntityNotFoundException("Toggle category access: Category with id: "+categoryId+" not found");
302
        }
303
        category.setIsPublic(!category.getIsPublic());
304

    
305
        this.toggleCategory(stakeholderId, topicId, category);
306

    
307
        return category.getIsPublic();
308
    }
309

    
310
    public void toggleCategory(String stakeholderId, String topicId, Category category) {
311
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
312

    
313
        if (stakeholder != null) {
314

    
315
            Topic<String> topic = topicDAO.findById(topicId);
316
            if (topic != null) {
317
                if (stakeholder.getTopics().contains(topicId)) {
318
                    if (topic.getCategories().contains(category.getId())) {
319
                        categoryDAO.save(category);
320
                        log.debug("Category toggled!");
321
                    } else {
322
                        // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
323
                        throw new PathNotValidException("Toggle category: Category with id: "+category.getId()+" not found in Topic: "+topicId);
324
                    }
325
                } else {
326
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
327
                    throw new PathNotValidException("Toggle category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
328
                }
329
            } else {
330
                // EXCEPTION - Topic not found
331
                throw new EntityNotFoundException("Toggle category: Topic with id: "+topicId+" not found");
332
            }
333
        } else {
334
            // EXCEPTION - Stakeholder not found
335
            throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found");
336
        }
337
    }
338
}
(1-1/8)