View Javadoc

1   /**
2    * Copyright 2011 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.dining.service;
17  
18  import java.io.IOException;
19  import java.net.URL;
20  import java.net.URLConnection;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.ArrayList;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.jdom.Document;
30  import org.jdom.Element;
31  import org.jdom.JDOMException;
32  import org.jdom.input.SAXBuilder;
33  import org.kuali.mobility.dining.entity.FoodItem;
34  import org.kuali.mobility.dining.entity.Menu;
35  
36  public class DiningServiceImpl implements DiningService {
37  	
38  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DiningServiceImpl.class);
39  	
40  	private Map<String, List<String>> diningUrls;
41  	
42  	@Override
43  	public List<Menu> getMenus(String location) {
44  		String url = getXmlUrl(location);
45  		if (url != null) {
46  			return parseUrl(url);
47  		}
48  		return null;
49  	}
50  	
51  	private String getXmlUrl(String campusCode) {
52  		String url = null;
53  		List<String> urls = diningUrls.get(campusCode);
54  		if (urls != null && urls.size() > 0) {
55  			url = urls.get(0);
56  		}
57  		return url;
58  	}
59  	
60  	@SuppressWarnings("unchecked")
61  	private List<Menu> parseUrl(String url){
62  		List<Menu> menus = new ArrayList<Menu>();
63  		try {
64  			// Set up formatters
65  			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
66  			// Process the XML	
67  			Document doc = retrieveDocumentFromUrl(url, 5000, 5000);
68  			Element root = doc.getRootElement();
69  			List<Element> xmlMenus = root.getChildren("menu");
70  			for (Iterator<Element> iterator = xmlMenus.iterator(); iterator.hasNext();) {
71  				Element xmlMenu = iterator.next();
72  				// Make the date format configurable
73  				String dateStr = xmlMenu.getChildText("date");
74  				Menu menu = new Menu();
75  				try {
76  					Date date = sdf.parse(dateStr);
77  					menu.setDate(date);
78  					List<Element> items = xmlMenu.getChildren("item");
79  					for (Iterator<Element> itemItr = items.iterator(); itemItr.hasNext();) {
80  						try {
81  							Element xmlItem = itemItr.next();
82  							String name = xmlItem.getChildText("name");
83  							String priceStr = xmlItem.getChildText("price");
84  							double price = Double.parseDouble(priceStr);
85  							FoodItem item = new FoodItem(name, price);
86  							menu.getItems().add(item);							
87  						} catch (Exception e) {}
88  					}
89  					menus.add(menu);
90  				} catch (ParseException e) {}
91  			}
92  		} catch (JDOMException e) {
93  			LOG.error(e.getMessage(), e);
94  		} catch (IOException e) {
95  			LOG.error(e.getMessage(), e);
96  		}
97  		return menus;
98  	}
99  	
100 	private Document retrieveDocumentFromUrl(String urlStr, int connectTimeout, int readTimeout) throws IOException, JDOMException {
101 		SAXBuilder builder = new SAXBuilder();
102 		Document doc = null;
103 		URL urlObj = new URL(urlStr);
104 		URLConnection urlConnection = urlObj.openConnection();
105 		urlConnection.setConnectTimeout(connectTimeout);
106 		urlConnection.setReadTimeout(readTimeout);
107 		doc = builder.build(urlConnection.getInputStream());
108 		return doc;
109 	}
110 
111 	public Map<String, List<String>> getDiningUrls() {
112 		return diningUrls;
113 	}
114 
115 	public void setDiningUrls(Map<String, List<String>> diningUrls) {
116 		this.diningUrls = diningUrls;
117 	}
118 
119 }
120 
121