Package org.springframework.springfaces.traveladvisor.web.controller

Source Code of org.springframework.springfaces.traveladvisor.web.controller.HotelController

/*
* Copyright 2010-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.springfaces.traveladvisor.web.controller;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.springfaces.message.ObjectMessageSource;
import org.springframework.springfaces.message.ObjectMessageSourceUtils;
import org.springframework.springfaces.mvc.navigation.NavigationOutcome;
import org.springframework.springfaces.mvc.navigation.annotation.NavigationMapping;
import org.springframework.springfaces.traveladvisor.domain.City;
import org.springframework.springfaces.traveladvisor.domain.Hotel;
import org.springframework.springfaces.traveladvisor.domain.Rating;
import org.springframework.springfaces.traveladvisor.domain.Review;
import org.springframework.springfaces.traveladvisor.domain.ReviewDetails;
import org.springframework.springfaces.traveladvisor.service.CityService;
import org.springframework.springfaces.traveladvisor.service.HotelService;
import org.springframework.springfaces.traveladvisor.service.ReviewsSummary;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HotelController implements MessageSourceAware {

  private CityService cityService;

  private HotelService hotelService;

  private ObjectMessageSource messageSource;

  @RequestMapping("/advisor/{country}/{city}/{name}")
  public String hotel(@PathVariable String country, @PathVariable String city, @PathVariable String name, Model model) {
    Hotel hotel = getHotel(country, city, name);
    model.addAttribute(hotel);
    model.addAttribute("reviewsSummary", toChartModel(this.hotelService.getReviewSummary(hotel)));
    return "hotel";
  }

  private CartesianChartModel toChartModel(ReviewsSummary reviewSummary) {
    CartesianChartModel model = new CartesianChartModel();
    ChartSeries chartSeries = new ChartSeries(" ");
    for (Rating rating : Rating.values()) {
      String string = this.messageSource.getMessage(rating, null, null);
      chartSeries.set(string, reviewSummary.getNumberOfReviewsWithRating(rating));
    }
    model.addSeries(chartSeries);
    return model;
  }

  @RequestMapping("/advisor/{country}/{city}/{name}/review/{index}")
  public String hotelReview(@PathVariable String country, @PathVariable String city, @PathVariable String name,
      @PathVariable int index, Model model) {
    Review review = this.hotelService.getReview(getHotel(country, city, name), index);
    model.addAttribute(review);
    return "review";
  }

  @RequestMapping("/advisor/{country}/{city}/{name}/write-review")
  public String writeHotelReview(@PathVariable String country, @PathVariable String city, @PathVariable String name,
      Model model) {
    // FIXME is it wise that we can get here from a bookmark. Back button issues?
    ReviewDetails review = new ReviewDetails();
    model.addAttribute(getHotel(country, city, name)).addAttribute("review", review);
    return "writeReview";
  }

  @NavigationMapping
  public NavigationOutcome onSubmitReview(Hotel hotel, ReviewDetails details) {
    /* Review review = */this.hotelService.addReview(hotel, details);
    // FIXME put something in flash scope to be displayed on hotel screen
    ModelMap model = new ExtendedModelMap();
    model.addAttribute("country", hotel.getCity().getCountry());
    model.addAttribute("city", hotel.getCity().getName());
    model.addAttribute("hotel", hotel.getName());
    return new NavigationOutcome("@hotel", model);
  }

  private Hotel getHotel(String country, String cityName, String hotelName) {
    City city = this.cityService.getCity(cityName, country);
    Hotel hotel = this.hotelService.getHotel(city, hotelName);
    return hotel;
  }

  @Autowired
  public void setCityService(CityService cityService) {
    this.cityService = cityService;
  }

  @Autowired
  public void setHotelService(HotelService hotelService) {
    this.hotelService = hotelService;
  }

  public void setMessageSource(MessageSource messageSource) {
    this.messageSource = ObjectMessageSourceUtils.getObjectMessageSource(messageSource, null);
  }
}
TOP

Related Classes of org.springframework.springfaces.traveladvisor.web.controller.HotelController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.