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.IndicatorDAO;
|
6
|
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
7
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.web.bind.annotation.*;
|
11
|
|
12
|
import java.text.SimpleDateFormat;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Date;
|
15
|
import java.util.Iterator;
|
16
|
import java.util.List;
|
17
|
|
18
|
@RestController
|
19
|
@CrossOrigin(origins = "*")
|
20
|
public class StakeholderController {
|
21
|
private final Logger log = Logger.getLogger(this.getClass());
|
22
|
|
23
|
@Autowired
|
24
|
private StakeholderDAO stakeholderDAO;
|
25
|
|
26
|
@Autowired
|
27
|
private IndicatorDAO indicatorDAO;
|
28
|
|
29
|
public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) {
|
30
|
for (Topic topic: stakeholder.getTopics()) {
|
31
|
for(Category category : topic.getCategories()) {
|
32
|
List<SubCategory> subCategories = new ArrayList<>();
|
33
|
|
34
|
for(SubCategory<String> subCategory : category.getSubCategories()) {
|
35
|
SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
|
36
|
|
37
|
List<Indicator> charts = new ArrayList<>();
|
38
|
for(String indicatorId : subCategory.getCharts()) {
|
39
|
charts.add(indicatorDAO.findById(indicatorId));
|
40
|
}
|
41
|
subCategoryFull.setCharts(charts);
|
42
|
|
43
|
List<Indicator> numbers = new ArrayList<>();
|
44
|
for(String indicatorId : subCategory.getNumbers()) {
|
45
|
numbers.add(indicatorDAO.findById(indicatorId));
|
46
|
}
|
47
|
subCategoryFull.setNumbers(numbers);
|
48
|
|
49
|
subCategories.add(subCategoryFull);
|
50
|
}
|
51
|
|
52
|
category.setSubCategories(subCategories);
|
53
|
}
|
54
|
|
55
|
}
|
56
|
return stakeholder;
|
57
|
}
|
58
|
|
59
|
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
|
60
|
public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
|
61
|
List<Stakeholder> stakeholders;
|
62
|
if(type == null) {
|
63
|
stakeholders = stakeholderDAO.findAll();
|
64
|
} else {
|
65
|
stakeholders = stakeholderDAO.findByType(type);
|
66
|
}
|
67
|
|
68
|
for(Stakeholder stakeholder : stakeholders) {
|
69
|
this.setIndicatorsForStakeholder(stakeholder);
|
70
|
}
|
71
|
|
72
|
return stakeholders;
|
73
|
}
|
74
|
|
75
|
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
|
76
|
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
|
77
|
List<Stakeholder> stakeholders;
|
78
|
if(type == null) {
|
79
|
stakeholders = stakeholderDAO.findByIsDefaultProfile(true);
|
80
|
} else {
|
81
|
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type);
|
82
|
}
|
83
|
|
84
|
for(Stakeholder stakeholder : stakeholders) {
|
85
|
this.setIndicatorsForStakeholder(stakeholder);
|
86
|
}
|
87
|
return stakeholders;
|
88
|
}
|
89
|
|
90
|
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
|
91
|
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
|
92
|
List<Stakeholder> stakeholders;
|
93
|
if(type == null) {
|
94
|
stakeholders = stakeholderDAO.findByIsDefaultProfile(false);
|
95
|
} else {
|
96
|
stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type);
|
97
|
}
|
98
|
|
99
|
for(Stakeholder stakeholder : stakeholders) {
|
100
|
this.setIndicatorsForStakeholder(stakeholder);
|
101
|
}
|
102
|
log.debug(new Date());
|
103
|
|
104
|
return stakeholders;
|
105
|
}
|
106
|
|
107
|
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
|
108
|
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
|
109
|
Stakeholder stakeholder = stakeholderDAO.findByAlias(alias);
|
110
|
this.setIndicatorsForStakeholder(stakeholder);
|
111
|
|
112
|
return stakeholder;
|
113
|
}
|
114
|
|
115
|
|
116
|
@RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST)
|
117
|
public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) {
|
118
|
log.debug("save stakeholder");
|
119
|
|
120
|
Stakeholder stakeholderFull = new Stakeholder(stakeholder);
|
121
|
|
122
|
List<Topic> topicsFull = new ArrayList<>();
|
123
|
|
124
|
for (Topic topic: stakeholder.getTopics()) {
|
125
|
Topic topicFull = new Topic(topic);
|
126
|
|
127
|
List<Category> categoriesFull = new ArrayList<>();
|
128
|
|
129
|
for(Category category : topic.getCategories()) {
|
130
|
Category categoryFull = new Category(category);
|
131
|
|
132
|
List<SubCategory> subCategories = new ArrayList<>();
|
133
|
List<SubCategory> subCategoriesFull = new ArrayList<>();
|
134
|
|
135
|
for(SubCategory<Indicator> subCategoryFull : category.getSubCategories()) {
|
136
|
SubCategory subCategory = new SubCategory<String>(subCategoryFull);
|
137
|
|
138
|
List<String> charts = new ArrayList<>();
|
139
|
|
140
|
ObjectMapper mapper = new ObjectMapper();
|
141
|
//Jackson's use of generics
|
142
|
List<Indicator> chartsFull = mapper.convertValue(subCategoryFull.getCharts(), new TypeReference<List<Indicator>>(){});
|
143
|
|
144
|
//List<Indicator> chartsFull = (List<Indicator>)subCategoryFull.getCharts();
|
145
|
//log.debug(chartsFull);
|
146
|
|
147
|
for(Indicator indicator : chartsFull) {
|
148
|
charts.add(indicator.getId());
|
149
|
}
|
150
|
|
151
|
subCategory.setCharts(charts);
|
152
|
subCategoryFull.setCharts(chartsFull);
|
153
|
|
154
|
List<String> numbers = new ArrayList<>();
|
155
|
List<Indicator> numbersFull = mapper.convertValue(subCategoryFull.getNumbers(), new TypeReference<List<Indicator>>(){});
|
156
|
|
157
|
for(Indicator indicator : numbersFull) {
|
158
|
numbers.add(indicator.getId());
|
159
|
}
|
160
|
subCategory.setNumbers(numbers);
|
161
|
subCategoryFull.setNumbers(numbersFull);
|
162
|
|
163
|
subCategories.add(subCategory);
|
164
|
subCategoriesFull.add(subCategoryFull);
|
165
|
}
|
166
|
|
167
|
category.setSubCategories(subCategories);
|
168
|
categoryFull.setSubCategories(subCategoriesFull);
|
169
|
|
170
|
categoriesFull.add(categoryFull);
|
171
|
}
|
172
|
topicFull.setCategories(categoriesFull);
|
173
|
topicsFull.add(topicFull);
|
174
|
}
|
175
|
stakeholderFull.setTopics(topicsFull);
|
176
|
|
177
|
log.debug("after minimize stakeholder");
|
178
|
Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder);
|
179
|
log.debug("stakeholder saved!");
|
180
|
|
181
|
stakeholderFull.setId(stakeholderSaved.getId());
|
182
|
return stakeholderFull;
|
183
|
}
|
184
|
|
185
|
@RequestMapping(value = "/{stakeholder}", method = RequestMethod.DELETE)
|
186
|
public boolean deleteStakeholder(@PathVariable("stakeholder") String stakeholder) {
|
187
|
Stakeholder _stakeholder = stakeholderDAO.findById(stakeholder);
|
188
|
if(_stakeholder != null) {
|
189
|
for (Topic topic : _stakeholder.getTopics()) {
|
190
|
for (Category category : topic.getCategories()) {
|
191
|
for (SubCategory<String> subcategory : category.getSubCategories()) {
|
192
|
for(String chartId : subcategory.getCharts()) {
|
193
|
indicatorDAO.delete(chartId);
|
194
|
}
|
195
|
for(String numberId : subcategory.getNumbers()) {
|
196
|
indicatorDAO.delete(numberId);
|
197
|
}
|
198
|
|
199
|
subcategory.setCharts(null);
|
200
|
subcategory.setNumbers(null);
|
201
|
}
|
202
|
category.setSubCategories(null);
|
203
|
}
|
204
|
topic.setCategories(null);
|
205
|
}
|
206
|
|
207
|
} else {
|
208
|
return false;
|
209
|
// EXCEPTION - Stakeholder not found
|
210
|
}
|
211
|
|
212
|
stakeholderDAO.delete(_stakeholder.getId());
|
213
|
_stakeholder = null;
|
214
|
return true;
|
215
|
}
|
216
|
|
217
|
@RequestMapping(value = "/{stakeholder}/{topic}", method = RequestMethod.DELETE)
|
218
|
public Stakeholder deleteTopic(@PathVariable("stakeholder") String stakeholder,
|
219
|
@PathVariable("topic") String topic) {
|
220
|
|
221
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
222
|
if(_stakeholder != null) {
|
223
|
boolean topicFound = false;
|
224
|
|
225
|
Iterator<Topic> topicIterator = _stakeholder.getTopics().iterator();
|
226
|
while (topicIterator.hasNext()) {
|
227
|
Topic _topic = topicIterator.next();
|
228
|
if(_topic.getAlias().equals(topic)) {
|
229
|
for (Category category : _topic.getCategories()) {
|
230
|
for (SubCategory<String> subcategory : category.getSubCategories()) {
|
231
|
for(String chartId : subcategory.getCharts()) {
|
232
|
indicatorDAO.delete(chartId);
|
233
|
}
|
234
|
for(String numberId : subcategory.getNumbers()) {
|
235
|
indicatorDAO.delete(numberId);
|
236
|
}
|
237
|
|
238
|
subcategory.setCharts(null);
|
239
|
subcategory.setNumbers(null);
|
240
|
}
|
241
|
category.setSubCategories(null);
|
242
|
}
|
243
|
_topic.setCategories(null);
|
244
|
topicIterator.remove();
|
245
|
stakeholderDAO.save(_stakeholder);
|
246
|
|
247
|
topicFound = true;
|
248
|
break;
|
249
|
}
|
250
|
}
|
251
|
if(!topicFound) {
|
252
|
// EXCEPTION - Topic not found
|
253
|
}
|
254
|
} else {
|
255
|
// EXCEPTION - Stakeholder not found
|
256
|
}
|
257
|
this.setIndicatorsForStakeholder(_stakeholder);
|
258
|
|
259
|
return _stakeholder;
|
260
|
}
|
261
|
|
262
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}", method = RequestMethod.DELETE)
|
263
|
public Stakeholder deleteCategory(@PathVariable("stakeholder") String stakeholder,
|
264
|
@PathVariable("topic") String topic,
|
265
|
@PathVariable("category") String category) {
|
266
|
|
267
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
268
|
if(_stakeholder != null) {
|
269
|
Topic _topic = _stakeholder.getTopics().stream()
|
270
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
271
|
.findFirst()
|
272
|
.orElse(null);
|
273
|
if(_topic != null) {
|
274
|
boolean categoryFound = false;
|
275
|
|
276
|
Iterator<Category> categoryIterator = _topic.getCategories().iterator();
|
277
|
while (categoryIterator.hasNext()) {
|
278
|
Category _category = categoryIterator.next();
|
279
|
if (_category.getAlias().equals(category)) {
|
280
|
for (SubCategory<String> subcategory : _category.getSubCategories()) {
|
281
|
for(String chartId : subcategory.getCharts()) {
|
282
|
indicatorDAO.delete(chartId);
|
283
|
}
|
284
|
for(String numberId : subcategory.getNumbers()) {
|
285
|
indicatorDAO.delete(numberId);
|
286
|
}
|
287
|
|
288
|
subcategory.setCharts(null);
|
289
|
subcategory.setNumbers(null);
|
290
|
}
|
291
|
_category.setSubCategories(null);
|
292
|
categoryIterator.remove();
|
293
|
stakeholderDAO.save(_stakeholder);
|
294
|
|
295
|
categoryFound = true;
|
296
|
break;
|
297
|
}
|
298
|
}
|
299
|
if(!categoryFound) {
|
300
|
// EXCEPTION - Category not found
|
301
|
}
|
302
|
} else {
|
303
|
// EXCEPTION - Topic not found
|
304
|
}
|
305
|
} else {
|
306
|
// EXCEPTION - Stakeholder not found
|
307
|
}
|
308
|
this.setIndicatorsForStakeholder(_stakeholder);
|
309
|
|
310
|
return _stakeholder;
|
311
|
}
|
312
|
|
313
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}", method = RequestMethod.DELETE)
|
314
|
public Stakeholder deleteSubCategory(@PathVariable("stakeholder") String stakeholder,
|
315
|
@PathVariable("topic") String topic,
|
316
|
@PathVariable("category") String category,
|
317
|
@PathVariable("subcategory") String subcategory) {
|
318
|
|
319
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
320
|
if(_stakeholder != null) {
|
321
|
Topic _topic = _stakeholder.getTopics().stream()
|
322
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
323
|
.findFirst()
|
324
|
.orElse(null);
|
325
|
if(_topic != null) {
|
326
|
Category _category = _topic.getCategories().stream()
|
327
|
.filter(current_category -> current_category.getAlias().equals(category))
|
328
|
.findFirst()
|
329
|
.orElse(null);
|
330
|
if(_category != null) {
|
331
|
boolean subCategoryFound = false;
|
332
|
|
333
|
Iterator<SubCategory> subCategoryIterator = _category.getSubCategories().iterator();
|
334
|
while (subCategoryIterator.hasNext()) {
|
335
|
SubCategory<String> _subCategory = subCategoryIterator.next();
|
336
|
if (_subCategory.getAlias().equals(subcategory)) {
|
337
|
|
338
|
for(String chartId : _subCategory.getCharts()) {
|
339
|
indicatorDAO.delete(chartId);
|
340
|
}
|
341
|
for(String numberId : _subCategory.getNumbers()) {
|
342
|
indicatorDAO.delete(numberId);
|
343
|
}
|
344
|
|
345
|
_subCategory.setCharts(null);
|
346
|
_subCategory.setNumbers(null);
|
347
|
|
348
|
subCategoryIterator.remove();
|
349
|
stakeholderDAO.save(_stakeholder);
|
350
|
|
351
|
subCategoryFound = true;
|
352
|
break;
|
353
|
|
354
|
}
|
355
|
}
|
356
|
if (!subCategoryFound) {
|
357
|
// EXCEPTION - SubCategory not found
|
358
|
}
|
359
|
} else {
|
360
|
// EXCEPTION - Category not found
|
361
|
}
|
362
|
} else {
|
363
|
// EXCEPTION - Topic not found
|
364
|
}
|
365
|
} else {
|
366
|
// EXCEPTION - Stakeholder not found
|
367
|
}
|
368
|
this.setIndicatorsForStakeholder(_stakeholder);
|
369
|
|
370
|
return _stakeholder;
|
371
|
}
|
372
|
|
373
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/{id}", method = RequestMethod.DELETE)
|
374
|
public boolean deleteIndicator(@PathVariable("stakeholder") String stakeholder,
|
375
|
@PathVariable("topic") String topic,
|
376
|
@PathVariable("category") String category,
|
377
|
@PathVariable("subcategory") String subcategory,
|
378
|
@PathVariable("id") String id) {
|
379
|
|
380
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
381
|
if(_stakeholder != null) {
|
382
|
Topic _topic = _stakeholder.getTopics().stream()
|
383
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
384
|
.findFirst()
|
385
|
.orElse(null);
|
386
|
if(_topic != null) {
|
387
|
Category _category = _topic.getCategories().stream()
|
388
|
.filter(current_category -> current_category.getAlias().equals(category))
|
389
|
.findFirst()
|
390
|
.orElse(null);
|
391
|
if(_category != null) {
|
392
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
393
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
394
|
.findFirst()
|
395
|
.orElse(null);
|
396
|
if(_subCategory != null) {
|
397
|
List<String> indicators = null;
|
398
|
|
399
|
Indicator indicator = indicatorDAO.findById(id);
|
400
|
if(indicator.hasType("chart")) {
|
401
|
indicators =_subCategory.getCharts();
|
402
|
} else if(indicator.hasType("number")) {
|
403
|
indicators =_subCategory.getNumbers();
|
404
|
}
|
405
|
|
406
|
if(indicators == null) {
|
407
|
// EXCEPTION - No indicators found
|
408
|
}
|
409
|
|
410
|
//List<String> finalIndicators = indicators;
|
411
|
//log.debug("Indicators size: "+finalIndicators.size());
|
412
|
// int index = IntStream.range(0, indicators.size())
|
413
|
// .filter(i -> indicatorId.equals(finalIndicators.get(i)))
|
414
|
// .findFirst()
|
415
|
// .orElse(-1); // return -1 if target is not found
|
416
|
|
417
|
boolean indicatorFound = false;
|
418
|
Iterator<String> indicatorIterator = indicators.iterator();
|
419
|
while (indicatorIterator.hasNext()) {
|
420
|
String indicatorId = indicatorIterator.next();
|
421
|
log.debug(id + " vs "+indicatorId);
|
422
|
if(id.equals(indicatorId)) {
|
423
|
indicatorIterator.remove();
|
424
|
indicatorFound = true;
|
425
|
break;
|
426
|
}
|
427
|
}
|
428
|
|
429
|
log.debug(indicatorFound);
|
430
|
if(!indicatorFound) {
|
431
|
return false;
|
432
|
// EXCEPTION - Indicator not found
|
433
|
}
|
434
|
//indicators.remove(index);
|
435
|
|
436
|
stakeholderDAO.save(_stakeholder);
|
437
|
indicatorDAO.delete(id);
|
438
|
} else {
|
439
|
// EXCEPTION - Subcategory not found
|
440
|
}
|
441
|
} else {
|
442
|
// EXCEPTION - Category not found
|
443
|
}
|
444
|
} else {
|
445
|
// EXCEPTION - Topic not found
|
446
|
}
|
447
|
} else {
|
448
|
// EXCEPTION - Stakeholder not found
|
449
|
}
|
450
|
return true;
|
451
|
}
|
452
|
|
453
|
// @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/delete", method = RequestMethod.POST)
|
454
|
// public boolean deleteChartPost(@PathVariable("stakeholder") String stakeholder,
|
455
|
// @PathVariable("topic") String topic,
|
456
|
// @PathVariable("category") String category,
|
457
|
// @PathVariable("subcategory") String subcategory,
|
458
|
// @RequestBody String indicatorId) {
|
459
|
// //String id = chart.getId();
|
460
|
// return deleteIndicator(stakeholder, topic, category, subcategory, indicatorId);
|
461
|
// }
|
462
|
|
463
|
|
464
|
// path variables are alias-es. Each alias must be unique.
|
465
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/save", method = RequestMethod.POST)
|
466
|
public Indicator saveIndicator(@PathVariable("stakeholder") String stakeholder,
|
467
|
@PathVariable("topic") String topic,
|
468
|
@PathVariable("category") String category,
|
469
|
@PathVariable("subcategory") String subcategory,
|
470
|
@RequestBody Indicator indicator) {
|
471
|
|
472
|
Indicator indicatorSaved = null;
|
473
|
if(indicator.getId() != null) {
|
474
|
log.debug("indicator is already saved");
|
475
|
indicatorSaved = indicatorDAO.save(indicator);
|
476
|
} else {
|
477
|
log.debug("to save indicator");
|
478
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
479
|
if (_stakeholder != null) {
|
480
|
Topic _topic = _stakeholder.getTopics().stream()
|
481
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
482
|
.findFirst()
|
483
|
.orElse(null);
|
484
|
if (_topic != null) {
|
485
|
Category _category = _topic.getCategories().stream()
|
486
|
.filter(current_category -> current_category.getAlias().equals(category))
|
487
|
.findFirst()
|
488
|
.orElse(null);
|
489
|
if (_category != null) {
|
490
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
491
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
492
|
.findFirst()
|
493
|
.orElse(null);
|
494
|
if (_subCategory != null) {
|
495
|
indicatorSaved = indicatorDAO.save(indicator);
|
496
|
|
497
|
List<String> indicators = null;
|
498
|
|
499
|
if (indicator.hasType("chart")) {
|
500
|
indicators = _subCategory.getCharts();
|
501
|
} else if (indicator.hasType("number")) {
|
502
|
indicators = _subCategory.getNumbers();
|
503
|
}
|
504
|
|
505
|
String indicatorId;
|
506
|
if (indicator.getId() != null) {
|
507
|
indicatorId = indicators.stream()
|
508
|
.filter(current_indicator -> current_indicator.equals(indicator.getId()))
|
509
|
.findFirst()
|
510
|
.orElse(null);
|
511
|
|
512
|
if (indicatorId == null) { // indicator is not already at this position
|
513
|
indicators.add(indicator.getId());
|
514
|
}
|
515
|
}
|
516
|
stakeholderDAO.save(_stakeholder);
|
517
|
} else {
|
518
|
// EXCEPTION - Subcategory not found
|
519
|
}
|
520
|
} else {
|
521
|
// EXCEPTION - Category not found
|
522
|
}
|
523
|
} else {
|
524
|
// EXCEPTION - Topic not found
|
525
|
}
|
526
|
} else {
|
527
|
// EXCEPTION - Stakeholder not found
|
528
|
}
|
529
|
}
|
530
|
|
531
|
return indicatorSaved;
|
532
|
}
|
533
|
|
534
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicators/reorder", method = RequestMethod.POST)
|
535
|
public List<Indicator> reorderIndicators(String stakeholder, String topic,
|
536
|
String category, String subcategory,
|
537
|
List<Indicator> indicators, String type) {
|
538
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
539
|
if(_stakeholder != null) {
|
540
|
Topic _topic = _stakeholder.getTopics().stream()
|
541
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
542
|
.findFirst()
|
543
|
.orElse(null);
|
544
|
if(_topic != null) {
|
545
|
Category _category = _topic.getCategories().stream()
|
546
|
.filter(current_category -> current_category.getAlias().equals(category))
|
547
|
.findFirst()
|
548
|
.orElse(null);
|
549
|
if(_category != null) {
|
550
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
551
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
552
|
.findFirst()
|
553
|
.orElse(null);
|
554
|
if(_subCategory != null) {
|
555
|
List<String> _indicators = null;
|
556
|
if(type.equals("chart")) {
|
557
|
_indicators = _subCategory.getCharts();
|
558
|
} else if(type.equals("number")) {
|
559
|
_indicators = _subCategory.getNumbers();
|
560
|
}
|
561
|
|
562
|
_indicators.clear();
|
563
|
for(Indicator indicator : indicators) {
|
564
|
_indicators.add(indicator.getId());
|
565
|
}
|
566
|
stakeholderDAO.save(_stakeholder);
|
567
|
} else {
|
568
|
// EXCEPTION - Subcategory not found
|
569
|
}
|
570
|
} else {
|
571
|
// EXCEPTION - Vategory not found
|
572
|
}
|
573
|
} else {
|
574
|
// EXCEPTION - Topic not found
|
575
|
}
|
576
|
} else {
|
577
|
// EXCEPTION - Stakeholder not found
|
578
|
}
|
579
|
|
580
|
return indicators;
|
581
|
}
|
582
|
|
583
|
|
584
|
|
585
|
// The following are not supposed to be used
|
586
|
|
587
|
// should i delete indicators that were in the list but they are not in the new list?
|
588
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/charts/save", method = RequestMethod.POST)
|
589
|
public List<Indicator> saveCharts(@PathVariable("stakeholder") String stakeholder,
|
590
|
@PathVariable("topic") String topic,
|
591
|
@PathVariable("category") String category,
|
592
|
@PathVariable("subcategory") String subcategory,
|
593
|
@RequestBody List<Indicator> charts) {
|
594
|
log.debug(charts);
|
595
|
log.debug(charts.size());
|
596
|
log.debug(charts.getClass().getName());
|
597
|
log.debug(charts.get(0).getClass().getName());
|
598
|
return saveIndicators(stakeholder, topic, category, subcategory, charts, "chart");
|
599
|
}
|
600
|
|
601
|
// should i delete indicators that were in the list but they are not in the new list?
|
602
|
@RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/numbers/save", method = RequestMethod.POST)
|
603
|
public List<Indicator> saveNumbers(@PathVariable("stakeholder") String stakeholder,
|
604
|
@PathVariable("topic") String topic,
|
605
|
@PathVariable("category") String category,
|
606
|
@PathVariable("subcategory") String subcategory,
|
607
|
@RequestBody List<Indicator> numbers) {
|
608
|
return saveIndicators(stakeholder, topic, category, subcategory, numbers, "number");
|
609
|
}
|
610
|
|
611
|
public List<Indicator> saveIndicators(String stakeholder, String topic,
|
612
|
String category, String subcategory,
|
613
|
List<Indicator> indicators, String type) {
|
614
|
log.debug("to save indicators: "+indicators.size());
|
615
|
List<Indicator> indicatorsSaved = new ArrayList<>();
|
616
|
for(Indicator indicator : indicators) {
|
617
|
indicatorsSaved.add(indicatorDAO.save(indicator));
|
618
|
}
|
619
|
log.debug("saved indicators: "+indicators.size());
|
620
|
|
621
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
622
|
if(_stakeholder != null) {
|
623
|
Topic _topic = _stakeholder.getTopics().stream()
|
624
|
.filter(current_topic -> current_topic.getAlias().equals(topic))
|
625
|
.findFirst()
|
626
|
.orElse(null);
|
627
|
if(_topic != null) {
|
628
|
Category _category = _topic.getCategories().stream()
|
629
|
.filter(current_category -> current_category.getAlias().equals(category))
|
630
|
.findFirst()
|
631
|
.orElse(null);
|
632
|
if(_category != null) {
|
633
|
SubCategory _subCategory = _category.getSubCategories().stream()
|
634
|
.filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
|
635
|
.findFirst()
|
636
|
.orElse(null);
|
637
|
if(_subCategory != null) {
|
638
|
List<String> _indicators = null;
|
639
|
if(type.equals("chart")) {
|
640
|
_indicators = _subCategory.getCharts();
|
641
|
} else if(type.equals("number")) {
|
642
|
_indicators = _subCategory.getNumbers();
|
643
|
}
|
644
|
|
645
|
_indicators.clear();
|
646
|
for(Indicator indicator : indicators) {
|
647
|
_indicators.add(indicator.getId());
|
648
|
}
|
649
|
stakeholderDAO.save(_stakeholder);
|
650
|
} else {
|
651
|
// EXCEPTION - Subcategory not found
|
652
|
}
|
653
|
} else {
|
654
|
// EXCEPTION - Vategory not found
|
655
|
}
|
656
|
} else {
|
657
|
// EXCEPTION - Topic not found
|
658
|
}
|
659
|
} else {
|
660
|
// EXCEPTION - Stakeholder not found
|
661
|
}
|
662
|
|
663
|
return indicatorsSaved;
|
664
|
}
|
665
|
|
666
|
|
667
|
|
668
|
// Remember to check if alias is not already used before saving
|
669
|
@RequestMapping(value = "/{stakeholder}/topic/save", method = RequestMethod.POST)
|
670
|
public Stakeholder saveTopic(@PathVariable("stakeholder") String stakeholder,
|
671
|
@RequestBody Topic topic) {
|
672
|
Stakeholder stakeholderSaved = null;
|
673
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
674
|
if (stakeholder != null) {
|
675
|
List<Topic> topics = _stakeholder.getTopics();
|
676
|
Topic _topic = topics.stream()
|
677
|
.filter(current_topic -> current_topic.getAlias().equals(topic.getAlias()))
|
678
|
.findFirst()
|
679
|
.orElse(null);
|
680
|
if(_topic != null) {
|
681
|
_topic = topic;
|
682
|
} else {
|
683
|
topics.add(topic);
|
684
|
_stakeholder.setTopics(topics);
|
685
|
}
|
686
|
stakeholderSaved = stakeholderDAO.save(_stakeholder);
|
687
|
}
|
688
|
return stakeholderSaved;
|
689
|
}
|
690
|
|
691
|
@RequestMapping(value = "/{stakeholder}/topics/save", method = RequestMethod.POST)
|
692
|
public Stakeholder saveTopics(@PathVariable("stakeholder") String stakeholder,
|
693
|
@RequestBody List<Topic> topics) {
|
694
|
Stakeholder stakeholderSaved = null;
|
695
|
Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
|
696
|
if (stakeholder != null) {
|
697
|
_stakeholder.setTopics(topics);
|
698
|
stakeholderSaved = stakeholderDAO.save(_stakeholder);
|
699
|
}
|
700
|
return stakeholderSaved;
|
701
|
}
|
702
|
|
703
|
|
704
|
@RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
|
705
|
public List<Date> getAllStakeholderDates() {
|
706
|
List<Stakeholder> profiles = stakeholderDAO.findAll();
|
707
|
List<Date> profileDates = new ArrayList<>();
|
708
|
|
709
|
int i=0;
|
710
|
for(Stakeholder profile : profiles) {
|
711
|
log.debug(profile.getCreationDate());
|
712
|
profileDates.add(profile.getCreationDate());
|
713
|
log.debug(profileDates.get(i));
|
714
|
i++;
|
715
|
}
|
716
|
return profileDates;
|
717
|
}
|
718
|
|
719
|
@RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
|
720
|
public List<String> getAllStakeholderDates1() {
|
721
|
List<Stakeholder> profiles = stakeholderDAO.findAll();
|
722
|
List<String> profileDates = new ArrayList<>();
|
723
|
|
724
|
for(Stakeholder profile : profiles) {
|
725
|
log.debug(profile.getCreationDate().toString());
|
726
|
profileDates.add(profile.getCreationDate().toString());
|
727
|
}
|
728
|
return profileDates;
|
729
|
}
|
730
|
|
731
|
@RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
|
732
|
public List<String> getAllStakeholderDates2() {
|
733
|
List<Stakeholder> profiles = stakeholderDAO.findAll();
|
734
|
List<String> profileDates = new ArrayList<>();
|
735
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
736
|
|
737
|
for(Stakeholder profile : profiles) {
|
738
|
log.debug(format.format(profile.getCreationDate()));
|
739
|
profileDates.add(format.format(profile.getCreationDate()));
|
740
|
}
|
741
|
return profileDates;
|
742
|
}
|
743
|
}
|