Project

General

Profile

1 57671 konstantin
package eu.dnetlib.uoamonitorservice.controllers;
2
3
import eu.dnetlib.uoamonitorservice.dao.*;
4 57964 konstantin
import eu.dnetlib.uoamonitorservice.entities.*;
5 57671 konstantin
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 57671 konstantin
import java.util.List;
14
15
@RestController
16
@CrossOrigin(origins = "*")
17
public class TopicController {
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 57964 konstantin
    private SectionDAO sectionDAO;
34
35
    @Autowired
36 57671 konstantin
    private IndicatorDAO indicatorDAO;
37
38
    @Autowired
39
    private CategoryController categoryController;
40
41
    public Topic<Category> buildTopic(Topic<Category> topicFull) {
42
        Topic<String> topic = new Topic<>(topicFull);
43
44
        List<String> categories = new ArrayList<>();
45
        List<Category> categoriesFull = new ArrayList<>();
46
        for(Category<SubCategory> category : topicFull.getCategories()) {
47
            Category<SubCategory> categoryFull = categoryController.buildCategory(category);
48
            categoriesFull.add(categoryFull);
49
            categories.add(categoryFull.getId());
50
        }
51
        topicFull.setCategories(categoriesFull);
52
        topic.setCategories(categories);
53
54 57923 konstantin
        topicDAO.save(topic);
55 57671 konstantin
56 57923 konstantin
        topicFull.setId(topic.getId());
57 57671 konstantin
        return topicFull;
58
    }
59
60
    @RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
61
    public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
62
                                     @RequestBody Topic<Category> topicFull) {
63
        log.debug("save topic");
64 57923 konstantin
        log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId);
65 57671 konstantin
66
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
67
68
        if(stakeholder != null) {
69 58708 konstantin
            Topic<String> oldTopic = null;
70
            if(topicFull.getId() != null) {
71
                oldTopic = topicDAO.findById(topicFull.getId());
72
            }
73 57671 konstantin
74
            Topic<String> topic = new Topic<>(topicFull);
75
76
            List<String> categories = new ArrayList<>();
77
            for(Category category : topicFull.getCategories()) {
78
                categories.add(category.getId());
79
            }
80
            topic.setCategories(categories);
81
82 57923 konstantin
            topicDAO.save(topic);
83 57671 konstantin
84 57923 konstantin
            if(stakeholder.getDefaultId() == null) {
85
                if(topicFull.getId() == null) {
86
                    onSaveDefaultTopic(topic, stakeholderId);
87
                } else {
88 58708 konstantin
                    onUpdateDefaultTopic(topic, oldTopic);
89 57923 konstantin
                }
90
            }
91
92 57671 konstantin
            List<String> topics = stakeholder.getTopics();
93 57923 konstantin
            int index = topics.indexOf(topic.getId());
94 57671 konstantin
            if(index == -1) {
95 57923 konstantin
                topics.add(topic.getId());
96 57671 konstantin
                stakeholderDAO.save(stakeholder);
97
                log.debug("Topic saved!");
98
99 57923 konstantin
                topicFull.setId(topic.getId());
100 57671 konstantin
            }
101
102
            categories = null;
103
            topic = null;
104
        } else {
105
            // EXCEPTION - Stakeholder not found
106
            throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
107
        }
108
        return topicFull;
109
    }
110
111 57923 konstantin
    public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
112
        log.debug("On save default topic");
113
114
        List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
115
        for(Stakeholder _stakeholder : stakeholders) {
116
            Topic topicNew = new Topic();
117
            topicNew.copyFromDefault(topic);
118
119
            topicDAO.save(topicNew);
120
121
            List<String> topics = _stakeholder.getTopics();
122
            topics.add(topicNew.getId());
123
124
            stakeholderDAO.save(_stakeholder);
125
        }
126
    }
127
128 58708 konstantin
    public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
129 57923 konstantin
        log.debug("On update default topic");
130
131
        List<Topic> topics = topicDAO.findByDefaultId(topic.getId());
132
        boolean changed = false;
133
        for(Topic topicBasedOnDefault : topics) {
134 58708 konstantin
            if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())
