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