View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.participatorybudget.web.campaign;
35  
36  import java.sql.Timestamp;
37  import java.util.Calendar;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignPhase;
44  import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignPhaseHome;
45  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  /**
54   * This class provides the user interface to manage CampaignPhase features ( manage, create, modify, remove )
55   */
56  @Controller( controllerJsp = "ManageCampaignPhases.jsp", controllerPath = "jsp/admin/plugins/participatorybudget/campaign/", right = "CAMPAIGN_MANAGEMENT" )
57  public class CampaignPhaseJspBean extends ManageCampaignJspBean
58  {
59  
60      // //////////////////////////////////////////////////////////////////////////
61      // Constants
62  
63      // templates
64      private static final String TEMPLATE_MANAGE_CAMPAIGNPHASES = "/admin/plugins/participatorybudget/campaign/manage_campaignphases.html";
65      private static final String TEMPLATE_CREATE_CAMPAIGNPHASE = "/admin/plugins/participatorybudget/campaign/create_campaignphase.html";
66      private static final String TEMPLATE_MODIFY_CAMPAIGNPHASE = "/admin/plugins/participatorybudget/campaign/modify_campaignphase.html";
67  
68      // Parameters
69      private static final String PARAMETER_ID_CAMPAIGNPHASE = "id";
70  
71      // Properties for page titles
72      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNPHASES = "participatorybudget.manage_campaignphases.pageTitle";
73      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNPHASE = "participatorybudget.modify_campaignphase.pageTitle";
74      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNPHASE = "participatorybudget.create_campaignphase.pageTitle";
75  
76      // Markers
77      private static final String MARK_CAMPAIGNPHASE_LIST = "campaignphase_list";
78      private static final String MARK_CAMPAIGNPHASE = "campaignphase";
79  
80      private static final String JSP_MANAGE_CAMPAIGNPHASES = "jsp/admin/plugins/participatorybudget/campaign/ManageCampaignPhases.jsp";
81  
82      // Properties
83      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAIGNPHASE = "participatorybudget.message.confirmRemoveCampaignPhase";
84      private static final String MESSAGE_CONFIRM_TARGET_CAMPAIGNPHASE = "participatorybudget.message.confirmTargetCampaignPhase";
85  
86      private static final String VALIDATION_ATTRIBUTES_PREFIX = "participatorybudget.model.entity.campaignphase.attribute.";
87  
88      // Views
89      private static final String VIEW_MANAGE_CAMPAIGNPHASES = "manageCampaignPhases";
90      private static final String VIEW_CREATE_CAMPAIGNPHASE = "createCampaignPhase";
91      private static final String VIEW_MODIFY_CAMPAIGNPHASE = "modifyCampaignPhase";
92  
93      // Actions
94      private static final String ACTION_CREATE_CAMPAIGNPHASE = "createCampaignPhase";
95      private static final String ACTION_MODIFY_CAMPAIGNPHASE = "modifyCampaignPhase";
96      private static final String ACTION_REMOVE_CAMPAIGNPHASE = "removeCampaignPhase";
97      private static final String ACTION_TARGET_CAMPAIGNPHASE = "targetCampaignPhase";
98      private static final String ACTION_CONFIRM_REMOVE_CAMPAIGNPHASE = "confirmRemoveCampaignPhase";
99      private static final String ACTION_CONFIRM_TARGET_CAMPAIGNPHASE = "confirmTargetCampaignPhase";
100 
101     // Infos
102     private static final String INFO_CAMPAIGNPHASE_CREATED = "participatorybudget.info.campaignphase.created";
103     private static final String INFO_CAMPAIGNPHASE_UPDATED = "participatorybudget.info.campaignphase.updated";
104     private static final String INFO_CAMPAIGNPHASE_REMOVED = "participatorybudget.info.campaignphase.removed";
105     private static final String INFO_CAMPAIGNPHASE_TARGETED = "participatorybudget.info.campaignphase.targeted";
106 
107     // Session variable to store working values
108     private CampaignPhase _campaignphase;
109 
110     /**
111      * Build the Manage View
112      * 
113      * @param request
114      *            The HTTP request
115      * @return The page
116      */
117     @View( value = VIEW_MANAGE_CAMPAIGNPHASES, defaultView = true )
118     public String getManageCampaignPhases( HttpServletRequest request )
119     {
120         _campaignphase = null;
121         List<CampaignPhase> listCampaignPhases = (List<CampaignPhase>) CampaignPhaseHome.getCampaignPhasesList( );
122         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAIGNPHASE_LIST, listCampaignPhases, JSP_MANAGE_CAMPAIGNPHASES );
123 
124         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNPHASES, TEMPLATE_MANAGE_CAMPAIGNPHASES, model );
125     }
126 
127     /**
128      * Returns the form to create a campaignphase
129      *
130      * @param request
131      *            The Http request
132      * @return the html code of the campaignphase form
133      */
134     @View( VIEW_CREATE_CAMPAIGNPHASE )
135     public String getCreateCampaignPhase( HttpServletRequest request )
136     {
137         _campaignphase = ( _campaignphase != null ) ? _campaignphase : new CampaignPhase( );
138 
139         Map<String, Object> model = getModel( );
140         model.put( MARK_CAMPAIGNPHASE, _campaignphase );
141 
142         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNPHASE, TEMPLATE_CREATE_CAMPAIGNPHASE, model );
143     }
144 
145     /**
146      * Process the data capture form of a new campaignphase
147      *
148      * @param request
149      *            The Http Request
150      * @return The Jsp URL of the process result
151      */
152     @Action( ACTION_CREATE_CAMPAIGNPHASE )
153     public String doCreateCampaignPhase( HttpServletRequest request )
154     {
155         populate( _campaignphase, request );
156 
157         // Check constraints
158         if ( !validateBean( _campaignphase, VALIDATION_ATTRIBUTES_PREFIX ) )
159         {
160             return redirectView( request, VIEW_CREATE_CAMPAIGNPHASE );
161         }
162 
163         CampaignPhaseHome.create( _campaignphase );
164         addInfo( INFO_CAMPAIGNPHASE_CREATED, getLocale( ) );
165 
166         CampaignService.getInstance( ).reset( );
167 
168         return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
169     }
170 
171     /**
172      * Manages the removal form of a campaignphase whose identifier is in the http request
173      *
174      * @param request
175      *            The Http request
176      * @return the html code to confirm
177      */
178     @Action( ACTION_CONFIRM_REMOVE_CAMPAIGNPHASE )
179     public String getConfirmRemoveCampaignPhase( HttpServletRequest request )
180     {
181         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
182         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAIGNPHASE ) );
183         url.addParameter( PARAMETER_ID_CAMPAIGNPHASE, nId );
184 
185         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAIGNPHASE, url.getUrl( ),
186                 AdminMessage.TYPE_CONFIRMATION );
187 
188         return redirect( request, strMessageUrl );
189     }
190 
191     /**
192      * Manages the target form of a campaignphase whose identifier is in the http request
193      *
194      * @param request
195      *            The Http request
196      * @return the html code to confirm
197      */
198     @Action( ACTION_CONFIRM_TARGET_CAMPAIGNPHASE )
199     public String getConfirmTargetCampaignPhase( HttpServletRequest request )
200     {
201         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
202         UrlItem url = new UrlItem( getActionUrl( ACTION_TARGET_CAMPAIGNPHASE ) );
203         url.addParameter( PARAMETER_ID_CAMPAIGNPHASE, nId );
204 
205         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_TARGET_CAMPAIGNPHASE, url.getUrl( ),
206                 AdminMessage.TYPE_CONFIRMATION );
207 
208         return redirect( request, strMessageUrl );
209     }
210 
211     /**
212      * Handles the removal form of a campaignphase
213      *
214      * @param request
215      *            The Http request
216      * @return the jsp URL to display the form to manage campaignphases
217      */
218     @Action( ACTION_REMOVE_CAMPAIGNPHASE )
219     public String doRemoveCampaignPhase( HttpServletRequest request )
220     {
221         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
222         CampaignPhaseHome.remove( nId );
223         addInfo( INFO_CAMPAIGNPHASE_REMOVED, getLocale( ) );
224 
225         CampaignService.getInstance( ).reset( );
226 
227         return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
228     }
229 
230     /**
231      * Handles the target form of a campaignphase
232      *
233      * @param request
234      *            The Http request
235      * @return the jsp URL to display the form to manage campaignphases
236      */
237     @Action( ACTION_TARGET_CAMPAIGNPHASE )
238     public String doTargetCampaignPhase( HttpServletRequest request )
239     {
240         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
241         CampaignPhase targetedPhase = CampaignPhaseHome.findByPrimaryKey( nId );
242 
243         List<CampaignPhase> phases = CampaignPhaseHome.getCampaignPhasesOrderedList( );
244 
245         // Search the position of the targeted hase in the list
246         int index = 0;
247         for ( index = 0; index < phases.size( ); index++ )
248         {
249             if ( targetedPhase.getId( ) == phases.get( index ).getId( ) )
250             {
251                 break;
252             }
253         }
254 
255         // Calculate the date of the very first phase
256         Calendar cal = Calendar.getInstance( );
257         cal.setTimeInMillis( System.currentTimeMillis( ) );
258         cal.add( Calendar.HOUR, -( 2 * 24 ) );
259         cal.add( Calendar.DAY_OF_YEAR, -( index * 7 ) );
260 
261         // Set each phase begin/end dates
262         for ( CampaignPhase phase : phases )
263         {
264             phase.setStart( new Timestamp( cal.getTime( ).getTime( ) ) );
265 
266             cal.add( Calendar.DAY_OF_YEAR, 7 );
267             cal.add( Calendar.SECOND, -1 );
268             phase.setEnd( new Timestamp( cal.getTime( ).getTime( ) ) );
269 
270             CampaignPhaseHome.update( phase );
271 
272             cal.add( Calendar.SECOND, 1 );
273         }
274 
275         addInfo( INFO_CAMPAIGNPHASE_TARGETED, getLocale( ) );
276 
277         CampaignService.getInstance( ).reset( );
278 
279         return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
280     }
281 
282     /**
283      * Returns the form to update info about a campaignphase
284      *
285      * @param request
286      *            The Http request
287      * @return The HTML form to update info
288      */
289     @View( VIEW_MODIFY_CAMPAIGNPHASE )
290     public String getModifyCampaignPhase( HttpServletRequest request )
291     {
292         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
293 
294         if ( _campaignphase == null || ( _campaignphase.getId( ) != nId ) )
295         {
296             _campaignphase = CampaignPhaseHome.findByPrimaryKey( nId );
297         }
298 
299         Map<String, Object> model = getModel( );
300         model.put( MARK_CAMPAIGNPHASE, _campaignphase );
301 
302         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNPHASE, TEMPLATE_MODIFY_CAMPAIGNPHASE, model );
303     }
304 
305     /**
306      * Process the change form of a campaignphase
307      *
308      * @param request
309      *            The Http request
310      * @return The Jsp URL of the process result
311      */
312     @Action( ACTION_MODIFY_CAMPAIGNPHASE )
313     public String doModifyCampaignPhase( HttpServletRequest request )
314     {
315         populate( _campaignphase, request );
316 
317         // Check constraints
318         if ( !validateBean( _campaignphase, VALIDATION_ATTRIBUTES_PREFIX ) )
319         {
320             return redirect( request, VIEW_MODIFY_CAMPAIGNPHASE, PARAMETER_ID_CAMPAIGNPHASE, _campaignphase.getId( ) );
321         }
322 
323         CampaignPhaseHome.update( _campaignphase );
324         addInfo( INFO_CAMPAIGNPHASE_UPDATED, getLocale( ) );
325 
326         CampaignService.getInstance( ).reset( );
327 
328         return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
329     }
330 }