View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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  
35  package fr.paris.lutece.plugins.campaign.web;
36  
37  import fr.paris.lutece.portal.service.message.AdminMessage;
38  import fr.paris.lutece.portal.service.message.AdminMessageService;
39  import fr.paris.lutece.portal.service.security.SecurityTokenService;
40  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.util.url.UrlItem;
45  
46  import java.util.List;
47  import java.util.Map;
48  import javax.servlet.http.HttpServletRequest;
49  import fr.paris.lutece.plugins.campaign.business.Campaign;
50  import fr.paris.lutece.plugins.campaign.business.CampaignHome;
51  
52  /**
53   * This class provides the user interface to manage Campaign features ( manage, create, modify, remove )
54   */
55  @Controller( controllerJsp = "ManageCampaigns.jsp", controllerPath = "jsp/admin/plugins/campaign/", right = "CAMPAIGN_MANAGEMENT" )
56  public class CampaignJspBean extends AbstractManageCampaignsJspBean
57  {
58      /**
59       * serialVersionUID
60       */
61      private static final long serialVersionUID = 2485209123338980294L;
62      // Templates
63      private static final String TEMPLATE_MANAGE_CAMPAIGNS = "/admin/plugins/campaign/manage_campaigns.html";
64      private static final String TEMPLATE_CREATE_CAMPAIGN = "/admin/plugins/campaign/create_campaign.html";
65      private static final String TEMPLATE_MODIFY_CAMPAIGN = "/admin/plugins/campaign/modify_campaign.html";
66  
67      // Parameters
68      private static final String PARAMETER_ID_CAMPAIGN = "id";
69  
70      // Properties for page titles
71      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNS = "campaign.manage_campaigns.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGN = "campaign.modify_campaign.pageTitle";
73      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAIGN = "campaign.create_campaign.pageTitle";
74  
75      // Markers
76      private static final String MARK_CAMPAIGN_LIST = "campaign_list";
77      private static final String MARK_CAMPAIGN = "campaign";
78  
79      private static final String JSP_MANAGE_CAMPAIGNS = "jsp/admin/plugins/campaign/ManageCampaigns.jsp";
80  
81      // Properties
82      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAIGN = "campaign.message.confirmRemoveCampaign";
83  
84      // Validations
85      private static final String VALIDATION_ATTRIBUTES_PREFIX = "campaign.model.entity.campaign.attribute.";
86  
87      // Views
88      private static final String VIEW_MANAGE_CAMPAIGNS = "manageCampaigns";
89      private static final String VIEW_CREATE_CAMPAIGN = "createCampaign";
90      private static final String VIEW_MODIFY_CAMPAIGN = "modifyCampaign";
91  
92      // Actions
93      private static final String ACTION_CREATE_CAMPAIGN = "createCampaign";
94      private static final String ACTION_MODIFY_CAMPAIGN = "modifyCampaign";
95      private static final String ACTION_REMOVE_CAMPAIGN = "removeCampaign";
96      private static final String ACTION_CONFIRM_REMOVE_CAMPAIGN = "confirmRemoveCampaign";
97  
98      // Infos
99      private static final String INFO_CAMPAIGN_CREATED = "campaign.info.campaign.created";
100     private static final String INFO_CAMPAIGN_UPDATED = "campaign.info.campaign.updated";
101     private static final String INFO_CAMPAIGN_REMOVED = "campaign.info.campaign.removed";
102 
103     private static final String ERROR_CAMPAIGN_CODE_ALREADY_USED = "campaign.error.campaign_code.already.used";
104 
105     // Session variable to store working values
106     private Campaign _campaign;
107 
108     /**
109      * Build the Manage View
110      * 
111      * @param request
112      *            The HTTP request
113      * @return The page
114      */
115     @View( value = VIEW_MANAGE_CAMPAIGNS, defaultView = true )
116     public String getManageCampaigns( HttpServletRequest request )
117     {
118         _campaign = null;
119         List<Campaign> listCampaigns = CampaignHome.getCampaignsList( );
120         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAIGN_LIST, listCampaigns, JSP_MANAGE_CAMPAIGNS );
121 
122         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNS, TEMPLATE_MANAGE_CAMPAIGNS, model );
123     }
124 
125     /**
126      * Returns the form to create a campaign
127      *
128      * @param request
129      *            The Http request
130      * @return the html code of the campaign form
131      */
132     @View( VIEW_CREATE_CAMPAIGN )
133     public String getCreateCampaign( HttpServletRequest request )
134     {
135         _campaign = ( _campaign != null ) ? _campaign : new Campaign( );
136 
137         Map<String, Object> model = getModel( );
138         model.put( MARK_CAMPAIGN, _campaign );
139         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_CAMPAIGN ) );
140 
141         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAIGN, TEMPLATE_CREATE_CAMPAIGN, model );
142     }
143 
144     /**
145      * Process the data capture form of a new campaign
146      *
147      * @param request
148      *            The Http Request
149      * @return The Jsp URL of the process result
150      * @throws AccessDeniedException
151      */
152     @Action( ACTION_CREATE_CAMPAIGN )
153     public String doCreateCampaign( HttpServletRequest request ) throws AccessDeniedException
154     {
155         populate( _campaign, request, getLocale( ) );
156 
157         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_CAMPAIGN ) )
158         {
159             throw new AccessDeniedException( "Invalid security token" );
160         }
161 
162         // Check constraints
163         if ( !validateBean( _campaign, VALIDATION_ATTRIBUTES_PREFIX ) )
164         {
165             return redirectView( request, VIEW_CREATE_CAMPAIGN );
166         }
167         if ( CampaignHome.findByCampaignCode( _campaign.getCampaignCode( ) ) != null )
168         {
169 
170             addError( ERROR_CAMPAIGN_CODE_ALREADY_USED, getLocale( ) );
171             return redirectView( request, VIEW_CREATE_CAMPAIGN );
172         }
173         CampaignHome.create( _campaign );
174         addInfo( INFO_CAMPAIGN_CREATED, getLocale( ) );
175 
176         return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
177     }
178 
179     /**
180      * Manages the removal form of a campaign whose identifier is in the http request
181      *
182      * @param request
183      *            The Http request
184      * @return the html code to confirm
185      */
186     @Action( ACTION_CONFIRM_REMOVE_CAMPAIGN )
187     public String getConfirmRemoveCampaign( HttpServletRequest request )
188     {
189         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
190         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAIGN ) );
191         url.addParameter( PARAMETER_ID_CAMPAIGN, nId );
192 
193         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAIGN, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
194 
195         return redirect( request, strMessageUrl );
196     }
197 
198     /**
199      * Handles the removal form of a campaign
200      *
201      * @param request
202      *            The Http request
203      * @return the jsp URL to display the form to manage campaigns
204      */
205     @Action( ACTION_REMOVE_CAMPAIGN )
206     public String doRemoveCampaign( HttpServletRequest request )
207     {
208         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
209         CampaignHome.remove( nId );
210         addInfo( INFO_CAMPAIGN_REMOVED, getLocale( ) );
211 
212         return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
213     }
214 
215     /**
216      * Returns the form to update info about a campaign
217      *
218      * @param request
219      *            The Http request
220      * @return The HTML form to update info
221      */
222     @View( VIEW_MODIFY_CAMPAIGN )
223     public String getModifyCampaign( HttpServletRequest request )
224     {
225         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
226 
227         if ( _campaign == null || ( _campaign.getId( ) != nId ) )
228         {
229             _campaign = CampaignHome.findByPrimaryKey( nId );
230         }
231 
232         Map<String, Object> model = getModel( );
233         model.put( MARK_CAMPAIGN, _campaign );
234         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_CAMPAIGN ) );
235 
236         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGN, TEMPLATE_MODIFY_CAMPAIGN, model );
237     }
238 
239     /**
240      * Process the change form of a campaign
241      *
242      * @param request
243      *            The Http request
244      * @return The Jsp URL of the process result
245      * @throws AccessDeniedException
246      */
247     @Action( ACTION_MODIFY_CAMPAIGN )
248     public String doModifyCampaign( HttpServletRequest request ) throws AccessDeniedException
249     {
250         populate( _campaign, request, getLocale( ) );
251 
252         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_CAMPAIGN ) )
253         {
254             throw new AccessDeniedException( "Invalid security token" );
255         }
256 
257         // Check constraints
258         if ( !validateBean( _campaign, VALIDATION_ATTRIBUTES_PREFIX ) )
259         {
260             return redirect( request, VIEW_MODIFY_CAMPAIGN, PARAMETER_ID_CAMPAIGN, _campaign.getId( ) );
261         }
262 
263         CampaignHome.update( _campaign );
264         addInfo( INFO_CAMPAIGN_UPDATED, getLocale( ) );
265 
266         return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
267     }
268 }