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 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
    private SectionDAO sectionDAO;
34

    
35
    @Autowired
36
    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
        topicDAO.save(topic);
55

    
56
        topicFull.setId(topic.getId());
57
        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
        log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId);
65

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

    
68
        if(stakeholder != null) {
69
            Topic<String> oldTopic = null;
70
            if(topicFull.getId() != null) {
71
                oldTopic = topicDAO.findById(topicFull.getId());
72
            }
73

    
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
            topicDAO.save(topic);
83

    
84
            if(stakeholder.getDefaultId() == null) {
85
                if(topicFull.getId() == null) {
86
                    onSaveDefaultTopic(topic, stakeholderId);
87
                } else {
88
                    onUpdateDefaultTopic(topic, oldTopic);
89
                }
90
            }
91

    
92
            List<String> topics = stakeholder.getTopics();
93
            int index = topics.indexOf(topic.getId());
94
            if(index == -1) {
95
                topics.add(topic.getId());
96
                stakeholderDAO.save(stakeholder);
97
                log.debug("Topic saved!");
98

    
99
                topicFull.setId(topic.getId());
100
            }
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
    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
    public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
129
        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
            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
                changed = true;
139
            }
140
            if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())
141
                    && (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) {
142

    
143
                topicBasedOnDefault.setDescription(topic.getDescription());
144
                changed = true;
145
            }
146
            if(topic.getIcon() != null && !topic.getIcon().equals(topicBasedOnDefault.getIcon())
147
                    && (oldTopic.getIcon() == null || oldTopic.getIcon().equals(topicBasedOnDefault.getIcon()))) {
148

    
149
                topicBasedOnDefault.setIcon(topic.getIcon());
150
                changed = true;
151
            }
152

    
153
            if(!changed) {
154
//                break;
155
                continue;
156
            }
157

    
158
//            topicBasedOnDefault.setName(topic.getName());
159
//            topicBasedOnDefault.setDescription(topic.getDescription());
160
            topicDAO.save(topicBasedOnDefault);
161
        }
162
    }
163

    
164
    @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
165
    public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
166
                               @PathVariable("topicId") String topicId,
167
                               @RequestParam(required = false) String children) {
168
        log.debug("delete topic");
169
        log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
170

    
171
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
172

    
173
        if(stakeholder != null) {
174

    
175
            Topic<String> topic = topicDAO.findById(topicId);
176
            if(topic != null) {
177

    
178
                List<String> topics = stakeholder.getTopics();
179
                int index = topics.indexOf(topicId);
180
                if(index != -1) {
181
                    // this topic belongs in default profile
182
                    if(stakeholder.getDefaultId() == null && children != null) {
183
                        onDeleteDefaultTopic(topicId, stakeholderId, children);
184
                    }
185

    
186
//                    for(String categoryId : topic.getCategories()) {
187
//                        Category<String> category = categoryDAO.findById(categoryId);
188
//                        if(category == null) {
189
//                            // EXCEPTION - Category not found
190
//                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
191
//                        }
192
//
193
//                        for(String subCategoryId : category.getSubCategories()) {
194
//                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
195
//                            if (subcategory == null) {
196
//                                // EXCEPTION - SubCategory not found
197
//                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
198
//                            }
199
//
200
//                            for(String chartSectionId : subcategory.getCharts()) {
201
//                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
202
//                                if (chartSection == null) {
203
//                                    // EXCEPTION - Section not found
204
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
205
//                                }
206
//
207
//                                for (String chartId : chartSection.getIndicators()) {
208
//                                    indicatorDAO.delete(chartId);
209
//                                }
210
//                                subcategory.setCharts(null);
211
//                                sectionDAO.delete(chartSectionId);
212
//                            }
213
//
214
//                            for(String numberSectionId : subcategory.getNumbers()) {
215
//                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
216
//                                if (numberSection == null) {
217
//                                    // EXCEPTION - Section not found
218
//                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
219
//                                }
220
//
221
//                                for (String numberId : numberSection.getIndicators()) {
222
//                                    indicatorDAO.delete(numberId);
223
//                                }
224
//                                subcategory.setNumbers(null);
225
//                                sectionDAO.delete(numberSectionId);
226
//                            }
227
//
228
//                            subCategoryDAO.delete(subCategoryId);
229
//                        }
230
//                        category.setSubCategories(null);
231
//                        categoryDAO.delete(categoryId);
232
//                    }
233
                    categoryController.deleteTree(topic);
234

    
235
                    topic.setCategories(null);
236

    
237
                    topics.remove(index);
238
                    stakeholderDAO.save(stakeholder);
239

    
240
                    topicDAO.delete(topicId);
241
                    log.debug("Category deleted!");
242
                } else {
243
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
244
                    throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
245
                }
246

    
247
            } else {
248
                // EXCEPTION - Topic not found
249
                throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
250
            }
251
        } else {
252
            // EXCEPTION - Stakeholder not found
253
            throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
254
        }
255
        return true;
256
    }
