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
|
|
146
|
if(!changed) {
|
147
|
// break;
|
148
|
continue;
|
149
|
}
|
150
|
|
151
|
// topicBasedOnDefault.setName(topic.getName());
|
152
|
// topicBasedOnDefault.setDescription(topic.getDescription());
|
153
|
topicDAO.save(topicBasedOnDefault);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
|
158
|
public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
|
159
|
@PathVariable("topicId") String topicId) {
|
160
|
log.debug("delete topic");
|
161
|
log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId);
|
162
|
|
163
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
164
|
|
165
|
if(stakeholder != null) {
|
166
|
|
167
|
Topic<String> topic = topicDAO.findById(topicId);
|
168
|
if(topic != null) {
|
169
|
|
170
|
List<String> topics = stakeholder.getTopics();
|
171
|
int index = topics.indexOf(topicId);
|
172
|
if(index != -1) {
|
173
|
for(String categoryId : topic.getCategories()) {
|
174
|
Category<String> category = categoryDAO.findById(categoryId);
|
175
|
if(category == null) {
|
176
|
// EXCEPTION - Category not found
|
177
|
throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
178
|
}
|
179
|
|
180
|
for(String subCategoryId : category.getSubCategories()) {
|
181
|
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
182
|
if (subcategory == null) {
|
183
|
// EXCEPTION - SubCategory not found
|
184
|
throw new EntityNotFoundException("Delete topic: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
185
|
}
|
186
|
|
187
|
for(String chartSectionId : subcategory.getCharts()) {
|
188
|
Section<String> chartSection = sectionDAO.findById(chartSectionId);
|
189
|
if (chartSection == null) {
|
190
|
// EXCEPTION - Section not found
|
191
|
throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
192
|
}
|
193
|
|
194
|
for (String chartId : chartSection.getIndicators()) {
|
195
|
indicatorDAO.delete(chartId);
|
196
|
}
|
197
|
subcategory.setCharts(null);
|
198
|
sectionDAO.delete(chartSectionId);
|
199
|
}
|
200
|
|
201
|
for(String numberSectionId : subcategory.getNumbers()) {
|
202
|
Section<String> numberSection = sectionDAO.findById(numberSectionId);
|
203
|
if (numberSection == null) {
|
204
|
// EXCEPTION - Section not found
|
205
|
throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
206
|
}
|
207
|
|
208
|
for (String numberId : numberSection.getIndicators()) {
|
209
|
indicatorDAO.delete(numberId);
|
210
|
}
|
211
|
subcategory.setNumbers(null);
|
212
|
sectionDAO.delete(numberSectionId);
|
213
|
}
|
214
|
|
215
|
subCategoryDAO.delete(subCategoryId);
|
216
|
}
|
217
|
category.setSubCategories(null);
|
218
|
categoryDAO.delete(categoryId);
|
219
|
}
|
220
|
topic.setCategories(null);
|
221
|
|
222
|
topics.remove(index);
|
223
|
stakeholderDAO.save(stakeholder);
|
224
|
|
225
|
topicDAO.delete(topicId);
|
226
|
log.debug("Category deleted!");
|
227
|
} else {
|
228
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
229
|
throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
230
|
}
|
231
|
|
232
|
} else {
|
233
|
// EXCEPTION - Topic not found
|
234
|
throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
|
235
|
}
|
236
|
} else {
|
237
|
// EXCEPTION - Stakeholder not found
|
238
|
throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
|
239
|
}
|
240
|
return true;
|
241
|
}
|
242
|
|
243
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-status", method = RequestMethod.POST)
|
244
|
public Boolean toggleTopicStatus(@PathVariable("stakeholderId") String stakeholderId,
|
245
|
@PathVariable("topicId") String topicId) {
|
246
|
log.debug("toggle topic status (isActive)");
|
247
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
248
|
|
249
|
Topic topic = topicDAO.findById(topicId);
|
250
|
if (topic == null) {
|
251
|
// EXCEPTION - Topic not found
|
252
|
throw new EntityNotFoundException("Toggle topic status: Topic with id: "+topicId+" not found");
|
253
|
}
|
254
|
topic.setIsActive(!topic.getIsActive());
|
255
|
|
256
|
this.toggleTopic(stakeholderId, topic);
|
257
|
|
258
|
return topic.getIsActive();
|
259
|
}
|
260
|
|
261
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/toggle-access", method = RequestMethod.POST)
|
262
|
public Boolean toggleTopicAccess(@PathVariable("stakeholderId") String stakeholderId,
|
263
|
@PathVariable("topicId") String topicId) {
|
264
|
log.debug("toggle topic access (isPublic)");
|
265
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
|
266
|
|
267
|
Topic topic = topicDAO.findById(topicId);
|
268
|
if (topic == null) {
|
269
|
// EXCEPTION - Topic not found
|
270
|
throw new EntityNotFoundException("Toggle topic access: Topic with id: "+topicId+" not found");
|
271
|
}
|
272
|
topic.setIsPublic(!topic.getIsPublic());
|
273
|
|
274
|
this.toggleTopic(stakeholderId, topic);
|
275
|
|
276
|
return topic.getIsPublic();
|
277
|
}
|
278
|
|
279
|
public void toggleTopic(String stakeholderId, Topic topic) {
|
280
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
281
|
|
282
|
if (stakeholder != null) {
|
283
|
if (stakeholder.getTopics().contains(topic.getId())) {
|
284
|
topicDAO.save(topic);
|
285
|
log.debug("Topic toggled!");
|
286
|
} else {
|
287
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
288
|
throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
|
289
|
}
|
290
|
} else {
|
291
|
// EXCEPTION - Stakeholder not found
|
292
|
throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
|
293
|
}
|
294
|
}
|
295
|
}
|