View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.weather.dao;
17  
18  import java.io.IOException;
19  import java.net.URL;
20  import java.net.URLConnection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.jdom.Document;
26  import org.jdom.Element;
27  import org.jdom.JDOMException;
28  import org.jdom.input.SAXBuilder;
29  import org.kuali.mobility.weather.entity.Weather;
30  
31  public class WeatherDaoImpl implements WeatherDao {
32  
33  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WeatherDaoImpl.class);
34  
35  	private String url;
36  
37  	@SuppressWarnings("unchecked")
38  	public Weather parseWeather() {
39  		Weather weatherData = new Weather();
40  		try {
41  			Document doc = retrieveDocumentFromUrl(url, 5000, 5000);
42  			Element root = doc.getRootElement();
43  			List<Element> data = root.getChildren("data");
44  			for (Iterator<Element> iterator = data.iterator(); iterator.hasNext();) {
45  				Element dataItem = iterator.next();				
46  				if ("forecast".equalsIgnoreCase(dataItem.getAttribute("type").getValue().trim())) {
47  					List<Element> items = dataItem.getChildren("time-layout");
48  					for (Iterator<Element> itemItr = items.iterator(); itemItr.hasNext();) {
49  						Element xmlItem = itemItr.next();
50  						if (xmlItem.getChild("layout-key").getValue().trim().startsWith("k-p12h-")) {
51  							List<Element> items2 = xmlItem.getChildren("start-valid-time");
52  							int i = 0;
53  							for (Iterator<Element> itemItr2 = items2.iterator(); itemItr2.hasNext();) {
54  								Element xmlItem2 = itemItr2.next();
55  								HashMap<String, String> temp;
56  								try {
57  									temp = weatherData.getForecasts().get(i);
58  								} catch (Exception e) {
59  									temp = new HashMap<String, String>();
60  									weatherData.getForecasts().add(temp);
61  								}
62  								temp.put("name", xmlItem2.getAttributeValue("period-name"));
63  								i++;
64  								LOG.debug(xmlItem2.getAttributeValue("period-name"));
65  							}
66  						}
67  					}					
68  					List<Element> itemsIcons = dataItem.getChild("parameters").getChildren("conditions-icon");
69  					for (Iterator<Element> itemItr = itemsIcons.iterator(); itemItr.hasNext();) {
70  						Element xmlItem = itemItr.next();
71  						if (xmlItem.getAttributeValue("time-layout").trim().startsWith("k-p12h-")) {
72  							List<Element> items2 = xmlItem.getChildren("icon-link");
73  							int i = 0;
74  							for (Iterator<Element> itemItr2 = items2.iterator(); itemItr2.hasNext();) {
75  								Element xmlItem2 = itemItr2.next();
76  								HashMap<String, String> temp;
77  								try {
78  									temp = weatherData.getForecasts().get(i);
79  								} catch (Exception e) {
80  									temp = new HashMap<String, String>();
81  									weatherData.getForecasts().add(temp);
82  								}
83  								temp.put("iconLink", xmlItem2.getValue());
84  								i++;
85  								LOG.debug(xmlItem2.getValue());
86  							}
87  						}
88  					}					
89  					List<Element> itemsText = dataItem.getChild("parameters").getChildren("wordedForecast");
90  					for (Iterator<Element> itemItr = itemsText.iterator(); itemItr.hasNext();) {
91  						Element xmlItem = itemItr.next();
92  						if (xmlItem.getAttributeValue("time-layout").trim().startsWith("k-p12h-")) {
93  							List<Element> items2 = xmlItem.getChildren("text");
94  							int i = 0;
95  							for (Iterator<Element> itemItr2 = items2.iterator(); itemItr2.hasNext();) {
96  								Element xmlItem2 = itemItr2.next();
97  								HashMap<String, String> temp;
98  								try {
99  									temp = weatherData.getForecasts().get(i);
100 								} catch (Exception e) {
101 									temp = new HashMap<String, String>();
102 									weatherData.getForecasts().add(temp);
103 								}
104 								temp.put("text", xmlItem2.getValue());
105 								i++;
106 								LOG.debug(xmlItem2.getValue());
107 							}
108 						}
109 					}					
110 				} else if ("current observations".equalsIgnoreCase(dataItem.getAttribute("type").getValue().trim())) {
111 					List<Element> items = dataItem.getChildren("parameters");
112 					for (Iterator<Element> itemItr = items.iterator(); itemItr.hasNext();) {
113 						Element xmlItem = itemItr.next();
114 						if (null != xmlItem.getChild("weather").getChild("weather-conditions").getAttributeValue("weather-summary")) {
115 							weatherData.setCurrentCondition(xmlItem.getChild("weather").getChild("weather-conditions").getAttributeValue("weather-summary"));
116 							LOG.debug("Current:" + xmlItem.getChild("weather").getChild("weather-conditions").getAttributeValue("weather-summary"));
117 						}
118 						if ("apparent".equalsIgnoreCase(xmlItem.getChild("temperature").getAttributeValue("type").trim())) {
119 							weatherData.setTemperature(xmlItem.getChild("temperature").getChildText("value"));
120 							LOG.debug("Temp:" + xmlItem.getChild("temperature").getChildText("value"));
121 						}
122 						if ("relative".equalsIgnoreCase(xmlItem.getChild("humidity").getAttributeValue("type").trim())) {
123 							weatherData.setHumidity(xmlItem.getChild("humidity").getChildText("value"));
124 							LOG.debug("Humidity:" + xmlItem.getChild("humidity").getChildText("value"));
125 						}
126 						if ("barometer".equalsIgnoreCase(xmlItem.getChild("pressure").getAttributeValue("type").trim())) {
127 							weatherData.setPressure(xmlItem.getChild("pressure").getChildText("value"));
128 							LOG.debug("Pressure:" + xmlItem.getChild("pressure").getChildText("value"));
129 						}
130 						if ("wind".equalsIgnoreCase(xmlItem.getChild("direction").getAttributeValue("type").trim())) {
131 							weatherData.setWindDirection(xmlItem.getChild("direction").getChildText("value"));
132 							LOG.debug("Wind direction:" + xmlItem.getChild("direction").getChildText("value"));
133 						}						
134 						List<Element> items2 = xmlItem.getChildren("wind-speed");
135 						for (Iterator<Element> itemItr2 = items2.iterator(); itemItr2.hasNext();) {
136 							Element xmlItem2 = itemItr2.next();
137 							if ("sustained".equalsIgnoreCase(xmlItem2.getAttributeValue("type").trim())) {
138 								weatherData.setWindSpeed(xmlItem2.getChildText("value"));
139 								LOG.debug("Wind speed:" + xmlItem2.getChildText("value"));
140 							}			
141 						}
142 					}										
143 				}
144 			}
145 		} catch (Exception e) {
146 			weatherData = null;
147 			LOG.error(e.getMessage(), e);
148 		}
149 		return weatherData;
150 	}
151 	
152 	private Document retrieveDocumentFromUrl(String urlStr, int connectTimeout, int readTimeout) throws IOException, JDOMException {
153 		SAXBuilder builder = new SAXBuilder();
154 		Document doc = null;
155 		URL urlObj = new URL(urlStr);
156 		URLConnection urlConnection = urlObj.openConnection();
157 		urlConnection.setConnectTimeout(connectTimeout);
158 		urlConnection.setReadTimeout(readTimeout);
159 		doc = builder.build(urlConnection.getInputStream());
160 		return doc;
161 	}	
162 
163 	public String getUrl() {
164 		return url;
165 	}
166 
167 	public void setUrl(String url) {
168 		this.url = url;
169 	}
170 
171 }