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.AppointmentFilter;
39  import fr.paris.lutece.plugins.transparency.business.AppointmentHome;
40  import fr.paris.lutece.plugins.transparency.business.ElectedOfficialHome;
41  import fr.paris.lutece.plugins.transparency.business.LobbyHome;
42  import fr.paris.lutece.portal.service.security.UserNotSignedException;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.xpages.XPage;
46  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
48  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
49  import fr.paris.lutece.util.html.Paginator;
50  import fr.paris.lutece.util.string.StringUtil;
51  import java.util.List;
52  import java.util.Map;
53  import javax.servlet.http.HttpServletRequest;
54  
55  /**
56   * This class provides the user interface to manage Appointment xpages ( manage, create, modify, remove )
57   */
58  @Controller( xpageName = "publicmeeting", pageTitleI18nKey = "transparency.xpage.appointment.pageTitle", pagePathI18nKey = "transparency.xpage.appointment.pagePathLabel" )
59  public class AppointmentPublicXPage extends MVCApplication
60  {
61      // Templates
62      private static final String TEMPLATE_MANAGE_APPOINTMENTS = "/skin/plugins/transparency/manage_public_appointments.html";
63      private static final String TEMPLATE_DETAIL_APPOINTMENT = "/skin/plugins/transparency/detail_public_appointment.html";
64  
65      // Parameters
66      private static final String PARAMETER_ID_APPOINTMENT = "id";
67      private static final String PARAMETER_SEARCH_PERIOD = "search_period";
68      private static final String PARAMETER_SEARCH_ELECTED_OFFICIAL = "search_elected_official";
69      private static final String PARAMETER_SEARCH_LOBBY = "search_lobby";
70      private static final String PARAMETER_SEARCH_TITLE = "search_title";
71      private static final String PARAMETER_SORTED_ATTRIBUTE_NAME = "sorted_attribute_name";
72      private static final String PARAMETER_START_DATE = "start_date";
73      private static final String PARAMETER_ASC = "asc_sort";
74  
75      // Markers
76      private static final String MARK_APPOINTMENT_LIST = "appointment_list";
77      private static final String MARK_APPOINTMENT = "appointment";
78      private static final String MARK_BASE_URL = "base_url";
79      private static final String MARK_LOBBY_REFERENCE_START_URL = "lobbyReferenceStartUrl";
80      private static final String MARK_PAGINATOR = "paginator" ;
81      private static final String MARK_SEARCH_FILTER = "search_filter" ;
82  
83      // Properties
84      private static final String PROPERTY_LOBBY_REFERENCE_START_URL_KEY = "lobby.json.detail.startUrl";
85  
86      // Views
87      private static final String VIEW_MANAGE_APPOINTMENTS = "manageAppointments";
88      private static final String VIEW_DETAIL_APPOINTMENT = "detailAppointment";
89  
90      // Session variable to store working values
91      private Appointment _appointment;
92      private List<Integer> _appointmentIdsList;
93      private AppointmentFilter _filter = new AppointmentFilter( );
94  
95      private int _nItemsPerPage = 10;
96      private String _strCurrentPageIndex = "1";
97      
98      // Constants
99      private static int CONSTANT_DEFAULT_SEARCH_PERIOD = 92 ;
100     
101 
102 
103     /**
104      * Build the Manage View
105      *
106      * @param request
107      *            The HTTP request
108      * @return The Xpage
109      */
110     @View( value = VIEW_MANAGE_APPOINTMENTS, defaultView = true )
111     public XPage getManageAppointments( HttpServletRequest request )
112     {
113         _appointment = null;
114         List<Appointment> appointmentList = null;
115         Paginator<Integer> paginator ;
116         
117         // check type of request : paginating / sorting / new search
118         if ( request.getParameter( Paginator.PARAMETER_PAGE_INDEX ) != null && _appointmentIdsList != null ) 
119         {
120             // paginate list
121             _strCurrentPageIndex = request.getParameter( Paginator.PARAMETER_PAGE_INDEX );
122             paginator = new Paginator<Integer>( _appointmentIdsList, _nItemsPerPage, getViewFullUrl( VIEW_MANAGE_APPOINTMENTS ),
123                 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
124             
125             _filter.setListIds( paginator.getPageItems( ) );
126             
127             // get full appointments corresponding to the Ids
128             appointmentList = AppointmentHome.getFullAppointmentsList( _filter );
129         }
130         else if ( request.getParameter( PARAMETER_SORTED_ATTRIBUTE_NAME ) != null && _appointmentIdsList != null )
131         {
132             // sort list
133             if ( request.getParameter( PARAMETER_SORTED_ATTRIBUTE_NAME ).equals( PARAMETER_START_DATE ) )
134             {
135                 if ( request.getParameter( PARAMETER_ASC ) != null && request.getParameter( PARAMETER_ASC ).equals( "true" ) )
136                 {
137                     _filter.setOrderBy( PARAMETER_START_DATE + " ASC " );
138                 }
139                 else
140                 {
141                     _filter.setOrderBy( PARAMETER_START_DATE + " DESC " );
142                 }
143             }
144             
145             // reinitialize
146             _strCurrentPageIndex = "1" ;
147             _filter.setListIds( null ) ;
148             
149             // search all Ids whith the same filter
150             _appointmentIdsList = AppointmentHome.getAppointmentIdsList( _filter );
151             
152             paginator = new Paginator<Integer>( _appointmentIdsList, _nItemsPerPage, getViewFullUrl( VIEW_MANAGE_APPOINTMENTS ) ,
153                 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
154             
155             _filter.setListIds(  paginator.getPageItems( ) );
156             
157             // get full appointments corresponding to the Ids
158             appointmentList = AppointmentHome.getFullAppointmentsList( _filter );
159         }
160         else
161         {
162             // new search 
163             String strSearchPeriod = request.getParameter( PARAMETER_SEARCH_PERIOD );
164             String strSearchElectedOfficial = request.getParameter( PARAMETER_SEARCH_ELECTED_OFFICIAL );
165             String strSearchLobby = request.getParameter( PARAMETER_SEARCH_LOBBY );
166             String strSearchTitle = request.getParameter( PARAMETER_SEARCH_TITLE );
167 
168             
169             if ( strSearchPeriod != null ) _filter.setNumberOfDays( StringUtil.getIntValue( strSearchPeriod, CONSTANT_DEFAULT_SEARCH_PERIOD ) );
170             _filter.setLobbyName( strSearchLobby );
171             _filter.setElectedOfficialName( strSearchElectedOfficial );
172             _filter.setTitle( strSearchTitle );
173             
174             // reinitialize
175             _strCurrentPageIndex = "1" ;
176             _filter.setListIds( null ) ;
177             _filter.setOrderBy( null );
178             
179             // search all Ids 
180             _appointmentIdsList = AppointmentHome.getAppointmentIdsList( _filter );
181             
182             paginator = new Paginator<Integer>( _appointmentIdsList, _nItemsPerPage, getViewFullUrl( VIEW_MANAGE_APPOINTMENTS ) ,
183                 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
184             
185             _filter.setListIds( paginator.getPageItems( ) );
186             
187             // get full appointments corresponding to the Ids
188             appointmentList = AppointmentHome.getFullAppointmentsList( _filter );
189             
190         }
191 
192         Map<String, Object> model = getModel( );
193         model.put( MARK_APPOINTMENT_LIST, appointmentList );
194         model.put( MARK_PAGINATOR, paginator);
195         model.put( MARK_SEARCH_FILTER, _filter);
196         model.put( MARK_BASE_URL, AppPathService.getBaseUrl( request ) );
197         model.put( MARK_LOBBY_REFERENCE_START_URL, AppPropertiesService.getProperty( PROPERTY_LOBBY_REFERENCE_START_URL_KEY ) );
198 
199         return getXPage( TEMPLATE_MANAGE_APPOINTMENTS, request.getLocale( ), model );
200     }
201 
202     /**
203      * Returns the form to update info about a appointment
204      *
205      * @param request
206      *            The Http request
207      * @return The HTML form to update info
208      */
209     @View( VIEW_DETAIL_APPOINTMENT )
210     public XPage getDetailAppointment( HttpServletRequest request )
211     {
212         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPOINTMENT ) );
213 
214         if ( _appointment == null || ( _appointment.getId( ) != nId ) )
215         {
216             _appointment = AppointmentHome.findByPrimaryKey( nId );
217         }
218 
219         _appointment.setElectedOfficialList( ElectedOfficialHome.getElectedOfficialsListByAppointment( _appointment.getId( ) ) );
220         _appointment.setLobbyList( LobbyHome.getLobbiesListByAppointment( _appointment.getId( ) ) );
221 
222         Map<String, Object> model = getModel( );
223         model.put( MARK_APPOINTMENT, _appointment );
224         model.put( MARK_LOBBY_REFERENCE_START_URL, AppPropertiesService.getProperty( PROPERTY_LOBBY_REFERENCE_START_URL_KEY ) );
225 
226         return getXPage( TEMPLATE_DETAIL_APPOINTMENT, request.getLocale( ), model );
227     }
228 
229 }