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.business;
35  
36  import fr.paris.lutece.plugins.calendar.service.Utils;
37  
38  import java.util.ArrayList;
39  import java.util.Date;
40  import java.util.GregorianCalendar;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.Set;
46  
47  
48  /**
49   * A basic implementation of an agenda
50   */
51  public class SimpleAgenda implements Agenda
52  {
53      private static final long serialVersionUID = -798521682637709581L;
54      private String _strName;
55      private String _strKeyName;
56      private Map<String, List<Event>> _mapDays = new HashMap<String, List<Event>>( );
57      private List<Event> _list = new ArrayList<Event>( );
58  
59      /**
60       * Default constructor
61       */
62      public SimpleAgenda( )
63      {
64      }
65  
66      /**
67       * Returns the name of the Agenda
68       * @return The agenda's name
69       */
70      public String getName( )
71      {
72          return _strName;
73      }
74  
75      /**
76       * Defines the name of the Agenda
77       * @param strName The agenda's name
78       */
79      public void setName( String strName )
80      {
81          _strName = strName;
82      }
83  
84      /**
85       * Returns the KeyName
86       * 
87       * @return The KeyName
88       */
89      public String getKeyName( )
90      {
91          return _strKeyName;
92      }
93  
94      /**
95       * Sets the KeyName
96       * 
97       * @param strKeyName The KeyName
98       */
99      public void setKeyName( String strKeyName )
100     {
101         _strKeyName = strKeyName;
102     }
103 
104     /**
105      * Indicates if the agenda gets events for a given date
106      * @param strDate A date code
107      * @return True if there is events, otherwise false
108      */
109     public boolean hasEvents( String strDate )
110     {
111         return _mapDays.containsKey( strDate );
112     }
113 
114     /**
115      * Retrieves events for a given date
116      * @param strDate A date code
117      * @return A list of events
118      */
119     public List<Event> getEventsByDate( String strDate )
120     {
121         List<Event> listEvents = null;
122 
123         if ( hasEvents( strDate ) )
124         {
125             listEvents = _mapDays.get( strDate );
126         }
127 
128         return listEvents;
129     }
130 
131     /**
132      * Add an event to the agenda
133      * @param event The event to add
134      */
135     public void addEvent( Event event )
136     {
137         String strDate = Utils.getDate( event.getDate( ) );
138         List<Event> listEvents = null;
139 
140         if ( hasEvents( strDate ) )
141         {
142             listEvents = getEventsByDate( strDate );
143         }
144         else
145         {
146             listEvents = new ArrayList<Event>( );
147             _mapDays.put( strDate, listEvents );
148         }
149 
150         listEvents.add( event );
151     }
152 
153     /**
154      * Retrieves all events of the agenda
155      * @return A list of events
156      */
157     public List<Event> getEvents( )
158     {
159         _list.clear( );
160 
161         Set<String> setKey = _mapDays.keySet( );
162 
163         for ( String strDate : setKey )
164         {
165             List<Event> listEvents = getEventsByDate( strDate );
166 
167             for ( Event event : listEvents )
168             {
169                 _list.add( event );
170             }
171         }
172 
173         return _list;
174     }
175 
176     /**
177      * Fetches the events by date
178      * @param dateBegin The start date
179      * @param dateEnd The end date
180      * @param localeEnv the locale
181      * @return The list of events
182      */
183     public List<Event> getEventsByDate( Date dateBegin, Date dateEnd, Locale localeEnv )
184     {
185         _list.clear( );
186 
187         java.util.Calendar calendar = new GregorianCalendar( localeEnv );
188         calendar.setTime( dateBegin );
189 
190         java.util.Calendar calendar1 = new GregorianCalendar( localeEnv );
191         calendar1.setTime( dateEnd );
192 
193         while ( !Utils.getDate( calendar ).equals( Utils.getDate( calendar1 ) ) )
194         {
195             List<Event> listEvents = getEventsByDate( Utils.getDate( calendar ) );
196 
197             if ( listEvents != null )
198             {
199                 for ( Event event : listEvents )
200                 {
201                     _list.add( event );
202                 }
203             }
204 
205             calendar.add( java.util.Calendar.DATE, 1 );
206         }
207 
208         return _list;
209     }
210 }