135
                    && (oldTopic.getName() == null || oldTopic.getName().equals(topicBasedOnDefault.getName()))) {
136
137
                topicBasedOnDefault.setName(topic.getName());
138 59483 konstantin
                topicBasedOnDefault.setAlias(topic.getAlias());
139 57923 konstantin
                changed = true;
140
            }
141 58708 konstantin
            if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())
142
                    && (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) {
143
144
                topicBasedOnDefault.setDescription(topic.getDescription());
145 57923 konstantin
                changed = true;
146
            }
147 58954 konstantin
            if(topic.getIcon() != null && !topic.getIcon().equals(topicBasedOnDefault.getIcon())
148
                    && (oldTopic.getIcon() == null || oldTopic.getIcon().equals(topicBasedOnDefault.getIcon()))) {
149 57923 konstantin
150 58954 konstantin
                topicBasedOnDefault.setIcon(topic.getIcon());
151
                changed = true;
152
            }
153
154 57923 konstantin
            if(!changed) {
155 58708 konstantin
//                break;
156
                continue;
157 57923 konstantin
            }
158 58708 konstantin
159
//            topicBasedOnDefault.setName(topic.getName());
160
//            topicBasedOnDefault.setDescription(topic.getDescription());
161 57923 konstantin
            topicDAO.save(topicBasedOnDefault);
162
        }
163
    }
164
165 57671 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
166
    public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
167 58978 konstantin
                               @PathVariable("topicId") String topicId,
168
                               @RequestParam(required = false) String children) {
169 57671 konstantin
        log.debug("delete topic");
170 57923 konstantin
        log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
171 57671 konstantin
172
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
173
174
        if(stakeholder != null) {
175
176
            Topic<String> topic = topicDAO.findById(topicId);
177
            if(topic != null) {
178
179
                List<String> topics = stakeholder.getTopics();
180
                int index = topics.indexOf(topicId);
181
                if(index != -1) {
182 58978 konstantin
                    // this topic belongs in default profile
183
                    if(stakeholder.getDefaultId() == null && children != null) {
184
                        onDeleteDefaultTopic(topicId, stakeholderId, children);
185
                    }
186 57671 konstantin
187 58978 konstantin
//                    for(String categoryId : topic.getCategories()) {
188
//                        Category<String> category = categoryDAO.findById(categoryId);
189
//                        if(category == null) {
190
//                            // EXCEPTION - Category not found
191
//                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
192
//                        }
193
//
194
//                        for(String subCategoryId : category.getSubCategories()) {
195
//                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
196
//                            if (subcategory == null) {
197
//                                // EXCEPTION - SubCategory not found
198
//                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
199
//                            }
200
//
201
//                            for(String chartSectionId : subcategory.getCharts()) {
202
//                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
203
//                                if (chartSection == null) {
204
//                                    // EXCEPTION - Section not found
205
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
206
//                                }
207
//
208
//                                for (String chartId : chartSection.getIndicators()) {
209
//                                    indicatorDAO.delete(chartId);
210
//                                }
211
//                                subcategory.setCharts(null);
212
//                                sectionDAO.delete(chartSectionId);
213
//                            }
214
//
215
//                            for(String numberSectionId : subcategory.getNumbers()) {
216
//                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
217
//                                if (numberSection == null) {
218
//                                    // EXCEPTION - Section not found
219
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
220
//                                }
221
//
222
//                                for (String numberId : numberSection.getIndicators()) {
223
//                                    indicatorDAO.delete(numberId);
224
//                                }
225
//                                subcategory.setNumbers(null);
226
//                                sectionDAO.delete(numberSectionId);
227
//                            }
228
//
229
//                            subCategoryDAO.delete(subCategoryId);
230
//                        }
231
//                        category.setSubCategories(null);
232
//                        categoryDAO.delete(categoryId);
233
//                    }
234
                    categoryController.deleteTree(topic);
235 57671 konstantin
236
                    topic.setCategories(null);
237
238
                    topics.remove(index);
239
                    stakeholderDAO.save(stakeholder);
240
241
                    topicDAO.delete(topicId);
242
                    log.debug("Category deleted!");
243
                } else {
244
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
245
                    throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
246
                }
247
248
            } else {
249
                // EXCEPTION - Topic not found
250
                throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
251
            }
252
        } else {
253
            // EXCEPTION - Stakeholder not found
254
            throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
255
        }
256
        return true;
257
    }
