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.uoaadmintoolslibrary.handlers.ForbiddenException;
|
7
|
import eu.dnetlib.uoamonitorservice.handlers.utils.RolesUtils;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
|
11
|
import org.springframework.security.access.AccessDeniedException;
|
12
|
import org.springframework.security.access.AuthorizationServiceException;
|
13
|
import org.springframework.security.access.prepost.PreAuthorize;
|
14
|
import org.springframework.web.bind.annotation.*;
|
15
|
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Date;
|
18
|
import java.util.Iterator;
|
19
|
import java.util.List;
|
20
|
|
21
|
@RestController
|
22
|
@CrossOrigin(origins = "*")
|
23
|
public class StakeholderController {
|
24
|
private final Logger log = Logger.getLogger(this.getClass());
|
25
|
|
26
|
@Autowired
|
27
|
private RolesUtils rolesUtils;
|
28
|
|
29
|
@Autowired
|
30
|
private StakeholderDAO stakeholderDAO;
|
31
|
|
32
|
@Autowired
|
33
|
private TopicDAO topicDAO;
|
34
|
|
35
|
@Autowired
|
36
|
private CategoryDAO categoryDAO;
|
37
|
|
38
|
@Autowired
|
39
|
private SubCategoryDAO subCategoryDAO;
|
40
|
|
41
|
@Autowired
|
42
|
private SectionDAO sectionDAO;
|
43
|
|
44
|
@Autowired
|
45
|
private IndicatorDAO indicatorDAO;
|
46
|
|
47
|
@Autowired
|
48
|
private TopicController topicController;
|
49
|
|
50
|
@PreAuthorize("isAuthenticated()")
|
51
|
@RequestMapping(value = "/stakeholder/alias", method = RequestMethod.GET)
|
52
|
public List<String> getAllReservedStakeholderAlias() {
|
53
|
// log.debug("get all stakeholder reserved alias-es");
|
54
|
List<String> stakeholderAlias = new ArrayList<>();
|
55
|
|
56
|
List<Stakeholder> stakeholders = stakeholderDAO.findAll();
|
57
|
if(stakeholders != null) {
|
58
|
stakeholders.forEach(stakeholder -> {
|
59
|
stakeholderAlias.add(stakeholder.getAlias());
|
60
|
});
|
61
|
}
|
62
|
stakeholderAlias.add( "all");
|
63
|
stakeholderAlias.add("default");
|
64
|
stakeholderAlias.add("alias");
|
65
|
|
66
|
return stakeholderAlias;
|
67
|
}
|
68
|
|
69
|
// @PreAuthorize("isAuthenticated()")
|
70
|
@PreAuthorize("hasAnyAuthority(" +
|
71
|
"@AuthorizationService.PORTAL_ADMIN, " +
|
72
|
"@AuthorizationService.curator(#stakeholderFull.getType()))")
|
73
|
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
|
74
|
public Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> stakeholderFull) {
|
75
|
log.debug("build stakeholder");
|
76
|
log.debug("Alias: "+stakeholderFull.getAlias());
|
77
|
|
78
|
Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
|
79
|
|
80
|
List<String> topics = new ArrayList<>();
|
81
|
List<Topic<Category<SubCategory<Section<Indicator>>>>> topicsFull = new ArrayList<>();
|
82
|
for(Topic topic : stakeholderFull.getTopics()) {
|
83
|
Topic<Category<SubCategory<Section<Indicator>>>> topicFull = topicController.buildTopic(topic);
|
84
|
topicsFull.add(topicFull);
|
85
|
topics.add(topicFull.getId());
|
86
|
}
|
87
|
stakeholderFull.setTopics(topicsFull);
|
88
|
stakeholder.setTopics(topics);
|
89
|
|
90
|
Date date = new Date();
|
91
|
stakeholder.setCreationDate(date);
|
92
|
stakeholder.setUpdateDate(date);
|
93
|
|
94
|
stakeholderFull.setCreationDate(date);
|
95
|
stakeholderFull.setUpdateDate(date);
|
96
|
|
97
|
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
|
98
|
stakeholderFull.setId(stakeholderSaved.getId());
|
99
|
return stakeholderFull;
|
100
|
//return null;
|
101
|
}
|
102
|
|
103
|
public Stakeholder setFullEntities(Stakeholder<String> stakeholder, List<String> roles) {
|
104
|
boolean addAll = false;
|
105
|
boolean addPublicAndRestricted = false;
|
106
|
|
107
|
// if(roles == null
|
108
|
// || roles.contains(authorizationService.PORTAL_ADMIN)
|
109
|
// || roles.contains(authorizationService.curator(stakeholder.getType()))
|
110
|
// || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
|
111
|
if(rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
112
|
//if(visibility == null || visibility == (Visibility.PRIVATE)) {
|
113
|
addAll = true;
|
114
|
//}
|
115
|
//if(visibility == null || visibility == (Visibility.PRIVATE) || visibility == (Visibility.RESTRICTED)) {
|
116
|
addPublicAndRestricted = true;
|
117
|
//}
|
118
|
// } else if(roles != null && roles.contains(authorizationService.member(stakeholder.getType(), stakeholder.getAlias()))) {
|
119
|
} else if(rolesUtils.isMember(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
120
|
//if(visibility == null || visibility == (Visibility.PRIVATE) || visibility == (Visibility.RESTRICTED)) {
|
121
|
addPublicAndRestricted = true;
|
122
|
//}
|
123
|
}
|
124
|
|
125
|
Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
|
126
|
|
127
|
List<Topic> topics = new ArrayList<>();
|
128
|
|
129
|
for (String topicId: (List<String>)stakeholder.getTopics()) {
|
130
|
Topic<String> topic = topicDAO.findById(topicId);
|
131
|
if(topic == null) {
|
132
|
// EXCEPTION - Topic not found
|
133
|
throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
|
134
|
}
|
135
|
|
136
|
if((!addAll && topic.getVisibility() == Visibility.PRIVATE)
|
137
|
|| (!addPublicAndRestricted && topic.getVisibility() == Visibility.RESTRICTED)) {
|
138
|
continue;
|
139
|
}
|
140
|
|
141
|
Topic<Category> topicFull = new Topic<Category>(topic);
|
142
|
|
143
|
List<Category> categories = new ArrayList<>();
|
144
|
|
145
|
for(String categoryId : topic.getCategories()) {
|
146
|
Category<String> category = categoryDAO.findById(categoryId);
|
147
|
if(category == null) {
|
148
|
// EXCEPTION - Category not found
|
149
|
throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
150
|
}
|
151
|
|
152
|
if((!addAll && category.getVisibility() == Visibility.PRIVATE)
|
153
|
|| (!addPublicAndRestricted && category.getVisibility() == Visibility.RESTRICTED)) {
|
154
|
continue;
|
155
|
}
|
156
|
|
157
|
Category<SubCategory> categoryFull = new Category<SubCategory>(category);
|
158
|
|
159
|
List<SubCategory> subCategories = new ArrayList<>();
|
160
|
|
161
|
for(String subCategoryId : category.getSubCategories()) {
|
162
|
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
|
163
|
if(subCategory == null) {
|
164
|
// EXCEPTION - SubCategory not found
|
165
|
throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
|
166
|
}
|
167
|
|
168
|
if((!addAll && subCategory.getVisibility() == Visibility.PRIVATE)
|
169
|
|| (!addPublicAndRestricted && subCategory.getVisibility() == Visibility.RESTRICTED)) {
|
170
|
continue;
|
171
|
}
|
172
|
|
173
|
SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
|
174
|
|
175
|
List<Section> sectionsCharts = new ArrayList<>();
|
176
|
|
177
|
for(String sectionId : subCategory.getCharts()) {
|
178
|
sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
|
179
|
}
|
180
|
subCategoryFull.setCharts(sectionsCharts);
|
181
|
|
182
|
List<Section> sectionsNumbers = new ArrayList<>();
|
183
|
|
184
|
for(String sectionId : subCategory.getNumbers()) {
|
185
|
sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
|
186
|
}
|
187
|
subCategoryFull.setNumbers(sectionsNumbers);
|
188
|
|
189
|
// List<Indicator> charts = new ArrayList<>();
|
190
|
// for(String indicatorId : subCategory.getCharts()) {
|
191
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
192
|
// if(indicator == null) {
|
193
|
// // EXCEPTION - Indicator not found
|
194
|
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
|
195
|
// }
|
196
|
// charts.add(indicator);
|
197
|
// }
|
198
|
// subCategoryFull.setCharts(charts);
|
199
|
//
|
200
|
// List<Indicator> numbers = new ArrayList<>();
|
201
|
// for (String indicatorId : subCategory.getNumbers()) {
|
202
|
// Indicator indicator = indicatorDAO.findById(indicatorId);
|
203
|
// if (indicator == null) {
|
204
|
// // EXCEPTION - Indicator not found
|
205
|
// throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")");
|
206
|
// }
|
207
|
// numbers.add(indicator);
|
208
|
// }
|
209
|
// subCategoryFull.setNumbers(numbers);
|
210
|
|
211
|
subCategories.add(subCategoryFull);
|
212
|
}
|
213
|
|
214
|
categoryFull.setSubCategories(subCategories);
|
215
|
categories.add(categoryFull);
|
216
|
}
|
217
|
|
218
|
topicFull.setCategories(categories);
|
219
|
topics.add(topicFull);
|
220
|
}
|
221
|
|
222
|
stakeholderFull.setTopics(topics);
|
223
|
return stakeholderFull;
|
224
|
}
|
225
|
|
226
|
// private SubCategory setFullSubcategory(SubCategory subCategory) {
|
227
|
// SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
|
228
|
//
|
229
|
// List<Section> sectionsCharts = new ArrayList<>();
|
230
|
//
|
231
|
// for(String sectionId : subCategory.getCharts()) {
|
232
|
// sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
|
233
|
// }
|
234
|
// subCategoryFull.setCharts(sectionsCharts);
|
235
|
//
|
236
|
// List<Section> sectionsNumbers = new ArrayList<>();
|
237
|
//
|
238
|
// for(String sectionId : subCategory.getNumbers()) {
|
239
|
// sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted));
|
240
|
// }
|
241
|
// subCategoryFull.setNumbers(sectionsNumbers);
|
242
|
// }
|
243
|
|
244
|
private Section getSectionFull(String sectionId, String subCategoryId, boolean addAll, boolean addPublicAndRestricted) {
|
245
|
Section<String> section = sectionDAO.findById(sectionId);
|
246
|
if (section == null) {
|
247
|
// EXCEPTION - Section not found
|
248
|
throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")");
|
249
|
}
|
250
|
|
251
|
Section sectionFull = new Section<Indicator>(section);
|
252
|
|
253
|
List<Indicator> indicators = new ArrayList<>();
|
254
|
for (String indicatorId : section.getIndicators()) {
|
255
|
Indicator indicator = indicatorDAO.findById(indicatorId);
|
256
|
if (indicator == null) {
|
257
|
// EXCEPTION - Indicator not found
|
258
|
throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")");
|
259
|
}
|
260
|
|
261
|
if((!addAll && indicator.getVisibility() == Visibility.PRIVATE)
|
262
|
|| (!addPublicAndRestricted && indicator.getVisibility() == Visibility.RESTRICTED)) {
|
263
|
continue;
|
264
|
}
|
265
|
|
266
|
indicators.add(indicator);
|
267
|
}
|
268
|
sectionFull.setIndicators(indicators);
|
269
|
|
270
|
return sectionFull;
|
271
|
}
|
272
|
|
273
|
@PreAuthorize("hasAnyAuthority(" +
|
274
|
"@AuthorizationService.PORTAL_ADMIN)")
|
275
|
@RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
|
276
|
public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
|
277
|
// log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
|
278
|
|
279
|
List<Stakeholder> stakeholders;
|
280
|
if(type == null) {
|
281
|
stakeholders = stakeholderDAO.findAll();
|
282
|
} else {
|
283
|
stakeholders = stakeholderDAO.findByType(type);
|
284
|
}
|
285
|
|
286
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
287
|
for(Stakeholder stakeholder : stakeholders) {
|
288
|
List<String> roles = rolesUtils.getRoles();
|
289
|
stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
|
290
|
}
|
291
|
|
292
|
return stakeholdersFull;
|
293
|
}
|
294
|
|
295
|
@PreAuthorize("isAuthenticated()")
|
296
|
@RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
|
297
|
public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
|
298
|
// log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
|
299
|
|
300
|
List<Stakeholder> stakeholders;
|
301
|
if(type == null) {
|
302
|
stakeholders = stakeholderDAO.findByDefaultId(null);
|
303
|
} else {
|
304
|
stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
|
305
|
}
|
306
|
|
307
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
308
|
|
309
|
// Remove stakeholders for which i do not have authority
|
310
|
if(stakeholders != null && stakeholders.size() > 0) {
|
311
|
List<String> roles = rolesUtils.getRoles();
|
312
|
// log.debug("ROLES: ");
|
313
|
// roles.forEach(role -> log.debug(role));
|
314
|
//
|
315
|
// if (roles.contains(authorizationService.PORTAL_ADMIN)) {
|
316
|
if (rolesUtils.isPortalAdmin(roles)) {
|
317
|
for(Stakeholder stakeholder : stakeholders) {
|
318
|
stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
|
319
|
}
|
320
|
return stakeholdersFull;
|
321
|
}
|
322
|
|
323
|
Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
|
324
|
while(stakeholderIterator.hasNext()) {
|
325
|
Stakeholder stakeholder = stakeholderIterator.next();
|
326
|
|
327
|
// if(roles.contains(authorizationService.curator(stakeholder.getType()))) {
|
328
|
if(rolesUtils.isCurator(roles, stakeholder.getType())) {
|
329
|
stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
|
330
|
continue;
|
331
|
}
|
332
|
stakeholderIterator.remove();
|
333
|
}
|
334
|
}
|
335
|
|
336
|
return stakeholdersFull;
|
337
|
}
|
338
|
|
339
|
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
|
340
|
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
|
341
|
// log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
|
342
|
|
343
|
List<Stakeholder> stakeholders;
|
344
|
if(type == null) {
|
345
|
stakeholders = stakeholderDAO.findByDefaultIdNot(null);
|
346
|
} else {
|
347
|
stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
|
348
|
}
|
349
|
|
350
|
//List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
351
|
|
352
|
if(stakeholders != null && stakeholders.size() > 0) {
|
353
|
// List<String> roles = authorizationService.getRoles();
|
354
|
List<String> roles = rolesUtils.getRoles();
|
355
|
|
356
|
// if (roles.contains(authorizationService.PORTAL_ADMIN)) {
|
357
|
if (rolesUtils.isPortalAdmin(roles)) {
|
358
|
// for(Stakeholder stakeholder : stakeholders) {
|
359
|
// stakeholdersFull.add(this.setFullEntities(stakeholder));
|
360
|
// }
|
361
|
// return stakeholdersFull;
|
362
|
return stakeholders;
|
363
|
}
|
364
|
|
365
|
Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
|
366
|
while(stakeholderIterator.hasNext()) {
|
367
|
Stakeholder stakeholder = stakeholderIterator.next();
|
368
|
|
369
|
// if(roles.contains(authorizationService.curator(stakeholder.getType()))
|
370
|
// || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))
|
371
|
// || stakeholder.getVisibility() == Visibility.PUBLIC
|
372
|
// || (stakeholder.getVisibility() == Visibility.RESTRICTED && roles.contains(authorizationService.member(stakeholder.getType(), stakeholder.getAlias())))) {
|
373
|
if(rolesUtils.isCurator(roles, stakeholder.getType())
|
374
|
|| rolesUtils.isManager(roles, stakeholder.getType(), stakeholder.getAlias())
|
375
|
|| stakeholder.getVisibility() == Visibility.PUBLIC
|
376
|
|| (stakeholder.getVisibility() == Visibility.RESTRICTED && rolesUtils.isMember(roles, stakeholder.getType(), stakeholder.getAlias()))) {
|
377
|
//stakeholdersFull.add(this.setFullEntities(stakeholder));
|
378
|
continue;
|
379
|
}
|
380
|
stakeholderIterator.remove();
|
381
|
}
|
382
|
}
|
383
|
|
384
|
// log.debug(new Date());
|
385
|
|
386
|
// return stakeholdersFull;
|
387
|
return stakeholders;
|
388
|
}
|
389
|
|
390
|
@PreAuthorize("isAuthenticated()")
|
391
|
@RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET)
|
392
|
public List<Stakeholder> getMyRealStakeholders(@RequestParam(required = false) String type) {
|
393
|
// log.debug("get my NOT default stakeholders" + (type != null ? " with type: "+type : ""));
|
394
|
|
395
|
List<Stakeholder> stakeholders;
|
396
|
if(type == null) {
|
397
|
stakeholders = stakeholderDAO.findByDefaultIdNot(null);
|
398
|
} else {
|
399
|
stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
|
400
|
}
|
401
|
|
402
|
List<Stakeholder> stakeholdersFull = new ArrayList<>();
|
403
|
|
404
|
if(stakeholders != null && stakeholders.size() > 0) {
|
405
|
// List<String> roles = authorizationService.getRoles();
|
406
|
List<String> roles = rolesUtils.getRoles();
|
407
|
// log.debug("ROLES: ");
|
408
|
// roles.forEach(role -> log.debug(role));
|
409
|
|
410
|
// if (roles.contains(authorizationService.PORTAL_ADMIN)) {
|
411
|
if (rolesUtils.isPortalAdmin(roles)) {
|
412
|
// for(Stakeholder stakeholder : stakeholders) {
|
413
|
// stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
|
414
|
// }
|
415
|
// return stakeholdersFull;
|
416
|
return stakeholders;
|
417
|
}
|
418
|
|
419
|
Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
|
420
|
while(stakeholderIterator.hasNext()) {
|
421
|
Stakeholder stakeholder = stakeholderIterator.next();
|
422
|
|
423
|
// if(roles.contains(authorizationService.curator(stakeholder.getType()))
|
424
|
// || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
|
425
|
if(rolesUtils.isCurator(roles, stakeholder.getType())
|
426
|
|| rolesUtils.isManager(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
427
|
//stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
|
428
|
continue;
|
429
|
} else {
|
430
|
stakeholderIterator.remove();
|
431
|
}
|
432
|
}
|
433
|
}
|
434
|
|
435
|
// log.debug(new Date());
|
436
|
|
437
|
return stakeholders;
|
438
|
}
|
439
|
|
440
|
@RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
|
441
|
public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
|
442
|
// log.debug("get stakeholder: "+alias);
|
443
|
|
444
|
Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
|
445
|
if(stakeholder == null) {
|
446
|
// EXCEPTION - Stakeholder not found
|
447
|
throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
|
448
|
}
|
449
|
|
450
|
// List<String> roles = authorizationService.getRoles();
|
451
|
List<String> roles = rolesUtils.getRoles();
|
452
|
|
453
|
if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn(roles)) {
|
454
|
// EXCEPTION - Unauthorized
|
455
|
throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias);
|
456
|
}
|
457
|
if(stakeholder.getDefaultId() == null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
|
458
|
// EXCEPTION - Access denied
|
459
|
throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: "+alias);
|
460
|
}
|
461
|
|
462
|
if((stakeholder.getVisibility() == Visibility.PRIVATE && !rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())
|
463
|
|| (stakeholder.getVisibility() == Visibility.RESTRICTED && !rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias()) && !rolesUtils.isMember(roles, stakeholder.getType(), stakeholder.getAlias())))) {
|
464
|
// // EXCEPTION - Access denied
|
465
|
// throw new ForbiddenException("Get stakeholder: You are not authorized to get stakeholder with alias: "+alias);
|
466
|
List<String> topicsEmpty = stakeholder.getTopics();
|
467
|
topicsEmpty.clear();
|
468
|
stakeholder.setTopics(topicsEmpty);
|
469
|
stakeholder.setVisibility(Visibility.PRIVATE);
|
470
|
return stakeholder;
|
471
|
}
|
472
|
|
473
|
return this.setFullEntities(stakeholder, roles);
|
474
|
}
|
475
|
|
476
|
// @PreAuthorize("isAuthenticated()")
|
477
|
@PreAuthorize("hasAnyAuthority("
|
478
|
+ "@AuthorizationService.PORTAL_ADMIN, "
|
479
|
+ "@AuthorizationService.curator(#_stakeholder.getType()), "
|
480
|
+ "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) "
|
481
|
+ ")")
|
482
|
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
483
|
public Stakeholder saveStakeholder(@RequestBody Stakeholder _stakeholder) {
|
484
|
log.debug("save stakeholder");
|
485
|
log.debug("Alias: "+_stakeholder.getAlias() + " - Id: "+_stakeholder.getId());
|
486
|
|
487
|
// if(_stakeholder == null) {
|
488
|
// log.debug("stakeholder null");
|
489
|
// // EXCEPTION - Parameter for Stakeholder is not accepted
|
490
|
// }
|
491
|
|
492
|
Stakeholder<String> stakeholder = new Stakeholder<>(_stakeholder);
|
493
|
|
494
|
Date date = new Date();
|
495
|
stakeholder.setUpdateDate(date);
|
496
|
|
497
|
List<String> topics = new ArrayList<>();
|
498
|
|
499
|
// stakeholder does not exist in DB
|
500
|
if(_stakeholder.getId() == null) {
|
501
|
stakeholder.setCreationDate(date);
|
502
|
|
503
|
// for(Topic topic : _stakeholder.getTopics()) {
|
504
|
// topics.add(topic.getId());
|
505
|
// }
|
506
|
} else {
|
507
|
Stakeholder<String> oldStakeholder = stakeholderDAO.findById(_stakeholder.getId());
|
508
|
if(oldStakeholder == null) {
|
509
|
// EXCEPTION - Stakeholder not found
|
510
|
throw new EntityNotFoundException("save stakeholder: Stakeholder with id: "+_stakeholder.getId()+" not found");
|
511
|
}
|
512
|
for(String topicId : oldStakeholder.getTopics()) {
|
513
|
Topic topic = topicDAO.findById(topicId);
|
514
|
if (topic == null) {
|
515
|
// EXCEPTION - Topic not found
|
516
|
throw new EntityNotFoundException("Save stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
|
517
|
}
|
518
|
topics.add(topic.getId());
|
519
|
}
|
520
|
// stakeholder.setTopics(topics);
|
521
|
// _stakeholder = this.setFullEntities(stakeholder, rolesUtils.getRoles());
|
522
|
}
|
523
|
|
524
|
stakeholder.setTopics(topics);
|
525
|
|
526
|
Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
|
527
|
_stakeholder.setId(stakeholderSaved.getId());
|
528
|
_stakeholder.setCreationDate(stakeholderSaved.getCreationDate());
|
529
|
_stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate());
|
530
|
|
531
|
topics = null;
|
532
|
stakeholder = null;
|
533
|
stakeholderSaved = null;
|
534
|
|
535
|
return _stakeholder;
|
536
|
}
|
537
|
|
538
|
@PreAuthorize("isAuthenticated()")
|
539
|
@RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
|
540
|
public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
|
541
|
log.debug("delete stakeholder");
|
542
|
log.debug("Id: "+stakeholderId);
|
543
|
|
544
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
545
|
|
546
|
if(stakeholder != null) {
|
547
|
// List<String> roles = authorizationService.getRoles();
|
548
|
List<String> roles = rolesUtils.getRoles();
|
549
|
|
550
|
// if(!roles.contains(authorizationService.PORTAL_ADMIN)
|
551
|
// && !roles.contains(authorizationService.curator(stakeholder.getType()))) {
|
552
|
if(!rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
|
553
|
// EXCEPTION - Access denied
|
554
|
throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId);
|
555
|
}
|
556
|
|
557
|
// for(String topicId : stakeholder.getTopics()) {
|
558
|
// Topic<String> topic = topicDAO.findById(topicId);
|
559
|
// if (topic == null) {
|
560
|
// // EXCEPTION - Topic not found
|
561
|
// throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
|
562
|
// }
|
563
|
//
|
564
|
// for (String categoryId : topic.getCategories()) {
|
565
|
// Category<String> category = categoryDAO.findById(categoryId);
|
566
|
// if (category == null) {
|
567
|
// // EXCEPTION - Category not found
|
568
|
// throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
569
|
// }
|
570
|
//
|
571
|
// for (String subCategoryId : category.getSubCategories()) {
|
572
|
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
573
|
// if (subcategory == null) {
|
574
|
// // EXCEPTION - SubCategory not found
|
575
|
// throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
576
|
// }
|
577
|
//
|
578
|
// for(String chartSectionId : subcategory.getCharts()) {
|
579
|
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
|
580
|
// if (chartSection == null) {
|
581
|
// // EXCEPTION - Section not found
|
582
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
583
|
// }
|
584
|
//
|
585
|
// for (String chartId : chartSection.getIndicators()) {
|
586
|
// indicatorDAO.delete(chartId);
|
587
|
// }
|
588
|
// subcategory.setCharts(null);
|
589
|
// sectionDAO.delete(chartSectionId);
|
590
|
// }
|
591
|
//
|
592
|
// for(String numberSectionId : subcategory.getNumbers()) {
|
593
|
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
|
594
|
// if (numberSection == null) {
|
595
|
// // EXCEPTION - Section not found
|
596
|
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
|
597
|
// }
|
598
|
//
|
599
|
// for (String numberId : numberSection.getIndicators()) {
|
600
|
// indicatorDAO.delete(numberId);
|
601
|
// }
|
602
|
// subcategory.setNumbers(null);
|
603
|
// sectionDAO.delete(numberSectionId);
|
604
|
// }
|
605
|
//
|
606
|
// subCategoryDAO.delete(subCategoryId);
|
607
|
// }
|
608
|
// category.setSubCategories(null);
|
609
|
// categoryDAO.delete(categoryId);
|
610
|
// }
|
611
|
// topic.setCategories(null);
|
612
|
// topicDAO.delete(topicId);
|
613
|
// }
|
614
|
|
615
|
topicController.deleteTree(stakeholder);
|
616
|
|
617
|
stakeholder.setTopics(null);
|
618
|
stakeholderDAO.delete(stakeholderId);
|
619
|
log.debug("Stakeholder deleted!");
|
620
|
} else {
|
621
|
// EXCEPTION - Stakeholder not found
|
622
|
throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
|
623
|
}
|
624
|
return true;
|
625
|
}
|
626
|
|
627
|
|
628
|
// @RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST)
|
629
|
// public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) {
|
630
|
// log.debug("toggle stakeholder status (isActive)");
|
631
|
// log.debug("Stakeholder: "+stakeholderId);
|
632
|
//
|
633
|
// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
|
634
|
// if (stakeholder == null) {
|
635
|
// // EXCEPTION - Stakeholder not found
|
636
|
// throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found");
|
637
|
// }
|
638
|
// stakeholder.setIsActive(!stakeholder.getIsActive());
|
639
|
//
|
640
|
// stakeholderDAO.save(stakeholder);
|
641
|
// log.debug("Stakeholder toggled!");
|
642
|
//
|
643
|
// return stakeholder.getIsActive();
|
644
|
// }
|
645
|
//
|
646
|
// @RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST)
|
647
|
// public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) {
|
648
|
// log.debug("toggle stakeholder access (isPublic)");
|
649
|
// log.debug("Stakeholder: "+stakeholderId);
|
650
|
//
|
651
|
// Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
|
652
|
// if (stakeholder == null) {
|
653
|
// // EXCEPTION - Stakeholder not found
|
654
|
// throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found");
|
655
|
// }
|
656
|
// stakeholder.setIsPublic(!stakeholder.getIsPublic());
|
657
|
//
|
658
|
// stakeholderDAO.save(stakeholder);
|
659
|
// log.debug("Stakeholder toggled!");
|
660
|
//
|
661
|
// return stakeholder.getIsPublic();
|
662
|
// }
|
663
|
|
664
|
|
665
|
@PreAuthorize("isAuthenticated()")
|
666
|
@RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST)
|
667
|
public Visibility toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId,
|
668
|
@RequestParam("visibility") Visibility visibility) {
|
669
|
log.debug("change stakeholder visibility: "+visibility);
|
670
|
log.debug("Stakeholder: "+stakeholderId);
|
671
|
|
672
|
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
|
673
|
if (stakeholder == null) {
|
674
|
// EXCEPTION - Stakeholder not found
|
675
|
throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found");
|
676
|
}
|
677
|
|
678
|
// List<String> roles = authorizationService.getRoles();
|
679
|
List<String> roles = rolesUtils.getRoles();
|
680
|
|
681
|
// if(!roles.contains(authorizationService.PORTAL_ADMIN)
|
682
|
// && !roles.contains(authorizationService.curator(stakeholder.getType()))
|
683
|
// && !roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
|
684
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
685
|
// EXCEPTION - Access denied
|
686
|
throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId);
|
687
|
}
|
688
|
stakeholder.setVisibility(visibility);
|
689
|
|
690
|
stakeholderDAO.save(stakeholder);
|
691
|
log.debug("Stakeholder toggled!");
|
692
|
|
693
|
return stakeholder.getVisibility();
|
694
|
}
|
695
|
|
696
|
// The following are not supposed to be used
|
697
|
// @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
|
698
|
// public List<Date> getAllStakeholderDates() {
|
699
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
700
|
// List<Date> profileDates = new ArrayList<>();
|
701
|
//
|
702
|
// int i=0;
|
703
|
// for(Stakeholder profile : profiles) {
|
704
|
// log.debug(profile.getCreationDate());
|
705
|
// profileDates.add(profile.getCreationDate());
|
706
|
// log.debug(profileDates.get(i));
|
707
|
// i++;
|
708
|
// }
|
709
|
// return profileDates;
|
710
|
// }
|
711
|
//
|
712
|
// @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
|
713
|
// public List<String> getAllStakeholderDates1() {
|
714
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
715
|
// List<String> profileDates = new ArrayList<>();
|
716
|
//
|
717
|
// for(Stakeholder profile : profiles) {
|
718
|
// log.debug(profile.getCreationDate().toString());
|
719
|
// profileDates.add(profile.getCreationDate().toString());
|
720
|
// }
|
721
|
// return profileDates;
|
722
|
// }
|
723
|
//
|
724
|
// @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
|
725
|
// public List<String> getAllStakeholderDates2() {
|
726
|
// List<Stakeholder> profiles = stakeholderDAO.findAll();
|
727
|
// List<String> profileDates = new ArrayList<>();
|
728
|
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
729
|
//
|
730
|
// for(Stakeholder profile : profiles) {
|
731
|
// log.debug(format.format(profile.getCreationDate()));
|
732
|
// profileDates.add(format.format(profile.getCreationDate()));
|
733
|
// }
|
734
|
// return profileDates;
|
735
|
// }
|
736
|
}
|