View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.web.features;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.portal.business.right.Level;
42  import fr.paris.lutece.portal.business.right.LevelHome;
43  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.service.security.SecurityTokenService;
47  import fr.paris.lutece.portal.service.template.AppTemplateService;
48  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
49  import fr.paris.lutece.portal.web.constants.Messages;
50  import fr.paris.lutece.portal.web.constants.Parameters;
51  import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  
54  /**
55   * This class provides the user interface to manage levels features ( manage, create, modify )
56   */
57  public class LevelsJspBean extends AdminFeaturesPageJspBean
58  {
59      private static final long serialVersionUID = 5513182604869973362L;
60  
61      // Right
62      public static final String RIGHT_MANAGE_LEVELS = "CORE_LEVEL_RIGHT_MANAGEMENT";
63  
64      // Properties for page titles
65      private static final String PROPERTY_PAGE_TITLE_CREATE_LEVEL = "portal.features.create_level.pageTitle";
66      private static final String PROPERTY_PAGE_TITLE_MODIFY_LEVEL = "portal.features.modify_level.pageTitle";
67  
68      // Markers
69      private static final String MARK_LEVEL = "level";
70  
71      // Templates files path
72      private static final String TEMPLATE_CREATE_LEVEL = "admin/features/create_level.html";
73      private static final String TEMPLATE_MODIFY_LEVEL = "admin/features/modify_level.html";
74  
75      private static final String ANCHOR_RIGHT_LEVELS = "right_levels";
76  
77      /**
78       * Returns the level form of creation
79       *
80       * @param request
81       *            The Http request
82       * @return the html code of the level
83       */
84      public String getCreateLevel( HttpServletRequest request )
85      {
86          setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_LEVEL );
87  
88          Map<String, Object> model = new HashMap<>( );
89          model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_LEVEL ) );
90          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_LEVEL, getLocale( ), model );
91  
92          return getAdminPage( template.getHtml( ) );
93      }
94  
95      /**
96       * Processes the creation form of a new level by recovering the parameters in the http request
97       *
98       * @param request
99       *            the http request
100      * @return The Jsp URL of the process result
101      * @throws AccessDeniedException
102      *             if the security token is invalid
103      */
104     public String doCreateLevel( HttpServletRequest request ) throws AccessDeniedException
105     {
106         String strName = request.getParameter( Parameters.LEVEL_NAME );
107 
108         // Mandatory fields
109         if ( strName.equals( "" ) )
110         {
111             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
112         }
113         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_LEVEL ) )
114         {
115             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
116         }
117         Levelal/business/right/Level.html#Level">Level level = new Level( );
118         level.setName( strName );
119         LevelHome.create( level );
120 
121         // If the process is successfull, redirects towards the theme view
122         return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
123     }
124 
125     /**
126      * Returns the level form of update
127      *
128      * @param request
129      *            The Http request
130      * @return the html code of the level form
131      */
132     public String getModifyLevel( HttpServletRequest request )
133     {
134         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_LEVEL );
135 
136         String strId = request.getParameter( Parameters.LEVEL_ID );
137 
138         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );
139 
140         if ( level == null )
141         {
142             return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
143         }
144 
145         HashMap<String, Object> model = new HashMap<>( );
146         model.put( MARK_LEVEL, level );
147         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_LEVEL ) );
148 
149         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_LEVEL, getLocale( ), model );
150 
151         return getAdminPage( template.getHtml( ) );
152     }
153 
154     /**
155      * Processes the updating form of a level whose new parameters are stored in the http request
156      *
157      * @param request
158      *            The http request
159      * @return The Jsp URL of the process result
160      * @throws AccessDeniedException
161      *             if the security token is invalid
162      */
163     public String doModifyLevel( HttpServletRequest request ) throws AccessDeniedException
164     {
165         String strId = request.getParameter( Parameters.LEVEL_ID );
166         String strName = request.getParameter( Parameters.LEVEL_NAME );
167 
168         // Mandatory fields
169         if ( strName.equals( "" ) )
170         {
171             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
172         }
173         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_LEVEL ) )
174         {
175             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
176         }
177 
178         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );
179         level.setName( strName );
180         LevelHome.update( level );
181 
182         // If the process is successfull, redirects towards the level management page
183         return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
184     }
185 }