View Javadoc

1   /**
2    * Copyright 2011-2013 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 javax.ws.rs.GET;
30  import javax.ws.rs.Path;
31  import javax.ws.rs.QueryParam;
32  
33  import org.jdom.Document;
34  import org.jdom.Element;
35  import org.jdom.JDOMException;
36  import org.jdom.input.SAXBuilder;
37  import org.kuali.mobility.dining.dao.DiningDao;
38  import org.kuali.mobility.dining.entity.FoodItem;
39  import org.kuali.mobility.dining.entity.Menu;
40  import org.kuali.mobility.dining.entity.Place;
41  import org.springframework.beans.factory.annotation.Autowired;
42  import org.springframework.beans.factory.annotation.Qualifier;
43  
44  public class DiningServiceImpl implements DiningService {
45  
46  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DiningServiceImpl.class);
47  
48  	private Map<String, List<String>> diningUrls;
49  
50  	@Autowired
51  	@Qualifier("diningDao")
52  	private DiningDao dao;
53  
54  	@GET
55  	@Path("/menu")
56  	@Override
57  	public List<Menu> getMenus(@QueryParam(value = "location"  )String location) {
58  		String url = getXmlUrl(location);
59  		if (url != null) {
60  			return parseUrl(url);
61  		}
62  		return null;
63  	}
64  
65  	private String getXmlUrl(String campusCode) {
66  		String url = null;
67  		List<String> urls = diningUrls.get(campusCode);
68  		if (urls != null && urls.size() > 0) {
69  			url = urls.get(0);
70  		}
71  		return url;
72  	}
73  
74  	@SuppressWarnings("unchecked")
75  	private List<Menu> parseUrl(String url){
76  		List<Menu> menus = new ArrayList<Menu>();
77  		try {
78  			// Set up formatters
79  			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
80  			// Process the XML
81  			Document doc = retrieveDocumentFromUrl(url, 5000, 5000);
82  			Element root = doc.getRootElement();
83  			List<Element> xmlMenus = root.getChildren("menu");
84  			for (Iterator<Element> iterator = xmlMenus.iterator(); iterator.hasNext();) {
85  				Element xmlMenu = iterator.next();
86  				// Make the date format configurable
87  				String dateStr = xmlMenu.getChildText("date");
88  				Menu menu = new Menu();
89  				try {
90  					Date date = sdf.parse(dateStr);
91  					menu.setDate(date);
92  					List<Element> items = xmlMenu.getChildren("item");
93  					for (Iterator<Element> itemItr = items.iterator(); itemItr.hasNext();) {
94  						try {
95  							Element xmlItem = itemItr.next();
96  							String name = xmlItem.getChildText("name");
97  							String priceStr = xmlItem.getChildText("price");
98  							double price = Double.parseDouble(priceStr);
99  							FoodItem item = new FoodItem(name, price);
100 							menu.getItems().add(item);
101 						} catch (Exception e) {}
102 					}
103 					menus.add(menu);
104 				} catch (ParseException e) {}
105 			}
106 		} catch (JDOMException e) {
107 			LOG.error(e.getMessage(), e);
108 		} catch (IOException e) {
109 			LOG.error(e.getMessage(), e);
110 		}
111 		return menus;
112 	}
113 
114 	private Document retrieveDocumentFromUrl(String urlStr, int connectTimeout, int readTimeout) throws IOException, JDOMException {
115 		SAXBuilder builder = new SAXBuilder();
116 		Document doc = null;
117 		URL urlObj = new URL(urlStr);
118 		URLConnection urlConnection = urlObj.openConnection();
119 		urlConnection.setConnectTimeout(connectTimeout);
120 		urlConnection.setReadTimeout(readTimeout);
121 		doc = builder.build(urlConnection.getInputStream());
122 		return doc;
123 	}
124 
125 	public Map<String, List<String>> getDiningUrls() {
126 		return diningUrls;
127 	}
128 
129 	public void setDiningUrls(Map<String, List<String>> diningUrls) {
130 		this.diningUrls = diningUrls;
131 	}
132 
133 //	@GET
134 //	@Path("/places")
135 //	@Override
136 	public List<Place> getPlaces() {
137 		return dao.getPlaceList();
138 	}
139 
140 	@Override
141 	public String getMenusJson(String name, String location) {
142 		return dao.getMenusJson(name, location);
143 	}
144 
145 }
146 
147