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

    
69
            Topic<String> topic = new Topic<>(topicFull);
70

    
71
            List<String> categories = new ArrayList<>();
72
            for(Category category : topicFull.getCategories()) {
73
                categories.add(category.getId());
74
            }
75
            topic.setCategories(categories);
76

    
77
            topicDAO.save(topic);
78

    
79
            if(stakeholder.getDefaultId() == null) {
80
                if(topicFull.getId() == null) {
81
                    onSaveDefaultTopic(topic, stakeholderId);
82
                } else {
83
                    onUpdateDefaultTopic(topic);
84
                }
85
            }
86

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

    
94
                topicFull.setId(topic.getId());
95
            }
96

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

    
106
    public void onSaveDefaultTopic(Topic topic, String stakeholderId) {
107
        log.debug("On save default topic");
108

    
109
        List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
110
        for(Stakeholder _stakeholder : stakeholders) {
111
            Topic topicNew = new Topic();
112
            topicNew.copyFromDefault(topic);
113

    
114
            topicDAO.save(topicNew);
115

    
116
            List<String> topics = _stakeholder.getTopics();
117
            topics.add(topicNew.getId());
118

    
119
            stakeholderDAO.save(_stakeholder);
120
        }
121
    }
122

    
123
    public void onUpdateDefaultTopic(Topic topic) {
124
        log.debug("On update default topic");
125

    
126
        List<Topic> topics = topicDAO.findByDefaultId(topic.getId());
127
        boolean changed = false;
128
        for(Topic topicBasedOnDefault : topics) {
129
            if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName())) {
130
                changed = true;
131
            }
132
            if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription())) {
133
                changed = true;
134
            }
135

    
136
            if(!changed) {
137
                break;
138
            }
139
            topicBasedOnDefault.setName(topic.getName());
140
            topicBasedOnDefault.setDescription(topic.getDescription());
141
            topicDAO.save(topicBasedOnDefault);
142
        }
143
    }
144

    
145
    @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
146
    public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
147
                               @PathVariable("topicId") String topicId) {
148
        log.debug("delete topic");
149
        log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
150

    
151
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
152

    
153
        if(stakeholder != null) {
154

    
155
            Topic<String> topic = topicDAO.findById(topicId);
156
            if(topic != null) {
157

    
158
                List<String> topics = stakeholder.getTopics();
159
                int index = topics.indexOf(topicId);
160
                if(index != -1) {
161
                    for(String categoryId : topic.getCategories()) {
162
                        Category<String> category = categoryDAO.findById(categoryId);
163
                        if(category == null) {
164
                            // EXCEPTION - Category not found
165
                            throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
166
                        }
167

    
168
                        for(String subCategoryId : category.getSubCategories()) {
169
                            SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
170
                            if (subcategory == null) {
171
                                // EXCEPTION - SubCategory not found
172
                                throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
173
                            }
174

    
175
                            for(String chartSectionId : subcategory.getCharts()) {
176
                                Section<String> chartSection = sectionDAO.findById(chartSectionId);
177
                                if (chartSection == null) {
178
                                    // EXCEPTION - Section not found
179
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
180
                                }
181

    
182
                                for (String chartId : chartSection.getIndicators()) {
183
                                    indicatorDAO.delete(chartId);
184
                                }
185
                                subcategory.setCharts(null);
186
                                sectionDAO.delete(chartSectionId);
187
                            }
188

    
189
                            for(String numberSectionId : subcategory.getNumbers()) {
190
                                Section<String> numberSection = sectionDAO.findById(numberSectionId);
191
                                if (numberSection == null) {
192
                                    // EXCEPTION - Section not found
193
                                    throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
194
                                }
195

    
196
                                for (String numberId : numberSection.getIndicators()) {
197
                                    indicatorDAO.delete(numberId);
198
                                }
199
                                subcategory.setNumbers(null);
200
                                sectionDAO.delete(numberSectionId);
201
                            }
202

    
203
                            subCategoryDAO.delete(subCategoryId);
204
                        }
205
                        category.setSubCategories(null);
206
                        categoryDAO.delete(categoryId);
207
                    }
208
                    topic.setCategories(null);
209

    
210
                    topics.remove(index);
211
                    stakeholderDAO.save(stakeholder);
212

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

    
220
            } else {
221
                // EXCEPTION - Topic not found
222
                throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
223
            }
224
        } else {
225
            // EXCEPTION - Stakeholder not found
226
            throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
227
        }
228
        return true;
229
    }
230

    
231
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
232
    public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
233
                                     @PathVariable("topicId") String topicId) {
234
        log.debug("toggle topic status (isActive)");
235
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
236

    
237
        Topic topic = topicDAO.findById(topicId);
238
        if (topic == null) {
239
            // EXCEPTION - Topic not found
240
            throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
241
        }
242
        topic.setIsActive(!topic.getIsActive());
243

    
244
        this.toggleTopic(stakeholderId, topic);
245

    
246
        return topic.getIsActive();
247
    }
248

    
249
    @RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
250
    public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId,
251
                                         @PathVariable("topicId") String topicId) {
252
        log.debug("toggle topic access (isPublic)");
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 access: Topic with id: "+topicId+" not found");
259
        }
260
        topic.setIsPublic(!topic.getIsPublic());
261

    
262
        this.toggleTopic(stakeholderId, topic);
263

    
264
        return topic.getIsPublic();
265
    }
266

    
267
    public void toggleTopic(String stakeholderId, Topic topic) {
268
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
269

    
270
        if (stakeholder != null) {
271
            if (stakeholder.getTopics().contains(topic.getId())) {
272
                topicDAO.save(topic);
273
                log.debug("Topic toggled!");
274
            } else {
275
                // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
276
                throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
277
            }
278
        } else {
279
            // EXCEPTION - Stakeholder not found
280
            throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
281
        }
282
    }
283
}
(8-8/8)