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.services.PortalService;
|
13
|
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.security.access.prepost.PreAuthorize;
|
17
|
import org.springframework.web.bind.annotation.*;
|
18
|
|
19
|
import java.util.List;
|
20
|
|
21
|
@RestController
|
22
|
@RequestMapping("/community")
|
23
|
@CrossOrigin(origins = "*")
|
24
|
public class CommunityController {
|
25
|
private final Logger log = Logger.getLogger(this.getClass());
|
26
|
|
27
|
@Autowired
|
28
|
private AuthorizationService authorizationService;
|
29
|
|
30
|
@Autowired
|
31
|
private LayoutService layoutService;
|
32
|
|
33
|
@Autowired
|
34
|
private NotificationsService notificationsService;
|
35
|
|
36
|
@Autowired
|
37
|
private StatisticsService statisticsService;
|
38
|
|
39
|
@Autowired
|
40
|
private SubscriberService subscriberService;
|
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) {
|
98
|
List<String> roles = authorizationService.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
|
}
|
118
|
|
119
|
return true;
|
120
|
}
|
121
|
|
122
|
@RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
|
123
|
public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
|
124
|
Portal portal = portalService.getPortal(pid);
|
125
|
if(portal == null) {
|
126
|
// EXCEPTION - Entity Not Found
|
127
|
throw new ContentNotFoundException("CommunityController - Get layout: Portal with pid: " + pid + " not found");
|
128
|
}
|
129
|
if(!portal.getType().equals("community")) {
|
130
|
// EXCEPTION - MismatchingContent
|
131
|
throw new MismatchingContentException("CommunityController - Get layout: Portal with pid: "+pid+" has type: "+portal.getType()+" instead of community");
|
132
|
}
|
133
|
return layoutService.findByPid(pid);
|
134
|
}
|
135
|
|
136
|
@PreAuthorize("hasAnyAuthority(" +
|
137
|
"@AuthorizationService.PORTAL_ADMIN, " +
|
138
|
"@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
|
139
|
@RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
|
140
|
public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
|
141
|
Portal portal = portalService.getPortal(pid);
|
142
|
if(portal == null) {
|
143
|
// EXCEPTION - Entity Not Found
|
144
|
throw new ContentNotFoundException("CommunityController - Update layout: Portal with pid: " + pid + " not found");
|
145
|
}
|
146
|
if(!portal.getType().equals("community")) {
|
147
|
// EXCEPTION - MismatchingContent
|
148
|
throw new MismatchingContentException("CommunityController - Update layout: Portal with pid: "+pid+" has type: "+portal.getType()+" instead of community");
|
149
|
}
|
150
|
if(!pid.equals(layout.getPortalPid())) {
|
151
|
// EXCEPTION - MismatchingContent
|
152
|
throw new MismatchingContentException("CommunityController - Update layout: Portal has pid: "+pid+" while layout has portalPid: "+layout.getPortalPid());
|
153
|
}
|
154
|
return layoutService.save(layout);
|
155
|
}
|
156
|
}
|
157
|
|