LevelsJspBean.java

  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. import java.util.HashMap;
  36. import java.util.Map;

  37. import javax.servlet.http.HttpServletRequest;

  38. import fr.paris.lutece.portal.business.right.Level;
  39. import fr.paris.lutece.portal.business.right.LevelHome;
  40. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  41. import fr.paris.lutece.portal.service.message.AdminMessage;
  42. import fr.paris.lutece.portal.service.message.AdminMessageService;
  43. import fr.paris.lutece.portal.service.security.SecurityTokenService;
  44. import fr.paris.lutece.portal.service.template.AppTemplateService;
  45. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  46. import fr.paris.lutece.portal.web.constants.Messages;
  47. import fr.paris.lutece.portal.web.constants.Parameters;
  48. import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
  49. import fr.paris.lutece.util.html.HtmlTemplate;

  50. /**
  51.  * This class provides the user interface to manage levels features ( manage, create, modify )
  52.  */
  53. public class LevelsJspBean extends AdminFeaturesPageJspBean
  54. {
  55.     private static final long serialVersionUID = 5513182604869973362L;

  56.     // Right
  57.     public static final String RIGHT_MANAGE_LEVELS = "CORE_LEVEL_RIGHT_MANAGEMENT";

  58.     // Properties for page titles
  59.     private static final String PROPERTY_PAGE_TITLE_CREATE_LEVEL = "portal.features.create_level.pageTitle";
  60.     private static final String PROPERTY_PAGE_TITLE_MODIFY_LEVEL = "portal.features.modify_level.pageTitle";

  61.     // Markers
  62.     private static final String MARK_LEVEL = "level";

  63.     // Templates files path
  64.     private static final String TEMPLATE_CREATE_LEVEL = "admin/features/create_level.html";
  65.     private static final String TEMPLATE_MODIFY_LEVEL = "admin/features/modify_level.html";

  66.     private static final String ANCHOR_RIGHT_LEVELS = "right_levels";

  67.     /**
  68.      * Returns the level form of creation
  69.      *
  70.      * @param request
  71.      *            The Http request
  72.      * @return the html code of the level
  73.      */
  74.     public String getCreateLevel( HttpServletRequest request )
  75.     {
  76.         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_LEVEL );

  77.         Map<String, Object> model = new HashMap<>( );
  78.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_LEVEL ) );
  79.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_LEVEL, getLocale( ), model );

  80.         return getAdminPage( template.getHtml( ) );
  81.     }

  82.     /**
  83.      * Processes the creation form of a new level by recovering the parameters in the http request
  84.      *
  85.      * @param request
  86.      *            the http request
  87.      * @return The Jsp URL of the process result
  88.      * @throws AccessDeniedException
  89.      *             if the security token is invalid
  90.      */
  91.     public String doCreateLevel( HttpServletRequest request ) throws AccessDeniedException
  92.     {
  93.         String strName = request.getParameter( Parameters.LEVEL_NAME );

  94.         // Mandatory fields
  95.         if ( strName.equals( "" ) )
  96.         {
  97.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  98.         }
  99.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_LEVEL ) )
  100.         {
  101.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  102.         }
  103.         Level level = new Level( );
  104.         level.setName( strName );
  105.         LevelHome.create( level );

  106.         // If the process is successfull, redirects towards the theme view
  107.         return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
  108.     }

  109.     /**
  110.      * Returns the level form of update
  111.      *
  112.      * @param request
  113.      *            The Http request
  114.      * @return the html code of the level form
  115.      */
  116.     public String getModifyLevel( HttpServletRequest request )
  117.     {
  118.         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_LEVEL );

  119.         String strId = request.getParameter( Parameters.LEVEL_ID );

  120.         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );

  121.         if ( level == null )
  122.         {
  123.             return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
  124.         }

  125.         HashMap<String, Object> model = new HashMap<>( );
  126.         model.put( MARK_LEVEL, level );
  127.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_LEVEL ) );

  128.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_LEVEL, getLocale( ), model );

  129.         return getAdminPage( template.getHtml( ) );
  130.     }

  131.     /**
  132.      * Processes the updating form of a level whose new parameters are stored in the http request
  133.      *
  134.      * @param request
  135.      *            The http request
  136.      * @return The Jsp URL of the process result
  137.      * @throws AccessDeniedException
  138.      *             if the security token is invalid
  139.      */
  140.     public String doModifyLevel( HttpServletRequest request ) throws AccessDeniedException
  141.     {
  142.         String strId = request.getParameter( Parameters.LEVEL_ID );
  143.         String strName = request.getParameter( Parameters.LEVEL_NAME );

  144.         // Mandatory fields
  145.         if ( strName.equals( "" ) )
  146.         {
  147.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  148.         }
  149.         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_LEVEL ) )
  150.         {
  151.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  152.         }

  153.         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );
  154.         level.setName( strName );
  155.         LevelHome.update( level );

  156.         // If the process is successfull, redirects towards the level management page
  157.         return getAdminDashboardsUrl( request, ANCHOR_RIGHT_LEVELS );
  158.     }
  159. }