Project

General

Profile

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.uoaadmintoolslibrary.entities.Portal;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
7
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
8
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
9
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
10
import org.apache.log4j.Logger;
11
import org.springframework.security.access.prepost.PreAuthorize;
12
import org.springframework.web.bind.annotation.*;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
import java.util.*;
16

    
17
@RestController
18
@RequestMapping("/connect")
19
@CrossOrigin(origins = "*")
20
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
21
public class ConnectController {
22
    private final Logger log = Logger.getLogger(this.getClass());
23

    
24
    @Autowired
25
    private LayoutService layoutService;
26

    
27
    @Autowired
28
    private PortalService portalService;
29

    
30
    @RequestMapping(value = "/update", method = RequestMethod.POST)
31
    public PortalResponse updateConnect(@RequestBody Portal portal) {
32
        if(!portal.getType().equals("connect")) {
33
            // EXCEPTION - MismatchingContent
34
            throw new MismatchingContentException("Update Connect: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of connect");
35
        }
36

    
37
        PortalResponse portalResponse = portalService.updatePortal(portal);
38

    
39
        String old_pid = portalResponse.getPid();
40
        String new_pid = portal.getPid();
41
        if(!old_pid.equals(new_pid)) {
42
            layoutService.updatePid(old_pid, new_pid);
43
        }
44

    
45
        return portalResponse;
46
    }
47

    
48
    @RequestMapping(value = "/save", method = RequestMethod.POST)
49
    public PortalResponse insertConnect(@RequestBody Portal portal) {
50
        if(!portal.getType().equals("connect")) {
51
            // EXCEPTION - MismatchingContent
52
            throw new MismatchingContentException("Save Connect: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of connect");
53
        }
54

    
55
        PortalResponse portalResponse = portalService.insertPortal(portal);
56
        return portalResponse;
57
    }
58

    
59
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
60
    public Boolean deleteConnect(@RequestBody List<String> portals) {
61
        for (String id: portals) {
62
            Portal portal = portalService.getPortalById(id);
63
            if(portal == null) {
64
                // EXCEPTION - Entity Not Found
65
                throw new ContentNotFoundException("Delete connect: Portal with id: " + id + " not found");
66
            }
67
            if(!portal.getType().equals("connect")) {
68
                // EXCEPTION - MismatchingContent
69
                throw new MismatchingContentException("Delete Connect: Portal with id: "+id+" has type: "+portal.getType()+" instead of connect");
70
            }
71

    
72
            String pid = portalService.deletePortal(id);
73
            layoutService.deleteByPid(pid);
74
        }
75

    
76
        return true;
77
    }
78

    
79
//    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.GET)
80
//    public Layout getLayoutForConnect(@PathVariable(value = "pid") String pid) {
81
//        return layoutService.findByPid(pid);
82
//    }
83
//
84
//    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
85
//    @RequestMapping(value = "/{pid}/layout", method = RequestMethod.POST)
86
//    public Layout updateLayoutForConnect(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
87
//        return layoutService.save(layout);
88
//    }
89
}
90

    
(2-2/9)