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.events.dao;
17  
18  import java.io.IOException;
19  import java.io.InputStreamReader;
20  import java.io.UnsupportedEncodingException;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.net.URLEncoder;
24  import java.sql.Timestamp;
25  import java.util.ArrayList;
26  import java.util.HashSet;
27  import java.util.List;
28  
29  
30  import org.kuali.mobility.events.entity.Category;
31  import org.kuali.mobility.events.entity.Event;
32  import org.kuali.mobility.util.mapper.DataMapperImpl;
33  import org.springframework.context.ApplicationContext;
34  import org.springframework.context.ApplicationContextAware;
35  import org.springframework.stereotype.Repository;
36  
37  import com.sun.syndication.feed.synd.SyndEntryImpl;
38  import com.sun.syndication.feed.synd.SyndFeed;
39  import com.sun.syndication.io.FeedException;
40  import com.sun.syndication.io.SyndFeedInput;
41  
42  @Repository
43  public class EventsDaoImpl implements EventsDao, ApplicationContextAware {
44  
45      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( EventsDaoImpl.class );
46      
47      private ApplicationContext applicationContext;
48      
49      private DataMapperImpl mapper;
50      
51  	private String categorySourceFile;
52  	private String categorySourceUrl;
53  	private String categoryMappingFile;
54  	private String categoryMappingUrl;
55  
56      private List<Category> categories;
57      private List<Event> events;
58      
59      public void initData( final String campus, final String categoryId, final String eventId )
60      {
61          if( null == getEvents() || getEvents().isEmpty() )
62          {
63              this.initData( campus, categoryId );
64          }
65      }
66     
67      
68      @SuppressWarnings("unchecked")
69  	public void initData( final String campus, final String categoryId )
70      {
71          if( null == getEvents() || getEvents().isEmpty() )
72          {
73              setEvents( new ArrayList<Event>() );
74          }
75          
76          if( null == getCategories() || getCategories().isEmpty() )
77          { 
78              initData(campus);
79          }
80  
81          List<Event> newEvents = new ArrayList<Event>();
82  
83  		Category category = null;
84  		for( Category c : getCategories() )
85  		{
86  			if( c.getCategoryId() != null && c.getCategoryId().equalsIgnoreCase(categoryId) )
87  			{
88  				category = c;
89  				break;
90  			}
91  		}
92  		
93  		if ( category != null ) {
94  		
95  	        try
96  	        {
97  	            URL url = new URL( category.getUrlString() );
98  	            if( url != null )
99  	            {
100 	            	SyndFeedInput input = new SyndFeedInput();
101 	            	SyndFeed feed = null;
102 	            	feed = input.build( new InputStreamReader( url.openStream() ) );
103 	            	
104 	            	if( feed != null )
105 	            	{
106 	            		for( SyndEntryImpl entry : (List<SyndEntryImpl>)feed.getEntries() )
107 	            		{
108 	            			Event event = (Event)getApplicationContext().getBean("event");
109 	            			event.setTitle( entry.getTitle() );
110                                         
111 	            			if( entry.getDescription() != null ) {
112 	            				List<String> d = new ArrayList<String>();
113 	            				d.add( entry.getDescription().getValue() );
114 	            				event.setDescription( d );
115 	            			}
116 	            			event.setLink( entry.getLink() );
117 	        				try
118 	        				{
119 	        					event.setStartDate(new Timestamp(entry.getPublishedDate().getTime()));
120 	        				}
121 	        				catch( Exception e )
122 	        				{
123 	        					LOG.error( "Error creating timestamp for Event: "+entry.getTitle() );
124 	        					LOG.error( e.getLocalizedMessage() );
125 	        				}
126 	        				event.setEventId(category.getCategoryId());
127 	        				try {
128 	        					event.setEventId(URLEncoder.encode(entry.getUri(), "UTF-8"));
129 	        				} catch (UnsupportedEncodingException e) {
130 	        					event.setEventId(entry.getUri());
131 	        				}
132 	        				//event.setType("Dummy Type");
133 	        				newEvents.add( event );
134 	            		}
135 	            	}
136 	            }
137 	        }
138 	        catch( MalformedURLException mue )
139 	        {
140 	            LOG.error( mue.getLocalizedMessage() );
141 	        }
142 	        catch( FeedException fe )
143 	        {
144 	        	LOG.error( fe.getLocalizedMessage() );
145 	        }
146 	        catch( IOException ioe )
147 	        {
148 	            LOG.error( ioe.getLocalizedMessage() );
149 	        }
150 		}
151 		
152 		this.addEvents(newEvents);
153     }
154     
155     public void initData( final String campus )
156     {
157         if( null == getCategories() || getCategories().isEmpty() )
158         {
159             List<Category> cats = new ArrayList<Category>();
160 
161     		boolean isCategorySourceUrlAvailable = (getCategorySourceUrl() != null ? true : false);
162     		boolean isCategoryMappingUrlAvailable = (getCategoryMappingUrl() != null ? true : false);
163 
164             try
165             {
166             	if( isCategorySourceUrlAvailable ) {
167         			LOG.debug( "Loading categories from "+getCategorySourceUrl());
168             		if( isCategoryMappingUrlAvailable ) {
169             			cats = mapper.mapData( cats, 
170             					new URL( getCategorySourceUrl() ), 
171             					new URL( getCategoryMappingUrl() ) );
172             		} else {
173             			cats = mapper.mapData( cats, 
174             					new URL( getCategorySourceUrl() ), 
175             					getCategoryMappingFile() );
176             		}
177             	}
178             	else {
179         			LOG.debug( "Loading categories from "+getCategorySourceFile());
180             		if( isCategoryMappingUrlAvailable ) {
181             			// not supported in mapper.mapData
182             			LOG.error( "DataMapper does not support this case." );
183             			return;
184             		} else {
185             			cats = mapper.mapData( cats, 
186             					getCategorySourceFile(), 
187             					getCategoryMappingFile() );
188             			
189             		}
190             	}
191             }
192             catch( ClassNotFoundException cnfe )
193             {
194                 LOG.error( cnfe.getMessage() );
195             }
196             catch( MalformedURLException mue )
197             {
198                 LOG.error( mue.getMessage() );
199             }
200             catch( IOException ioe )
201             {
202                 LOG.error( ioe.getMessage() );
203             }
204             
205             LOG.debug( cats.size()+" categories mapped.");
206             if( cats.size() > 0 )
207             {
208                 setCategories( cats );
209             }
210         }
211     }
212     
213     /**
214      * @return the events
215      */
216     @Override
217     public List<Event> getEvents() {
218         return events;
219     }
220 
221     /**
222      * @param events the events to set
223      */
224     public void setEvents(List<Event> events) {
225         this.events = events;
226     }
227 
228     /**
229      * @return the categories
230      */
231     @Override
232     public List<Category> getCategories() {
233         return categories;
234     }
235 
236     /**
237      * @param categories the categories to set
238      */
239     public void setCategories(List<Category> categories) {
240         this.categories = categories;
241     }
242 
243     /**
244      * @return the mapper
245      */
246     public DataMapperImpl getMapper() {
247         return mapper;
248     }
249 
250     /**
251      * @param mapper the mapper to set
252      */
253     public void setMapper(DataMapperImpl mapper) {
254         this.mapper = mapper;
255     }
256 
257 	public String getCategorySourceFile() {
258 		return categorySourceFile;
259 	}
260 
261 	public void setCategorySourceFile(String categorySourceFile) {
262 		this.categorySourceFile = categorySourceFile;
263 	}
264 
265 	public String getCategorySourceUrl() {
266 		return categorySourceUrl;
267 	}
268 
269 	public void setCategorySourceUrl(String categorySourceUrl) {
270 		this.categorySourceUrl = categorySourceUrl;
271 	}
272 
273 	public String getCategoryMappingFile() {
274 		return categoryMappingFile;
275 	}
276 
277 	public void setCategoryMappingFile(String categoryMappingFile) {
278 		this.categoryMappingFile = categoryMappingFile;
279 	}
280 
281 	public String getCategoryMappingUrl() {
282 		return categoryMappingUrl;
283 	}
284 
285 	public void setCategoryMappingUrl(String categoryMappingUrl) {
286 		this.categoryMappingUrl = categoryMappingUrl;
287 	}
288 
289 
290 	public ApplicationContext getApplicationContext() {
291 		return applicationContext;
292 	}
293 
294 
295 	public void setApplicationContext(ApplicationContext applicationContext) {
296 		this.applicationContext = applicationContext;
297 	}
298 
299 
300 	@Override
301 	public void addEvents(List<Event> newEvents) {
302 		if (null == newEvents ) {
303 			return;
304 		}
305 				
306         List<Event> oldEvents = getEvents();
307         
308         if (null == oldEvents) {
309         	setEvents( newEvents );	        	
310         } else {
311     		HashSet tempSet = new HashSet();
312     		tempSet.addAll (newEvents);
313     		
314         	tempSet.addAll( oldEvents );
315         
316         	oldEvents.clear();
317         	oldEvents.addAll( tempSet );
318 
319         	setEvents( oldEvents );	
320         }
321 	}
322 
323 }