uoa-admin-tools/src/main/java/eu/dnetlib/uoaadmintools/controllers/CommunityController.java

100 lines
3.8 KiB
Java

package eu.dnetlib.uoaadmintools.controllers;
import eu.dnetlib.uoaadmintools.entities.Layout;
import eu.dnetlib.uoaadmintools.services.LayoutService;
import eu.dnetlib.uoaadmintools.services.StatisticsService;
import eu.dnetlib.uoaadmintools.services.SubscriberService;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
@RestController
@RequestMapping("/community")
@CrossOrigin(origins = "*")
public class CommunityController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private LayoutService layoutService;
@Autowired
private StatisticsService statisticsService;
@Autowired
private SubscriberService subscriberService;
@Autowired
private PortalService portalService;
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public List<Portal> getAllCommunities() {
return portalService.getAllPortalsByType("community");
}
@RequestMapping(value = {"/full"}, method = RequestMethod.GET)
public List<PortalResponse> getAllCommunitiesFull() {
return portalService.getAllPortalsFullByType("community");
}
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/update", method = RequestMethod.POST)
public PortalResponse updateCommunity(@RequestBody Portal portal) {
PortalResponse portalResponse = portalService.updatePortal(portal);
String old_pid = portalResponse.getPid();
String new_pid = portal.getPid();
if(!old_pid.equals(new_pid)) {
statisticsService.updatePid(old_pid, new_pid);
subscriberService.updatePid(old_pid, new_pid);
layoutService.updatePid(old_pid, new_pid);
}
return portalResponse;
}
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public PortalResponse insertCommunity(@RequestBody Portal portal) {
PortalResponse portalResponse = portalService.insertPortal(portal);
statisticsService.createPortalStatistics(portal.getPid());
subscriberService.createPortalSubscribers(portal.getPid());
return portalResponse;
}
// cannot handle MismatchingContent
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public Boolean deleteCommunities(@RequestBody List<String> portals) {
for (String id: portals) {
String pid = portalService.deletePortal(id);
statisticsService.deleteByPid(pid);
subscriberService.deletePortalSubscribers(pid);
layoutService.deleteByPid(pid);
}
return true;
}
@RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
return layoutService.findByPid(pid);
}
// @PreAuthorize("hasAnyAuthority(" +
// "@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
// "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
@RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
return layoutService.save(layout);
}
}