Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.entities.Layout;
4
import eu.dnetlib.uoaadmintools.services.*;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse;
7
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
8
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
9
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
10
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
11
import org.apache.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.security.access.prepost.PreAuthorize;
14
import org.springframework.web.bind.annotation.*;
15

    
16
import java.util.List;
17

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

    
24
    @Autowired
25
    private RolesUtils rolesUtils;
26

    
27
    @Autowired
28
    private LayoutService layoutService;
29

    
30
    @Autowired
31
    private NotificationsService notificationsService;
32

    
33
    @Autowired
34
    private StatisticsService statisticsService;
35

    
36
    @Autowired
37
    private SubscriberService subscriberService;
38

    
39
    @Autowired
40
    private MenuService menuService;
41

    
42
    @Autowired
43
    private PortalService portalService;
44

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

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

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

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

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

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

    
76
        return portalResponse;
77
    }
78

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

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

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

    
92
        return portalResponse;
93
    }
94

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

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

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

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

    
120
        return true;
121
    }
122

    
123
    @RequestMapping(value = "/layouts", method = RequestMethod.GET)
124
    public List<Layout> getAvailableLayouts() {
125
        return this.layoutService.findAll();
126
    }
127

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

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

    
163
    @PreAuthorize("hasAnyAuthority(" +
164
            "@AuthorizationService.PORTAL_ADMIN, " +
165
            "@AuthorizationService.curator('community'))")
166
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.DELETE)
167
    public boolean deleteLayoutForCommunity(@PathVariable(value = "pid") String pid) {
168
        Portal portal = portalService.getPortal(pid);
169
        if(portal == null) {
170
            // EXCEPTION - Entity Not Found
171
            throw new ContentNotFoundException("CommunityController - Delete layout: Portal with pid: " + pid + " not found");
172
        }
173
        if(!portal.getType().equals("community")) {
174
            // EXCEPTION - MismatchingContent
175
            throw new MismatchingContentException("CommunityController - Delete layout: Portal with pid: "+pid+" has type: "+portal.getType()+" instead of community");
176
        }
177
        return layoutService.deleteByPid(pid);
178
    }
179
}
180

    
(3-3/12)