package hello.controllers;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import hello.dao.GreetingService;
import hello.entities.Greeting;

@RestController
@RequestMapping(value = "/greeting", produces = { MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE })
public class GreetingControllerRest {

	@Autowired
	private GreetingService greetingService;

	@RequestMapping(method = RequestMethod.GET)
	public Resources<Resource<Greeting>> list() {
		return greetingToResource(greetingService.findAll());
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Resource<Greeting> greeting(@PathVariable("id") long id) {
		Greeting greeting = greetingService.findOne(id);

		return greetingToResource(greeting);
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ResponseEntity<?> newGreeting() {
		return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).build();
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> createGreeting(@RequestBody @Valid Greeting greeting,
			BindingResult result, UriComponentsBuilder b) {

		if (result.hasErrors()) {
			return ResponseEntity.unprocessableEntity().build();
		}

		greetingService.save(greeting);
		UriComponents location = b.path("/greeting/{id}").buildAndExpand(greeting.getId());

		return ResponseEntity.created(location.toUri()).build();
	}

	private Resource<Greeting> greetingToResource(Greeting greeting) {
		Link selfLink = linkTo(GreetingControllerRest.class).slash(greeting.getId()).withSelfRel();

		return new Resource<Greeting>(greeting, selfLink);
	}

	private Resources<Resource<Greeting>> greetingToResource(Iterable<Greeting> greetings) {
		Link selfLink = linkTo(methodOn(GreetingControllerRest.class).list()).withSelfRel();

		List<Resource<Greeting>> resources = new ArrayList<Resource<Greeting>>();
		for (Greeting greeting : greetings) {
			resources.add(greetingToResource(greeting));
		}

		return new Resources<Resource<Greeting>>(resources, selfLink);
	}
}

@@ -176,16 +176,37 @@
 hodOn;%0A%0A
+import java.net.URI;%0A
 import j
@@ -1023,117 +1023,8 @@
 ler;
-%0Aimport org.springframework.web.util.UriComponents;%0Aimport org.springframework.web.util.UriComponentsBuilder;
 %0A%0Aim
@@ -2031,20 +2031,17 @@
 reeting,
-%0A%09%09%09
+ 
 BindingR
@@ -2056,32 +2056,8 @@
 sult
-, UriComponentsBuilder b
 ) %7B%0A
@@ -2187,71 +2187,64 @@
 %0A%09%09U
-riComponents location = b.path(%22/greeting/%7Bid%7D%22).buildAndExpand
+RI location = linkTo(GreetingControllerRest.class).slash
 (gre
@@ -2257,16 +2257,24 @@
 getId())
+.toUri()
 ;%0A%0A%09%09ret
@@ -2308,24 +2308,16 @@
 location
-.toUri()
 ).build(

Set the location of a created greeting with linkTo.