Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.controllers;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
4
import eu.dnetlib.uoamonitorservice.dao.*;
5
import eu.dnetlib.uoamonitorservice.entities.*;
6
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
7
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
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
                                                    @RequestParam(required = false) String defaultId) {
342
//        log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
343

    
344
        List<Stakeholder> stakeholders;
345
        if(type != null && defaultId != null) {
346
            stakeholders = stakeholderDAO.findByDefaultIdAndType(defaultId, type);
347
        } else if(defaultId != null) {
348
            stakeholders = stakeholderDAO.findByDefaultId(defaultId);
349
        } else if(type != null) {
350
            stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
351
        } else {
352
            stakeholders = stakeholderDAO.findByDefaultIdNot(null);
353
        }
354

    
355
        //List<Stakeholder> stakeholdersFull = new ArrayList<>();
356

    
357
        if(stakeholders != null && stakeholders.size() > 0) {
358
//            List<String> roles = authorizationService.getRoles();
359
            List<String> roles = rolesUtils.getRoles();
360

    
361
//            if (roles.contains(authorizationService.PORTAL_ADMIN)) {
362
            if (rolesUtils.isPortalAdmin(roles)) {
363
//                for(Stakeholder stakeholder : stakeholders) {
364
//                    stakeholdersFull.add(this.setFullEntities(stakeholder));
365
//                }
366
//                return stakeholdersFull;
367
                return stakeholders;
368
            }
369

    
370
            Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
371
            while(stakeholderIterator.hasNext()) {
372
                Stakeholder stakeholder = stakeholderIterator.next();
373

    
374
//                if(roles.contains(authorizationService.curator(stakeholder.getType()))
375
//                        || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))
376
//                        || stakeholder.getVisibility() == Visibility.PUBLIC
377
//                        || (stakeholder.getVisibility() == Visibility.RESTRICTED && roles.contains(authorizationService.member(stakeholder.getType(), stakeholder.getAlias())))) {
378
                if(rolesUtils.isCurator(roles, stakeholder.getType())
379
                        || rolesUtils.isManager(roles, stakeholder.getType(), stakeholder.getAlias())
380
                        || stakeholder.getVisibility() == Visibility.PUBLIC
381
                        || (stakeholder.getVisibility() == Visibility.RESTRICTED && rolesUtils.isMember(roles, stakeholder.getType(), stakeholder.getAlias()))) {
382
                    //stakeholdersFull.add(this.setFullEntities(stakeholder));
383
                    continue;
384
                }
385
                stakeholderIterator.remove();
386
            }
387
        }
388

    
389
//        log.debug(new Date());
390

    
391
//        return stakeholdersFull;
392
        return stakeholders;
393
    }
394

    
395
    @PreAuthorize("isAuthenticated()")
396
    @RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET)
397
    public List<Stakeholder> getMyRealStakeholders(@RequestParam(required = false) String type) {
398
//        log.debug("get my NOT default stakeholders" + (type != null ? " with type: "+type : ""));
399

    
400
        List<Stakeholder> stakeholders;
401
        if(type == null) {
402
            stakeholders = stakeholderDAO.findByDefaultIdNot(null);
403
        } else {
404
            stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
405
        }
406

    
407
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
408

    
409
        if(stakeholders != null && stakeholders.size() > 0) {
410
//            List<String> roles = authorizationService.getRoles();
411
            List<String> roles = rolesUtils.getRoles();
412
//            log.debug("ROLES: ");
413
//            roles.forEach(role -> log.debug(role));
414

    
415
//            if (roles.contains(authorizationService.PORTAL_ADMIN)) {
416
            if (rolesUtils.isPortalAdmin(roles)) {
417
//                for(Stakeholder stakeholder : stakeholders) {
418
//                    stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
419
//                }
420
//                return stakeholdersFull;
421
                return stakeholders;
422
            }
423

    
424
            Iterator<Stakeholder> stakeholderIterator = stakeholders.iterator();
425
            while(stakeholderIterator.hasNext()) {
426
                Stakeholder stakeholder = stakeholderIterator.next();
427

    
428
//                if(roles.contains(authorizationService.curator(stakeholder.getType()))
429
//                        || roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
430
                if(rolesUtils.isCurator(roles, stakeholder.getType())
431
                        || rolesUtils.isManager(roles, stakeholder.getType(), stakeholder.getAlias())) {
432
                    //stakeholdersFull.add(this.setFullEntities(stakeholder, roles));
433
                    continue;
434
                } else {
435
                    stakeholderIterator.remove();
436
                }
437
            }
438
        }
439

    
440
//        log.debug(new Date());
441

    
442
        return stakeholders;
443
    }
