View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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  
35  package fr.paris.lutece.plugins.transparency.web;
36  
37  import fr.paris.lutece.plugins.transparency.business.Appointment;
38  import fr.paris.lutece.plugins.transparency.business.AppointmentHome;
39  import fr.paris.lutece.plugins.transparency.business.ElectedOfficial;
40  import fr.paris.lutece.plugins.transparency.business.ElectedOfficialAppointment;
41  import fr.paris.lutece.plugins.transparency.business.ElectedOfficialAppointmentHome;
42  import fr.paris.lutece.plugins.transparency.business.ElectedOfficialHome;
43  import fr.paris.lutece.plugins.transparency.business.Lobby;
44  import fr.paris.lutece.plugins.transparency.business.LobbyAppointment;
45  import fr.paris.lutece.plugins.transparency.business.LobbyAppointmentHome;
46  import fr.paris.lutece.plugins.transparency.business.LobbyHome;
47  import fr.paris.lutece.plugins.transparency.service.ExportAppointmentService;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.util.AppPathService;
52  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
53  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
54  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
55  import fr.paris.lutece.util.ReferenceList;
56  import fr.paris.lutece.util.string.StringUtil;
57  import fr.paris.lutece.util.url.UrlItem;
58  import java.io.IOException;
59  import java.sql.Date;
60  
61  import java.util.List;
62  import java.util.Map;
63  import javax.servlet.http.HttpServletRequest;
64  import javax.servlet.http.HttpServletResponse;
65  import org.codehaus.plexus.util.StringUtils;
66  
67  /**
68   * This class provides the user interface to manage Appointment features ( manage, create, modify, remove )
69   */
70  @Controller( controllerJsp = "ManageAppointments.jsp", controllerPath = "jsp/admin/plugins/transparency/", right = "TRANSPARENCY_APPOINTMENTS_MANAGEMENT" )
71  public class AppointmentJspBean extends AbstractManageAppointementsJspBean
72  {
73      // Templates
74      private static final String TEMPLATE_MANAGE_APPOINTMENTS = "/admin/plugins/transparency/manage_appointments.html";
75      private static final String TEMPLATE_CREATE_APPOINTMENT = "/admin/plugins/transparency/create_appointment.html";
76      private static final String TEMPLATE_MODIFY_APPOINTMENT = "/admin/plugins/transparency/modify_appointment.html";
77  
78      // Parameters
79      private static final String PARAMETER_ID_APPOINTMENT = "id";
80      private static final String PARAMETER_ID_ELECTED_OFFICIAL = "id_elected_official";
81      private static final String PARAMETER_ID_LOBBY = "lobby_id";
82      private static final String PARAMETER_SELECT_LOBBY = "lobby_select";
83  
84      // Properties for page titles
85      private static final String PROPERTY_PAGE_TITLE_MANAGE_APPOINTMENTS = "transparency.manage_appointments.pageTitle";
86      private static final String PROPERTY_PAGE_TITLE_MODIFY_APPOINTMENT = "transparency.modify_appointment.pageTitle";
87      private static final String PROPERTY_PAGE_TITLE_CREATE_APPOINTMENT = "transparency.create_appointment.pageTitle";
88  
89      // Markers
90      private static final String MARK_APPOINTMENT_LIST = "appointment_list";
91      private static final String MARK_APPOINTMENT = "appointment";
92      private static final String MARK_ELECTEDOFFICIALS_LIST = "electedofficials_list";
93      private static final String MARK_BASE_URL = "base_url";
94  
95      // Jsp
96      private static final String JSP_MANAGE_APPOINTMENTS = "jsp/admin/plugins/transparency/ManageAppointments.jsp";
97  
98      // Properties
99      private static final String MESSAGE_CONFIRM_REMOVE_APPOINTMENT = "transparency.message.confirmRemoveAppointment";
100 
101     // Validations
102     private static final String VALIDATION_ATTRIBUTES_PREFIX = "transparency.model.entity.appointment.attribute.";
103 
104     // Views
105     private static final String VIEW_MANAGE_APPOINTMENTS = "manageAppointments";
106     private static final String VIEW_CREATE_APPOINTMENT = "createAppointment";
107     private static final String VIEW_MODIFY_APPOINTMENT = "modifyAppointment";
108 
109     // Actions
110     private static final String ACTION_CREATE_APPOINTMENT = "createAppointment";
111     private static final String ACTION_MODIFY_APPOINTMENT = "modifyAppointment";
112     private static final String ACTION_REMOVE_APPOINTMENT = "removeAppointment";
113     private static final String ACTION_EXPORT_APPOINTMENT = "exportAppointment";
114     private static final String ACTION_CONFIRM_REMOVE_APPOINTMENT = "confirmRemoveAppointment";
115     
116 
117     // Infos
118     private static final String INFO_APPOINTMENT_CREATED = "transparency.info.appointment.created";
119     private static final String INFO_APPOINTMENT_UPDATED = "transparency.info.appointment.updated";
120     private static final String INFO_APPOINTMENT_REMOVED = "transparency.info.appointment.removed";
121     private static final String INFO_APPOINTMENT_EXPORTED = "transparency.info.appointment.exported";
122 
123     // Session variable to store working values
124     private Appointment _appointment;
125 
126     /**
127      * Build the Manage View
128      * 
129      * @param request
130      *            The HTTP request
131      * @return The page
132      */
133     @View( value = VIEW_MANAGE_APPOINTMENTS, defaultView = true )
134     public String getManageAppointments( HttpServletRequest request )
135     {
136         _appointment = null;
137         AdminUser user = getUser( );
138         List<Appointment> listAppointments;
139 
140         listAppointments = AppointmentHome.getFullAppointmentsList( );
141 
142         Map<String, Object> model = getPaginatedListModel( request, MARK_APPOINTMENT_LIST, listAppointments, JSP_MANAGE_APPOINTMENTS );
143 
144         return getPage( PROPERTY_PAGE_TITLE_MANAGE_APPOINTMENTS, TEMPLATE_MANAGE_APPOINTMENTS, model );
145     }
146 
147     /**
148      * Returns the form to create a appointment
149      *
150      * @param request
151      *            The Http request
152      * @return the html code of the appointment form
153      */
154     @View( VIEW_CREATE_APPOINTMENT )
155     public String getCreateAppointment( HttpServletRequest request )
156     {
157         _appointment = ( _appointment != null ) ? _appointment : new Appointment( );
158 
159         ReferenceList electedOfficialsList = ElectedOfficialHome.getElectedOfficialsReferenceList( );
160 
161         Map<String, Object> model = getModel( );
162         model.put( MARK_APPOINTMENT, _appointment );
163         model.put( MARK_ELECTEDOFFICIALS_LIST, electedOfficialsList );
164         model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
165 
166         return getPage( PROPERTY_PAGE_TITLE_CREATE_APPOINTMENT, TEMPLATE_CREATE_APPOINTMENT, model );
167     }
168 
169     /**
170      * Process the data capture form of a new appointment
171      *
172      * @param request
173      *            The Http Request
174      * @return The Jsp URL of the process result
175      */
176     @Action( ACTION_CREATE_APPOINTMENT )
177     public String doCreateAppointment( HttpServletRequest request )
178     {
179 
180         populate( _appointment, request, request.getLocale( ) );
181 
182         // Check constraints
183         if ( !validateBean( _appointment, VALIDATION_ATTRIBUTES_PREFIX ) )
184         {
185             return redirectView( request, VIEW_CREATE_APPOINTMENT );
186         }
187 
188         // create appointment
189         AppointmentHome.create( _appointment );
190 
191         // add elected Official to the appointment
192         String strIdElectedOfficial = request.getParameter( PARAMETER_ID_ELECTED_OFFICIAL );
193 
194         ElectedOfficial electedOfficial = ElectedOfficialHome.findByPrimaryKey( strIdElectedOfficial );
195         if ( electedOfficial != null )
196         {
197             ElectedOfficialAppointmentHome.create( new ElectedOfficialAppointment( strIdElectedOfficial, _appointment.getId( ) ) );
198             _appointment.getElectedOfficialList( ).add( electedOfficial );
199         }
200 
201         // add Lobby to the appointment
202         String strIdLobby = request.getParameter( PARAMETER_ID_LOBBY );
203         String strSelectLobby = request.getParameter( PARAMETER_SELECT_LOBBY );
204 
205         int idLobby = StringUtil.getIntValue( strIdLobby, -1 );
206 
207         Lobby lobby = LobbyHome.findByPrimaryKey( idLobby );
208         if ( idLobby > 0 && lobby != null )
209         {
210             LobbyAppointmentHome.create( new LobbyAppointment( lobby.getId( ), _appointment.getId( ) ) );
211             _appointment.getLobbyList( ).add( lobby );
212         }
213         else
214             if ( !StringUtils.isBlank( strSelectLobby ) )
215             {
216                 Lobby newLobby = new Lobby( );
217                 newLobby.setName( strSelectLobby );
218                 newLobby.setVersionDate( new Date( ( new java.util.Date( ) ).getTime( ) ) );
219                 newLobby = LobbyHome.create( newLobby );
220 
221                 LobbyAppointmentHome.create( new LobbyAppointment( newLobby.getId( ), _appointment.getId( ) ) );
222                 _appointment.getLobbyList( ).add( newLobby );
223 
224             }
225 
226         addInfo( INFO_APPOINTMENT_CREATED, getLocale( ) );
227 
228         return redirectView( request, VIEW_MANAGE_APPOINTMENTS );
229     }
230 
231     /**
232      * Manages the removal form of a appointment whose identifier is in the http request
233      *
234      * @param request
235      *            The Http request
236      * @return the html code to confirm
237      */
238     @Action( ACTION_CONFIRM_REMOVE_APPOINTMENT )
239     public String getConfirmRemoveAppointment( HttpServletRequest request )
240     {
241         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPOINTMENT ) );
242         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_APPOINTMENT ) );
243         url.addParameter( PARAMETER_ID_APPOINTMENT, nId );
244 
245         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_APPOINTMENT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
246 
247         return redirect( request, strMessageUrl );
248     }
249 
250     /**
251      * Handles the removal form of a appointment
252      *
253      * @param request
254      *            The Http request
255      * @return the jsp URL to display the form to manage appointments
256      */
257     @Action( ACTION_REMOVE_APPOINTMENT )
258     public String doRemoveAppointment( HttpServletRequest request )
259     {
260         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPOINTMENT ) );
261         AppointmentHome.remove( nId );
262         addInfo( INFO_APPOINTMENT_REMOVED, getLocale( ) );
263 
264         return redirectView( request, VIEW_MANAGE_APPOINTMENTS );
265     }
266 
267     /**
268      * Returns the form to update info about a appointment
269      *
270      * @param request
271      *            The Http request
272      * @return The HTML form to update info
273      */
274     @View( VIEW_MODIFY_APPOINTMENT )
275     public String getModifyAppointment( HttpServletRequest request )
276     {
277         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPOINTMENT ) );
278 
279         if ( _appointment == null || ( _appointment.getId( ) != nId ) )
280         {
281             _appointment = AppointmentHome.getFullAppointmentById( nId );
282         }
283 
284         ReferenceList electedOfficialsList = ElectedOfficialHome.getElectedOfficialsReferenceList( );
285 
286         Map<String, Object> model = getModel( );
287         model.put( MARK_APPOINTMENT, _appointment );
288         model.put( MARK_ELECTEDOFFICIALS_LIST, electedOfficialsList );
289         model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
290 
291         return getPage( PROPERTY_PAGE_TITLE_MODIFY_APPOINTMENT, TEMPLATE_MODIFY_APPOINTMENT, model );
292     }
293 
294     /**
295      * Process the change form of a appointment
296      *
297      * @param request
298      *            The Http request
299      * @return The Jsp URL of the process result
300      */
301     @Action( ACTION_MODIFY_APPOINTMENT )
302     public String doModifyAppointment( HttpServletRequest request )
303     {
304 
305         populate( _appointment, request, request.getLocale( ) );
306 
307         // Check constraints
308         if ( !validateBean( _appointment, VALIDATION_ATTRIBUTES_PREFIX ) )
309         {
310             return redirect( request, VIEW_MODIFY_APPOINTMENT, PARAMETER_ID_APPOINTMENT, _appointment.getId( ) );
311         }
312 
313         AppointmentHome.update( _appointment );
314 
315         // change Lobby to the appointment
316         String strIdLobby = request.getParameter( PARAMETER_ID_LOBBY );
317         String strSelectLobby = request.getParameter( PARAMETER_SELECT_LOBBY );
318 
319         int idLobby = StringUtil.getIntValue( strIdLobby, -1 );
320 
321         Lobby lobby = LobbyHome.findByPrimaryKey( idLobby );
322 
323         // check if it's not a new lobby to create
324         if ( idLobby > 0 && lobby != null && lobby.getName( ).equals( strSelectLobby ) )
325         {
326             LobbyAppointmentHome.removeByAppointmentId( _appointment.getId( ) );
327             LobbyAppointmentHome.create( new LobbyAppointment( lobby.getId( ), _appointment.getId( ) ) );
328             _appointment.getLobbyList( ).add( lobby );
329         }
330         else
331             if ( !StringUtils.isBlank( strSelectLobby ) )
332             {
333                 Lobby newLobby = new Lobby( );
334                 newLobby.setName( strSelectLobby );
335                 newLobby.setVersionDate( new Date( ( new java.util.Date( ) ).getTime( ) ) );
336                 newLobby = LobbyHome.create( newLobby );
337 
338                 LobbyAppointmentHome.removeByAppointmentId( _appointment.getId( ) );
339                 LobbyAppointmentHome.create( new LobbyAppointment( newLobby.getId( ), _appointment.getId( ) ) );
340                 _appointment.getLobbyList( ).add( newLobby );
341 
342             }
343 
344         addInfo( INFO_APPOINTMENT_UPDATED, getLocale( ) );
345 
346         return redirectView( request, VIEW_MANAGE_APPOINTMENTS );
347     }
348     
349     /**
350      * Returns the form to create a appointment
351      *
352      * @param request
353      *            The Http request
354      * @param response
355      */
356     @Action( ACTION_EXPORT_APPOINTMENT )
357     public void doExportAppointment( HttpServletRequest request, HttpServletResponse response  ) 
358     {
359        
360         List<Appointment> listAppointments = AppointmentHome.getFullAppointmentsList( );
361         try {
362             ExportAppointmentService.exportAppointmentToCSV(request, response,  listAppointments);
363         } catch (IOException ex) {
364             addInfo( ex.getLocalizedMessage( ), getLocale( ) );
365            
366         }
367         
368         //addInfo( INFO_APPOINTMENT_EXPORTED, getLocale( ) );
369         //return redirectView( request, VIEW_MANAGE_APPOINTMENTS );
370         
371     }
372     
373      
374 }