// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/domain/Spanner.java
// public class Spanner {
// 
//     private Long id;
//     private String name;
//     private int size;
//     private String owner;
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public int getSize() {
//         return size;
//     }
// 
//     public void setSize(int size) {
//         this.size = size;
//     }
// 
//     public String getOwner() {
//         return owner;
//     }
// 
//     public void setOwner(String owner) {
//         this.owner = owner;
//     }
// }
// 
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/exception/SpannerNotFoundException.java
// public class SpannerNotFoundException extends Exception {
// 
//     private Long spannerId;
// 
//     /**
//      * New exception
//      * @param spannerId id of requested spanner
//      */
//     public SpannerNotFoundException(Long spannerId) {
//         super("Spanner not found: " + spannerId);
//         this.spannerId = spannerId;
//     }
// 
//     public Long getSpannerId() {
//         return spannerId;
//     }
// }
// 
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/services/SpannersService.java
// @Service
// public class SpannersService {
// 
//     private RestTemplate restTemplate;
// 
//     public SpannersService(RestTemplateBuilder builder,
//                            @Value("${app.service.url.spanners}") String rootUri) {
//         restTemplate = builder.messageConverters(halAwareMessageConverter())
//                               .rootUri(rootUri).build();
//     }
// 
// 
//     private HttpMessageConverter halAwareMessageConverter() {
//         ObjectMapper mapper = new ObjectMapper();
//         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//         mapper.registerModule(new Jackson2HalModule());
// 
//         return new MappingJackson2HttpMessageConverter(mapper);
//     }
// 
// 
//     public Collection<Spanner> findAll() {
//         ResponseEntity<PagedResources<Spanner>> response = restTemplate.exchange("/", HttpMethod.GET, null,
//                                                             new ParameterizedTypeReference<PagedResources<Spanner>>(){});
//         PagedResources<Spanner> pages = response.getBody();
//         return pages.getContent();
// 
//     }
// 
// 
//     public Spanner findOne(Long id) {
//         return restTemplate.getForObject("/{0}", Spanner.class, id);
//     }
// 
// 
//     @PreAuthorize("hasPermission(#spanner, 'owner')")
//     public void delete(Spanner spanner) {
//         restTemplate.delete("/{0}", spanner.getId());
//     }
// 
// 
//     public void create(Spanner spanner) {
//         restTemplate.postForObject("/", spanner, Spanner.class);
//     }
// 
// 
//     @PreAuthorize("hasPermission(#spanner, 'owner')")
//     public void update(Spanner spanner) {
//         restTemplate.put("/{0}", spanner, spanner.getId());
//     }
// }


import org.dontpanic.spanners.springmvc.domain.Spanner;
import org.dontpanic.spanners.springmvc.exception.SpannerNotFoundException;
import org.dontpanic.spanners.springmvc.services.SpannersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.time.LocalTime;
import java.util.Collection;
import java.util.function.Supplier;
package org.dontpanic.spanners.springmvc.controllers;



/**
 * Controller for page that displays all spanners
 * User: Stevie
 * Date: 05/10/13
 */
@Controller
public class DisplaySpannersController {

    static final String CONTROLLER_URL = "/displaySpanners";
    static final String VIEW_DISPLAY_SPANNERS = "displaySpanners";
    static final String MODEL_ATTRIBUTE_SPANNERS = "spanners";
    static final String MODEL_ATTRIBUTE_GREETING = "greeting";

    static final String GREETING_MORNING = "Good morning";
    static final String GREETING_AFTERNOON = "Good afternoon";
    static final String GREETING_EVENING = "Good Evening";

    private Supplier<LocalTime> currentTime = LocalTime::now;
    @Autowired private SpannersService spannersService;

    /**
     * Display all spanners
     */
    @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.GET)
    public String displaySpanners(ModelMap model) {

        // Load the spanners from database
        Collection<Spanner> spanners = spannersService.findAll();

        // Use greeting appropriate to time of day
        String greeting = timeOfDayGreeting();

        model.addAttribute(MODEL_ATTRIBUTE_SPANNERS, spanners);
        model.addAttribute(MODEL_ATTRIBUTE_GREETING, greeting);
        return VIEW_DISPLAY_SPANNERS;
    }


    /**
     * Delete a single spanner
     */
    @RequestMapping(value = "/deleteSpanner", method = RequestMethod.GET)
    public String deleteSpanner(@RequestParam Long id, ModelMap model) throws SpannerNotFoundException {

        // Fetch the spanner to be deleted
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/domain/Spanner.java
// public class Spanner {
// 
//     private Long id;
//     private String name;
//     private int size;
//     private String owner;
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public int getSize() {
//         return size;
//     }
// 
//     public void setSize(int size) {
//         this.size = size;
//     }
// 
//     public String getOwner() {
//         return owner;
//     }
// 
//     public void setOwner(String owner) {
//         this.owner = owner;
//     }
// }
// 
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/exception/SpannerNotFoundException.java
// public class SpannerNotFoundException extends Exception {
// 
//     private Long spannerId;
// 
//     /**
//      * New exception
//      * @param spannerId id of requested spanner
//      */
//     public SpannerNotFoundException(Long spannerId) {
//         super("Spanner not found: " + spannerId);
//         this.spannerId = spannerId;
//     }
// 
//     public Long getSpannerId() {
//         return spannerId;
//     }
// }
// 
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/services/SpannersService.java
// @Service
// public class SpannersService {
// 
//     private RestTemplate restTemplate;
// 
//     public SpannersService(RestTemplateBuilder builder,
//                            @Value("${app.service.url.spanners}") String rootUri) {
//         restTemplate = builder.messageConverters(halAwareMessageConverter())
//                               .rootUri(rootUri).build();
//     }
// 
// 
//     private HttpMessageConverter halAwareMessageConverter() {
//         ObjectMapper mapper = new ObjectMapper();
//         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//         mapper.registerModule(new Jackson2HalModule());
// 
//         return new MappingJackson2HttpMessageConverter(mapper);
//     }
// 
// 
//     public Collection<Spanner> findAll() {
//         ResponseEntity<PagedResources<Spanner>> response = restTemplate.exchange("/", HttpMethod.GET, null,
//                                                             new ParameterizedTypeReference<PagedResources<Spanner>>(){});
//         PagedResources<Spanner> pages = response.getBody();
//         return pages.getContent();
// 
//     }
// 
// 
//     public Spanner findOne(Long id) {
//         return restTemplate.getForObject("/{0}", Spanner.class, id);
//     }
// 
// 
//     @PreAuthorize("hasPermission(#spanner, 'owner')")
//     public void delete(Spanner spanner) {
//         restTemplate.delete("/{0}", spanner.getId());
//     }
// 
// 
//     public void create(Spanner spanner) {
//         restTemplate.postForObject("/", spanner, Spanner.class);
//     }
// 
// 
//     @PreAuthorize("hasPermission(#spanner, 'owner')")
//     public void update(Spanner spanner) {
//         restTemplate.put("/{0}", spanner, spanner.getId());
//     }
// }


// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/controllers/DisplaySpannersController.java
import org.dontpanic.spanners.springmvc.domain.Spanner;
import org.dontpanic.spanners.springmvc.exception.SpannerNotFoundException;
import org.dontpanic.spanners.springmvc.services.SpannersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.time.LocalTime;
import java.util.Collection;
import java.util.function.Supplier;

package org.dontpanic.spanners.springmvc.controllers;



/**
 * Controller for page that displays all spanners
 * User: Stevie
 * Date: 05/10/13
 */
@Controller
public class DisplaySpannersController {

    static final String CONTROLLER_URL = "/displaySpanners";
    static final String VIEW_DISPLAY_SPANNERS = "displaySpanners";
    static final String MODEL_ATTRIBUTE_SPANNERS = "spanners";
    static final String MODEL_ATTRIBUTE_GREETING = "greeting";

    static final String GREETING_MORNING = "Good morning";
    static final String GREETING_AFTERNOON = "Good afternoon";
    static final String GREETING_EVENING = "Good Evening";

    private Supplier<LocalTime> currentTime = LocalTime::now;
    @Autowired private SpannersService spannersService;

    /**
     * Display all spanners
     */
    @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.GET)
    public String displaySpanners(ModelMap model) {

        // Load the spanners from database
        Collection<Spanner> spanners = spannersService.findAll();

        // Use greeting appropriate to time of day
        String greeting = timeOfDayGreeting();

        model.addAttribute(MODEL_ATTRIBUTE_SPANNERS, spanners);
        model.addAttribute(MODEL_ATTRIBUTE_GREETING, greeting);
        return VIEW_DISPLAY_SPANNERS;
    }


    /**
     * Delete a single spanner
     */
    @RequestMapping(value = "/deleteSpanner", method = RequestMethod.GET)
    public String deleteSpanner(@RequestParam Long id, ModelMap model) throws SpannerNotFoundException {

        // Fetch the spanner to be deleted
        Spanner spanner = spannersService.findOne(id);