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