Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.gahee.weather.controller;
- import com.gahee.weather.buissnes.ActualWeather;
- import com.gahee.weather.buissnes.City;
- import com.gahee.weather.buissnes.HourlyWeather;
- import com.gahee.weather.data.PicturePicker;
- import com.google.gson.Gson;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import net.aksingh.owmjapis.CurrentWeather;
- import net.aksingh.owmjapis.HourlyForecast;
- import net.aksingh.owmjapis.HourlyForecast.Forecast;
- import net.aksingh.owmjapis.OpenWeatherMap;
- import org.jcp.xml.dsig.internal.dom.Utils;
- import org.json.JSONException;
- /**
- *
- * @author mscib
- */
- @WebServlet(urlPatterns = {"/weather"}, name="weather")
- public class WeatherServlet extends HttpServlet{
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException, JSONException{
- PrintWriter pw = response.getWriter();
- Gson gson = new Gson();
- OpenWeatherMap openWeatherMap = new OpenWeatherMap(
- OpenWeatherMap.Units.METRIC,
- OpenWeatherMap.Language.POLISH,
- "9751d95f8393e1ba8fe312a569747b91");
- String cityName = request.getParameter("cityName");
- City city = new City();
- city.setName(cityName);
- ActualWeather actualWeather = getActualWeather(cityName, openWeatherMap);
- ArrayList<HourlyWeather> hourlyWeatherList = new ArrayList<>();
- hourlyWeatherList = getHourlyWeather(cityName, openWeatherMap);
- String jsonHourlyForecastString = gson.toJson(hourlyWeatherList);
- request.setAttribute("weather", actualWeather);
- request.setAttribute("city", city);
- request.setAttribute("hourlyWeatherJson", jsonHourlyForecastString);
- //System.out.println(jsonHourlyForecastString);
- getServletContext().getRequestDispatcher("/weather.jsp").forward(request, response);
- }
- /*Provides information about actual weather in the city.*/
- private ActualWeather getActualWeather(String city, OpenWeatherMap opm) throws IOException{
- OpenWeatherMap openWeatherMap = opm;
- ActualWeather actualWeather = null;
- CurrentWeather currentWeather = openWeatherMap.currentWeatherByCityName(city);
- if(currentWeather.isValid()){
- actualWeather = new ActualWeather();
- if(currentWeather.getMainInstance() != null){
- if(currentWeather.getMainInstance().hasTemperature()){
- actualWeather.setTemperature(String.valueOf(currentWeather.getMainInstance().getTemperature()) + " \u00b0" + "C");
- }else{
- actualWeather.setTemperature("Temperature unavaible");
- }
- if(currentWeather.getMainInstance().hasPressure()){
- actualWeather.setPressure(String.valueOf(currentWeather.getMainInstance().getPressure()) + " hPa");
- }else{
- actualWeather.setPressure("Pressure unavabile");
- }
- if(currentWeather.getMainInstance().hasHumidity()){
- actualWeather.setHumidity(String.valueOf(currentWeather.getMainInstance().getHumidity()) + "%");
- }else{
- actualWeather.setHumidity("Humidity unavaible");
- }
- }else{
- actualWeather.setTemperature("Temperature unavaible");
- actualWeather.setPressure("Pressure unavabile");
- actualWeather.setHumidity("Humidity unavaible");
- }
- if(currentWeather.getRainInstance() != null){
- if(currentWeather.getRainInstance().hasRain()){
- actualWeather.setRain(String.valueOf(currentWeather.getRainInstance().getRain()) + "mm");
- }else{
- actualWeather.setRain("No rain");
- }
- }else{
- actualWeather.setRain("No rain.");
- }
- if(currentWeather.getCloudsInstance() != null){
- if(currentWeather.getCloudsInstance().hasPercentageOfClouds()){
- actualWeather.setCloudsPercentage(String.valueOf(currentWeather.getCloudsInstance().getPercentageOfClouds()) + "%");
- }else{
- actualWeather.setCloudsPercentage("No clouds");
- }
- }else{
- actualWeather.setCloudsPercentage("No clouds");
- }
- if(currentWeather.hasWeatherInstance()){
- if(currentWeather.getWeatherInstance(0).hasWeatherCode()){
- Date sunriseDate = currentWeather.getSysInstance().getSunriseTime();
- int weatherCode = currentWeather.getWeatherInstance(0).getWeatherCode();
- String iconURL = PicturePicker.getPictureURL(weatherCode, sunriseDate);
- if(!iconURL.equals("")){
- actualWeather.setWeatherIconURL(iconURL);
- }
- }else{
- }
- if(currentWeather.getWeatherInstance(0).hasWeatherDescription()){
- actualWeather.setWeatherDescription(currentWeather.getWeatherInstance(0).getWeatherDescription());
- }else{
- }
- }
- if(currentWeather.getWindInstance() != null){
- if(currentWeather.getWindInstance().hasWindSpeed()){
- actualWeather.setWindSpeed(String.valueOf(currentWeather.getWindInstance().getWindSpeed()) + " m/s");
- }
- }
- }else{
- }
- return actualWeather;
- }
- /* return list of object(HourlyWeather), which provides information about weather*/
- private ArrayList<HourlyWeather> getHourlyWeather(String cityName, OpenWeatherMap opm) throws IOException{
- OpenWeatherMap openWeatherMap = opm;
- HourlyForecast hourlyForecast = openWeatherMap.hourlyForecastByCityName(cityName);
- ArrayList<HourlyWeather> hourlyWeathers = new ArrayList<>();
- int index = hourlyForecast.getForecastCount()-1;
- for(int i=0; i<index;i++){
- HourlyWeather hourlyWeather = new HourlyWeather();
- if(hourlyForecast.isValid()){
- if(hourlyForecast.getForecastInstance(i).hasMainInstance()){
- if(hourlyForecast.getForecastInstance(i).getMainInstance().hasTemperature()){
- String temperature = String.valueOf(hourlyForecast.getForecastInstance(i).getMainInstance().getTemperature());
- hourlyWeather.setTemperature(temperature);
- }
- }
- if(hourlyForecast.getForecastInstance(i).hasWeatherInstance()){
- if(hourlyForecast.getForecastInstance(i).getWeatherInstance(0).hasWeatherCode()){
- int weatherCode = hourlyForecast.getForecastInstance(i).getWeatherInstance(0).getWeatherCode();
- Date date = hourlyForecast.getForecastInstance(i).getDateTime();
- hourlyWeather.setWeatherIconURL(PicturePicker.getPictureURL(weatherCode, date));
- }
- }
- hourlyWeather.setForecastTime(hourlyForecast.getForecastInstance(i).getDateTime());
- }
- hourlyWeathers.add(hourlyWeather);
- }
- return hourlyWeathers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement