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 IndicatorDAO indicatorDAO;
33

    
34
    @Autowired
35
    private SubCategoryController subCategoryController;
36

    
37
    public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
38
        Category<String> category = new Category<>(categoryFull);
39

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

    
50
        categoryDAO.save(category);
51

    
52
        categoryFull.setId(category.getId());
53
        return categoryFull;
54
    }
55

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

    
63
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
64

    
65
        if(stakeholder != null) {
66

    
67
            Topic<String> topic = topicDAO.findById(topicId);
68
            if(topic != null) {
69
                if(stakeholder.getTopics().contains(topicId)) {
70
                    // if category not exists (no id), create a new default subcategory, identical to category
71
                    if(categoryFull.getId() == null) {
72
                        SubCategory<String> subCategory = new SubCategory<>();
73
                        subCategory.createOverviewSubCategory(categoryFull);
74

    
75
                        subCategoryDAO.save(subCategory);
76
                        List<SubCategory> subCategories = categoryFull.getSubCategories();
77
                        subCategories.add(subCategory);
78
                    }
79

    
80

    
81
                    Category<String> category = new Category<>(categoryFull);
82

    
83
                    List<String> subCategories = new ArrayList<>();
84
                    for(SubCategory subCategory : categoryFull.getSubCategories()) {
85
                        subCategories.add(subCategory.getId());
86
                    }
87
                    category.setSubCategories(subCategories);
88

    
89
                    categoryDAO.save(category);
90

    
91
                    if(stakeholder.getDefaultId() == null) {
92
                        if(categoryFull.getId() == null) {
93
                            onSaveDefaultCategory(category, topicId);
94
                        } else {
95
                            onUpdateDefaultCategory(category);
96
                        }
97
                    }
98

    
99
                    List<String> categories = topic.getCategories();
100
                    int index = categories.indexOf(category.getId());
101
                    if(index == -1) {
102
                        categories.add(category.getId());
103
                        topicDAO.save(topic);
104
                        log.debug("Category saved!");
105

    
106
                        categoryFull.setId(category.getId());
107
                    }
108

    
109
                    subCategories = null;
110
                    category = null;
111
                } else {
112
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
113
                    throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
114
                }
115
            } else {
116
                // EXCEPTION - Topic not found
117
                throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
118
            }
119
        } else {
120
            // EXCEPTION - Stakeholder not found
121
            throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
122
        }
123
        return categoryFull;
124
    }
125

    
126
    public void onSaveDefaultCategory(Category<String> category, String topicId) {
127
        log.debug("On save default category");
128

    
129
        List<Topic> topics = topicDAO.findByDefaultId(topicId);
130
        for(Topic topic : topics) {
131
            Category categoryNew = new Category();
132
            categoryNew.copyFromDefault(category);
133

    
134
            categoryDAO.save(categoryNew);
135

    
136
            List<String> categories = topic.getCategories();
137
            categories.add(categoryNew.getId());
138

    
139
            topicDAO.save(topic);
140
        }
141
        String subCategoryOverviewId = category.getSubCategories().get(0);
142
        SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId);
143
        subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
144
    }
145

    
146
    public void onUpdateDefaultCategory(Category category) {
147
        log.debug("On update default category");
148

    
149
        List<Category> categories = categoryDAO.findByDefaultId(category.getId());
150
        boolean changed = false;
151
        for(Category categoryBasedOnDefault : categories) {
152
            if(category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName())) {
153
                changed = true;
154
            }
155
            if(category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription())) {
156
                changed = true;
157
            }
158

    
159
            if(!changed) {
160
                break;
161
            }
162
            categoryBasedOnDefault.setName(category.getName());
163
            categoryBasedOnDefault.setDescription(category.getDescription());
164
            categoryDAO.save(categoryBasedOnDefault);
165
        }
166
    }
167

    
168
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
169
    public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
170
                                  @PathVariable("topicId") String topicId,
171
                                  @PathVariable("categoryId") String categoryId) {
172
        log.debug("delete category");
173
        log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId);
