Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.24 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.gahee.weather.controller;
  7.  
  8. import com.gahee.weather.buissnes.ActualWeather;
  9. import com.gahee.weather.buissnes.City;
  10. import com.gahee.weather.buissnes.HourlyWeather;
  11. import com.gahee.weather.data.PicturePicker;
  12. import com.google.gson.Gson;
  13. import java.io.IOException;
  14. import java.io.PrintWriter;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.annotation.WebServlet;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import net.aksingh.owmjapis.CurrentWeather;
  24. import net.aksingh.owmjapis.HourlyForecast;
  25. import net.aksingh.owmjapis.HourlyForecast.Forecast;
  26. import net.aksingh.owmjapis.OpenWeatherMap;
  27. import org.jcp.xml.dsig.internal.dom.Utils;
  28. import org.json.JSONException;
  29.  
  30. /**
  31.  *
  32.  * @author mscib
  33.  */
  34. @WebServlet(urlPatterns = {"/weather"}, name="weather")
  35. public class WeatherServlet extends HttpServlet{
  36.     @Override
  37.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  38.             throws IOException, ServletException, JSONException{
  39.         PrintWriter pw = response.getWriter();
  40.         Gson gson = new Gson();
  41.         OpenWeatherMap openWeatherMap = new OpenWeatherMap(
  42.                 OpenWeatherMap.Units.METRIC,
  43.                 OpenWeatherMap.Language.POLISH,
  44.                 "9751d95f8393e1ba8fe312a569747b91");
  45.        
  46.         String cityName = request.getParameter("cityName");
  47.         City city = new City();
  48.         city.setName(cityName);
  49.        
  50.         ActualWeather actualWeather = getActualWeather(cityName, openWeatherMap);
  51.         ArrayList<HourlyWeather> hourlyWeatherList = new ArrayList<>();
  52.         hourlyWeatherList = getHourlyWeather(cityName, openWeatherMap);
  53.        
  54.         String jsonHourlyForecastString = gson.toJson(hourlyWeatherList);
  55.        
  56.         request.setAttribute("weather", actualWeather);
  57.         request.setAttribute("city", city);
  58.         request.setAttribute("hourlyWeatherJson", jsonHourlyForecastString);
  59.         //System.out.println(jsonHourlyForecastString);
  60.         getServletContext().getRequestDispatcher("/weather.jsp").forward(request, response);
  61.  
  62.        
  63.        
  64.     }
  65.    
  66.    
  67.     /*Provides information about actual weather in the city.*/
  68.     private ActualWeather getActualWeather(String city, OpenWeatherMap opm) throws IOException{
  69.         OpenWeatherMap openWeatherMap = opm;
  70.         ActualWeather actualWeather = null;
  71.  
  72.         CurrentWeather currentWeather = openWeatherMap.currentWeatherByCityName(city);
  73.        
  74.         if(currentWeather.isValid()){
  75.             actualWeather = new ActualWeather();
  76.             if(currentWeather.getMainInstance() != null){
  77.                 if(currentWeather.getMainInstance().hasTemperature()){
  78.                     actualWeather.setTemperature(String.valueOf(currentWeather.getMainInstance().getTemperature()) + " \u00b0" + "C");
  79.                    
  80.                 }else{
  81.                     actualWeather.setTemperature("Temperature unavaible");
  82.                 }
  83.                
  84.                 if(currentWeather.getMainInstance().hasPressure()){
  85.                     actualWeather.setPressure(String.valueOf(currentWeather.getMainInstance().getPressure()) + " hPa");
  86.                 }else{
  87.                     actualWeather.setPressure("Pressure unavabile");
  88.                 }
  89.                
  90.                 if(currentWeather.getMainInstance().hasHumidity()){
  91.                     actualWeather.setHumidity(String.valueOf(currentWeather.getMainInstance().getHumidity()) + "%");
  92.                 }else{
  93.                     actualWeather.setHumidity("Humidity unavaible");
  94.                 }
  95.                
  96.             }else{
  97.                
  98.                 actualWeather.setTemperature("Temperature unavaible");
  99.                 actualWeather.setPressure("Pressure unavabile");
  100.                 actualWeather.setHumidity("Humidity unavaible");
  101.             }
  102.            
  103.             if(currentWeather.getRainInstance() != null){
  104.                 if(currentWeather.getRainInstance().hasRain()){
  105.                     actualWeather.setRain(String.valueOf(currentWeather.getRainInstance().getRain()) + "mm");
  106.                 }else{
  107.                     actualWeather.setRain("No rain");
  108.                 }
  109.             }else{
  110.                 actualWeather.setRain("No rain.");
  111.             }
  112.            
  113.             if(currentWeather.getCloudsInstance() != null){
  114.                 if(currentWeather.getCloudsInstance().hasPercentageOfClouds()){
  115.                     actualWeather.setCloudsPercentage(String.valueOf(currentWeather.getCloudsInstance().getPercentageOfClouds()) + "%");
  116.                 }else{
  117.                     actualWeather.setCloudsPercentage("No clouds");
  118.                 }
  119.             }else{
  120.                 actualWeather.setCloudsPercentage("No clouds");
  121.             }
  122.            
  123.             if(currentWeather.hasWeatherInstance()){
  124.                 if(currentWeather.getWeatherInstance(0).hasWeatherCode()){
  125.  
  126.                     Date sunriseDate = currentWeather.getSysInstance().getSunriseTime();
  127.                     int weatherCode = currentWeather.getWeatherInstance(0).getWeatherCode();
  128.                    
  129.                     String iconURL = PicturePicker.getPictureURL(weatherCode, sunriseDate);
  130.                    
  131.                     if(!iconURL.equals("")){
  132.                         actualWeather.setWeatherIconURL(iconURL);
  133.                     }
  134.                 }else{
  135.                    
  136.                 }
  137.                
  138.                 if(currentWeather.getWeatherInstance(0).hasWeatherDescription()){
  139.                     actualWeather.setWeatherDescription(currentWeather.getWeatherInstance(0).getWeatherDescription());
  140.                 }else{
  141.                    
  142.                 }
  143.                
  144.             }
  145.             if(currentWeather.getWindInstance() != null){
  146.                 if(currentWeather.getWindInstance().hasWindSpeed()){
  147.                     actualWeather.setWindSpeed(String.valueOf(currentWeather.getWindInstance().getWindSpeed()) + " m/s");
  148.                 }
  149.             }
  150.         }else{
  151.            
  152.         }
  153.         return actualWeather;
  154.     }
  155.  
  156.   /* return list of object(HourlyWeather), which provides information about weather*/
  157.     private  ArrayList<HourlyWeather> getHourlyWeather(String cityName, OpenWeatherMap opm) throws IOException{
  158.         OpenWeatherMap openWeatherMap = opm;
  159.         HourlyForecast hourlyForecast = openWeatherMap.hourlyForecastByCityName(cityName);
  160.         ArrayList<HourlyWeather> hourlyWeathers = new ArrayList<>();
  161.        
  162.         int index = hourlyForecast.getForecastCount()-1;
  163.        
  164.         for(int i=0; i<index;i++){
  165.             HourlyWeather hourlyWeather = new HourlyWeather();
  166.             if(hourlyForecast.isValid()){
  167.                 if(hourlyForecast.getForecastInstance(i).hasMainInstance()){
  168.                     if(hourlyForecast.getForecastInstance(i).getMainInstance().hasTemperature()){
  169.                         String temperature = String.valueOf(hourlyForecast.getForecastInstance(i).getMainInstance().getTemperature());
  170.                         hourlyWeather.setTemperature(temperature);
  171.                     }
  172.                 }
  173.                 if(hourlyForecast.getForecastInstance(i).hasWeatherInstance()){
  174.                     if(hourlyForecast.getForecastInstance(i).getWeatherInstance(0).hasWeatherCode()){
  175.                         int weatherCode = hourlyForecast.getForecastInstance(i).getWeatherInstance(0).getWeatherCode();
  176.                         Date date = hourlyForecast.getForecastInstance(i).getDateTime();
  177.                         hourlyWeather.setWeatherIconURL(PicturePicker.getPictureURL(weatherCode, date));
  178.                     }
  179.                    
  180.                 }
  181.                 hourlyWeather.setForecastTime(hourlyForecast.getForecastInstance(i).getDateTime());
  182.             }
  183.             hourlyWeathers.add(hourlyWeather);
  184.         }
  185.        
  186.        
  187.         return hourlyWeathers;
  188.     }
  189.    
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement