1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.events.dao;
16
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.collections.CollectionUtils;
25 import org.kuali.mobility.events.entity.Category;
26 import org.kuali.mobility.events.entity.Event;
27 import org.kuali.mobility.events.entity.EventContact;
28 import org.kuali.mobility.events.entity.UMEvent;
29 import org.kuali.mobility.events.entity.UMEventReader;
30 import org.kuali.mobility.events.entity.UMSponsor;
31 import org.kuali.mobility.events.util.CategoryPredicate;
32
33 import com.thoughtworks.xstream.XStream;
34
35 public class EventsDaoUMImpl extends EventsDaoImpl {
36
37 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( EventsDaoUMImpl.class );
38
39
40 private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
41
42 @Override
43 public void initData( final String campus, final String categoryId ) {
44 this.addEvents(loadEventsForCategory (campus, categoryId));
45
46 }
47 public List<Event> loadEventsForCategory( final String campus, final String categoryId ) {
48
49 LOG.debug( "Loading event feed for category "+categoryId );
50 if( null == getEvents() || getEvents().isEmpty() )
51 {
52 LOG.debug( "Events list was empty, creating a new one." );
53
54 }
55 if( null == getCategories() || getCategories().isEmpty() )
56 {
57 LOG.debug( "Category list was empty, initializing a new one." );
58 initData( campus );
59 }
60
61 List<Event> newEvents = new ArrayList<Event>();
62
63 Category category = (Category) CollectionUtils.find ( getCategories(), new CategoryPredicate( campus, categoryId ) );;
64
65 if ( category != null ) {
66 LOG.debug( "Found category object for id "+categoryId );
67 XStream xstream = new XStream();
68 xstream.processAnnotations(UMEventReader.class);
69 xstream.processAnnotations(UMEvent.class);
70 xstream.processAnnotations(UMSponsor.class);
71 UMEventReader eventReader = null;
72 try {
73 URL url = new URL(category.getUrlString()+"&_type=xml");
74 LOG.debug("Mapping events from url: " + category.getUrlString());
75
76 if (url != null) {
77 eventReader = (UMEventReader) xstream.fromXML(url);
78 }
79 } catch (MalformedURLException mue) {
80 LOG.error( mue.getLocalizedMessage() );
81 }
82 LOG.debug("check eventReader " + (eventReader == null?"null":"mnot null"));
83 LOG.debug("check eventReader.getEvents " + (eventReader.getEvents() == null?"null":"mnot null"));
84
85 if ( eventReader != null && eventReader.getEvents() != null) {
86 for ( UMEvent e : eventReader.getEvents()){
87 LOG.debug("processing e " + e.getTitle());
88 Event newEvent = (Event) getApplicationContext().getBean("event");
89 newEvent.setEventId(e.getId());
90 newEvent.setCategory(category);
91 newEvent.setTitle(e.getTitle());
92 newEvent.setDisplayStartTime(e.getTimeBegin());
93
94 newEvent.setType(e.getType());
95 newEvent.setDisplayStartDate(e.getDateBegin());
96 newEvent.setLocation(e.getBuildingName());
97 newEvent.setLink(e.getUrl());
98 try {
99 if ( e.getTsBegin() != null && e.getTsBegin().isEmpty() == false) {
100 newEvent.setStartDate(sdf.parse(e.getTsBegin()));
101 }
102 if ( e.getTsEnd() != null && e.getTsEnd().isEmpty() == false) {
103 newEvent.setEndDate(sdf.parse(e.getTsEnd()));
104 }
105 } catch (ParseException e1) {
106 LOG.error(e1.getLocalizedMessage());
107 }
108 newEvent.setDisplayEndTime(e.getTimeEnd());
109 newEvent.setDisplayEndDate(e.getDateEnd());
110 List<String> myDescription = new ArrayList<String>();
111 myDescription.add(e.getDescription());
112 newEvent.setDescription( myDescription );
113 List<EventContact> myContacts = new ArrayList<EventContact>();
114 for ( UMSponsor f : e.getSponsors()){
115 EventContact newContact = (EventContact) getApplicationContext().getBean("eventContact");
116 newContact.setName(f.getGroupName());
117 newContact.setUrl(f.getWebsite());
118 myContacts.add(newContact);
119 }
120 newEvent.setContact(myContacts);
121 LOG.debug("CONTACT " + newEvent.getContact());
122 newEvents.add(newEvent);
123 }
124 }
125 }
126 return( newEvents );
127 }
128
129 }