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