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

91 lines
3.6 KiB
Java

package eu.dnetlib.uoaadmintools.controllers;
import eu.dnetlib.uoaadmintools.entities.Layout;
import eu.dnetlib.uoaadmintools.services.LayoutService;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
import org.apache.log4j.Logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
@RestController
@RequestMapping("/connect")
@CrossOrigin(origins = "*")
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
public class ConnectController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private LayoutService layoutService;
@Autowired
private PortalService portalService;
@RequestMapping(value = "/update", method = RequestMethod.POST)
public PortalResponse updateConnect(@RequestBody Portal portal) {
if(!portal.getType().equals("connect")) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException("Update Connect: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of connect");
}
PortalResponse portalResponse = portalService.updatePortal(portal);
String old_pid = portalResponse.getPid();
String new_pid = portal.getPid();
if(!old_pid.equals(new_pid)) {
layoutService.updatePid(old_pid, new_pid);
}
return portalResponse;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public PortalResponse insertConnect(@RequestBody Portal portal) {
if(!portal.getType().equals("connect")) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException("Save Connect: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of connect");
}
PortalResponse portalResponse = portalService.insertPortal(portal);
return portalResponse;
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public Boolean deleteConnect(@RequestBody List<String> portals) {
for (String id: portals) {
Portal portal = portalService.getPortalById(id);
if(portal == null) {
// EXCEPTION - Entity Not Found
throw new ContentNotFoundException("Delete connect: Portal with id: " + id + " not found");
}
if(!portal.getType().equals("connect")) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException("Delete Connect: Portal with id: "+id+" has type: "+portal.getType()+" instead of connect");
}
String pid = portalService.deletePortal(id);
layoutService.deleteByPid(pid);
}
return true;
}
// @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
// public Layout getLayoutForConnect(@PathVariable(value = "pid") String pid) {
// return layoutService.findByPid(pid);
// }
//
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
// @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
// public Layout updateLayoutForConnect(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
// return layoutService.save(layout);
// }
}