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