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.PortalResponse;
10
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
11
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
12
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
13
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
14
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
15
import org.apache.log4j.Logger;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.security.access.prepost.PreAuthorize;
18
import org.springframework.web.bind.annotation.*;
19

    
20
import java.util.List;
21

    
22
@RestController
23
@RequestMapping("/community")
24
@CrossOrigin(origins = "*")
25
public class CommunityController {
26
    private final Logger log = Logger.getLogger(this.getClass());
27

    
28
    @Autowired
29
    private RolesUtils rolesUtils;
30

    
31
    @Autowired
32
    private LayoutService layoutService;
33

    
34
    @Autowired
35
    private NotificationsService notificationsService;
36

    
37
    @Autowired
38
    private StatisticsService statisticsService;
39

    
40
    @Autowired
41
    private SubscriberService subscriberService;
42

    
43
    @Autowired
44
    private PortalService portalService;
45

    
46
    @RequestMapping(value = {""}, method = RequestMethod.GET)
47
    public List<Portal> getAllCommunities() {
48
        return portalService.getAllPortalsByType("community");
49
    }
50

    
51
    @RequestMapping(value = {"/full"}, method = RequestMethod.GET)
52
    public List<PortalResponse> getAllCommunitiesFull() {
53
        return portalService.getAllPortalsFullByType("community");
54
    }
55

    
56
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
57
    @RequestMapping(value = "/update", method = RequestMethod.POST)
58
    public PortalResponse updateCommunity(@RequestBody Portal portal) {
59
        if(!portal.getType().equals("community")) {
60
            // EXCEPTION - MismatchingContent
61
            throw new MismatchingContentException("Update Community: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
62
        }
63

    
64
        String old_pid = portalService.getPortalById(portal.getId()).getPid();
65
        String new_pid = portal.getPid();
66

    
67
        PortalResponse portalResponse = portalService.updatePortal(portal);
68

    
69
        if(!old_pid.equals(new_pid)) {
70
            log.debug("update portal pid - old: "+old_pid + " - new: "+new_pid);
71
            statisticsService.updatePid(old_pid, new_pid);
72
            subscriberService.updatePid(old_pid, new_pid);
73
            layoutService.updatePid(old_pid, new_pid);
74
            notificationsService.updatePid(old_pid, new_pid);
75
        }
76

    
77
        return portalResponse;
78
    }
79

    
80
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
81
    @RequestMapping(value = "/save", method = RequestMethod.POST)
82
    public PortalResponse insertCommunity(@RequestBody Portal portal) {
83
        if(!portal.getType().equals("community")) {
84
            // EXCEPTION - MismatchingContent
85
            throw new MismatchingContentException("Save Community: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
86
        }
87

    
88
        PortalResponse portalResponse = portalService.insertPortal(portal);
89

    
90
        statisticsService.createPortalStatistics(portal.getPid());
91
        subscriberService.createPortalSubscribers(portal.getPid());
92

    
93
        return portalResponse;
94
    }
95

    
96
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
97
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
98
    public Boolean deleteCommunities(@RequestBody List<String> portals) {
99
        List<String> roles = rolesUtils.getRoles();
100

    
101
        for (String id: portals) {
102
            Portal portal = portalService.getPortalById(id);
103
            if(portal == null) {
104
                // EXCEPTION - Entity Not Found
105
                throw new ContentNotFoundException("Delete community: Portal with id: " + id + " not found");
106
            }
107
            if(!portal.getType().equals("community")) {
108
                // EXCEPTION - MismatchingContent
109
                throw new MismatchingContentException("Delete Community: Portal with id: "+id+" has type: "+portal.getType()+" instead of community");
110
            }
111

    
112
            String pid = portalService.deletePortal(id);
113

    
114
            statisticsService.deleteByPid(pid);
115
            subscriberService.deletePortalSubscribers(pid);
116
            layoutService.deleteByPid(pid);
117
            notificationsService.deleteByPid(pid);
118
        }
119

    
120
        return true;
121
    }
122

    
123
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
124
    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
125
        Portal portal = portalService.getPortal(pid);
126
        if(portal == null) {
127
            // EXCEPTION - Entity Not Found
128
            throw new ContentNotFoundException("CommunityController - Get layout: Portal with pid: " + pid + " not found");
129
        }
130
        if(!portal.getType().equals("community")) {
131
            // EXCEPTION - MismatchingContent
132
            throw new MismatchingContentException("CommunityController - Get layout: Portal with pid: "+pid+" has type: "+portal.getType()+" instead of community");
133
        }
134
        return layoutService.findByPid(pid);
135
    }
136

    
137
    @PreAuthorize("hasAnyAuthority(" +
138
            "@AuthorizationService.PORTAL_ADMIN, " +
139
            "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
140
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
141
    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
142
        Portal portal = portalService.getPortal(pid);
143
        if(portal == null) {
144
            // EXCEPTION - Entity Not Found
145
            throw new ContentNotFoundException("CommunityController - Update layout: Portal with pid: " + pid + " not found");
146
        }
147
        if(!portal.getType().equals("community")) {
148
            // EXCEPTION - MismatchingContent
149
            throw new MismatchingContentException("CommunityController - Update layout: Portal with pid: "+pid+" has type: "+portal.getType()+" instead of community");
150
        }
151
        if(!pid.equals(layout.getPortalPid())) {
152
            // EXCEPTION - MismatchingContent
153
            throw new MismatchingContentException("CommunityController - Update layout: Portal has pid: "+pid+" while layout has portalPid: "+layout.getPortalPid());
154
        }
155
        return layoutService.save(layout);
156
    }
157
}
158

    
(1-1/9)