444

    
445
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
446
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
447
//        log.debug("get stakeholder: "+alias);
448

    
449
        Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
450
        if(stakeholder == null) {
451
            // EXCEPTION - Stakeholder not found
452
            throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
453
        }
454

    
455
//        List<String> roles = authorizationService.getRoles();
456
        List<String> roles = rolesUtils.getRoles();
457

    
458
        if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn(roles)) {
459
            // EXCEPTION - Unauthorized
460
            throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias);
461
        }
462
        if(stakeholder.getDefaultId() == null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
463
            // EXCEPTION - Access denied
464
            throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: "+alias);
465
        }
466

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

    
478
        return this.setFullEntities(stakeholder, roles);
479
    }
480

    
481
//    @PreAuthorize("isAuthenticated()")
482
    @PreAuthorize("hasAnyAuthority("
483
        + "@AuthorizationService.PORTAL_ADMIN, "
484
        + "@AuthorizationService.curator(#_stakeholder.getType()), "
485
        + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) "
486
    + ")")
487
    @RequestMapping(value = "/save", method = RequestMethod.POST)
488
    public Stakeholder saveStakeholder(@RequestBody Stakeholder _stakeholder) {
489
        log.debug("save stakeholder");
490
        log.debug("Alias: "+_stakeholder.getAlias() + " - Id: "+_stakeholder.getId());
491

    
492
//        if(_stakeholder == null) {
493
//            log.debug("stakeholder null");
494
//            // EXCEPTION - Parameter for Stakeholder is not accepted
495
//        }
496

    
497
        Stakeholder<String> stakeholder = new Stakeholder<>(_stakeholder);
498

    
499
        Date date = new Date();
500
        stakeholder.setUpdateDate(date);
501

    
502
        List<String> topics = new ArrayList<>();
503

    
504
        // stakeholder does not exist in DB
505
        if(_stakeholder.getId() == null) {
506
            stakeholder.setCreationDate(date);
507

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

    
529
        stakeholder.setTopics(topics);
530

    
531
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
532
        _stakeholder.setId(stakeholderSaved.getId());
533
        _stakeholder.setCreationDate(stakeholderSaved.getCreationDate());
534
        _stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate());
535

    
536
        topics = null;
537
        stakeholder = null;
538
        stakeholderSaved = null;
539

    
540
        return _stakeholder;
541
    }
542

    
543
    @PreAuthorize("isAuthenticated()")
544
    @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
545
    public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
546
        log.debug("delete stakeholder");
547
        log.debug("Id: "+stakeholderId);
548

    
549
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
550

    
551
        if(stakeholder != null) {
552
//            List<String> roles = authorizationService.getRoles();
553
            List<String> roles = rolesUtils.getRoles();
554

    
555
//            if(!roles.contains(authorizationService.PORTAL_ADMIN)
556
//                    && !roles.contains(authorizationService.curator(stakeholder.getType()))) {
557
            if(!rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
558
                // EXCEPTION - Access denied
559
                throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId);
560
            }
561

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

    
620
            topicController.deleteTree(stakeholder);
621

    
622
            stakeholder.setTopics(null);
623
            stakeholderDAO.delete(stakeholderId);
624
            log.debug("Stakeholder deleted!");
625
        } else {
626
            // EXCEPTION - Stakeholder not found
627
            throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
628
        }
629
        return true;
630
    }
631

    
632

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

    
669

    
670
    @PreAuthorize("isAuthenticated()")
671
    @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST)
672
    public Visibility toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId,
673
                                              @RequestParam("visibility") Visibility visibility) {
674
        log.debug("change stakeholder visibility: "+visibility);
675
        log.debug("Stakeholder: "+stakeholderId);
676

    
677
        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
678
        if (stakeholder == null) {
679
            // EXCEPTION - Stakeholder not found
680
            throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found");
681
        }
682

    
683
//        List<String> roles = authorizationService.getRoles();
684
        List<String> roles = rolesUtils.getRoles();
685

    
686
//        if(!roles.contains(authorizationService.PORTAL_ADMIN)
687
//                && !roles.contains(authorizationService.curator(stakeholder.getType()))
688
//                && !roles.contains(authorizationService.manager(stakeholder.getType(), stakeholder.getAlias()))) {
689
        if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
690
            // EXCEPTION - Access denied
691
            throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId);
692
        }
693
        stakeholder.setVisibility(visibility);
694

    
695
        stakeholderDAO.save(stakeholder);
696
        log.debug("Stakeholder toggled!");
697

    
698
        return stakeholder.getVisibility();
699
    }
700

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