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.List;
42  import java.util.Locale;
43  
44  
45  /**
46   * This Class provides an agenda that aggregate other agendas.
47   */
48  public class MultiAgenda implements Agenda
49  {
50      private static final long serialVersionUID = 564170210812312845L;
51      private String _strName;
52      private String _strKeyName;
53      private List<Agenda> _listAgendas = new ArrayList<Agenda>( );
54  
55      /**
56       * Default constructor
57       */
58      public MultiAgenda( )
59      {
60      }
61  
62      /**
63       * Indicates if the agenda gets events for a given date
64       * @param strDate A date code
65       * @return True if there is events, otherwise false
66       */
67      public boolean hasEvents( String strDate )
68      {
69          for ( int i = 0; i < _listAgendas.size( ); i++ )
70          {
71              Agenda agenda = _listAgendas.get( i );
72  
73              if ( agenda != null && agenda.hasEvents( strDate ) )
74              {
75                  return true;
76              }
77          }
78  
79          return false;
80      }
81  
82      /**
83       * Retrieves events for a given date
84       * @param strDate A date code
85       * @return A list of events
86       */
87      public List<Event> getEventsByDate( String strDate )
88      {
89          List<Event> list = new ArrayList<Event>( );
90          List<Event> listEvents = getEvents( );
91  
92          for ( Event event : listEvents )
93          {
94              if ( Utils.getDate( event.getDate( ) ).equals( strDate ) )
95              {
96                  list.add( event );
97              }
98          }
99  
100         return list;
101     }
102 
103     /**
104      * Retrieves all events of the agenda
105      * @return A list of events
106      */
107     public List<Event> getEvents( )
108     {
109         List<Event> list = new ArrayList<Event>( );
110 
111         for ( int i = 0; i < _listAgendas.size( ); i++ )
112         {
113             Agenda agenda = _listAgendas.get( i );
114             List<Event> listEvents = agenda.getEvents( );
115 
116             for ( Event e : listEvents )
117             {
118                 MultiAgendaEvent event = new MultiAgendaEvent( e, agenda.getKeyName( ) );
119                 list.add( event );
120             }
121         }
122 
123         return list;
124     }
125 
126     /**
127      * Returns the name of the Agenda
128      * @return The agenda's name
129      */
130     public String getName( )
131     {
132         return _strName;
133     }
134 
135     /**
136      * Defines the name of the Agenda
137      * @param strName The agenda's name
138      */
139     public void setName( String strName )
140     {
141         _strName = strName;
142     }
143 
144     /**
145      * Add an agenda
146      * @param agenda The agenda
147      */
148     public void addAgenda( Agenda agenda )
149     {
150         _listAgendas.add( agenda );
151     }
152 
153     /**
154      * Returns the KeyName
155      * 
156      * @return The KeyName
157      */
158     public String getKeyName( )
159     {
160         return _strKeyName;
161     }
162 
163     /**
164      * Sets the KeyName
165      * 
166      * @param strKeyName The KeyName
167      */
168     public void setKeyName( String strKeyName )
169     {
170         _strKeyName = strKeyName;
171     }
172 
173     /**
174      * Gets agendas
175      * @return A list that contain all agendas
176      */
177     public List<Agenda> getAgendas( )
178     {
179         return _listAgendas;
180     }
181 
182     /**
183      * Fetches the events present between two dates
184      * @param dateBegin The start date
185      * @param dateEnd The end date
186      * @param localeEnv the locale
187      * @return The events
188      */
189     public List<Event> getEventsByDate( Date dateBegin, Date dateEnd, Locale localeEnv )
190     {
191         List<Event> listEvents = getEvents( );
192         List<Event> list = new ArrayList<Event>( );
193 
194         java.util.Calendar calendar = new GregorianCalendar( );
195         calendar.setTime( dateBegin );
196 
197         java.util.Calendar calendar1 = new GregorianCalendar( );
198         calendar1.setTime( dateEnd );
199 
200         while ( dateBegin.compareTo( dateEnd ) != 0 )
201         {
202             listEvents = getEventsByDate( Utils.getDate( calendar ) );
203 
204             if ( listEvents != null )
205             {
206                 for ( Event event : listEvents )
207                 {
208                     list.add( event );
209                 }
210             }
211 
212             calendar.add( java.util.Calendar.DATE, 1 );
213         }
214 
215         return list;
216     }
217 
218     /**
219      * Fetch agenda ids
220      * @return agenda ids
221      */
222     public String[] getAgendaIds( )
223     {
224         String[] arrayAgendaIds = new String[_listAgendas.size( )];
225         if ( !_listAgendas.isEmpty( ) || !( _listAgendas.size( ) == 1 && _listAgendas.get( 0 ) == null ) )
226         {
227             for ( int i = 0; i < _listAgendas.size( ); i++ )
228             {
229                 Agenda agenda = _listAgendas.get( i );
230                 if ( agenda != null )
231                 {
232                     arrayAgendaIds[i] = agenda.getKeyName( );
233                 }
234             }
235             return arrayAgendaIds;
236         }
237 
238         return null;
239     }
240 }