package info.agrueneberg.fhir.controllers;

import info.agrueneberg.fhir.exceptions.AccessDeniedException;
import info.agrueneberg.fhir.exceptions.NotFoundException;
import info.agrueneberg.fhir.models.MetadataResource;
import info.agrueneberg.fhir.models.User;
import info.agrueneberg.fhir.services.MetadataService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@ResponseBody
public class MetadataController {

    private final MetadataService metadataService;

    @Autowired
    public MetadataController(MetadataService metadataService) {
        this.metadataService = metadataService;
    }

    @RequestMapping(value = "/.well-known/governance", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public List<Map<String, String>> getAcls(@RequestParam(value = "resource") String resourcePath) throws NotFoundException, AccessDeniedException {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        boolean hasAccess = metadataService.hasPermission(resourcePath, user, "govern");
        if (!hasAccess) {
            throw new AccessDeniedException();
        }
        MetadataResource metadataResource = metadataService.getResource(resourcePath);
        List<Map<String, String>> acls = metadataResource.getAcls();
        if (acls == null) {
            acls = new ArrayList<Map<String, String>>(0);
        }
        return acls;
    }

    @RequestMapping(value = "/.well-known/governance", method = RequestMethod.PUT, consumes = "application/json")
    @ResponseStatus(HttpStatus.OK)
    public void updateAcls(@RequestParam(value = "resource") String resourcePath, @RequestBody List<Map<String, String>> acls) throws NotFoundException, AccessDeniedException {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        boolean hasAccess = metadataService.hasPermission(resourcePath, user, "govern");
        if (!hasAccess) {
            throw new AccessDeniedException();
        }
        MetadataResource metadataResource = metadataService.getResource(resourcePath);
        metadataResource.setAcls(acls);
        metadataService.updateResource(metadataResource);
    }

}
@@ -383,16 +383,63 @@
 il.Map;%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import o
@@ -1494,16 +1494,46 @@
 urcePath
+, HttpServletResponse response
 ) throws
@@ -1925,32 +1925,203 @@
 (resourcePath);%0A
+        if (metadataResource.getInheritFrom() != null) %7B%0A            response.addHeader(%22Link%22, %22%3C%22 + metadataResource.getInheritFrom() + %22%3E; rel=%5C%22inherit%5C%22%22);%0A        %7D%0A
         List%3CMap

Add inherit link relation.