1 8 9 package org.exoplatform.portlets.weather; 10 11 import java.util.HashMap ; 12 import java.util.ResourceBundle ; 13 14 import javax.faces.context.ExternalContext; 15 import javax.faces.context.FacesContext; 16 import javax.portlet.PortletRequest; 17 import org.exoplatform.faces.core.Util; 18 import org.exoplatform.faces.core.component.UIPortlet; 19 20 import com.capeclear.www.GlobalWeather_wsdl.GlobalWeather_ServiceLocator; 21 import com.capeclear.www.GlobalWeather_wsdl.StationInfo; 22 import com.capeclear.www.GlobalWeather_xsd.Station; 23 import com.capeclear.www.GlobalWeather_xsd.WeatherReport; 24 25 26 public class WeatherUtil { 27 28 private static WeatherUtil singleton_; 29 private GlobalWeather_ServiceLocator globalWeatherServiceLocator_; 30 private boolean serviceInitialized_; 31 private boolean serviceAvailable_; 32 private ResourceBundle resources_; 33 private String contextPath_; 34 private HashMap weatherDataMap_; 35 36 public WeatherUtil() { 37 serviceAvailable_ = false; 38 try { 39 globalWeatherServiceLocator_ = new GlobalWeather_ServiceLocator(); 40 } 41 catch (Exception ex) { 42 ex.printStackTrace(); 43 serviceInitialized_ = false; 44 } 45 serviceInitialized_ = true; 46 47 if (serviceInitialized_) { 48 String proxySet = null; 49 ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext() ; 50 resources_ = Util.getApplicationResourceBundle() ; 51 try { 52 proxySet = resources_.getString("proxySet"); 53 } catch (Exception ex) { 54 ex.printStackTrace() ; 55 } 56 if (proxySet != null) { 57 if (proxySet.equals("true")) { 58 try { 59 String proxyHost = resources_.getString("proxyHost"); 60 String proxyPort = resources_.getString("proxyPort"); 61 System.setProperty("http.proxySet", "true"); 62 System.setProperty("http.proxyHost", proxyHost); 63 System.setProperty("http.proxyPort", proxyPort); 64 } catch (Exception ex) { 65 ex.printStackTrace() ; 66 } 67 } 68 } 69 PortletRequest request = (PortletRequest) eContext.getRequest(); 70 contextPath_ = request.getContextPath(); 71 } 72 } 73 74 public void checkRequest(WeatherData weatherData) throws Exception { 75 StationInfo stationInfo; 76 77 weatherData.setStationList(null); 78 weatherData.setStationFound(false); 79 weatherData.setNbStations(0); 80 81 if (weatherData.getSearchStationCode() == null) weatherData.setSearchStationCode(""); 82 if (weatherData.getSearchStationName() == null) weatherData.setSearchStationName(""); 83 if (weatherData.getDisplayedStationCode() == null) weatherData.setDisplayedStationCode(""); 85 86 if (weatherData.getDisplayedStationCode().equals("")) { 87 if (weatherData.getSearchStationCode().equals("")) { 88 if (weatherData.getSearchStationName().equals("")) 89 weatherData.setDisplayedStationCode(weatherData.getPreferredStationCode()); 90 else weatherData.setDisplayedStationCode(""); 91 } 92 else weatherData.setDisplayedStationCode(weatherData.getSearchStationCode()); 93 } 94 95 if (weatherData.getDisplayedStationCode().equals("") 96 && weatherData.getSearchStationName().equals("")) return; 97 98 try { 99 stationInfo = globalWeatherServiceLocator_.getStationInfo(); 100 } 101 catch (Exception ex) { 102 throw ex; 103 } 104 serviceAvailable_ = true; 105 weatherData.setStationFound(false); 106 weatherData.setNbStations(0); 107 if (!weatherData.getDisplayedStationCode().equals("")) { 108 weatherData.setStationList(stationInfo.searchByCode(weatherData.getDisplayedStationCode())); 109 if (weatherData.getStationsList().length == 0) { 110 } 111 else if (weatherData.getStationsList().length == 1) { 112 weatherData.setStationFound(true); 113 weatherData.setNbStations(1); 114 } 115 else if (weatherData.getStationsList().length > 1) { 116 weatherData.setStationFound(true); 117 weatherData.setNbStations(weatherData.getStationsList().length); 118 } 119 } 120 121 if (!weatherData.isStationFound() && !weatherData.getSearchStationName().equals("")) { 123 Station [] stationsList = stationInfo.searchByName(weatherData.getSearchStationName()); 124 weatherData.setStationList(stationsList); 125 if (stationsList.length == 0) { 126 weatherData.setStationFound(false); 128 weatherData.setNbStations(0); 129 } 130 else if (stationsList.length == 1) { 131 weatherData.setStationFound(true); 133 weatherData.setNbStations(1); 134 weatherData.setDisplayedStationCode(stationsList[0].getIata()); 135 if (weatherData.getDisplayedStationCode() == null) 136 weatherData.setDisplayedStationCode(stationsList[0].getIcao()); 137 } 138 else if (stationsList.length > 1) { 139 weatherData.setStationFound(true); 141 weatherData.setNbStations(stationsList.length); 142 } 143 } 144 } 145 146 public String getContextPath() { 147 return contextPath_; 148 } 149 150 public GlobalWeather_ServiceLocator getGlobalWeatherServiceLocator() { 151 return globalWeatherServiceLocator_; 152 } 153 154 public ResourceBundle getResources() { 155 return resources_; 156 } 157 158 public WeatherReport getWeatherReport(String displayedStationCode) throws Exception { 159 if (displayedStationCode == null) return null; 160 WeatherReport weatherReport = null; 161 try { 162 weatherReport = globalWeatherServiceLocator_.getGlobalWeather().getWeatherReport(displayedStationCode); 163 } 164 catch (Exception ex) { 165 throw ex; 166 } 167 return weatherReport; 168 } 169 170 public boolean isServiceAvailable() { 171 return serviceAvailable_; 172 } 173 174 public boolean isServiceInitialized() { 175 return serviceInitialized_; 176 } 177 178 public void setServiceAvailable(boolean serviceAvailable) { 179 this.serviceAvailable_ = serviceAvailable; 180 } 181 182 public static WeatherUtil getInstance(UIPortlet uiPortlet) { 183 if (singleton_ == null) { 184 synchronized (WeatherUtil.class) { 185 singleton_ = new WeatherUtil(); 186 } 187 } 188 return singleton_; 189 } 190 } 191 | Popular Tags |