Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse;
5
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
6
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
7
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.security.access.prepost.PreAuthorize;
11
import org.springframework.web.bind.annotation.*;
12

    
13
import java.util.List;
14

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

    
22
    @Autowired
23
    private PortalService portalService;
24

    
25
    @RequestMapping(value = "/update", method = RequestMethod.POST)
26
    public PortalResponse updateExplore(@RequestBody Portal portal) {
27
        if(!portal.getType().equals("explore")) {
28
            // EXCEPTION - MismatchingContent
29
            throw new MismatchingContentException("Update Explore: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of explore");
30
        }
31
        PortalResponse portalResponse = portalService.updatePortal(portal);
32
        return portalResponse;
33
    }
34

    
35
    @RequestMapping(value = "/save", method = RequestMethod.POST)
36
    public PortalResponse insertExplore(@RequestBody Portal portal) {
37
        if(!portal.getType().equals("explore")) {
38
            // EXCEPTION - MismatchingContent
39
            throw new MismatchingContentException("Save Explore: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of explore");
40
        }
41
        PortalResponse portalResponse = portalService.insertPortal(portal);
42
        return portalResponse;
43
    }
44

    
45
    // cannot handle MismatchingContent
46
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
47
    public Boolean deleteExplore(@RequestBody List<String> portals) throws Exception {
48
        for (String id : portals) {
49
            Portal portal = portalService.getPortalById(id);
50
            if(portal == null) {
51
                // EXCEPTION - Entity Not Found
52
                throw new ContentNotFoundException("Delete Explore: Portal with id: " + id + " not found");
53
            }
54
            if(!portal.getType().equals("explore")) {
55
                // EXCEPTION - MismatchingContent
56
                throw new MismatchingContentException("Delete Explore: Portal with id: "+id+" has type: "+portal.getType()+" instead of explore");
57
            }
58
            portalService.deletePortal(id);
59
        }
60

    
61
        return true;
62
    }
63
}
64

    
(5-5/9)