258 57934 konstantin
259 58978 konstantin
260
    public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) {
261
        if(children.equals("delete")) {
262
            List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId);
263
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
264
265
            for(Stakeholder stakeholder : stakeholders) {
266
                Iterator<Topic> topicsIterator = topics.iterator();
267
                while(topicsIterator.hasNext()) {
268
                    Topic topic = topicsIterator.next();
269
270
                    String topicId = topic.getId();
271
272
                    if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) {
273
                        topicsIterator.remove();
274
275
                        stakeholder.getTopics().remove(topicId);
276
                        stakeholderDAO.save(stakeholder);
277
278
                        categoryController.deleteTree(topic);
279
280
                        topicDAO.delete(topicId);
281
                        log.debug("Topic with id: "+topicId+" deleted!");
282
283
                        break;
284
                    }
285
                }
286
            }
287
        } else if(children.equals("disconnect")) {
288
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
289
            for(Topic topic : topics) {
290
                categoryController.disConnectTree(topic);
291
292
                topic.setDefaultId(null);
293
                topicDAO.save(topic);
294
295
                log.debug("DefaultId for Topic with id: "+topic.getId()+" empty!");
296
            }
297
        }
298
        return true;
299
    }
300
301 58992 konstantin
    @RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
302
    public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
303
                                     @RequestBody List<String> topics) {
304
        log.debug("reorder topics");
305
        log.debug("Stakeholder: "+stakeholderId);
306
307
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
308
309
        if(stakeholder != null) {
310
            stakeholder.setTopics(topics);
311
312
            stakeholderDAO.save(stakeholder);
313
            log.debug("Topics reordered!");
314
315
            List<Topic> topicsFull = new ArrayList<>();
316
            for (String topicId : topics) {
317
                topicsFull.add(topicDAO.findById(topicId));
318
            }
319
            return topicsFull;
320
        } else {
321
            // EXCEPTION - Stakeholder not found
322
            throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found");
323
        }
324
    }
325
326 57934 konstantin
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
327
    public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
328
                                     @PathVariable("topicId") String topicId) {
329
        log.debug("toggle topic status (isActive)");
330
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
331
332
        Topic topic = topicDAO.findById(topicId);
333
        if (topic == null) {
334
            // EXCEPTION - Topic not found
335
            throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
336
        }
337
        topic.setIsActive(!topic.getIsActive());
338
339
        this.toggleTopic(stakeholderId, topic);
340
341
        return topic.getIsActive();
342
    }
343
344
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
345 57964 konstantin
    public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId,
346 57934 konstantin
                                         @PathVariable("topicId") String topicId) {
347
        log.debug("toggle topic access (isPublic)");
348
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
349
350
        Topic topic = topicDAO.findById(topicId);
351
        if (topic == null) {
352
            // EXCEPTION - Topic not found
353
            throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found");
354
        }
355
        topic.setIsPublic(!topic.getIsPublic());
356
357
        this.toggleTopic(stakeholderId, topic);
358
359
        return topic.getIsPublic();
360
    }
361
362
    public void toggleTopic(String stakeholderId, Topic topic) {
363
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
364
365
        if (stakeholder != null) {
366
            if (stakeholder.getTopics().contains(topic.getId())) {
367
                topicDAO.save(topic);
368
                log.debug("Topic toggled!");
369
            } else {
370
                // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
371
                throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
372
            }
373
        } else {
374
            // EXCEPTION - Stakeholder not found
375
            throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
376
        }
377
    }
378 58978 konstantin
379
    public void deleteTree(Stakeholder stakeholder) {
380
        List<String> topics = stakeholder.getTopics();
381
        for(String topicId : topics) {
382
            Topic topic = topicDAO.findById(topicId);
383
            if (topic == null) {
384
                // EXCEPTION - Topic not found
385
                throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
386
            }
387
388
            categoryController.deleteTree(topic);
389
390
            topicDAO.delete(topicId);
391
        }
392
    }
393 57671 konstantin
}