View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.sitelabels.web;
35  
36  import fr.paris.lutece.plugins.sitelabels.business.Label;
37  import fr.paris.lutece.plugins.sitelabels.service.LabelService;
38  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.security.SecurityTokenService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.html.Paginator;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  
57  /**
58   * This class provides the user interface to manage Label features ( manage, create, modify, remove )
59   */
60  @Controller( controllerJsp = "ManageLabels.jsp", controllerPath = "jsp/admin/plugins/sitelabels/", right = "SITELABELS_MANAGEMENT" )
61  public class LabelJspBean extends ManageSiteLabelsJspBean
62  {
63      ////////////////////////////////////////////////////////////////////////////
64      // Constants
65  
66      // templates
67      private static final String TEMPLATE_MANAGE_LABELS = "/admin/plugins/sitelabels/manage_labels.html";
68      private static final String TEMPLATE_CREATE_LABEL = "/admin/plugins/sitelabels/create_label.html";
69      private static final String TEMPLATE_MODIFY_LABEL = "/admin/plugins/sitelabels/modify_label.html";
70  
71      // Parameters
72      private static final String PARAMETER_KEY = "id";
73  
74      // Properties for page titles
75      private static final String PROPERTY_PAGE_TITLE_MANAGE_LABELS = "sitelabels.manage_labels.pageTitle";
76      private static final String PROPERTY_PAGE_TITLE_MODIFY_LABEL = "sitelabels.modify_label.pageTitle";
77      private static final String PROPERTY_PAGE_TITLE_CREATE_LABEL = "sitelabels.create_label.pageTitle";
78  
79      // Markers
80      private static final String MARK_LABEL_LIST = "label_list";
81      private static final String MARK_LABEL = "label";
82      private static final String JSP_MANAGE_LABELS = "jsp/admin/plugins/sitelabels/ManageLabels.jsp";
83  
84      // Properties
85      private static final String MESSAGE_CONFIRM_REMOVE_LABEL = "sitelabels.message.confirmRemoveLabel";
86      private static final String PROPERTY_DEFAULT_LIST_LABEL_PER_PAGE = "sitelabels.listLabels.itemsPerPage";
87      private static final String VALIDATION_ATTRIBUTES_PREFIX = "sitelabels.model.entity.label.attribute.";
88  
89      // Views
90      private static final String VIEW_MANAGE_LABELS = "manageLabels";
91      private static final String VIEW_CREATE_LABEL = "createLabel";
92      private static final String VIEW_MODIFY_LABEL = "modifyLabel";
93  
94      // Actions
95      private static final String ACTION_CREATE_LABEL = "createLabel";
96      private static final String ACTION_MODIFY_LABEL = "modifyLabel";
97      private static final String ACTION_REMOVE_LABEL = "removeLabel";
98      private static final String ACTION_CONFIRM_REMOVE_LABEL = "confirmRemoveLabel";
99  
100     // Infos
101     private static final String INFO_LABEL_CREATED = "sitelabels.info.label.created";
102     private static final String INFO_LABEL_UPDATED = "sitelabels.info.label.updated";
103     private static final String INFO_LABEL_REMOVED = "sitelabels.info.label.removed";
104 
105     // Session variable to store working values
106     private Label _label;
107     //Variables
108     private int _nDefaultItemsPerPage;
109     private String _strCurrentPageIndex;
110     private int _nItemsPerPage;
111 
112     /**
113      * Get ManageLabels View
114      * @param request The HTTP request
115      * @return The view
116      */
117     @View( value = VIEW_MANAGE_LABELS, defaultView = true )
118     public String getManageLabels( HttpServletRequest request )
119     {
120         _label = null;
121         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
122         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_LABEL_PER_PAGE, 50 );
123         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
124                 _nDefaultItemsPerPage );
125 
126         UrlItem url = new UrlItem( JSP_MANAGE_LABELS );
127         String strUrl = url.getUrl(  );
128         List<Label> listLabels = (List<Label>) LabelService.getLabelsList(  );
129 
130         // PAGINATOR
131         LocalizedPaginator paginator = new LocalizedPaginator( listLabels, _nItemsPerPage, strUrl,
132                 PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale(  ) );
133 
134         Map<String, Object> model = getModel(  );
135 
136         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
137         model.put( MARK_PAGINATOR, paginator );
138         model.put( MARK_LABEL_LIST, paginator.getPageItems(  ) );
139 
140         return getPage( PROPERTY_PAGE_TITLE_MANAGE_LABELS, TEMPLATE_MANAGE_LABELS, model );
141     }
142 
143     /**
144      * Returns the form to create a label
145      *
146      * @param request The Http request
147      * @return the html code of the label form
148      */
149     @View( VIEW_CREATE_LABEL )
150     public String getCreateLabel( HttpServletRequest request )
151     {
152         _label = ( _label != null ) ? _label : new Label(  );
153 
154         Map<String, Object> model = getModel(  );
155         model.put( MARK_LABEL, _label );
156         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_LABEL ) );
157 
158         return getPage( PROPERTY_PAGE_TITLE_CREATE_LABEL, TEMPLATE_CREATE_LABEL, model );
159     }
160 
161     /**
162      * Process the data capture form of a new label
163      *
164      * @param request The Http Request
165      * @return The Jsp URL of the process result
166      * @throws AccessDeniedException if the CSRF token cannot be validated
167      */
168     @Action( ACTION_CREATE_LABEL )
169     public String doCreateLabel( HttpServletRequest request ) throws AccessDeniedException
170     {
171         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_LABEL ) ) {
172             throw new AccessDeniedException( "Invalid CSRF token" );
173         }
174 
175         populate( _label, request );
176 
177         // Check constraints
178         if ( !validateBean( _label, VALIDATION_ATTRIBUTES_PREFIX ) )
179         {
180             return redirectView( request, VIEW_CREATE_LABEL );
181         }
182 
183         _label.setKey( LabelService.PREFIX + _label.getKey(  ) );
184         LabelService.create( _label );
185         addInfo( INFO_LABEL_CREATED, getLocale(  ) );
186 
187         return redirectView( request, VIEW_MANAGE_LABELS );
188     }
189 
190     /**
191      * Manages the removal form of a label whose identifier is in the http
192      * request
193      *
194      * @param request The Http request
195      * @return the html code to confirm
196      */
197     @Action( ACTION_CONFIRM_REMOVE_LABEL )
198     public String getConfirmRemoveLabel( HttpServletRequest request )
199     {
200         String strKey = request.getParameter( PARAMETER_KEY );
201         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_LABEL ) );
202         url.addParameter( PARAMETER_KEY, strKey );
203         url.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_REMOVE_LABEL ) );
204 
205         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_LABEL,
206                 new Object[] { strKey }, url.getUrl(  ), AdminMessage.TYPE_CONFIRMATION );
207 
208         return redirect( request, strMessageUrl );
209     }
210 
211     /**
212      * Handles the removal form of a label
213      *
214      * @param request The Http request
215      * @return the jsp URL to display the form to manage labels
216      * @throws AccessDeniedException if the CSRF token cannot be validated
217      */
218     @Action( ACTION_REMOVE_LABEL )
219     public String doRemoveLabel( HttpServletRequest request ) throws AccessDeniedException
220     {
221         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_REMOVE_LABEL ) ) {
222             throw new AccessDeniedException( "Invalid CSRF token" );
223         }
224 
225         String strKey = request.getParameter( PARAMETER_KEY );
226         LabelService.remove( strKey );
227         addInfo( INFO_LABEL_REMOVED, getLocale(  ) );
228 
229         return redirectView( request, VIEW_MANAGE_LABELS );
230     }
231 
232     /**
233      * Returns the form to update info about a label
234      *
235      * @param request The Http request
236      * @return The HTML form to update info
237      */
238     @View( VIEW_MODIFY_LABEL )
239     public String getModifyLabel( HttpServletRequest request )
240     {
241         String strKey = request.getParameter( PARAMETER_KEY );
242 
243         if ( _label == null || ( !_label.getKey().equals( strKey )))
244         {
245             _label = LabelService.findByPrimaryKey( strKey );
246         }
247 
248         Map<String, Object> model = getModel(  );
249         model.put( MARK_LABEL, _label );
250         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_LABEL ) );
251 
252         return getPage( PROPERTY_PAGE_TITLE_MODIFY_LABEL, TEMPLATE_MODIFY_LABEL, model );
253     }
254 
255     /**
256      * Process the change form of a label
257      *
258      * @param request The Http request
259      * @return The Jsp URL of the process result
260      * @throws AccessDeniedException if the CSRF token cannot be validated
261      */
262     @Action( ACTION_MODIFY_LABEL )
263     public String doModifyLabel( HttpServletRequest request ) throws AccessDeniedException
264     {
265         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_LABEL ) ) {
266             throw new AccessDeniedException( "Invalid CSRF token" );
267         }
268 
269         populate( _label, request );
270 
271         // Check constraints
272         if ( !validateBean( _label, VALIDATION_ATTRIBUTES_PREFIX ) )
273         {
274             Map<String, String> mapParameters = new HashMap<String, String>(  );
275             mapParameters.put( PARAMETER_KEY, _label.getKey(  ) );
276 
277             return redirect( request, VIEW_MODIFY_LABEL, mapParameters );
278         }
279 
280         LabelService.update( _label );
281         addInfo( INFO_LABEL_UPDATED, getLocale(  ) );
282 
283         return redirectView( request, VIEW_MANAGE_LABELS );
284     }
285 }