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 TopicController {
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 CategoryController categoryController;
39

    
40
    public Topic<Category> buildTopic(Topic<Category> topicFull) {
41
        Topic<String> topic = new Topic<>(topicFull);
42

    
43
        List<String> categories = new ArrayList<>();
44
        List<Category> categoriesFull = new ArrayList<>();
45
        for(Category<SubCategory> category : topicFull.getCategories()) {
46
            Category<SubCategory> categoryFull = categoryController.buildCategory(category);
47
            categoriesFull.add(categoryFull);
48
            categories.add(categoryFull.getId());
49
        }
50
        topicFull.setCategories(categoriesFull);
51
        topic.setCategories(categories);
52

    
53
        topicDAO.save(topic);
54

    
55
        topicFull.setId(topic.getId());
56
        return topicFull;
57
    }
58

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

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

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

    
73
            Topic<String> topic = new Topic<>(topicFull);
74

    
75
            List<String> categories = new ArrayList<>();
76
            for(Category category : topicFull.getCategories()) {
77
                categories.add(category.getId());
78
            }
79
            topic.setCategories(categories);
80

    
81
            topicDAO.save(topic);
82

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

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

    
98
                topicFull.setId(topic.getId());
99
            }
100

    
101
            categories = null;
102
            topic = null;
103
        } else {
104
            // EXCEPTION - Stakeholder not found
105
            throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
106
        }
107
        return topicFull;
108
    }
109

    
110
    public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
111
        log.debug("On save default topic");
112

    
113
        List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
114
        for(Stakeholder _stakeholder : stakeholders) {
115
            Topic topicNew = new Topic();
116
            topicNew.copyFromDefault(topic);
117

    
118
            topicDAO.save(topicNew);
119

    
120
            List<String> topics = _stakeholder.getTopics();
121
            topics.add(topicNew.getId());
122

    
123
            stakeholderDAO.save(_stakeholder);
124
        }
125
    }
126

    
127
    public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
128
        log.debug("On update default topic");
129

    
130
        List<Topic> topics = topicDAO.findByDefaultId(topic.getId());
131
        boolean changed = false;
132
        for(Topic topicBasedOnDefault : topics) {
133
            if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())
134
                    && (oldTopic.getName() == null || oldTopic.getName().equals(topicBasedOnDefault.getName()))) {
135

    
136
                topicBasedOnDefault.setName(topic.getName());
137
                changed = true;
138
            }
139
            if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())
140
                    && (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) {
141

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

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

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

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

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

    
169
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
170

    
171
        if(stakeholder != null) {
172

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

    
176
                List<String> topics = stakeholder.getTopics();
177
                int index = topics.indexOf(topicId);
178
                if(index != -1) {
179
                    for(String categoryId : topic.getCategories()) {
180
                        Category<String> category = categoryDAO.findById(categoryId);
181
                        if(category == null) {
182
                            // EXCEPTION - Category not found
183
                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
184
                        }
185

    
186
                        for(String subCategoryId : category.getSubCategories()) {
187
                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
188
                            if (subcategory == null) {
189
                                // EXCEPTION - SubCategory not found
190
                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
191
                            }
192

    
193
                            for(String chartSectionId : subcategory.getCharts()) {
194
                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
195
                                if (chartSection == null) {
196
                                    // EXCEPTION - Section not found
197
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
198
                                }
199

    
200
                                for (String chartId : chartSection.getIndicators()) {
201
                                    indicatorDAO.delete(chartId);
202
                                }
203
                                subcategory.setCharts(null);
204
                                sectionDAO.delete(chartSectionId);
205
                            }
206

    
207
                            for(String numberSectionId : subcategory.getNumbers()) {
208
                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
209
                                if (numberSection == null) {
210
                                    // EXCEPTION - Section not found
211
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
212
                                }
213

    
214
                                for (String numberId : numberSection.getIndicators()) {
215
                                    indicatorDAO.delete(numberId);
216
                                }
217
                                subcategory.setNumbers(null);
218
                                sectionDAO.delete(numberSectionId);
219
                            }
220

    
221
                            subCategoryDAO.delete(subCategoryId);
222
                        }
223
                        category.setSubCategories(null);
224
                        categoryDAO.delete(categoryId);
225
                    }
226
                    topic.setCategories(null);
227

    
228
                    topics.remove(index);
229
                    stakeholderDAO.save(stakeholder);
230

    
231
                    topicDAO.delete(topicId);
232
                    log.debug("Category deleted!");
233
                } else {
234
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
235
                    throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
236
                }
237

    
238
            } else {
239
                // EXCEPTION - Topic not found
240
                throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
241
            }
242
        } else {
243
            // EXCEPTION - Stakeholder not found
244
            throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
245
        }
246
        return true;
247
    }
248

    
249
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
250
    public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
251
                                     @PathVariable("topicId") String topicId) {
252
        log.debug("toggle topic status (isActive)");
253
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
254

    
255
        Topic topic = topicDAO.findById(topicId);
256
        if (topic == null) {
257
            // EXCEPTION - Topic not found
258
            throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
259
        }
260
        topic.setIsActive(!topic.getIsActive());
261

    
262
        this.toggleTopic(stakeholderId, topic);
263

    
264
        return topic.getIsActive();
265
    }
266

    
267
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
268
    public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId,
269
                                         @PathVariable("topicId") String topicId) {
270
        log.debug("toggle topic access (isPublic)");
271
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
272

    
273
        Topic topic = topicDAO.findById(topicId);
274
        if (topic == null) {
275
            // EXCEPTION - Topic not found
276
            throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found");
277
        }
278
        topic.setIsPublic(!topic.getIsPublic());
279

    
280
        this.toggleTopic(stakeholderId, topic);
281

    
282
        return topic.getIsPublic();
283
    }
284

    
285
    public void toggleTopic(String stakeholderId, Topic topic) {
286
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
287

    
288
        if (stakeholder != null) {
289
            if (stakeholder.getTopics().contains(topic.getId())) {
290
                topicDAO.save(topic);
291
                log.debug("Topic toggled!");
292
            } else {
293
                // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
294
                throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
295
            }
296
        } else {
297
            // EXCEPTION - Stakeholder not found
298
            throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
299
        }
300
    }
301
}
(8-8/8)