package org.synyx.urlaubsverwaltung.person.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.synyx.urlaubsverwaltung.person.Person;
import org.synyx.urlaubsverwaltung.person.PersonService;
import org.synyx.urlaubsverwaltung.security.SecurityRules;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static java.util.stream.Collectors.toList;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.OK;

@Api("Persons: Get information about the persons of the application")
@RestController("restApiPersonController")
@RequestMapping(PersonApiController.ROOT_URL)
public class PersonApiController {

    static final String ROOT_URL = "/api/persons";
    private static final String PERSON_URL = "/{id}";

    private final PersonService personService;

    @Autowired
    PersonApiController(PersonService personService) {

        this.personService = personService;
    }

    @ApiOperation(
        value = "Get all active persons of the application", notes = "Get all active persons of the application"
    )
    @GetMapping
    @PreAuthorize(SecurityRules.IS_OFFICE)
    public ResponseEntity<List<PersonResponse>> persons(HttpServletRequest request) {

        List<PersonResponse> persons = personService.getActivePersons().stream()
            .map(p -> createPersonResponse(p, request))
            .collect(toList());

        return new ResponseEntity<>(persons, OK);
    }

    @ApiOperation(value = "Get one active person by id", notes = "Get one active person by id")
    @GetMapping(PERSON_URL)
    @PreAuthorize(SecurityRules.IS_OFFICE)
    public ResponseEntity<PersonResponse> getPerson(@PathVariable Integer id, HttpServletRequest request) {

        return personService.getPersonByID(id)
            .map(value -> new ResponseEntity<>(createPersonResponse(value, request), OK))
            .orElseGet(() -> new ResponseEntity<>(NOT_FOUND));
    }

    private PersonResponse createPersonResponse(Person person, HttpServletRequest request) {
        final PersonResponse personResponse = PersonResponseMapper.mapToResponse(person);
        final String baseUri = new URIBuilder()
            .setScheme(request.getScheme())
            .setPort(request.getServerPort())
            .setHost(request.getServerName())
            .setPath(ROOT_URL)
            .toString();
        personResponse.add(new Link(baseUri + "/" + person.getId()).withSelfRel());
        return personResponse;
    }
}

@@ -125,56 +125,8 @@
 on;%0A
-import org.apache.http.client.utils.URIBuilder;%0A
 impo
@@ -188,49 +188,8 @@
 ed;%0A
-import org.springframework.hateoas.Link;%0A
 impo
@@ -714,54 +714,8 @@
 s;%0A%0A
-import javax.servlet.http.HttpServletRequest;%0A
 impo
@@ -784,16 +784,170 @@
 toList;%0A
+import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;%0Aimport static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;%0A
 import s
@@ -1782,34 +1782,8 @@
 ons(
-HttpServletRequest request
 ) %7B%0A
@@ -1885,13 +1885,14 @@
 map(
-p -%3E 
+this::
 crea
@@ -1911,20 +1911,8 @@
 onse
-(p, request)
 )%0A  
@@ -2246,36 +2246,8 @@
 r id
-, HttpServletRequest request
 ) %7B%0A
@@ -2371,17 +2371,8 @@
 alue
-, request
 ), O
@@ -2510,36 +2510,8 @@
 rson
-, HttpServletRequest request
 ) %7B%0A
@@ -2612,292 +2612,80 @@
     
-final String baseUri = new URIBuilder()%0A            .setScheme(request.getScheme())%0A            .setPort(request.getServerPort())%0A            .setHost(request.getServerName())%0A            .setPath(ROOT_URL)%0A            .toString();%0A        personResponse.add(new Link(baseUri + %22/%22 + 
+personResponse.add(linkTo(methodOn(PersonApiController.class).getPerson(
 pers
@@ -2695,16 +2695,17 @@
 getId())
+)
 .withSel

replace apache client URIBuilder with spring hateoas ControllerLinkBuilder