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.conference.service;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStreamReader;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.net.URLConnection;
24  import java.text.DateFormat;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.ArrayList;
28  import java.util.Date;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import net.sf.json.JSONArray;
33  import net.sf.json.JSONObject;
34  import net.sf.json.JSONSerializer;
35  
36  import org.kuali.mobility.conference.entity.Attendee;
37  import org.kuali.mobility.conference.entity.ContentBlock;
38  import org.kuali.mobility.conference.entity.Session;
39  
40  public class ConferenceServiceImpl implements ConferenceService {
41  
42  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ConferenceServiceImpl.class);
43  
44  	private String attendeesFeedUrl;
45  	private String featuredSpeakersFeedUrl;
46  	private String sessionsFeedUrl;
47  	private String welcomeFeedUrl;
48  	private String toEmailAddress;
49  	private String fromEmailAddress;
50  	
51  	@SuppressWarnings("unchecked")
52  	@Override
53  	public List<ContentBlock> findAllContentBlocks() {
54  		List<ContentBlock> contentBlocks = new ArrayList<ContentBlock>();
55  		try {
56  			String json = retrieveJSON(welcomeFeedUrl);
57  
58  			JSONArray simpleContentArray = (JSONArray) JSONSerializer.toJSON(json);
59  			
60  			for (Iterator<JSONObject> iter = simpleContentArray.iterator(); iter.hasNext();) {
61  				try {
62  					JSONObject contentBlockObject = iter.next();
63  					
64  					ContentBlock contentBlock = new ContentBlock();
65  					contentBlock.setContentBlock(contentBlockObject.getString("welcome"));
66  
67  					contentBlocks.add(contentBlock);
68  				} catch (Exception e) {
69  					LOG.error(e.getMessage(), e);
70  				}
71  			}
72  		} catch (Exception e) {
73  			LOG.error(e.getMessage(), e);
74  		}
75  
76  		return contentBlocks;
77  	}
78  	
79  	@SuppressWarnings("unchecked")
80  	@Override
81  	public List<ContentBlock> findFeaturedSpeakers() {
82  		List<ContentBlock> contentBlocks = new ArrayList<ContentBlock>();
83  		try {
84  			String json = retrieveJSON(featuredSpeakersFeedUrl);
85  
86  			JSONArray simpleContentArray = (JSONArray) JSONSerializer.toJSON(json);
87  			
88  			for (Iterator<JSONObject> iter = simpleContentArray.iterator(); iter.hasNext();) {
89  				try {
90  					JSONObject contentBlockObject = iter.next();
91  					
92  					ContentBlock contentBlock = new ContentBlock();
93  					contentBlock.setContentBlock(contentBlockObject.getString("welcome"));
94  
95  					contentBlocks.add(contentBlock);
96  				} catch (Exception e) {
97  					LOG.error(e.getMessage(), e);
98  				}
99  			}
100 		} catch (Exception e) {
101 			LOG.error(e.getMessage(), e);
102 		}
103 
104 		return contentBlocks;
105 	}
106 	
107 	@SuppressWarnings("unchecked")
108 	@Override
109 	public List<Attendee> findAllAttendees(char start, char end) {
110 		
111 		List<Attendee> attendees = new ArrayList<Attendee>();
112 		try {
113 			String json = retrieveJSON(attendeesFeedUrl);
114 
115 			JSONArray attendeeArray = (JSONArray) JSONSerializer.toJSON(json);
116 			
117 			for (Iterator<JSONObject> iter = attendeeArray.iterator(); iter.hasNext();) {
118 				try {
119 					JSONObject attendeeObject = iter.next();
120 					
121 					Attendee attendee = new Attendee();
122 					attendee.setId(attendeeObject.getString("id"));
123 					attendee.setEmail(attendeeObject.getString("email"));
124 					attendee.setFirstName(attendeeObject.getString("firstName"));
125 					attendee.setLastName(attendeeObject.getString("lastName"));
126 					attendee.setInstitution(attendeeObject.getString("institution"));
127 					attendee.setCampus(attendeeObject.getString("campus"));
128 					attendee.setTitle(attendeeObject.getString("title"));
129 					
130 					char c = attendee.getLastName().toUpperCase().charAt(0);
131 					if (c >= start && c <= end) {						
132 						attendees.add(attendee);
133 					}
134 				} catch (Exception e) {
135 					LOG.error(e.getMessage(), e);
136 				}
137 			}
138 		} catch (Exception e) {
139 			LOG.error(e.getMessage(), e);
140 		}
141 
142 		return attendees;
143 	}
144 
145 	@SuppressWarnings("unchecked")
146 	@Override
147 	public List<Session> findAllSessions(String date) {
148 	
149 		List<Session> sessions = new ArrayList<Session>();
150 		try {
151 			String dateString = (null == date ? "" : date);
152 			String json = retrieveJSON(sessionsFeedUrl + "?d=" + dateString);
153 
154 			JSONArray sessionArray = (JSONArray) JSONSerializer.toJSON(json);
155 			
156 			for (Iterator<JSONObject> iter = sessionArray.iterator(); iter.hasNext();) {
157 				try {
158 					JSONObject sessionObject = iter.next();
159 					
160 					Session session = new Session();
161 					session.setId(sessionObject.getString("id"));
162 					session.setTitle(sessionObject.getString("title"));
163 					session.setDescription(sessionObject.getString("description"));
164 					session.setLocation(sessionObject.getString("location"));
165 					session.setLatitude(sessionObject.getString("latitude"));
166 					session.setLongitude(sessionObject.getString("longitude"));
167 					session.setLink(sessionObject.getString("link"));
168 					
169 					try {
170 						String str_startDate = sessionObject.getString("startTime");
171 						String str_endDate = sessionObject.getString("endTime");
172 						DateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
173 						Date startDate = (Date)parser.parse(str_startDate);
174 						Date endDate = (Date)parser.parse(str_endDate);
175 						DateFormat formatter = new SimpleDateFormat("E hh:mm a");
176 						String formattedStartDate, formattedEndDate;
177 						formattedStartDate = formatter.format(startDate);
178 						formattedEndDate = formatter.format(endDate);
179 						session.setStartTime(formattedStartDate);
180 						session.setEndTime(formattedEndDate);
181 					} catch (ParseException e) {
182 					}
183 
184 					
185 					JSONArray tempSpeakers = sessionObject.getJSONArray("speakers");
186 		
187 					
188 				List<Attendee> speakers = new ArrayList<Attendee>();
189 					
190 					for (int i = 0; i < tempSpeakers.size(); i++) {
191 		            	JSONObject speakersObject = tempSpeakers.getJSONObject(i);
192 
193 		            	Attendee speaker = new Attendee();
194 		            	speaker.setFirstName(speakersObject.getString("firstName"));
195 		            	speaker.setLastName(speakersObject.getString("lastName"));
196 		            	speaker.setEmail(speakersObject.getString("email"));
197 		            	speaker.setTitle(speakersObject.getString("title"));
198 		            	speaker.setInstitution(speakersObject.getString("organization"));
199 		            	speakers.add(speaker);
200 		            }
201 					
202 					session.setSpeakers(speakers);
203 			
204 				sessions.add(session);
205 				} catch (Exception e) {
206 					LOG.error(e.getMessage(), e);
207 				}
208 			}
209 		} catch (Exception e) {
210 			LOG.error(e.getMessage(), e);
211 		}
212 		return sessions;
213 	}
214 
215 	
216 	private String retrieveJSON(String feedUrl) throws MalformedURLException, IOException {
217 	    URL url = new URL(feedUrl);
218 	    URLConnection conn = url.openConnection();
219 
220 	    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
221 	    StringBuffer sb = new StringBuffer();
222 	    String line;
223 	    while ((line = rd.readLine()) != null) {
224 	    	sb.append(line);
225 	    }
226 	    rd.close();
227 	    String json = sb.toString();
228 	    return json;
229     }
230 
231 	@Override
232     public Attendee findAttendeeById(String id) {
233 	    List<Attendee> attendees = findAllAttendees('A', 'Z');
234 	    for (Attendee attendee : attendees) {
235 	    	if (attendee.getId() != null && attendee.getId().equals(id)) {
236 	    		return attendee;
237 	    	}
238         }
239 	    return null;
240     }
241 
242 	@Override
243     public Session findSessionById(String id) {
244 		List<Session> sessions = findAllSessions("");
245 	    for (Session session : sessions) {
246 	    	if (session.getId() != null && session.getId().equals(id)) {
247 	    		return session;
248 	    	}
249         }
250 	    return null;
251     }
252 
253 	public String getAttendeesFeedUrl() {
254 		return attendeesFeedUrl;
255 	}
256 
257 	public void setAttendeesFeedUrl(String attendeesFeedUrl) {
258 		this.attendeesFeedUrl = attendeesFeedUrl;
259 	}
260 
261 	public String getFeaturedSpeakersFeedUrl() {
262 		return featuredSpeakersFeedUrl;
263 	}
264 
265 	public void setFeaturedSpeakersFeedUrl(String featuredSpeakersFeedUrl) {
266 		this.featuredSpeakersFeedUrl = featuredSpeakersFeedUrl;
267 	}
268 
269 	public String getSessionsFeedUrl() {
270 		return sessionsFeedUrl;
271 	}
272 
273 	public void setSessionsFeedUrl(String sessionsFeedUrl) {
274 		this.sessionsFeedUrl = sessionsFeedUrl;
275 	}
276 
277 	public String getWelcomeFeedUrl() {
278 		return welcomeFeedUrl;
279 	}
280 
281 	public void setWelcomeFeedUrl(String welcomeFeedUrl) {
282 		this.welcomeFeedUrl = welcomeFeedUrl;
283 	}
284 
285 	public String getToEmailAddress() {
286 		return toEmailAddress;
287 	}
288 
289 	public void setToEmailAddress(String toEmailAddress) {
290 		this.toEmailAddress = toEmailAddress;
291 	}
292 
293 	public String getFromEmailAddress() {
294 		return fromEmailAddress;
295 	}
296 
297 	public void setFromEmailAddress(String fromEmailAddress) {
298 		this.fromEmailAddress = fromEmailAddress;
299 	}
300 	
301 }