174

    
175
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
176

    
177
        if(stakeholder != null) {
178

    
179
            Topic<String> topic = topicDAO.findById(topicId);
180
            if(topic != null) {
181
                if(stakeholder.getTopics().contains(topicId)) {
182

    
183
                    Category<String> category = categoryDAO.findById(categoryId);
184
                    if(category != null) {
185

    
186
                        List<String> categories = topic.getCategories();
187
                        int index = categories.indexOf(categoryId);
188
                        if(index != -1) {
189
                            for(String subCategoryId : category.getSubCategories()) {
190
                                SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
191
                                if(subcategory == null) {
192
                                    // EXCEPTION - SubCategory not found
193
                                    throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
194
                                }
195
                                for(String chartId : subcategory.getCharts()) {
196
                                    indicatorDAO.delete(chartId);
197
                                }
198
                                subcategory.setCharts(null);
199

    
200
                                for(String numberId : subcategory.getNumbers()) {
201
                                    indicatorDAO.delete(numberId);
202
                                }
203
                                subcategory.setNumbers(null);
204

    
205
                                subCategoryDAO.delete(subCategoryId);
206
                            }
207
                            category.setSubCategories(null);
208

    
209
                            categories.remove(index);
210
                            topicDAO.save(topic);
211

    
212
                            categoryDAO.delete(categoryId);
213
                            log.debug("Category deleted!");
214
                        } else {
215
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
216
                            throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
217
                        }
218

    
219
                    } else {
220
                        // EXCEPTION - Category not found
221
                        throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
222
                    }
223
                } else {
224
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
225
                    throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
226
                }
227
            } else {
228
                // EXCEPTION - Topic not found
229
                throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
230
            }
231
        } else {
232
            // EXCEPTION - Stakeholder not found
233
            throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
234
        }
235
        return true;
236
    }
237

    
238
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST)
239
    public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
240
                                        @PathVariable("topicId") String topicId,
241
                                        @PathVariable("categoryId") String categoryId) {
242
        log.debug("toggle category status (isActive)");
243
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
244

    
245
        Category category = categoryDAO.findById(categoryId);
246
        if (category == null) {
247
            // EXCEPTION - Category not found
248
            throw new EntityNotFoundException("Toggle category status: Category with id: "+categoryId+" not found");
249
        }
250
        category.setIsActive(!category.getIsActive());
251

    
252
        this.toggleCategory(stakeholderId, topicId, category);
253

    
254
        return category.getIsActive();
255
    }
256

    
257
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-access", method = RequestMethod.POST)
258
    public Boolean toggleCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
259
                                        @PathVariable("topicId") String topicId,
260
                                        @PathVariable("categoryId") String categoryId) {
261
        log.debug("toggle category access (isPublic)");
262
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
263

    
264
        Category category = categoryDAO.findById(categoryId);
265
        if (category == null) {
266
            // EXCEPTION - Category not found
267
            throw new EntityNotFoundException("Toggle category access: Category with id: "+categoryId+" not found");
268
        }
269
        category.setIsPublic(!category.getIsPublic());
270

    
271
        this.toggleCategory(stakeholderId, topicId, category);
272

    
273
        return category.getIsPublic();
274
    }
275

    
276
    public void toggleCategory(String stakeholderId, String topicId, Category category) {
277
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
278

    
279
        if (stakeholder != null) {
280

    
281
            Topic<String> topic = topicDAO.findById(topicId);
282
            if (topic != null) {
283
                if (stakeholder.getTopics().contains(topicId)) {
284
                    if (topic.getCategories().contains(category.getId())) {
285
                        categoryDAO.save(category);
286
                        log.debug("Category toggled!");
287
                    } else {
288
                        // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
289
                        throw new PathNotValidException("Toggle category: Category with id: "+category.getId()+" not found in Topic: "+topicId);
290
                    }
291
                } else {
292
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
293
                    throw new PathNotValidException("Toggle category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
294
                }
295
            } else {
296
                // EXCEPTION - Topic not found
297
                throw new EntityNotFoundException("Toggle category: Topic with id: "+topicId+" not found");
298
            }
299
        } else {
300
            // EXCEPTION - Stakeholder not found
301
            throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found");
302
        }
303
    }
304
}
(1-1/7)