KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > weather > WeatherUtil


1 /*
2  * Jun 29, 2004, 11:33:30 AM
3  * @author: F. MORON
4  * @email: francois.moron@rd.francetelecom.com
5  * @version: WeatherConfigurator.java
6  *
7  * */

8
9 package org.exoplatform.portlets.weather;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
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 JavaDoc resources_;
33     private String JavaDoc contextPath_;
34     private HashMap JavaDoc weatherDataMap_;
35
36     public WeatherUtil() {
37         serviceAvailable_ = false;
38         try {
39             globalWeatherServiceLocator_ = new GlobalWeather_ServiceLocator();
40         }
41         catch (Exception JavaDoc ex) {
42             ex.printStackTrace();
43             serviceInitialized_ = false;
44         }
45         serviceInitialized_ = true;
46
47         if (serviceInitialized_) {
48             String JavaDoc proxySet = null;
49             ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext() ;
50             resources_ = Util.getApplicationResourceBundle() ;
51             try {
52                 proxySet = resources_.getString("proxySet");
53             } catch (Exception JavaDoc ex) {
54                 ex.printStackTrace() ;
55             }
56             if (proxySet != null) {
57                 if (proxySet.equals("true")) {
58                     try {
59                         String JavaDoc proxyHost = resources_.getString("proxyHost");
60                         String JavaDoc 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 JavaDoc 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 JavaDoc {
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.getPreferredStationCode() == null) weatherData.setPreferredStationCode("LFRN");
84
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 JavaDoc 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         // Si pas de station trouv�e pour le code et un nom de ville � rechercher
122
if (!weatherData.isStationFound() && !weatherData.getSearchStationName().equals("")) {
123             Station [] stationsList = stationInfo.searchByName(weatherData.getSearchStationName());
124             weatherData.setStationList(stationsList);
125             if (stationsList.length == 0) {
126                 // Pas de station trouvee pour ce nom de ville
127
weatherData.setStationFound(false);
128                 weatherData.setNbStations(0);
129             }
130             else if (stationsList.length == 1) {
131                 // Une station trouvee pour ce nom de ville
132
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                 // Plusieurs stations trouvees pour ce nom de ville
140
weatherData.setStationFound(true);
141                 weatherData.setNbStations(stationsList.length);
142             }
143         }
144     }
145
146     public String JavaDoc getContextPath() {
147         return contextPath_;
148     }
149
150     public GlobalWeather_ServiceLocator getGlobalWeatherServiceLocator() {
151         return globalWeatherServiceLocator_;
152     }
153
154     public ResourceBundle JavaDoc getResources() {
155         return resources_;
156     }
157
158     public WeatherReport getWeatherReport(String JavaDoc displayedStationCode) throws Exception JavaDoc{
159         if (displayedStationCode == null) return null;
160         WeatherReport weatherReport = null;
161         try {
162             weatherReport = globalWeatherServiceLocator_.getGlobalWeather().getWeatherReport(displayedStationCode);
163         }
164         catch (Exception JavaDoc 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