Project

General

Profile

1 50222 konstantin
package eu.dnetlib.uoaadmintools.controllers;
2
3 59470 konstantin
import eu.dnetlib.uoaadmintools.entities.Layout;
4
import eu.dnetlib.uoaadmintools.services.LayoutService;
5 60034 konstantin
import eu.dnetlib.uoaadmintools.services.NotificationsService;
6 59470 konstantin
import eu.dnetlib.uoaadmintools.services.StatisticsService;
7
import eu.dnetlib.uoaadmintools.services.SubscriberService;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
9 60509 k.triantaf
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse;
10 60501 konstantin
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
11
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
12 60513 k.triantaf
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
13 59470 konstantin
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
14 60509 k.triantaf
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
15 50222 konstantin
import org.apache.log4j.Logger;
16
import org.springframework.beans.factory.annotation.Autowired;
17 60501 konstantin
import org.springframework.security.access.prepost.PreAuthorize;
18 60509 k.triantaf
import org.springframework.web.bind.annotation.*;
19 50222 konstantin
20 60509 k.triantaf
import java.util.List;
21
22 50222 konstantin
@RestController
23 59470 konstantin
@RequestMapping("/community")
24 50222 konstantin
@CrossOrigin(origins = "*")
25
public class CommunityController {
26
    private final Logger log = Logger.getLogger(this.getClass());
27
28
    @Autowired
29 60513 k.triantaf
    private RolesUtils rolesUtils;
30 60501 konstantin
31
    @Autowired
32 59470 konstantin
    private LayoutService layoutService;
33 50222 konstantin
34
    @Autowired
35 60034 konstantin
    private NotificationsService notificationsService;
36
37
    @Autowired
38 59470 konstantin
    private StatisticsService statisticsService;
39 55027 k.triantaf
40
    @Autowired
41 59470 konstantin
    private SubscriberService subscriberService;
42 50222 konstantin
43
    @Autowired
44 59470 konstantin
    private PortalService portalService;
45 50222 konstantin
46 59482 konstantin
    @RequestMapping(value = {""}, method = RequestMethod.GET)
47 59470 konstantin
    public List<Portal> getAllCommunities() {
48
        return portalService.getAllPortalsByType("community");
49 50222 konstantin
    }
50
51 59470 konstantin
    @RequestMapping(value = {"/full"}, method = RequestMethod.GET)
52
    public List<PortalResponse> getAllCommunitiesFull() {
53
        return portalService.getAllPortalsFullByType("community");
54 50222 konstantin
    }
55
56 60501 konstantin
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
57 59470 konstantin
    @RequestMapping(value = "/update", method = RequestMethod.POST)
58
    public PortalResponse updateCommunity(@RequestBody Portal portal) {
59 60501 konstantin
        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 60034 konstantin
        String old_pid = portalService.getPortalById(portal.getId()).getPid();
65
        String new_pid = portal.getPid();
66
67 59470 konstantin
        PortalResponse portalResponse = portalService.updatePortal(portal);
68 50222 konstantin
69 59470 konstantin
        if(!old_pid.equals(new_pid)) {
70 60034 konstantin
            log.debug("update portal pid - old: "+old_pid + " - new: "+new_pid);
71 59470 konstantin
            statisticsService.updatePid(old_pid, new_pid);
72
            subscriberService.updatePid(old_pid, new_pid);
73
            layoutService.updatePid(old_pid, new_pid);
74 60034 konstantin
            notificationsService.updatePid(old_pid, new_pid);
75 50222 konstantin
        }
76
77 59470 konstantin
        return portalResponse;
78 50222 konstantin
    }
79
80 60501 konstantin
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
81 59470 konstantin
    @RequestMapping(value = "/save", method = RequestMethod.POST)
82
    public PortalResponse insertCommunity(@RequestBody Portal portal) {
83 60501 konstantin
        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 59470 konstantin
        PortalResponse portalResponse = portalService.insertPortal(portal);
89 50222 konstantin
90 59470 konstantin
        statisticsService.createPortalStatistics(portal.getPid());
91
        subscriberService.createPortalSubscribers(portal.getPid());
92 50222 konstantin
93 59470 konstantin
        return portalResponse;
94 50222 konstantin
    }
95
96 60501 konstantin
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
97 59470 konstantin
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
98
    public Boolean deleteCommunities(@RequestBody List<String> portals) {
99 60513 k.triantaf
        List<String> roles = rolesUtils.getRoles();
100 60501 konstantin
101 59470 konstantin
        for (String id: portals) {
102 60501 konstantin
            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 59470 konstantin
            String pid = portalService.deletePortal(id);
113 57497 konstantin
114 59470 konstantin
            statisticsService.deleteByPid(pid);
115
            subscriberService.deletePortalSubscribers(pid);
116
            layoutService.deleteByPid(pid);
117 60034 konstantin
            notificationsService.deleteByPid(pid);
118 50222 konstantin
        }
119 51340 konstantin
120 50222 konstantin
        return true;
121
    }
122
123 59470 konstantin
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
124 55027 k.triantaf
    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
125 60501 konstantin
        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 59470 konstantin
        return layoutService.findByPid(pid);
135 55027 k.triantaf
    }
136
137 60501 konstantin
    @PreAuthorize("hasAnyAuthority(" +
138
            "@AuthorizationService.PORTAL_ADMIN, " +
139
            "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
140 59470 konstantin
    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
141 57319 k.triantaf
    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
142 60501 konstantin
        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 59470 konstantin
        return layoutService.save(layout);
156 55027 k.triantaf
    }
157 50222 konstantin
}