Project

General

Profile

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
            }
417

    
418
            Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
419
            while(stakeholderIterator.hasNext()) {
420
                Stakeholder stakeholder = stakeholderIterator.next();
421

    
422
//                if(roles.contains(authorizationService.curator(stakeholder.getType()))
423
//                        || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
424
                if(rolesUtils.isCurator(roles, stakeholder.getType())
425
                        || rolesUtils.isManager(roles, stakeholder.getType(), stakeholder.getAlias())) {
426
                    stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
427
                    continue;
428
                }
429
                stakeholderIterator.remove();
430
            }
431
        }
432

    
433
//        log.debug(new Date());
434

    
435
        return stakeholdersFull;
436
    }
437

    
438
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
439
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
440
//        log.debug("get stakeholder: "+alias);
441

    
442
        Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
443
        if(stakeholder == null) {
444
            // EXCEPTION - Stakeholder not found
445
            throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
446
        }
447

    
448
//        List<String> roles = authorizationService.getRoles();
449
        List<String> roles = rolesUtils.getRoles();
450

    
451
        if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn(roles)) {
452
            // EXCEPTION - Unauthorized
453
            throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias);
454
        }
455
        if(stakeholder.getDefaultId() == null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
456
            // EXCEPTION - Access denied
457
            throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: "+alias);
458
        }
459

    
460
        if((stakeholder.getVisibility() == Visibility.PRIVATE && !rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())
461
                || (stakeholder.getVisibility() == Visibility.RESTRICTED && !rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias()) && !rolesUtils.isMember(roles, stakeholder.getType(), stakeholder.getAlias())))) {
462
//            // EXCEPTION - Access denied
463
//            throw new ForbiddenException("Get stakeholder: You are not authorized to get stakeholder with alias: "+alias);
464
            List<String> topicsEmpty = stakeholder.getTopics();
465
            topicsEmpty.clear();
466
            stakeholder.setTopics(topicsEmpty);
467
            stakeholder.setVisibility(Visibility.PRIVATE);
468
            return stakeholder;
469
        }
470

    
471
        return this.setFullEntities(stakeholder, roles);
472
    }
473

    
474
//    @PreAuthorize("isAuthenticated()")
475
    @PreAuthorize("hasAnyAuthority("
476
        + "@AuthorizationService.PORTAL_ADMIN, "
477
        + "@AuthorizationService.curator(#stakeholderFull.getType()), "
478
        + "@AuthorizationService.manager(#stakeholderFull.getType(), #stakeholderFull.getAlias()) "
479
    + ")")
480
    @RequestMapping(value = "/save", method = RequestMethod.POST)
481
    public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
482
        log.debug("save stakeholder");
483
        log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId());
484

    
485
//        if(stakeholderFull == null) {
486
//            log.debug("stakeholder null");
487
//            // EXCEPTION - Parameter for Stakeholder is not accepted
488
//        }
489

    
490
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
491

    
492
        Date date = new Date();
493
        stakeholder.setUpdateDate(date);
494

    
495
        List<String> topics = new ArrayList<>();
496

    
497
        // stakeholder does not exist in DB
498
        if(stakeholderFull.getId() == null) {
499
            stakeholder.setCreationDate(date);
500

    
501
            for(Topic topic : stakeholderFull.getTopics()) {
502
                topics.add(topic.getId());
503
            }
504
        } else {
505
            Stakeholder<String> oldStakeholder = stakeholderDAO.findById(stakeholderFull.getId());
506
            if(oldStakeholder == null) {
507
                // EXCEPTION - Stakeholder not found
508
                throw new EntityNotFoundException("save stakeholder: Stakeholder with id: "+stakeholderFull.getId()+" not found");
509
            }
510
            for(String topicId : oldStakeholder.getTopics()) {
511
                Topic topic = topicDAO.findById(topicId);
512
                if (topic == null) {
513
                    // EXCEPTION - Topic not found
514
                    throw new EntityNotFoundException("Save stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
515
                }
516
                topics.add(topic.getId());
517
            }
518
//            stakeholder.setTopics(topics);
519
//            stakeholderFull = this.setFullEntities(stakeholder, rolesUtils.getRoles());
520
        }
521

    
522
        stakeholder.setTopics(topics);
523

    
524
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
525
        stakeholderFull.setId(stakeholderSaved.getId());
526
        stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate());
527
        stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate());
528

    
529
        topics = null;
530
        stakeholder = null;
531
        stakeholderSaved = null;
532

    
533
        return stakeholderFull;
534
    }
535

    
536
    @PreAuthorize("isAuthenticated()")
537
    @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
538
    public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
539
        log.debug("delete stakeholder");
540
        log.debug("Id: "+stakeholderId);
541

    
542
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
543

    
544
        if(stakeholder != null) {
545
//            List<String> roles = authorizationService.getRoles();
546
            List<String> roles = rolesUtils.getRoles();
547

    
548
//            if(!roles.contains(authorizationService.PORTAL_ADMIN)
549
//                    && !roles.contains(authorizationService.curator(stakeholder.getType()))) {
550
            if(!rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
551
                // EXCEPTION - Access denied
552
                throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId);
553
            }
