/* (non-Javadoc)
   * @see org.jasig.portlet.weather.dao.IWeatherDao#getWeather(java.lang.String, org.jasig.portlet.weather.TemperatureUnit)
   */
  public Weather getWeather(String locationCode, TemperatureUnit unit) {
    final String worldwideweatherUrl =
        WEATHER_URL
            .replace("@KEY@", this.key)
            .replace("@LOCATION@", QuietUrlCodec.encode(locationCode, Constants.URL_ENCODING));

    Weather weather = (Weather) this.getAndDeserialize(worldwideweatherUrl, unit);
    String[] locationParts = locationCode.split(", ");
    Location location = new Location();
    location.setCity(locationParts[0]);
    if (locationParts.length > 1) {
      location.setStateOrCountry(locationParts[1]);
    }
    weather.setLocation(location);
    weather.setMoreInformationLink(
        "http://www.worldweatheronline.com/weather.aspx?q="
            + QuietUrlCodec.encode(locationCode, Constants.URL_ENCODING));

    return weather;
  }
  protected Weather deserializeWeatherResult(InputStream inputStream, TemperatureUnit unit)
      throws JAXBException, ParseException {
    JAXBContext jaxbContext = JAXBContext.newInstance(WeatherData.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    WeatherData data = (WeatherData) unmarshaller.unmarshal(inputStream);

    Weather weather = new Weather();

    Current current = new Current();
    current.setCondition(data.getCondition().getDescription());
    current.setHumidity(data.getCondition().getHumidity());
    current.setPressure(data.getCondition().getPressure());
    current.setWindDirection(data.getCondition().getWindDir());

    if (data.getCondition().getWeatherIconUrl() != null
        && data.getCondition().getWeatherIconUrl().contains("night")) {
      current.setImgName(
          imageMapping.getProperty("image.night." + data.getCondition().getWeatherCode()));
    } else {
      current.setImgName(
          imageMapping.getProperty("image.day." + data.getCondition().getWeatherCode()));
    }

    switch (unit) {
      case C:
        current.setTemperature(data.getCondition().getTempC());
        current.setWindSpeed(data.getCondition().getSpeedKmph());
        weather.setTemperatureUnit("C");
        weather.setWindUnit("kmph");
        weather.setPressureUnit("mb");
        break;
      default:
        current.setTemperature(data.getCondition().getTempF());
        current.setWindSpeed(data.getCondition().getSpeedMiles());
        weather.setTemperatureUnit("F");
        weather.setWindUnit("mi");
        weather.setPressureUnit("mb");
    }
    weather.setCurrentWeather(current);

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
    SimpleDateFormat dayFormat = new SimpleDateFormat(DAY_PATTERN);

    List<Forecast> forecasts = new ArrayList<Forecast>();
    for (WeatherForecast f : data.getForecasts()) {
      Forecast forecast = new Forecast();
      forecast.setCondition(f.getDescription());

      if (f.getIconUrl() != null && f.getIconUrl().contains("night")) {
        forecast.setImgName(imageMapping.getProperty("image.night." + f.getWeatherCode()));
      } else {
        forecast.setImgName(imageMapping.getProperty("image.day." + f.getWeatherCode()));
      }

      Date date = dateFormat.parse(f.getDate());
      forecast.setDay(dayFormat.format(date));

      switch (unit) {
        case C:
          forecast.setHighTemperature(f.getTempMaxC());
          forecast.setLowTemperature(f.getTempMinC());
          break;
        default:
          forecast.setHighTemperature(f.getTempMaxF());
          forecast.setLowTemperature(f.getTempMinF());
      }
      forecasts.add(forecast);
    }
    weather.setForecast(forecasts);

    return weather;
  }