1
|
package eu.dnetlib.uoamonitorservice.controllers;
|
2
|
|
3
|
//import com.fasterxml.jackson.core.type.TypeReference;
|
4
|
//import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
6
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
7
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
8
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.web.bind.annotation.*;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Date;
|
15
|
import java.util.List;
|
16
|
|
17
|
@RestController
|
18
|
@CrossOrigin(origins = "*")
|
19
|
public class StakeholderController {
|
20
|
private final Logger log = Logger.getLogger(this.getClass());
|
21
|
|
22
|
@Autowired
|
23
|
private StakeholderDAO stakeholderDAO;
|
24
|
|
25
|
@Autowired
|
26
|
private TopicDAO topicDAO;
|
27
|
|
28
|
@Autowired
|
29
|
private CategoryDAO categoryDAO;
|
30
|
|
31
|
@Autowired
|
32
|
private SubCategoryDAO subCategoryDAO;
|
33
|
|
34
|
@Autowired
|
35
|
private SectionDAO sectionDAO;
|
36
|
|
37
|
@Autowired
|
38
|
private IndicatorDAO indicatorDAO;
|
39
|
|
40
|
@Autowired
|
41
|
private TopicController topicController;
|
42
|
|
43
|
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
|
44
|
public Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> stakeholderFull) {
|
45
|
log.debug("build stakeholder");
|
46
|
log.debug("Alias: "+stakeholderFull.getAlias());
|
47
|
|
48
|
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
|
49
|
|
50
|
List<String> topics = new ArrayList<>();
|
51
|
List<Topic<Category<SubCategory<Section<Indicator>>>>> topicsFull = new ArrayList<>();
|
52
|
for(Topic topic : stakeholderFull.getTopics()) {
|
53
|
Topic<Category<SubCategory<Section<Indicator>>>> topicFull = topicController.buildTopic(topic);
|
54
|
topicsFull.add(topicFull);
|
55
|
topics.add(topicFull.getId());
|
56
|
}
|
57
|
stakeholderFull.setTopics(topicsFull);
|
58
|
stakeholder.setTopics(topics);
|
59
|
|
60
|
Date date = new Date();
|
61
|
stakeholder.setCreationDate(date);
|
62
|
stakeholder.setUpdateDate(date);
|
63
|
|
64
|
stakeholderFull.setCreationDate(date);
|
65
|
stakeholderFull.setUpdateDate(date);
|
66
|
|
67
|
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
|
68
|
stakeholderFull.setId(stakeholderSaved.getId());
|
69
|
return stakeholderFull;
|
70
|
//return null;
|
71
|
}
|
72
|
|
73
|
public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
|
74
|
Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
|
75
|
|
76
|
List<Topic> topics = new ArrayList<>();
|
77
|
|
78
|
for (String topicId: (List<String>)stakeholder.getTopics()) {
|
79
|
Topic<String> topic = topicDAO.findById(topicId);
|
80
|
if(topic == null) {
|
81
|
// EXCEPTION - Topic not found
|
82
|
throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
|
83
|
}
|
84
|
Topic<Category> topicFull = new Topic<Category>(topic);
|
85
|
|
86
|
List<Category> categories = new ArrayList<>();
|
87
|
|
88
|
for(String categoryId : topic.getCategories()) {
|
89
|
Category<String> category = categoryDAO.findById(categoryId);
|
90
|
if(category == null) {
|
91
|
// EXCEPTION - Category not found
|
92
|
throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
93
|
}
|
94
|
Category<SubCategory> categoryFull = new Category<SubCategory>(category);
|
95
|
|
96
|
List<SubCategory> subCategories = new ArrayList<>();
|
97
|
|
98
|
for(String subCategoryId : category.getSubCategories()) {
|
99
|
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
|
100
|
if(subCategory == null) {
|
101
|
// EXCEPTION - SubCategory not found
|
102
|
throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
|
103
|
}
|
104
|
SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
|
105
|
|
106
|
List<Section> sectionsCharts = new ArrayList<>();
|
107
|
|
108
|
for(String sectionId : subCategory.getCharts()) {
|
109
|
sectionsCharts.add(getSectionFull(sectionId, subCategoryId));
|
110
|
}
|
111
|
subCategoryFull.setCharts(sectionsCharts);
|
112
|
|
113
|
List<Section> sectionsNumbers = new ArrayList<>();
|
114
|
|
115
|
for(String sectionId : subCategory.getNumbers()) {
|
116
|
sectionsNumbers.add(getSectionFull(sectionId, subCategoryId));
|
117
|
}
|
118
|
subCategoryFull.setNumbers(sectionsNumbers);
|
119
|
|
120
|
// List<Indicator> charts = new ArrayList<>();
|
121
|
// for(String indicatorId : subCategory.getCharts()) {
|
122
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
123
|
// if(indicator == null) {
|
124
|
// // EXCEPTION - Indicator not found
|
125
|
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
|
126
|
// }
|
127
|
// charts.add(indicator);
|
128
|
// }
|
129
|
// subCategoryFull.setCharts(charts);
|
130
|
//
|
131
|
// List<Indicator> numbers = new ArrayList<>();
|
132
|
// for (String indicatorId : subCategory.getNumbers()) {
|
133
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
134
|
// if (indicator == null) {
|
135
|
// // EXCEPTION - Indicator not found
|
136
|
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")");
|
137
|
// }
|
138
|
// numbers.add(indicator);
|
139
|
// }
|
140
|
// subCategoryFull.setNumbers(numbers);
|
141
|
|
142
|
subCategories.add(subCategoryFull);
|
143
|
}
|
144
|
|
145
|
categoryFull.setSubCategories(subCategories);
|
146
|
categories.add(categoryFull);
|
147
|
}
|
148
|
|
149
|
topicFull.setCategories(categories);
|
150
|
topics.add(topicFull);
|
151
|
}
|
152
|
|
153
|
stakeholderFull.setTopics(topics);
|
154
|
return stakeholderFull;
|
155
|
}
|
156
|
|
157
|
private Section getSectionFull(String sectionId, String subCategoryId) {
|
158
|
Section<String> section = sectionDAO.findById(sectionId);
|
159
|
if (section == null) {
|
160
|
// EXCEPTION - Section not found
|
161
|
throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")");
|
162
|
}
|
163
|
Section sectionFull = new Section<Indicator>(section);
|
164
|
|
165
|
List<Indicator> indicators = new ArrayList<>();
|
166
|
for (String indicatorId : section.getIndicators()) {
|
167
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
168
|
if (indicator == null) {
|
169
|
// EXCEPTION - Indicator not found
|
170
|
throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")");
|
171
|
}
|
172
|
indicators.add(indicator);
|
173
|
}
|
174
|
sectionFull.setIndicators(indicators);
|
175
|
|
176
|
return sectionFull;
|
177
|
}
|
178
|
|
179
|
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
|
180
|
public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
|
181
|
log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
|
182
|
|
183
|
List<Stakeholder> stakeholders;
|
184
|
if(type == null) {
|
185
|
stakeholders = stakeholderDAO.findAll();
|
186
|
} else {
|
187
|
stakeholders = stakeholderDAO.findByType(type);
|
188
|
}
|
189
|
|
190
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
191
|
for(Stakeholder stakeholder : stakeholders) {
|
192
|
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
193
|
}
|
194
|
|
195
|
return stakeholdersFull;
|
196
|
}
|
197
|
|
198
|
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
|
199
|
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
|
200
|
log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
|
201
|
|
202
|
List<Stakeholder> stakeholders;
|
203
|
if(type == null) {
|
204
|
stakeholders = stakeholderDAO.findByDefaultId(null);
|
205
|
} else {
|
206
|
stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
|
207
|
}
|
208
|
|
209
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
210
|
for(Stakeholder stakeholder : stakeholders) {
|
211
|
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
212
|
}
|
213
|
return stakeholdersFull;
|
214
|
}
|
215
|
|
216
|
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
|
217
|
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
|
218
|
log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
|
219
|
|
220
|
List<Stakeholder> stakeholders;
|
221
|
if(type == null) {
|
222
|
stakeholders = stakeholderDAO.findByDefaultIdNot(null);
|
223
|
} else {
|
224
|
stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
|
225
|
}
|
226
|
|
227
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
228
|
for(Stakeholder stakeholder : stakeholders) {
|
229
|
stakeholdersFull.add(this.setFullEntities(stakeholder));
|
230
|
}
|
231
|
log.debug(new Date());
|
232
|
|
233
|
return stakeholdersFull;
|
234
|
}
|
235
|
|
236
|
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
|
237
|
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
|
238
|
log.debug("get stakeholder: "+alias);
|
239
|
|
240
|
Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
|
241
|
if(stakeholder == null) {
|
242
|
// EXCEPTION - Stakeholder not found
|
243
|
throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
|
244
|
}
|
245
|
return this.setFullEntities(stakeholder);
|
246
|
}
|
247
|
|
248
|
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
249
|
public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
|
250
|
log.debug("save stakeholder");
|
251
|
log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId());
|
252
|
|
253
|
// if(stakeholderFull == null) {
|
254
|
// log.debug("stakeholder null");
|
255
|
// // EXCEPTION - Parameter for Stakeholder is not accepted
|
256
|
// }
|
257
|
|
258
|
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
|
259
|
|
260
|
Date date = new Date();
|
261
|
stakeholder.setUpdateDate(date);
|
262
|
|
263
|
// stakeholder does not exist in DB
|
264
|
if(stakeholderFull.getId() == null) {
|
265
|
stakeholder.setCreationDate(date);
|
266
|
}
|
267
|
|
268
|
List<String> topics = new ArrayList<>();
|
269
|
for(Topic topic : stakeholderFull.getTopics()) {
|
270
|
topics.add(topic.getId());
|
271
|
}
|
272
|
stakeholder.setTopics(topics);
|
273
|
|
274
|
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
|
275
|
stakeholderFull.setId(stakeholderSaved.getId());
|
276
|
stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate());
|
277
|
stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate());
|
278
|
|
279
|
topics = null;
|
280
|
stakeholder = null;
|
281
|
stakeholderSaved = null;
|
282
|
|
283
|
return stakeholderFull;
|
284
|
}
|
285
|
|
286
|
|
287
|
@RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
|
288
|
public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
|
289
|
log.debug("delete stakeholder");
|
290
|
log.debug("Id: "+stakeholderId);
|
291
|
|
292
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
293
|
|
294
|
if(stakeholder != null) {
|
295
|
|
296
|
// for(String topicId : stakeholder.getTopics()) {
|
297
|
// Topic<String> topic = topicDAO.findById(topicId);
|
298
|
// if (topic == null) {
|
299
|
// // EXCEPTION - Topic not found
|
300
|
// throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
|
301
|
// }
|
302
|
//
|
303
|
// for (String categoryId : topic.getCategories()) {
|
304
|
// Category<String> category = categoryDAO.findById(categoryId);
|
305
|
// if (category == null) {
|
306
|
// // EXCEPTION - Category not found
|
307
|
// throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
308
|
// }
|
309
|
//
|
310
|
// for (String subCategoryId : category.getSubCategories()) {
|
311
|
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
312
|
// if (subcategory == null) {
|
313
|
// // EXCEPTION - SubCategory not found
|
314
|
// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
315
|
// }
|
316
|
//
|
317
|
// for(String chartSectionId : subcategory.getCharts()) {
|
318
|
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
|
319
|
// if (chartSection == null) {
|
320
|
// // EXCEPTION - Section not found
|
321
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
322
|
// }
|
323
|
//
|
324
|
// for (String chartId : chartSection.getIndicators()) {
|
325
|
// indicatorDAO.delete(chartId);
|
326
|
// }
|
327
|
// subcategory.setCharts(null);
|
328
|
// sectionDAO.delete(chartSectionId);
|
329
|
// }
|
330
|
//
|
331
|
// for(String numberSectionId : subcategory.getNumbers()) {
|
332
|
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
|
333
|
// if (numberSection == null) {
|
334
|
// // EXCEPTION - Section not found
|
335
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
336
|
// }
|
337
|
//
|
338
|
// for (String numberId : numberSection.getIndicators()) {
|
339
|
// indicatorDAO.delete(numberId);
|
340
|
// }
|
341
|
// subcategory.setNumbers(null);
|
342
|
// sectionDAO.delete(numberSectionId);
|
343
|
// }
|
344
|
//
|
345
|
// subCategoryDAO.delete(subCategoryId);
|
346
|
// }
|
347
|
// category.setSubCategories(null);
|
348
|
// categoryDAO.delete(categoryId);
|
349
|
// }
|
350
|
// topic.setCategories(null);
|
351
|
// topicDAO.delete(topicId);
|
352
|
// }
|
353
|
|
354
|
topicController.deleteTree(stakeholder);
|
355
|
|
356
|
stakeholder.setTopics(null);
|
357
|
stakeholderDAO.delete(stakeholderId);
|
358
|
log.debug("Stakeholder deleted!");
|
359
|
} else {
|
360
|
// EXCEPTION - Stakeholder not found
|
361
|
throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
|
362
|
}
|
363
|
return true;
|
364
|
}
|
365
|
|
366
|
|
367
|
@RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST)
|
368
|
public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) {
|
369
|
log.debug("toggle stakeholder status (isActive)");
|
370
|
log.debug("Stakeholder: "+stakeholderId);
|
371
|
|
372
|
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
|
373
|
if (stakeholder == null) {
|
374
|
// EXCEPTION - Stakeholder not found
|
375
|
throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found");
|
376
|
}
|
377
|
stakeholder.setIsActive(!stakeholder.getIsActive());
|
378
|
|
379
|
stakeholderDAO.save(stakeholder);
|
380
|
log.debug("Stakeholder toggled!");
|
381
|
|
382
|
return stakeholder.getIsActive();
|
383
|
}
|
384
|
|
385
|
@RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST)
|
386
|
public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) {
|
387
|
log.debug("toggle stakeholder access (isPublic)");
|
388
|
log.debug("Stakeholder: "+stakeholderId);
|
389
|
|
390
|
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
|
391
|
if (stakeholder == null) {
|
392
|
// EXCEPTION - Stakeholder not found
|
393
|
throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found");
|
394
|
}
|
395
|
stakeholder.setIsPublic(!stakeholder.getIsPublic());
|
396
|
|
397
|
stakeholderDAO.save(stakeholder);
|
398
|
log.debug("Stakeholder toggled!");
|
399
|
|
400
|
return stakeholder.getIsPublic();
|
401
|
}
|
402
|
|
403
|
|
404
|
// The following are not supposed to be used
|
405
|
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
|
406
|
// public List<Date> getAllStakeholderDates() {
|
407
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
408
|
// List<Date> profileDates = new ArrayList<>();
|
409
|
//
|
410
|
// int i=0;
|
411
|
// for(Stakeholder profile : profiles) {
|
412
|
// log.debug(profile.getCreationDate());
|
413
|
// profileDates.add(profile.getCreationDate());
|
414
|
// log.debug(profileDates.get(i));
|
415
|
// i++;
|
416
|
// }
|
417
|
// return profileDates;
|
418
|
// }
|
419
|
//
|
420
|
// @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
|
421
|
// public List<String> getAllStakeholderDates1() {
|
422
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
423
|
// List<String> profileDates = new ArrayList<>();
|
424
|
//
|
425
|
// for(Stakeholder profile : profiles) {
|
426
|
// log.debug(profile.getCreationDate().toString());
|
427
|
// profileDates.add(profile.getCreationDate().toString());
|
428
|
// }
|
429
|
// return profileDates;
|
430
|
// }
|
431
|
//
|
432
|
// @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
|
433
|
// public List<String> getAllStakeholderDates2() {
|
434
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
435
|
// List<String> profileDates = new ArrayList<>();
|
436
|
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
437
|
//
|
438
|
// for(Stakeholder profile : profiles) {
|
439
|
// log.debug(format.format(profile.getCreationDate()));
|
440
|
// profileDates.add(format.format(profile.getCreationDate()));
|
441
|
// }
|
442
|
// return profileDates;
|
443
|
// }
|
444
|
}
|