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.CampagneTheme;
37  import fr.paris.lutece.plugins.campagnebp.business.CampagneThemeHome;
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 CampagneTheme features ( manage, create, modify, remove )
52   */
53  @Controller( controllerJsp = "ManageCampagneThemes.jsp", controllerPath = "jsp/admin/plugins/ideation/", right = "CAMPAGNEBP_MANAGEMENT" )
54  public class CampagneThemeJspBean extends ManageCampagnebpJspBean
55  {
56  
57      ////////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // templates
61      private static final String TEMPLATE_MANAGE_CAMPAGNETHEMES = "/admin/plugins/campagnebp/manage_campagnethemes.html";
62      private static final String TEMPLATE_CREATE_CAMPAGNETHEME = "/admin/plugins/campagnebp/create_campagnetheme.html";
63      private static final String TEMPLATE_MODIFY_CAMPAGNETHEME = "/admin/plugins/campagnebp/modify_campagnetheme.html";
64  
65      // Parameters
66      private static final String PARAMETER_ID_CAMPAGNETHEME = "id";
67  
68      // Properties for page titles
69      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNETHEMES = "campagnebp.manage_campagnethemes.pageTitle";
70      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNETHEME = "campagnebp.modify_campagnetheme.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAGNETHEME = "campagnebp.create_campagnetheme.pageTitle";
72  
73      // Markers
74      private static final String MARK_CAMPAGNETHEME_LIST = "campagnetheme_list";
75      private static final String MARK_CAMPAGNETHEME = "campagnetheme";
76  
77      private static final String JSP_MANAGE_CAMPAGNETHEMES = "jsp/admin/plugins/campagnebp/ManageCampagneThemes.jsp";
78  
79      // Properties
80      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAGNETHEME = "campagnebp.message.confirmRemoveCampagneTheme";
81      private static final String PROPERTY_DEFAULT_LIST_CAMPAGNETHEME_PER_PAGE = "campagnebp.listCampagneThemes.itemsPerPage";
82  
83      private static final String VALIDATION_ATTRIBUTES_PREFIX = "campagnebp.model.entity.campagnetheme.attribute.";
84  
85      // Views
86      private static final String VIEW_MANAGE_CAMPAGNETHEMES = "manageCampagneThemes";
87      private static final String VIEW_CREATE_CAMPAGNETHEME = "createCampagneTheme";
88      private static final String VIEW_MODIFY_CAMPAGNETHEME = "modifyCampagneTheme";
89  
90      // Actions
91      private static final String ACTION_CREATE_CAMPAGNETHEME = "createCampagneTheme";
92      private static final String ACTION_MODIFY_CAMPAGNETHEME = "modifyCampagneTheme";
93      private static final String ACTION_REMOVE_CAMPAGNETHEME = "removeCampagneTheme";
94      private static final String ACTION_CONFIRM_REMOVE_CAMPAGNETHEME = "confirmRemoveCampagneTheme";
95  
96      // Infos
97      private static final String INFO_CAMPAGNETHEME_CREATED = "campagnebp.info.campagnetheme.created";
98      private static final String INFO_CAMPAGNETHEME_UPDATED = "campagnebp.info.campagnetheme.updated";
99      private static final String INFO_CAMPAGNETHEME_REMOVED = "campagnebp.info.campagnetheme.removed";
100 
101     // Session variable to store working values
102     private CampagneTheme _campagnetheme;
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_CAMPAGNETHEMES, defaultView = true )
112     public String getManageCampagneThemes( HttpServletRequest request )
113     {
114         _campagnetheme = null;
115         List<CampagneTheme> listCampagneThemes = (List<CampagneTheme>) CampagneThemeHome.getCampagneThemesList( );
116         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAGNETHEME_LIST, listCampagneThemes, JSP_MANAGE_CAMPAGNETHEMES );
117 
118         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNETHEMES, TEMPLATE_MANAGE_CAMPAGNETHEMES, model );
119     }
120 
121     /**
122      * Returns the form to create a campagnetheme
123      *
124      * @param request
125      *            The Http request
126      * @return the html code of the campagnetheme form
127      */
128     @View( VIEW_CREATE_CAMPAGNETHEME )
129     public String getCreateCampagneTheme( HttpServletRequest request )
130     {
131         _campagnetheme = ( _campagnetheme != null ) ? _campagnetheme : new CampagneTheme( );
132 
133         Map<String, Object> model = getModel( );
134         model.put( MARK_CAMPAGNETHEME, _campagnetheme );
135 
136         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAGNETHEME, TEMPLATE_CREATE_CAMPAGNETHEME, model );
137     }
138 
139     /**
140      * Process the data capture form of a new campagnetheme
141      *
142      * @param request
143      *            The Http Request
144      * @return The Jsp URL of the process result
145      */
146     @Action( ACTION_CREATE_CAMPAGNETHEME )
147     public String doCreateCampagneTheme( HttpServletRequest request )
148     {
149         populate( _campagnetheme, request );
150 
151         // Check constraints
152         if ( !validateBean( _campagnetheme, VALIDATION_ATTRIBUTES_PREFIX ) )
153         {
154             return redirectView( request, VIEW_CREATE_CAMPAGNETHEME );
155         }
156 
157         CampagneThemeHome.create( _campagnetheme );
158         addInfo( INFO_CAMPAGNETHEME_CREATED, getLocale( ) );
159 
160         return redirectView( request, VIEW_MANAGE_CAMPAGNETHEMES );
161     }
162 
163     /**
164      * Manages the removal form of a campagnetheme 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_CAMPAGNETHEME )
171     public String getConfirmRemoveCampagneTheme( HttpServletRequest request )
172     {
173         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNETHEME ) );
174         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAGNETHEME ) );
175         url.addParameter( PARAMETER_ID_CAMPAGNETHEME, nId );
176 
177         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAGNETHEME, url.getUrl( ),
178                 AdminMessage.TYPE_CONFIRMATION );
179 
180         return redirect( request, strMessageUrl );
181     }
182 
183     /**
184      * Handles the removal form of a campagnetheme
185      *
186      * @param request
187      *            The Http request
188      * @return the jsp URL to display the form to manage campagnethemes
189      */
190     @Action( ACTION_REMOVE_CAMPAGNETHEME )
191     public String doRemoveCampagneTheme( HttpServletRequest request )
192     {
193         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNETHEME ) );
194         CampagneThemeHome.remove( nId );
195         addInfo( INFO_CAMPAGNETHEME_REMOVED, getLocale( ) );
196 
197         return redirectView( request, VIEW_MANAGE_CAMPAGNETHEMES );
198     }
199 
200     /**
201      * Returns the form to update info about a campagnetheme
202      *
203      * @param request
204      *            The Http request
205      * @return The HTML form to update info
206      */
207     @View( VIEW_MODIFY_CAMPAGNETHEME )
208     public String getModifyCampagneTheme( HttpServletRequest request )
209     {
210         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNETHEME ) );
211 
212         if ( _campagnetheme == null || ( _campagnetheme.getId( ) != nId ) )
213         {
214             _campagnetheme = CampagneThemeHome.findByPrimaryKey( nId );
215         }
216 
217         Map<String, Object> model = getModel( );
218         model.put( MARK_CAMPAGNETHEME, _campagnetheme );
219 
220         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNETHEME, TEMPLATE_MODIFY_CAMPAGNETHEME, model );
221     }
222 
223     /**
224      * Process the change form of a campagnetheme
225      *
226      * @param request
227      *            The Http request
228      * @return The Jsp URL of the process result
229      */
230     @Action( ACTION_MODIFY_CAMPAGNETHEME )
231     public String doModifyCampagneTheme( HttpServletRequest request )
232     {
233         populate( _campagnetheme, request );
234 
235         // Check constraints
236         if ( !validateBean( _campagnetheme, VALIDATION_ATTRIBUTES_PREFIX ) )
237         {
238             return redirect( request, VIEW_MODIFY_CAMPAGNETHEME, PARAMETER_ID_CAMPAGNETHEME, _campagnetheme.getId( ) );
239         }
240 
241         CampagneThemeHome.update( _campagnetheme );
242         addInfo( INFO_CAMPAGNETHEME_UPDATED, getLocale( ) );
243 
244         return redirectView( request, VIEW_MANAGE_CAMPAGNETHEMES );
245     }
246 }