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.blog.web;
35  
36  import fr.paris.lutece.api.user.User;
37  import java.util.Collections;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  
45  import fr.paris.lutece.plugins.blog.business.BlogHome;
46  import fr.paris.lutece.plugins.blog.business.Tag;
47  import fr.paris.lutece.plugins.blog.business.TagHome;
48  import fr.paris.lutece.portal.business.rbac.RBAC;
49  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
50  import fr.paris.lutece.portal.service.admin.AdminUserService;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  import fr.paris.lutece.portal.service.rbac.RBACService;
54  import fr.paris.lutece.portal.service.util.AppPathService;
55  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
56  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
57  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
58  import fr.paris.lutece.util.json.JsonResponse;
59  import fr.paris.lutece.util.json.JsonUtil;
60  import fr.paris.lutece.util.url.UrlItem;
61  
62  /**
63   * This class provides the user interface to manage Blog features ( manage, create, modify, remove )
64   */
65  @Controller( controllerJsp = "ManageTags.jsp", controllerPath = "jsp/admin/plugins/blog/", right = "BLOG_MANAGEMENT" )
66  public class TagJspBean extends ManageBlogJspBean
67  {
68      private static final long serialVersionUID = 3209382166137329118L;
69      // Templates
70      private static final String TEMPLATE_MANAGE_TAGS = "/admin/plugins/blog/tag/manage_tags.html";
71      private static final String TEMPLATE_CREATE_TAG = "/admin/plugins/blog/tag/create_tag.html";
72      private static final String TEMPLATE_MODIFY_TAG = "/admin/plugins/blog/tag/modify_tag.html";
73  
74      // Parameters
75      private static final String PARAMETER_ID_TAG = "id";
76  
77      // Properties for page titles
78      private static final String PROPERTY_PAGE_TITLE_MANAGE_TAGS = "blog.manage_tags.pageTitle";
79      private static final String PROPERTY_PAGE_TITLE_MODIFY_TAGS = "blog.modify_tags.pageTitle";
80      private static final String PROPERTY_PAGE_TITLE_CREATE_TAG = "blog.create_tag.pageTitle";
81  
82      private static final String MESSAGE_ERROR_TAG_IS_AFFECTED = "blog.message.errorTagIsAffected";
83  
84      // Markers
85      private static final String MARK_TAG_LIST = "tag_list";
86      private static final String MARK_TAG = "tags";
87      private static final String MARK_WEBAPP_URL = "webapp_url";
88  
89      private static final String JSP_MANAGE_TAGS = "jsp/admin/plugins/blog/ManageTags.jsp";
90  
91      // Validations
92      private static final String VALIDATION_ATTRIBUTES_PREFIX = "blog.model.entity.tag.attribute.";
93  
94      // Views
95      private static final String VIEW_MANAGE_TAGS = "manageTags";
96      private static final String VIEW_CREATE_TAG = "createTag";
97      private static final String VIEW_MODIFY_TAG = "modifyTag";
98  
99      // ActionscreateTag
100     private static final String ACTION_CREATE_TAG = "createTag";
101     private static final String ACTION_MODIFY_TAG = "modifyTag";
102     private static final String ACTION_REMOVE_TAG = "removeTag";
103     private static final String ACTION_CONFIRM_REMOVE_TAG = "confirmRemoveTag";
104     private static final String ACTION_CREATE_TAG_AJAX_REQUEST = "createTagByAjax";
105 
106     // Infos
107     private static final String INFO_TAG_CREATED = "blog.info.tag.created";
108     private static final String INFO_TAG_UPDATED = "blog.info.tag.updated";
109     private static final String INFO_TAG_REMOVED = "blog.info.tag.removed";
110 
111     // Session variable to store working values
112     private Tag _tag;
113 
114     /**
115      * Build the Manage View
116      * 
117      * @param request
118      *            The HTTP request
119      * @return The page
120      */
121     @View( value = VIEW_MANAGE_TAGS, defaultView = true )
122     public String getManageTags( HttpServletRequest request )
123     {
124         _tag = null;
125         List<Tag> listTag = TagHome.getTagList( );
126         Collections.sort( listTag, ( tag1, tag2 ) -> tag1.getName( ).compareToIgnoreCase( tag2.getName( ) ) );
127 
128         Map<String, Object> model = getPaginatedListModel( request, MARK_TAG_LIST, listTag, JSP_MANAGE_TAGS );
129         boolean bPermissionCreate = RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_CREATE, getUser( ) );
130         boolean bPermissionModify = RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_MODIFY, getUser( ) );
131         boolean bPermissionDelete = RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_DELETE, getUser( ) );
132 
133         model.put( MARK_PERMISSION_CREATE_TAG, bPermissionCreate );
134         model.put( MARK_PERMISSION_MODIFY_TAG, bPermissionModify );
135         model.put( MARK_PERMISSION_DELETE_TAG, bPermissionDelete );
136 
137         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
138 
139         return getPage( PROPERTY_PAGE_TITLE_MANAGE_TAGS, TEMPLATE_MANAGE_TAGS, model );
140     }
141 
142     /**
143      * Returns the form to create a tag
144      *
145      * @param request
146      *            The Http request
147      * @return the html code of the tag form
148      * @throws AccessDeniedException
149      */
150     @View( VIEW_CREATE_TAG )
151     public String getCreateTag( HttpServletRequest request ) throws AccessDeniedException
152     {
153         if ( !RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_CREATE, getUser( ) ) )
154         {
155             throw new AccessDeniedException( UNAUTHORIZED );
156         }
157         _tag = ( _tag != null ) ? _tag : new Tag( );
158 
159         Map<String, Object> model = getModel( );
160 
161         model.put( MARK_TAG, _tag );
162         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
163 
164         return getPage( PROPERTY_PAGE_TITLE_CREATE_TAG, TEMPLATE_CREATE_TAG, model );
165     }
166 
167     /**
168      * Process the data capture form of a new tag
169      *
170      * @param request
171      *            The Http Request
172      * @return The Jsp URL of the process result
173      */
174     @Action( ACTION_CREATE_TAG )
175     public String doCreateTag( HttpServletRequest request )
176     {
177         String strRequestAjax = request.getParameter( ACTION_CREATE_TAG_AJAX_REQUEST );
178         _tag = ( _tag != null ) ? _tag : new Tag( );
179         populate( _tag, request );
180         if ( RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Tag.PERMISSION_CREATE,
181                 AdminUserService.getAdminUser( request ) ) )
182         {
183             // Check constraints
184             if ( !validateBean( _tag, VALIDATION_ATTRIBUTES_PREFIX ) )
185             {
186                 return redirectView( request, VIEW_CREATE_TAG );
187             }
188 
189             if ( TagHome.findByName( _tag.getName( ) ) == null )
190             {
191 
192                 Tag tag = TagHome.create( _tag );
193 
194                 if ( strRequestAjax != null && ACTION_CREATE_TAG_AJAX_REQUEST.endsWith( strRequestAjax ) )
195                 {
196 
197                     return JsonUtil.buildJsonResponse( new JsonResponse( String.valueOf( tag.getIdTag( ) ) ) );
198                 }
199 
200                 addInfo( INFO_TAG_CREATED, getLocale( ) );
201 
202             }
203             else
204                 if ( strRequestAjax != null && ACTION_CREATE_TAG_AJAX_REQUEST.endsWith( strRequestAjax ) )
205                 {
206 
207                     return JsonUtil.buildJsonResponse( new JsonResponse( "TAG_EXIST" ) );
208                 }
209         }
210         return redirectView( request, VIEW_MANAGE_TAGS );
211     }
212 
213     /**
214      * Manages the removal form of a tag whose identifier is in the http request
215      *
216      * @param request
217      *            The Http request
218      * @return the html code to confirm
219      * @throws AccessDeniedException
220      */
221     @Action( ACTION_CONFIRM_REMOVE_TAG )
222     public String getConfirmRemoveTag( HttpServletRequest request ) throws AccessDeniedException
223     {
224 
225         String strId = request.getParameter( PARAMETER_ID_TAG );
226         int nId = Integer.parseInt( strId );
227         if ( !RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, strId, Tag.PERMISSION_DELETE, AdminUserService.getAdminUser( request ) ) )
228         {
229             throw new AccessDeniedException( UNAUTHORIZED );
230         }
231         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_TAG ) );
232         url.addParameter( PARAMETER_ID_TAG, nId );
233 
234         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_TAG, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
235 
236         return redirect( request, strMessageUrl );
237     }
238 
239     /**
240      * Handles the removal form of a tag
241      *
242      * @param request
243      *            The Http request
244      * @return the jsp URL to display the form to manage tag
245      */
246     @Action( ACTION_REMOVE_TAG )
247     public String doRemoveTag( HttpServletRequest request )
248     {
249         String strId = request.getParameter( PARAMETER_ID_TAG );
250         int nId = Integer.parseInt( strId );
251 
252         if ( CollectionUtils.isNotEmpty( BlogHome.getBlogByTag( nId ) ) )
253         {
254 
255             String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_TAG_IS_AFFECTED, AdminMessage.TYPE_STOP );
256             return redirect( request, strMessageUrl );
257         }
258         if ( RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, strId, Tag.PERMISSION_DELETE, AdminUserService.getAdminUser( request ) ) )
259         {
260             TagHome.remove( nId );
261 
262             addInfo( INFO_TAG_REMOVED, getLocale( ) );
263 
264         }
265         return redirectView( request, VIEW_MANAGE_TAGS );
266     }
267 
268     /**
269      * Returns the form to update info about a tag
270      *
271      * @param request
272      *            The Http request
273      * @return The HTML form to update info
274      * @throws AccessDeniedException
275      */
276     @View( VIEW_MODIFY_TAG )
277     public String getModifyTag( HttpServletRequest request ) throws AccessDeniedException
278     {
279         String strId = request.getParameter( PARAMETER_ID_TAG );
280         int nId = Integer.parseInt( strId );
281 
282         if ( !RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, strId, Tag.PERMISSION_MODIFY, getUser( ) ) )
283         {
284             throw new AccessDeniedException( UNAUTHORIZED );
285         }
286         if ( _tag == null || ( _tag.getIdTag( ) != nId ) )
287         {
288 
289             _tag = TagHome.findByPrimaryKey( nId );
290 
291         }
292 
293         Map<String, Object> model = getModel( );
294         model.put( MARK_TAG, _tag );
295         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
296 
297         return getPage( PROPERTY_PAGE_TITLE_MODIFY_TAGS, TEMPLATE_MODIFY_TAG, model );
298     }
299 
300     /**
301      * Process the change form of a tag
302      *
303      * @param request
304      *            The Http request
305      * @return The Jsp URL of the process result
306      */
307     @Action( ACTION_MODIFY_TAG )
308     public String doModifyTag( HttpServletRequest request )
309     {
310 
311         _tag = ( _tag != null ) ? _tag : new Tag( );
312         populate( _tag, request );
313         if ( RBACService.isAuthorized( Tag.PROPERTY_RESOURCE_TYPE, String.valueOf( _tag.getIdTag( ) ), Tag.PERMISSION_MODIFY,
314                 AdminUserService.getAdminUser( request ) ) )
315         {
316             // Check constraints
317             if ( !validateBean( _tag, VALIDATION_ATTRIBUTES_PREFIX ) )
318             {
319                 return redirect( request, VIEW_MODIFY_TAG, PARAMETER_ID_TAG, _tag.getIdTag( ) );
320             }
321 
322             if ( TagHome.findByName( _tag.getName( ) ) == null )
323             {
324 
325                 TagHome.update( _tag );
326                 addInfo( INFO_TAG_UPDATED, getLocale( ) );
327 
328             }
329         }
330         return redirectView( request, VIEW_MANAGE_TAGS );
331     }
332 
333 }