Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.entities.Layout;
4
import eu.dnetlib.uoaadmintools.services.LayoutService;
5
import eu.dnetlib.uoaadmintools.services.NotificationsService;
6
import eu.dnetlib.uoaadmintools.services.StatisticsService;
7
import eu.dnetlib.uoaadmintools.services.SubscriberService;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
9
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
10
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
11
import org.apache.log4j.Logger;
12
import org.springframework.web.bind.annotation.*;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
import java.util.*;
16

    
17
@RestController
18
@RequestMapping("/community")
19
@CrossOrigin(origins = "*")
20
public class CommunityController {
21
    private final Logger log = Logger.getLogger(this.getClass());
22

    
23
    @Autowired
24
    private LayoutService layoutService;
25

    
26
    @Autowired
27
    private NotificationsService notificationsService;
28

    
29
    @Autowired
30
    private StatisticsService statisticsService;
31

    
32
    @Autowired
33
    private SubscriberService subscriberService;
34

    
35
    @Autowired
36
    private PortalService portalService;
37

    
38
    @RequestMapping(value = {""}, method = RequestMethod.GET)
39
    public List<Portal> getAllCommunities() {
40
        return portalService.getAllPortalsByType("community");
41
    }
42

    
43
    @RequestMapping(value = {"/full"}, method = RequestMethod.GET)
44
    public List<PortalResponse> getAllCommunitiesFull() {
45
        return portalService.getAllPortalsFullByType("community");
46
    }
47

    
48
//    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
49
    @RequestMapping(value = "/update", method = RequestMethod.POST)
50
    public PortalResponse updateCommunity(@RequestBody Portal portal) {
51
        String old_pid = portalService.getPortalById(portal.getId()).getPid();
52
        String new_pid = portal.getPid();
53

    
54
        PortalResponse portalResponse = portalService.updatePortal(portal);
55

    
56
        if(!old_pid.equals(new_pid)) {
57
            log.debug("update portal pid - old: "+old_pid + " - new: "+new_pid);
58
            statisticsService.updatePid(old_pid, new_pid);
59
            subscriberService.updatePid(old_pid, new_pid);
60
            layoutService.updatePid(old_pid, new_pid);
61
            notificationsService.updatePid(old_pid, new_pid);
62
        }
63

    
64
        return portalResponse;
65
    }
66

    
67
//    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
68
    @RequestMapping(value = "/save", method = RequestMethod.POST)
69
    public PortalResponse insertCommunity(@RequestBody Portal portal) {
70
        PortalResponse portalResponse = portalService.insertPortal(portal);
71

    
72
        statisticsService.createPortalStatistics(portal.getPid());
73
        subscriberService.createPortalSubscribers(portal.getPid());
74

    
75
        return portalResponse;
76
    }
77

    
78
    // cannot handle MismatchingContent
79
//    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
80
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
81
    public Boolean deleteCommunities(@RequestBody List<String> portals) {
82
        for (String id: portals) {
83
            String pid = portalService.deletePortal(id);
84

    
85
            statisticsService.deleteByPid(pid);
86
            subscriberService.deletePortalSubscribers(pid);
87
            layoutService.deleteByPid(pid);
88
            notificationsService.deleteByPid(pid);
89
        }
90

    
91
        return true;
92
    }
93

    
94
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
95
    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
96
        return layoutService.findByPid(pid);
97
    }
98

    
99
//    @PreAuthorize("hasAnyAuthority(" +
100
//            "@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
101
//            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
102
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
103
    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
104
        return layoutService.save(layout);
105
    }
106
}
107

    
(1-1/9)