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.util.List;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignImage;
42  import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignImageHome;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
46  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  /**
51   * This class provides the user interface to manage CampaignImage features ( manage, create, modify, remove )
52   */
53  @Controller( controllerJsp = "ManageCampaignImages.jsp", controllerPath = "jsp/admin/plugins/participatorybudget/campaign/", right = "CAMPAIGN_MANAGEMENT" )
54  public class CampaignImageJspBean extends ManageCampaignJspBean
55  {
56  
57      // //////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // templates
61      private static final String TEMPLATE_MANAGE_CAMPAIGNIMAGES = "/admin/plugins/participatorybudget/campaign/manage_campaignimages.html";
62      private static final String TEMPLATE_CREATE_CAMPAIGNIMAGE = "/admin/plugins/participatorybudget/campaign/create_campaignimage.html";
63      private static final String TEMPLATE_MODIFY_CAMPAIGNIMAGE = "/admin/plugins/participatorybudget/campaign/modify_campaignimage.html";
64  
65      // Parameters
66      private static final String PARAMETER_ID_CAMPAIGNIMAGE = "id";
67  
68      // Properties for page titles
69      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNIMAGES = "participatorybudget.manage_campaignimages.pageTitle";
70      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNIMAGE = "participatorybudget.modify_campaignimage.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNIMAGE = "participatorybudget.create_campaignimage.pageTitle";
72  
73      // Markers
74      private static final String MARK_CAMPAIGNIMAGE_LIST = "campaignimage_list";
75      private static final String MARK_CAMPAIGNIMAGE = "campaignimage";
76  
77      private static final String JSP_MANAGE_CAMPAIGNIMAGES = "jsp/admin/plugins/participatorybudget/campaign/ManageCampaignImages.jsp";
78  
79      // Properties
80      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAIGNIMAGE = "participatorybudget.message.confirmRemoveCampaignImage";
81      private static final String PROPERTY_DEFAULT_LIST_CAMPAIGNIMAGE_PER_PAGE = "participatorybudget.listCampaignImages.itemsPerPage";
82  
83      private static final String VALIDATION_ATTRIBUTES_PREFIX = "participatorybudget.model.entity.campaignimage.attribute.";
84  
85      // Views
86      private static final String VIEW_MANAGE_CAMPAIGNIMAGES = "manageCampaignImages";
87      private static final String VIEW_CREATE_CAMPAIGNIMAGE = "createCampaignImage";
88      private static final String VIEW_MODIFY_CAMPAIGNIMAGE = "modifyCampaignImage";
89  
90      // Actions
91      private static final String ACTION_CREATE_CAMPAIGNIMAGE = "createCampaignImage";
92      private static final String ACTION_MODIFY_CAMPAIGNIMAGE = "modifyCampaignImage";
93      private static final String ACTION_REMOVE_CAMPAIGNIMAGE = "removeCampaignImage";
94      private static final String ACTION_CONFIRM_REMOVE_CAMPAIGNIMAGE = "confirmRemoveCampaignImage";
95  
96      // Infos
97      private static final String INFO_CAMPAIGNIMAGE_CREATED = "participatorybudget.info.campaignimage.created";
98      private static final String INFO_CAMPAIGNIMAGE_UPDATED = "participatorybudget.info.campaignimage.updated";
99      private static final String INFO_CAMPAIGNIMAGE_REMOVED = "participatorybudget.info.campaignimage.removed";
100 
101     // Session variable to store working values
102     private CampaignImage _campaignimage;
103 
104     /**
105      * Build the Manage View
106      * 
107      * @param request
108      *            The HTTP request
109      * @return The page
110      */
111     @View( value = VIEW_MANAGE_CAMPAIGNIMAGES, defaultView = true )
112     public String getManageCampaignImages( HttpServletRequest request )
113     {
114         _campaignimage = null;
115         List<CampaignImage> listCampaignImages = (List<CampaignImage>) CampaignImageHome.getCampaignImagesList( );
116         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAIGNIMAGE_LIST, listCampaignImages, JSP_MANAGE_CAMPAIGNIMAGES );
117 
118         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNIMAGES, TEMPLATE_MANAGE_CAMPAIGNIMAGES, model );
119     }
120 
121     /**
122      * Returns the form to create a campaignimage
123      *
124      * @param request
125      *            The Http request
126      * @return the html code of the campaignimage form
127      */
128     @View( VIEW_CREATE_CAMPAIGNIMAGE )
129     public String getCreateCampaignImage( HttpServletRequest request )
130     {
131         _campaignimage = ( _campaignimage != null ) ? _campaignimage : new CampaignImage( );
132 
133         Map<String, Object> model = getModel( );
134         model.put( MARK_CAMPAIGNIMAGE, _campaignimage );
135 
136         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNIMAGE, TEMPLATE_CREATE_CAMPAIGNIMAGE, model );
137     }
138 
139     /**
140      * Process the data capture form of a new campaignimage
141      *
142      * @param request
143      *            The Http Request
144      * @return The Jsp URL of the process result
145      */
146     @Action( ACTION_CREATE_CAMPAIGNIMAGE )
147     public String doCreateCampaignImage( HttpServletRequest request )
148     {
149         populate( _campaignimage, request );
150 
151         // Check constraints
152         if ( !validateBean( _campaignimage, VALIDATION_ATTRIBUTES_PREFIX ) )
153         {
154             return redirectView( request, VIEW_CREATE_CAMPAIGNIMAGE );
155         }
156 
157         CampaignImageHome.create( _campaignimage );
158         addInfo( INFO_CAMPAIGNIMAGE_CREATED, getLocale( ) );
159 
160         return redirectView( request, VIEW_MANAGE_CAMPAIGNIMAGES );
161     }
162 
163     /**
164      * Manages the removal form of a campaignimage whose identifier is in the http request
165      *
166      * @param request
167      *            The Http request
168      * @return the html code to confirm
169      */
170     @Action( ACTION_CONFIRM_REMOVE_CAMPAIGNIMAGE )
171     public String getConfirmRemoveCampaignImage( HttpServletRequest request )
172     {
173         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNIMAGE ) );
174         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAIGNIMAGE ) );
175         url.addParameter( PARAMETER_ID_CAMPAIGNIMAGE, nId );
176 
177         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAIGNIMAGE, url.getUrl( ),
178                 AdminMessage.TYPE_CONFIRMATION );
179 
180         return redirect( request, strMessageUrl );
181     }
182 
183     /**
184      * Handles the removal form of a campaignimage
185      *
186      * @param request
187      *            The Http request
188      * @return the jsp URL to display the form to manage campaignimages
189      */
190     @Action( ACTION_REMOVE_CAMPAIGNIMAGE )
191     public String doRemoveCampaignImage( HttpServletRequest request )
192     {
193         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNIMAGE ) );
194         CampaignImageHome.remove( nId );
195         addInfo( INFO_CAMPAIGNIMAGE_REMOVED, getLocale( ) );
196 
197         return redirectView( request, VIEW_MANAGE_CAMPAIGNIMAGES );
198     }
199 
200     /**
201      * Returns the form to update info about a campaignimage
202      *
203      * @param request
204      *            The Http request
205      * @return The HTML form to update info
206      */
207     @View( VIEW_MODIFY_CAMPAIGNIMAGE )
208     public String getModifyCampaignImage( HttpServletRequest request )
209     {
210         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNIMAGE ) );
211 
212         if ( _campaignimage == null || ( _campaignimage.getId( ) != nId ) )
213         {
214             _campaignimage = CampaignImageHome.findByPrimaryKey( nId );
215         }
216 
217         Map<String, Object> model = getModel( );
218         model.put( MARK_CAMPAIGNIMAGE, _campaignimage );
219 
220         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNIMAGE, TEMPLATE_MODIFY_CAMPAIGNIMAGE, model );
221     }
222 
223     /**
224      * Process the change form of a campaignimage
225      *
226      * @param request
227      *            The Http request
228      * @return The Jsp URL of the process result
229      */
230     @Action( ACTION_MODIFY_CAMPAIGNIMAGE )
231     public String doModifyCampaignImage( HttpServletRequest request )
232     {
233         populate( _campaignimage, request );
234 
235         // Check constraints
236         if ( !validateBean( _campaignimage, VALIDATION_ATTRIBUTES_PREFIX ) )
237         {
238             return redirect( request, VIEW_MODIFY_CAMPAIGNIMAGE, PARAMETER_ID_CAMPAIGNIMAGE, _campaignimage.getId( ) );
239         }
240 
241         CampaignImageHome.update( _campaignimage );
242         addInfo( INFO_CAMPAIGNIMAGE_UPDATED, getLocale( ) );
243 
244         return redirectView( request, VIEW_MANAGE_CAMPAIGNIMAGES );
245     }
246 }