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 SectionController {
|
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
|
public Section<Indicator> buildSection(Section<Indicator> sectionFull) {
|
38
|
Section<String> section = new Section<>(sectionFull);
|
39
|
|
40
|
List<String> indicators = new ArrayList<>();
|
41
|
List<Indicator> indicatorsFull = new ArrayList<>();
|
42
|
for(Indicator chart : sectionFull.getIndicators()) {
|
43
|
Indicator chartSaved = indicatorDAO.save(chart);
|
44
|
chart.setId(chartSaved.getId());
|
45
|
indicatorsFull.add(chart);
|
46
|
indicators.add(chartSaved.getId());
|
47
|
}
|
48
|
sectionFull.setIndicators(indicatorsFull);
|
49
|
section.setIndicators(indicators);
|
50
|
|
51
|
sectionDAO.save(section);
|
52
|
|
53
|
sectionFull.setId(section.getId());
|
54
|
return sectionFull;
|
55
|
}
|
56
|
|
57
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
|
58
|
public Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
|
59
|
@PathVariable("topicId") String topicId,
|
60
|
@PathVariable("categoryId") String categoryId,
|
61
|
@PathVariable("subcategoryId") String subcategoryId,
|
62
|
@PathVariable("index") String index,
|
63
|
@RequestBody Section<Indicator> sectionFull) {
|
64
|
log.debug("save section");
|
65
|
log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
66
|
|
67
|
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
|
68
|
Section<String> section = new Section<>(sectionFull);
|
69
|
|
70
|
String sectionId = sectionFull.getId();
|
71
|
List<String> indicators = new ArrayList<>();
|
72
|
for(Indicator indicator : sectionFull.getIndicators()) {
|
73
|
indicators.add(indicator.getId());
|
74
|
}
|
75
|
section.setIndicators(indicators);
|
76
|
sectionDAO.save(section);
|
77
|
|
78
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
79
|
// this section belongs in default profile and it is new or it is updated
|
80
|
if(stakeholder.getDefaultId() == null) {
|
81
|
if(sectionId == null) {
|
82
|
onSaveDefaultSection(sectionFull, topicId, categoryId, subcategoryId, stakeholder);
|
83
|
}
|
84
|
else {
|
85
|
onUpdateDefaultSection(sectionFull, stakeholder);
|
86
|
}
|
87
|
}
|
88
|
|
89
|
List<String> sections = null;
|
90
|
if(sectionFull.getType().equals("chart")) {
|
91
|
sections = subCategory.getCharts();
|
92
|
} else if(sectionFull.getType().equals("number")) {
|
93
|
sections = subCategory.getNumbers();
|
94
|
}
|
95
|
|
96
|
int existing_index = sections.indexOf(sectionFull.getId());
|
97
|
if (existing_index == -1) {
|
98
|
if(index != null) {
|
99
|
sections.add(Integer.parseInt(index), sectionFull.getId());
|
100
|
} else {
|
101
|
sections.add(sectionFull.getId());
|
102
|
}
|
103
|
subCategoryDAO.save(subCategory);
|
104
|
log.debug("Section saved!");
|
105
|
|
106
|
sectionFull.setId(section.getId());
|
107
|
}
|
108
|
|
109
|
return sectionFull;
|
110
|
}
|
111
|
|
112
|
public void onSaveDefaultSection(Section section,
|
113
|
String defaultTopicId, String defaultCategoryId,
|
114
|
String defaultSubcategoryId, Stakeholder defaultStakeholder) {
|
115
|
log.debug("On save default section");
|
116
|
|
117
|
|
118
|
// new section in default profile - add it on profiles of the same type
|
119
|
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
|
120
|
|
121
|
for (SubCategory subCategory : subCategories) {
|
122
|
Section sectionNew = new Section();
|
123
|
sectionNew.copyFromDefault(section);
|
124
|
|
125
|
sectionDAO.save(sectionNew);
|
126
|
|
127
|
List<String> sections = null;
|
128
|
if (section.getType().equals("chart")) {
|
129
|
sections = subCategory.getCharts();
|
130
|
} else if (section.getType().equals("number")) {
|
131
|
sections = subCategory.getNumbers();
|
132
|
}
|
133
|
sections.add(sectionNew.getId());
|
134
|
|
135
|
subCategoryDAO.save(subCategory);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
public void onUpdateDefaultSection(Section section, Stakeholder stakeholder) {
|
140
|
log.debug("On update default section");
|
141
|
|
142
|
// section already exists - check if changed and update all sections based on it
|
143
|
|
144
|
boolean changed = false;
|
145
|
List<Section> sections = sectionDAO.findByDefaultId(section.getId());
|
146
|
|
147
|
for(Section sectionBasedOnDefault : sections) {
|
148
|
if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())) {
|
149
|
changed = true;
|
150
|
}
|
151
|
|
152
|
if(!changed) {
|
153
|
break;
|
154
|
}
|
155
|
|
156
|
sectionBasedOnDefault.setTitle(section.getTitle());
|
157
|
sectionDAO.save(sectionBasedOnDefault);
|
158
|
}
|
159
|
}
|
160
|
|
161
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
|
162
|
public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,
|
163
|
@PathVariable("topicId") String topicId,
|
164
|
@PathVariable("categoryId") String categoryId,
|
165
|
@PathVariable("subcategoryId") String subcategoryId,
|
166
|
@PathVariable("sectionId") String sectionId) {
|
167
|
log.debug("delete section");
|
168
|
log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
169
|
|
170
|
Section section = sectionDAO.findById(sectionId);
|
171
|
if(section != null) {
|
172
|
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
|
173
|
|
174
|
List<String> sections = null;
|
175
|
if (section.getType().equals("chart")) {
|
176
|
sections = subCategory.getCharts();
|
177
|
} else if (section.getType().equals("number")) {
|
178
|
sections = subCategory.getNumbers();
|
179
|
}
|
180
|
|
181
|
int index = sections.indexOf(sectionId);
|
182
|
if (index != -1) {
|
183
|
sections.remove(index);
|
184
|
subCategoryDAO.save(subCategory);
|
185
|
|
186
|
sectionDAO.delete(sectionId);
|
187
|
log.debug("Section deleted!");
|
188
|
} else {
|
189
|
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
|
190
|
throw new PathNotValidException("Delete section: Section with id: "+sectionId+" not found in SubCategory: "+subcategoryId);
|
191
|
}
|
192
|
} else {
|
193
|
// EXCEPTION - Section not found
|
194
|
throw new EntityNotFoundException("Delete section: Section with id: "+sectionId+" not found");
|
195
|
}
|
196
|
return true;
|
197
|
}
|
198
|
|
199
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
|
200
|
public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
|
201
|
@PathVariable("topicId") String topicId,
|
202
|
@PathVariable("categoryId") String categoryId,
|
203
|
@PathVariable("subcategoryId") String subcategoryId,
|
204
|
@PathVariable("type") String type,
|
205
|
@RequestBody List<String> sections) {
|
206
|
log.debug("reorder sections of type: "+type);
|
207
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
208
|
|
209
|
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
|
210
|
|
211
|
if (type.equals("chart")) {
|
212
|
subCategory.setCharts(sections);
|
213
|
} else if (type.equals("number")) {
|
214
|
subCategory.setNumbers(sections);
|
215
|
}
|
216
|
|
217
|
subCategoryDAO.save(subCategory);
|
218
|
log.debug("Sections reordered!");
|
219
|
|
220
|
List<Section> sectionsFull = new ArrayList<>();
|
221
|
for(String sectionId : sections) {
|
222
|
sectionsFull.add(sectionDAO.findById(sectionId));
|
223
|
}
|
224
|
return sectionsFull;
|
225
|
}
|
226
|
|
227
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-status", method = RequestMethod.POST)
|
228
|
// public Boolean toggleSectionStatus(@PathVariable("stakeholderId") String stakeholderId,
|
229
|
// @PathVariable("topicId") String topicId,
|
230
|
// @PathVariable("categoryId") String categoryId,
|
231
|
// @PathVariable("subcategoryId") String subcategoryId,
|
232
|
// @PathVariable("sectionId") String sectionId) {
|
233
|
// log.debug("toggle section status (isActive)");
|
234
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Section: "+sectionId);
|
235
|
//
|
236
|
// Section section = sectionDAO.findById(sectionId);
|
237
|
// if (section == null) {
|
238
|
// // EXCEPTION - Section not found
|
239
|
// throw new EntityNotFoundException("Toggle section status: Section with id: "+sectionId+" not found");
|
240
|
// }
|
241
|
// section.setIsActive(!section.getIsActive());
|
242
|
//
|
243
|
// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId, section);
|
244
|
//
|
245
|
// return section.getIsActive();
|
246
|
// }
|
247
|
|
248
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-access", method = RequestMethod.POST)
|
249
|
// public Boolean toggleSectionAccess(@PathVariable("stakeholderId") String stakeholderId,
|
250
|
// @PathVariable("topicId") String topicId,
|
251
|
// @PathVariable("categoryId") String categoryId,
|
252
|
// @PathVariable("subcategoryId") String subcategoryId,
|
253
|
// @PathVariable("sectionId") String sectionId) {
|
254
|
// log.debug("toggle section access (isPublic)");
|
255
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
256
|
//
|
257
|
// Section section = sectionDAO.findById(sectionId);
|
258
|
// if (section == null) {
|
259
|
// // EXCEPTION - Section not found
|
260
|
// throw new EntityNotFoundException("Toggle section access: Section with id: "+sectionId+" not found");
|
261
|
// }
|
262
|
// section.setIsPublic(!section.getIsPublic());
|
263
|
//
|
264
|
// this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId);
|
265
|
//
|
266
|
// return section.getIsPublic();
|
267
|
// }
|
268
|
|
269
|
public void toggleSection(String stakeholderId, String topicId, String categoryId, String subcategoryId, Section section) {
|
270
|
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
|
271
|
|
272
|
List<String> sections = null;
|
273
|
if (section.getType().equals("chart")) {
|
274
|
sections = subCategory.getCharts();
|
275
|
} else if (section.getType().equals("number")) {
|
276
|
sections = subCategory.getNumbers();
|
277
|
}
|
278
|
|
279
|
if(sections.contains(section.getId())) {
|
280
|
sectionDAO.save(section);
|
281
|
log.debug("Section toggled!");
|
282
|
} else {
|
283
|
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias();
|
284
|
throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId);
|
285
|
}
|
286
|
|
287
|
}
|
288
|
|
289
|
private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) {
|
290
|
|
291
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
292
|
|
293
|
if(stakeholder == null) {
|
294
|
// EXCEPTION - Stakeholder not found
|
295
|
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
|
296
|
}
|
297
|
|
298
|
Topic<String> topic = topicDAO.findById(topicId);
|
299
|
if(topic == null) {
|
300
|
// EXCEPTION - Topic not found
|
301
|
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
|
302
|
}
|
303
|
|
304
|
if(!stakeholder.getTopics().contains(topicId)) {
|
305
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
306
|
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
|
307
|
}
|
308
|
|
309
|
Category<String> category = categoryDAO.findById(categoryId);
|
310
|
if(category == null) {
|
311
|
// EXCEPTION - Category not found
|
312
|
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
|
313
|
}
|
314
|
|
315
|
if(!topic.getCategories().contains(categoryId)) {
|
316
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
317
|
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
318
|
}
|
319
|
|
320
|
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
321
|
if(subcategory == null) {
|
322
|
// EXCEPTION - SubCategory not found
|
323
|
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
|
324
|
}
|
325
|
|
326
|
if (!category.getSubCategories().contains(subcategoryId)) {
|
327
|
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
328
|
throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
329
|
}
|
330
|
|
331
|
return subcategory;
|
332
|
}
|
333
|
}
|