554

    
555
//            for(String topicId : stakeholder.getTopics()) {
556
//                Topic<String> topic = topicDAO.findById(topicId);
557
//                if (topic == null) {
558
//                    // EXCEPTION - Topic not found
559
//                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
560
//                }
561
//
562
//                for (String categoryId : topic.getCategories()) {
563
//                    Category<String> category = categoryDAO.findById(categoryId);
564
//                    if (category == null) {
565
//                        // EXCEPTION - Category not found
566
//                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
567
//                    }
568
//
569
//                    for (String subCategoryId : category.getSubCategories()) {
570
//                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
571
//                        if (subcategory == null) {
572
//                            // EXCEPTION - SubCategory not found
573
//                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
574
//                        }
575
//
576
//                        for(String chartSectionId : subcategory.getCharts()) {
577
//                            Section<String> chartSection = sectionDAO.findById(chartSectionId);
578
//                            if (chartSection == null) {
579
//                                // EXCEPTION - Section not found
580
//                                throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
581
//                            }
582
//
583
//                            for (String chartId : chartSection.getIndicators()) {
584
//                                indicatorDAO.delete(chartId);
585
//                            }
586
//                            subcategory.setCharts(null);
587
//                            sectionDAO.delete(chartSectionId);
588
//                        }
589
//
590
//                        for(String numberSectionId : subcategory.getNumbers()) {
591
//                            Section<String> numberSection = sectionDAO.findById(numberSectionId);
592
//                            if (numberSection == null) {
593
//                                // EXCEPTION - Section not found
594
//                                throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
595
//                            }
596
//
597
//                            for (String numberId : numberSection.getIndicators()) {
598
//                                indicatorDAO.delete(numberId);
599
//                            }
600
//                            subcategory.setNumbers(null);
601
//                            sectionDAO.delete(numberSectionId);
602
//                        }
603
//
604
//                        subCategoryDAO.delete(subCategoryId);
605
//                    }
606
//                    category.setSubCategories(null);
607
//                    categoryDAO.delete(categoryId);
608
//                }
609
//                topic.setCategories(null);
610
//                topicDAO.delete(topicId);
611
//            }
612

    
613
            topicController.deleteTree(stakeholder);
614

    
615
            stakeholder.setTopics(null);
616
            stakeholderDAO.delete(stakeholderId);
617
            log.debug("Stakeholder deleted!");
618
        } else {
619
            // EXCEPTION - Stakeholder not found
620
            throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
621
        }
622
        return true;
623
    }
624

    
625

    
626
//    @RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST)
627
//    public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) {
628
//        log.debug("toggle stakeholder status (isActive)");
629
//        log.debug("Stakeholder: "+stakeholderId);
630
//
631
//        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
632
//        if (stakeholder == null) {
633
//            // EXCEPTION - Stakeholder not found
634
//            throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found");
635
//        }
636
//        stakeholder.setIsActive(!stakeholder.getIsActive());
637
//
638
//        stakeholderDAO.save(stakeholder);
639
//        log.debug("Stakeholder toggled!");
640
//
641
//        return stakeholder.getIsActive();
642
//    }
643
//
644
//    @RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST)
645
//    public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) {
646
//        log.debug("toggle stakeholder access (isPublic)");
647
//        log.debug("Stakeholder: "+stakeholderId);
648
//
649
//        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
650
//        if (stakeholder == null) {
651
//            // EXCEPTION - Stakeholder not found
652
//            throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found");
653
//        }
654
//        stakeholder.setIsPublic(!stakeholder.getIsPublic());
655
//
656
//        stakeholderDAO.save(stakeholder);
657
//        log.debug("Stakeholder toggled!");
658
//
659
//        return stakeholder.getIsPublic();
660
//    }
661

    
662

    
663
    @PreAuthorize("isAuthenticated()")
664
    @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST)
665
    public Visibility toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId,
666
                                              @RequestParam("visibility") Visibility visibility) {
667
        log.debug("change stakeholder visibility: "+visibility);
668
        log.debug("Stakeholder: "+stakeholderId);
669

    
670
        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
671
        if (stakeholder == null) {
672
            // EXCEPTION - Stakeholder not found
673
            throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found");
674
        }
675

    
676
//        List<String> roles = authorizationService.getRoles();
677
        List<String> roles = rolesUtils.getRoles();
678

    
679
//        if(!roles.contains(authorizationService.PORTAL_ADMIN)
680
//                && !roles.contains(authorizationService.curator(stakeholder.getType()))
681
//                && !roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
682
        if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
683
            // EXCEPTION - Access denied
684
            throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId);
685
        }
686
        stakeholder.setVisibility(visibility);
687

    
688
        stakeholderDAO.save(stakeholder);
689
        log.debug("Stakeholder toggled!");
690

    
691
        return stakeholder.getVisibility();
692
    }
693

    
694
    // The following are not supposed to be used
695
//    @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
696
//    public List<Date> getAllStakeholderDates() {
697
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
698
//        List<Date> profileDates = new ArrayList<>();
699
//
700
//        int i=0;
701
//        for(Stakeholder profile : profiles) {
702
//            log.debug(profile.getCreationDate());
703
//            profileDates.add(profile.getCreationDate());
704
//            log.debug(profileDates.get(i));
705
//            i++;
706
//        }
707
//        return profileDates;
708
//    }
709
//
710
//    @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
711
//    public List<String> getAllStakeholderDates1() {
712
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
713
//        List<String> profileDates = new ArrayList<>();
714
//
715
//        for(Stakeholder profile : profiles) {
716
//            log.debug(profile.getCreationDate().toString());
717
//            profileDates.add(profile.getCreationDate().toString());
718
//        }
719
//        return profileDates;
720
//    }
721
//
722
//    @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
723
//    public List<String> getAllStakeholderDates2() {
724
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
725
//        List<String> profileDates = new ArrayList<>();
726
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
727
//
728
//        for(Stakeholder profile : profiles) {
729
//            log.debug(format.format(profile.getCreationDate()));
730
//            profileDates.add(format.format(profile.getCreationDate()));
731
//        }
732
//        return profileDates;
733
//    }
734
}
(6-6/9)