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.campagnebp.web;
35  
36  import fr.paris.lutece.plugins.campagnebp.business.CampagneImage;
37  import fr.paris.lutece.plugins.campagnebp.business.CampagneImageHome;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
41  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43  import fr.paris.lutece.util.url.UrlItem;
44  
45  import java.util.List;
46  import java.util.Map;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   * This class provides the user interface to manage CampagneImage features ( manage, create, modify, remove )
52   */
53  @Controller( controllerJsp = "ManageCampagneImages.jsp", controllerPath = "jsp/admin/plugins/campagnebp/", right = "CAMPAGNEBP_MANAGEMENT" )
54  public class CampagneImageJspBean extends ManageCampagnebpJspBean
55  {
56  
57      ////////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // templates
61      private static final String TEMPLATE_MANAGE_CAMPAGNEIMAGES = "/admin/plugins/campagnebp/manage_campagneimages.html";
62      private static final String TEMPLATE_CREATE_CAMPAGNEIMAGE = "/admin/plugins/campagnebp/create_campagneimage.html";
63      private static final String TEMPLATE_MODIFY_CAMPAGNEIMAGE = "/admin/plugins/campagnebp/modify_campagneimage.html";
64  
65      // Parameters
66      private static final String PARAMETER_ID_CAMPAGNEIMAGE = "id";
67  
68      // Properties for page titles
69      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNEIMAGES = "campagnebp.manage_campagneimages.pageTitle";
70      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNEIMAGE = "campagnebp.modify_campagneimage.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAGNEIMAGE = "campagnebp.create_campagneimage.pageTitle";
72  
73      // Markers
74      private static final String MARK_CAMPAGNEIMAGE_LIST = "campagneimage_list";
75      private static final String MARK_CAMPAGNEIMAGE = "campagneimage";
76  
77      private static final String JSP_MANAGE_CAMPAGNEIMAGES = "jsp/admin/plugins/campagnebp/ManageCampagneImages.jsp";
78  
79      // Properties
80      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAGNEIMAGE = "campagnebp.message.confirmRemoveCampagneImage";
81      private static final String PROPERTY_DEFAULT_LIST_CAMPAGNEIMAGE_PER_PAGE = "campagnebp.listCampagneImages.itemsPerPage";
82  
83      private static final String VALIDATION_ATTRIBUTES_PREFIX = "campagnebp.model.entity.campagneimage.attribute.";
84  
85      // Views
86      private static final String VIEW_MANAGE_CAMPAGNEIMAGES = "manageCampagneImages";
87      private static final String VIEW_CREATE_CAMPAGNEIMAGE = "createCampagneImage";
88      private static final String VIEW_MODIFY_CAMPAGNEIMAGE = "modifyCampagneImage";
89  
90      // Actions
91      private static final String ACTION_CREATE_CAMPAGNEIMAGE = "createCampagneImage";
92      private static final String ACTION_MODIFY_CAMPAGNEIMAGE = "modifyCampagneImage";
93      private static final String ACTION_REMOVE_CAMPAGNEIMAGE = "removeCampagneImage";
94      private static final String ACTION_CONFIRM_REMOVE_CAMPAGNEIMAGE = "confirmRemoveCampagneImage";
95  
96      // Infos
97      private static final String INFO_CAMPAGNEIMAGE_CREATED = "campagnebp.info.campagneimage.created";
98      private static final String INFO_CAMPAGNEIMAGE_UPDATED = "campagnebp.info.campagneimage.updated";
99      private static final String INFO_CAMPAGNEIMAGE_REMOVED = "campagnebp.info.campagneimage.removed";
100 
101     // Session variable to store working values
102     private CampagneImage _campagneimage;
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_CAMPAGNEIMAGES, defaultView = true )
112     public String getManageCampagneImages( HttpServletRequest request )
113     {
114         _campagneimage = null;
115         List<CampagneImage> listCampagneImages = (List<CampagneImage>) CampagneImageHome.getCampagneImagesList( );
116         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAGNEIMAGE_LIST, listCampagneImages, JSP_MANAGE_CAMPAGNEIMAGES );
117 
118         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNEIMAGES, TEMPLATE_MANAGE_CAMPAGNEIMAGES, model );
119     }
120 
121     /**
122      * Returns the form to create a campagneimage
123      *
124      * @param request
125      *            The Http request
126      * @return the html code of the campagneimage form
127      */
128     @View( VIEW_CREATE_CAMPAGNEIMAGE )
129     public String getCreateCampagneImage( HttpServletRequest request )
130     {
131         _campagneimage = ( _campagneimage != null ) ? _campagneimage : new CampagneImage( );
132 
133         Map<String, Object> model = getModel( );
134         model.put( MARK_CAMPAGNEIMAGE, _campagneimage );
135 
136         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAGNEIMAGE, TEMPLATE_CREATE_CAMPAGNEIMAGE, model );
137     }
138 
139     /**
140      * Process the data capture form of a new campagneimage
141      *
142      * @param request
143      *            The Http Request
144      * @return The Jsp URL of the process result
145      */
146     @Action( ACTION_CREATE_CAMPAGNEIMAGE )
147     public String doCreateCampagneImage( HttpServletRequest request )
148     {
149         populate( _campagneimage, request );
150 
151         // Check constraints
152         if ( !validateBean( _campagneimage, VALIDATION_ATTRIBUTES_PREFIX ) )
153         {
154             return redirectView( request, VIEW_CREATE_CAMPAGNEIMAGE );
155         }
156 
157         CampagneImageHome.create( _campagneimage );
158         addInfo( INFO_CAMPAGNEIMAGE_CREATED, getLocale( ) );
159 
160         return redirectView( request, VIEW_MANAGE_CAMPAGNEIMAGES );
161     }
162 
163     /**
164      * Manages the removal form of a campagneimage 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_CAMPAGNEIMAGE )
171     public String getConfirmRemoveCampagneImage( HttpServletRequest request )
172     {
173         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEIMAGE ) );
174         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAGNEIMAGE ) );
175         url.addParameter( PARAMETER_ID_CAMPAGNEIMAGE, nId );
176 
177         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAGNEIMAGE, url.getUrl( ),
178                 AdminMessage.TYPE_CONFIRMATION );
179 
180         return redirect( request, strMessageUrl );
181     }
182 
183     /**
184      * Handles the removal form of a campagneimage
185      *
186      * @param request
187      *            The Http request
188      * @return the jsp URL to display the form to manage campagneimages
189      */
190     @Action( ACTION_REMOVE_CAMPAGNEIMAGE )
191     public String doRemoveCampagneImage( HttpServletRequest request )
192     {
193         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEIMAGE ) );
194         CampagneImageHome.remove( nId );
195         addInfo( INFO_CAMPAGNEIMAGE_REMOVED, getLocale( ) );
196 
197         return redirectView( request, VIEW_MANAGE_CAMPAGNEIMAGES );
198     }
199 
200     /**
201      * Returns the form to update info about a campagneimage
202      *
203      * @param request
204      *            The Http request
205      * @return The HTML form to update info
206      */
207     @View( VIEW_MODIFY_CAMPAGNEIMAGE )
208     public String getModifyCampagneImage( HttpServletRequest request )
209     {
210         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEIMAGE ) );
211 
212         if ( _campagneimage == null || ( _campagneimage.getId( ) != nId ) )
213         {
214             _campagneimage = CampagneImageHome.findByPrimaryKey( nId );
215         }
216 
217         Map<String, Object> model = getModel( );
218         model.put( MARK_CAMPAGNEIMAGE, _campagneimage );
219 
220         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNEIMAGE, TEMPLATE_MODIFY_CAMPAGNEIMAGE, model );
221     }
222 
223     /**
224      * Process the change form of a campagneimage
225      *
226      * @param request
227      *            The Http request
228      * @return The Jsp URL of the process result
229      */
230     @Action( ACTION_MODIFY_CAMPAGNEIMAGE )
231     public String doModifyCampagneImage( HttpServletRequest request )
232     {
233         populate( _campagneimage, request );
234 
235         // Check constraints
236         if ( !validateBean( _campagneimage, VALIDATION_ATTRIBUTES_PREFIX ) )
237         {
238             return redirect( request, VIEW_MODIFY_CAMPAGNEIMAGE, PARAMETER_ID_CAMPAGNEIMAGE, _campagneimage.getId( ) );
239         }
240 
241         CampagneImageHome.update( _campagneimage );
242         addInfo( INFO_CAMPAGNEIMAGE_UPDATED, getLocale( ) );
243 
244         return redirectView( request, VIEW_MANAGE_CAMPAGNEIMAGES );
245     }
246 }