View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.calendar.modules.ical;
35  
36  import fr.paris.lutece.plugins.calendar.business.Agenda;
37  import fr.paris.lutece.plugins.calendar.business.Event;
38  import fr.paris.lutece.plugins.calendar.service.Utils;
39  import fr.paris.lutece.plugins.calendar.web.Constants;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  
43  import java.util.ArrayList;
44  import java.util.Date;
45  import java.util.GregorianCalendar;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Locale;
49  import java.util.Map;
50  import java.util.Set;
51  
52  import net.fortuna.ical4j.model.Calendar;
53  import net.fortuna.ical4j.model.Component;
54  import net.fortuna.ical4j.model.Property;
55  
56  
57  /**
58   * This class is an implementation of an Agenda using
59   * the iCalendar format (RFC 2445).
60   */
61  public class ICalAgenda implements Agenda
62  {
63      private static final long serialVersionUID = 331412235577651908L;
64      private String _strName;
65      private String _strKeyName;
66      private Map<String, List<Event>> _mapDays = new HashMap<String, List<Event>>( );
67      private List<Event> _list = new ArrayList<Event>( );
68  
69      /**
70       * Default constructor
71       */
72      public ICalAgenda( )
73      {
74  
75      }
76  
77      /**
78       * Returns the name of the Agenda
79       * @return The agenda's name
80       */
81      public String getName( )
82      {
83          return _strName;
84      }
85  
86      /**
87       * Defines the name of the Agenda
88       * @param strName The agenda's name
89       */
90      public void setName( String strName )
91      {
92          _strName = strName;
93      }
94  
95      /**
96       * Returns the KeyName
97       * 
98       * @return The KeyName
99       */
100     public String getKeyName( )
101     {
102         return _strKeyName;
103     }
104 
105     /**
106      * Sets the KeyName
107      * 
108      * @param strKeyName The KeyName
109      */
110     public void setKeyName( String strKeyName )
111     {
112         _strKeyName = strKeyName;
113     }
114 
115     /**
116      * Indicates if the agenda gets events for a given date
117      * @param strDate A date code
118      * @return True if there is events, otherwise false
119      */
120     public boolean hasEvents( String strDate )
121     {
122         return _mapDays.containsKey( strDate );
123     }
124 
125     /**
126      * Retrieves events for a given date
127      * @param strDate A date code
128      * @return A list of events
129      */
130     public List<Event> getEventsByDate( String strDate )
131     {
132         List<Event> listEvents = null;
133 
134         if ( hasEvents( strDate ) )
135         {
136             listEvents = _mapDays.get( strDate );
137         }
138 
139         return listEvents;
140     }
141 
142     /**
143      * Retrieves all events of the agenda
144      * @return A list of events
145      */
146     public List<Event> getEvents( )
147     {
148         _list.clear( );
149 
150         Set<String> listKey = _mapDays.keySet( );
151 
152         for ( String strDate : listKey )
153         {
154             List<Event> listEvents = getEventsByDate( strDate );
155 
156             for ( Event event : listEvents )
157             {
158                 _list.add( event );
159             }
160         }
161 
162         return _list;
163     }
164 
165     /**
166      * Sets events of the agenda
167      * @param calendar An iCal calendar
168      */
169     public void setEvents( Calendar calendar )
170     {
171         String strTraceEnable = AppPropertiesService.getProperty( Constants.PROPERTY_ICAL_TRACE_ENABLE );
172         boolean bTrace = ( ( strTraceEnable != null ) && ( strTraceEnable.equals( "true" ) ) ) ? true : false;
173         List<Component> listComponent = calendar.getComponents( );
174 
175         for ( Component component : listComponent )
176         {
177             if ( bTrace )
178             {
179                 AppLogService.info( "Component [" + component.getName( ) + "]" );
180             }
181 
182             if ( component.getName( ).equals( Component.VEVENT ) )
183             {
184                 ICalEvent event = new ICalEvent( );
185                 List<Property> listProperty = component.getProperties( );
186 
187                 for ( Property property : listProperty )
188                 {
189                     if ( property.getName( ).equals( Property.SUMMARY ) )
190                     {
191                         event.setTitle( property.getValue( ) );
192                     }
193                     else if ( property.getName( ).equals( Property.DTSTART ) )
194                     {
195                         event.setDateTimeStart( property.getValue( ) );
196                     }
197                     else if ( property.getName( ).equals( Property.DTEND ) )
198                     {
199                         event.setDateTimeEnd( property.getValue( ) );
200                     }
201                     else if ( property.getName( ).equals( Property.LOCATION ) )
202                     {
203                         event.setLocation( property.getValue( ) );
204                     }
205                     else if ( property.getName( ).equals( Property.DESCRIPTION ) )
206                     {
207                         event.setDescription( property.getValue( ) );
208                     }
209                     else if ( property.getName( ).equals( Property.CLASS ) )
210                     {
211                         event.setEventClass( property.getValue( ) );
212                     }
213                     else if ( property.getName( ).equals( Property.CATEGORIES ) )
214                     {
215                         event.setCategories( property.getValue( ) );
216                     }
217                     else if ( property.getName( ).equals( Property.STATUS ) )
218                     {
219                         event.setStatus( property.getValue( ) );
220                     }
221                     else if ( property.getName( ).equals( Property.PRIORITY ) )
222                     {
223                         event.setPriority( Integer.parseInt( property.getValue( ) ) );
224                     }
225                     else if ( property.getName( ).equals( Property.URL ) )
226                     {
227                         event.setUrl( property.getValue( ) );
228                     }
229 
230                     if ( bTrace )
231                     {
232                         AppLogService.info( "Property [" + property.getName( ) + ", " + property.getValue( ) + "]" );
233                     }
234                 }
235 
236                 addEvent( event );
237             }
238         }
239     }
240 
241     /**
242      * Add an event to the agenda
243      * @param event The event to add
244      */
245     public void addEvent( Event event )
246     {
247         String strDate = Utils.getDate( event.getDate( ) );
248         List<Event> listEvents = null;
249 
250         if ( hasEvents( strDate ) )
251         {
252             listEvents = getEventsByDate( strDate );
253         }
254         else
255         {
256             listEvents = new ArrayList<Event>( );
257             _mapDays.put( strDate, listEvents );
258         }
259 
260         listEvents.add( event );
261     }
262 
263     /**
264      * The events which occur between a start and end date
265      * @param dateBegin The start date
266      * @param dateEnd The end date
267      * @param localeEnv The locale
268      * @return The list of events
269      */
270     public List<Event> getEventsByDate( Date dateBegin, Date dateEnd, Locale localeEnv )
271     {
272         // TODO implementer locale   
273         _list.clear( );
274 
275         java.util.Calendar calendar = new GregorianCalendar( );
276         calendar.setTime( dateBegin );
277 
278         java.util.Calendar calendar1 = new GregorianCalendar( );
279         calendar1.setTime( dateEnd );
280 
281         while ( !Utils.getDate( calendar ).equals( Utils.getDate( calendar1 ) ) )
282         {
283             List<Event> listEvents = getEventsByDate( Utils.getDate( calendar ) );
284 
285             if ( listEvents != null )
286             {
287                 for ( Event event : listEvents )
288                 {
289                     _list.add( event );
290                 }
291             }
292 
293             calendar.add( java.util.Calendar.DATE, 1 );
294         }
295 
296         return _list;
297     }
298 }