257

    
258

    
259
    public boolean onDeleteDefaultTopic(String defaultTopicId, String defaultStakeholderId, String children) {
260
        if(children.equals("delete")) {
261
            List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholderId);
262
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
263

    
264
            for(Stakeholder stakeholder : stakeholders) {
265
                Iterator<Topic> topicsIterator = topics.iterator();
266
                while(topicsIterator.hasNext()) {
267
                    Topic topic = topicsIterator.next();
268

    
269
                    String topicId = topic.getId();
270

    
271
                    if(stakeholder.getTopics() != null && stakeholder.getTopics().contains(topicId)) {
272
                        topicsIterator.remove();
273

    
274
                        stakeholder.getTopics().remove(topicId);
275
                        stakeholderDAO.save(stakeholder);
276

    
277
                        categoryController.deleteTree(topic);
278

    
279
                        topicDAO.delete(topicId);
280
                        log.debug("Topic with id: "+topicId+" deleted!");
281

    
282
                        break;
283
                    }
284
                }
285
            }
286
        } else if(children.equals("disconnect")) {
287
            List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
288
            for(Topic topic : topics) {
289
                categoryController.disConnectTree(topic);
290

    
291
                topic.setDefaultId(null);
292
                topicDAO.save(topic);
293

    
294
                log.debug("DefaultId for Topic with id: "+topic.getId()+" empty!");
295
            }
296
        }
297
        return true;
298
    }
299

    
300
    @RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
301
    public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
302
                                     @RequestBody List<String> topics) {
303
        log.debug("reorder topics");
304
        log.debug("Stakeholder: "+stakeholderId);
305

    
306
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
307

    
308
        if(stakeholder != null) {
309
            stakeholder.setTopics(topics);
310

    
311
            stakeholderDAO.save(stakeholder);
312
            log.debug("Topics reordered!");
313

    
314
            List<Topic> topicsFull = new ArrayList<>();
315
            for (String topicId : topics) {
316
                topicsFull.add(topicDAO.findById(topicId));
317
            }
318
            return topicsFull;
319
        } else {
320
            // EXCEPTION - Stakeholder not found
321
            throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found");
322
        }
323
    }
324

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

    
331
        Topic topic = topicDAO.findById(topicId);
332
        if (topic == null) {
333
            // EXCEPTION - Topic not found
334
            throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
335
        }
336
        topic.setIsActive(!topic.getIsActive());
337

    
338
        this.toggleTopic(stakeholderId, topic);
339

    
340
        return topic.getIsActive();
341
    }
342

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

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

    
356
        this.toggleTopic(stakeholderId, topic);
357

    
358
        return topic.getIsPublic();
359
    }
360

    
361
    public void toggleTopic(String stakeholderId, Topic topic) {
362
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
363

    
364
        if (stakeholder != null) {
365
            if (stakeholder.getTopics().contains(topic.getId())) {
366
                topicDAO.save(topic);
367
                log.debug("Topic toggled!");
368
            } else {
369
                // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
370
                throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
371
            }
372
        } else {
373
            // EXCEPTION - Stakeholder not found
374
            throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
375
        }
376
    }
377

    
378
    public void deleteTree(Stakeholder stakeholder) {
379
        List<String> topics = stakeholder.getTopics();
380
        for(String topicId : topics) {
381
            Topic topic = topicDAO.findById(topicId);
382
            if (topic == null) {
383
                // EXCEPTION - Topic not found
384
                throw new EntityNotFoundException("Topic delete tree: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
385
            }
386

    
387
            categoryController.deleteTree(topic);
388

    
389
            topicDAO.delete(topicId);
390
        }
391
    }
392
}
(8-8/8)