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