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.portlet;
35  
36  import fr.paris.lutece.plugins.calendar.service.AgendaResource;
37  import fr.paris.lutece.plugins.calendar.service.CalendarService;
38  import fr.paris.lutece.plugins.calendar.web.Constants;
39  import fr.paris.lutece.portal.business.portlet.Portlet;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.util.sql.DAOUtil;
42  
43  import java.util.ArrayList;
44  import java.util.Date;
45  import java.util.List;
46  
47  
48  /**
49   * This class provides Data Access methods for CalendarPortlet objects
50   */
51  public final class CalendarPortletDAO implements ICalendarPortletDAO
52  {
53      // Constants
54      private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?";
55      private static final String SQL_QUERY_SELECT_AGENDAS_BY_PORTLET = "select code_agenda_name from calendar_portlet where id_portlet = ?";
56      private static final String SQL_QUERY_INSERT_AGENDA = "INSERT INTO calendar_portlet( id_portlet, code_agenda_name, date_begin, date_end ) VALUES ( ?,?,?,? )";
57      private static final String SQL_QUERY_INSERT_AGENDA_DAYS = "INSERT INTO calendar_portlet( id_portlet, code_agenda_name, number_days ,date_begin , date_end ) VALUES ( ?,?,?,?,? )";
58      private static final String SQL_QUERY_DELETE_AGENDA = " DELETE FROM calendar_portlet WHERE id_portlet = ? AND code_agenda_name = ?";
59      private static final String SQL_QUERY_DELETE = "DELETE FROM calendar_portlet WHERE id_portlet = ?";
60      private static final String SQL_QUERY_SELECT_AGENDAS_BY_PORTLET_BY_DATE = "SELECT date_begin,date_end FROM calendar_portlet WHERE id_portlet = ?";
61      private static final String SQL_QUERY_BEGIN_DATE_BY_PORTLET = "SELECT date_begin FROM calendar_portlet where id_portlet = ?";
62      private static final String SQL_QUERY_END_DATE_BY_PORTLET = "SELECT date_end FROM calendar_portlet where id_portlet = ?";
63      private static final String SQL_QUERY_NUMBER_DAYS_BY_PORTLET = "SELECT number_days FROM calendar_portlet WHERE id_portlet = ?";
64  
65      ///////////////////////////////////////////////////////////////////////////////////////
66      //Access methods to data
67  
68      /**
69       * Inserts a new record in the table. Not implemented.
70       * 
71       * @param portlet the object to be inserted
72       */
73      public void insert( Portlet portlet )
74      {
75          // Not implemented.
76      }
77  
78      /**
79       * Deletes a record from the table.
80       * @param nPortletId the portlet id
81       */
82      public void delete( int nPortletId )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
85          daoUtil.setInt( 1, nPortletId );
86          daoUtil.executeUpdate( );
87          daoUtil.free( );
88      }
89  
90      /**
91       * Loads the data of the portlet from the table.
92       * 
93       * @param nPortletId the portlet id
94       * @return the Portlet object
95       */
96      public Portlet load( int nPortletId )
97      {
98          CalendarPortlet portlet = new CalendarPortlet( );
99  
100         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
101 
102         daoUtil.setInt( 1, nPortletId );
103 
104         daoUtil.executeQuery( );
105 
106         if ( daoUtil.next( ) )
107         {
108             portlet.setId( nPortletId );
109         }
110 
111         daoUtil.free( );
112 
113         return portlet;
114     }
115 
116     /**
117      * Updates the record in the table. Not implemented.
118      * 
119      * @param portlet
120      *            the instance of Portlet class to be updated
121      */
122     public void store( Portlet portlet )
123     {
124         // Not implemented.
125     }
126 
127     /**
128      * Associates a new agenda to a given portlet.
129      * @param strAgendaId The identifier of an agenda
130      * @param dDateBegin The beginning date
131      * @param dDateEnd The end date
132      * @param nPortletId The identifier of the portlet.
133      */
134     public void insertAgendaInterval( int nPortletId, String strAgendaId, String dDateBegin, String dDateEnd )
135     {
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_AGENDA );
137         daoUtil.setInt( 1, nPortletId );
138         daoUtil.setString( 2, strAgendaId );
139         daoUtil.setString( 3, dDateBegin );
140         daoUtil.setString( 4, dDateEnd );
141 
142         daoUtil.executeUpdate( );
143         daoUtil.free( );
144     }
145 
146     /**
147      * De-associate a agenda from a given portlet.
148      * @param strAgendaId The identifier of an agenda
149      * @param nPortletId The identifier of the portlet.
150      */
151     public void removeAgenda( int nPortletId, String strAgendaId )
152     {
153         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_AGENDA );
154         daoUtil.setInt( 1, nPortletId );
155         daoUtil.setString( 2, strAgendaId );
156 
157         daoUtil.executeUpdate( );
158         daoUtil.free( );
159     }
160 
161     /**
162      * Returns all the sendings associated with a given portlet.
163      * @param nPortletId the identifier of the portlet.
164      * @return a list of unfiltered agenda resources
165      */
166     public List<AgendaResource> findAgendasInPortlet( int nPortletId )
167     {
168         CalendarService calendarService = SpringContextService.getBean( Constants.BEAN_CALENDAR_CALENDARSERVICE );
169         List<AgendaResource> listSelectedAgendas = new ArrayList<AgendaResource>( );
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_AGENDAS_BY_PORTLET );
171         daoUtil.setInt( 1, nPortletId );
172         daoUtil.executeQuery( );
173 
174         while ( daoUtil.next( ) )
175         {
176             AgendaResource agendaResource = calendarService.getAgendaResource( daoUtil.getString( 1 ) );
177             if ( agendaResource != null )
178             {
179                 listSelectedAgendas.add( agendaResource );
180             }
181         }
182 
183         daoUtil.free( );
184 
185         return listSelectedAgendas;
186     }
187 
188     /**
189      * Find the list of agenda having events between two dates
190      * @param nPortletId The id of the portlet
191      * @param dateBegin The start date of the events display
192      * @param dateEnd The end date of the event display
193      * @return The list of agenda
194      */
195     public List<AgendaResource> findAgendaBetween( int nPortletId, Date dateBegin, Date dateEnd )
196     {
197         CalendarService calendarService = SpringContextService.getBean( Constants.BEAN_CALENDAR_CALENDARSERVICE );
198         List<AgendaResource> listSelectedAgendas = new ArrayList<AgendaResource>( );
199         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_AGENDAS_BY_PORTLET_BY_DATE );
200         daoUtil.setInt( 1, nPortletId );
201         daoUtil.executeQuery( );
202 
203         while ( daoUtil.next( ) )
204         {
205             AgendaResource agendaResource = calendarService.getAgendaResource( daoUtil.getString( 1 ) );
206             if ( agendaResource != null )
207             {
208                 listSelectedAgendas.add( agendaResource );
209             }
210         }
211 
212         daoUtil.free( );
213 
214         return listSelectedAgendas;
215     }
216 
217     /**
218      * Returns the beginning date when the events will be displayed
219      * @param nPortletId The id of the portlet
220      * @return The start date
221      */
222     public Date getBeginDate( int nPortletId )
223     {
224         Date dateBegin = null;
225         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_BEGIN_DATE_BY_PORTLET );
226         daoUtil.setInt( 1, nPortletId );
227         daoUtil.executeQuery( );
228 
229         while ( daoUtil.next( ) )
230         {
231             try
232             {
233                 dateBegin = new Date( daoUtil.getDate( 1 ).getTime( ) );
234             }
235             catch ( NullPointerException e )
236             {
237             }
238         }
239 
240         daoUtil.free( );
241 
242         return dateBegin;
243     }
244 
245     /**
246      * Returns the end date of the events display
247      * @param nPortletId The id of the portlet
248      * @return The end date of event display
249      */
250     public Date getEndDate( int nPortletId )
251     {
252         Date dateEnd = null;
253         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_END_DATE_BY_PORTLET );
254         daoUtil.setInt( 1, nPortletId );
255         daoUtil.executeQuery( );
256 
257         while ( daoUtil.next( ) )
258         {
259             try
260             {
261                 dateEnd = new Date( daoUtil.getDate( 1 ).getTime( ) );
262             }
263             catch ( NullPointerException e )
264             {
265             }
266         }
267 
268         daoUtil.free( );
269 
270         return dateEnd;
271     }
272 
273     /**
274      * Fetches the number of future days during which the events of the agenda
275      * will be displayed
276      * @param nPortletId The id of the portlet
277      * @return The number of days
278      */
279     public int getRepetitionDays( int nPortletId )
280     {
281         int nNumberDays = 0;
282         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NUMBER_DAYS_BY_PORTLET );
283         daoUtil.setInt( 1, nPortletId );
284         daoUtil.executeQuery( );
285 
286         while ( daoUtil.next( ) )
287         {
288             nNumberDays = daoUtil.getInt( 1 );
289         }
290 
291         daoUtil.free( );
292 
293         return nNumberDays;
294     }
295 
296     /**
297      * Inserts a calendar which will be displayed into the portlet for the
298      * following n days
299      * @param nPortletId The id of the portlet
300      * @param strAgendaId The id of the agenda
301      * @param nDays The number of days the events will be displayed
302      */
303     public void insertCalendar( int nPortletId, String strAgendaId, int nDays )
304     {
305         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_AGENDA_DAYS );
306         daoUtil.setInt( 1, nPortletId );
307         daoUtil.setString( 2, strAgendaId );
308         daoUtil.setInt( 3, nDays );
309         daoUtil.setString( 4, Constants.EMPTY_NULL );
310         daoUtil.setString( 5, Constants.EMPTY_NULL );
311         daoUtil.executeUpdate( );
312         daoUtil.free( );
313     }
314 }