AdminUserFieldService.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.service.user.attribute;

  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Locale;
  38. import java.util.Map;
  39. import java.util.Map.Entry;

  40. import javax.servlet.http.HttpServletRequest;

  41. import org.apache.commons.collections.CollectionUtils;
  42. import org.apache.commons.fileupload.FileItem;
  43. import org.apache.commons.lang3.StringUtils;

  44. import fr.paris.lutece.portal.business.user.AdminUser;
  45. import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
  46. import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldFilter;
  47. import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
  48. import fr.paris.lutece.portal.business.user.attribute.IAttribute;
  49. import fr.paris.lutece.portal.service.message.AdminMessage;
  50. import fr.paris.lutece.portal.service.message.AdminMessageService;
  51. import fr.paris.lutece.portal.service.spring.SpringContextService;
  52. import fr.paris.lutece.portal.web.constants.Messages;
  53. import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;

  54. /**
  55.  *
  56.  * AdminUserFieldService
  57.  *
  58.  */
  59. public final class AdminUserFieldService
  60. {
  61.     // CONSTANTS
  62.     private static final String CONSTANT_EMPTY_STRING = "";
  63.     private static final String CONSTANT_UNDERSCORE = "_";

  64.     // PARAMETERS
  65.     private static final String PARAMETER_ATTRIBUTE = "attribute";
  66.     private static final String PARAMETER_UPDATE_ATTRIBUTE = "update_attribute";
  67.     private static final AttributeService _attributeService = AttributeService.getInstance( );

  68.     /**
  69.      * Instantiates a new admin user field service.
  70.      */
  71.     private AdminUserFieldService( )
  72.     {
  73.     }

  74.     /**
  75.      * Check if the user fields are correctly filled
  76.      *
  77.      * @param request
  78.      *            HttpServletRequest
  79.      * @param locale
  80.      *            locale
  81.      * @return null if there are no problem
  82.      */
  83.     public static String checkUserFields( HttpServletRequest request, Locale locale )
  84.     {
  85.         // Specific attributes
  86.         List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( locale );

  87.         for ( IAttribute attribute : listAttributes )
  88.         {
  89.             if ( attribute.isAttributeImage( ) )
  90.             {
  91.                 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  92.                 FileItem fileItem = multipartRequest.getFile( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
  93.                 String strUpdateAttribute = request.getParameter( PARAMETER_UPDATE_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );

  94.                 if ( attribute.isMandatory( ) && ( strUpdateAttribute != null ) && ( ( fileItem == null ) || ( fileItem.getSize( ) == 0 ) ) )
  95.                 {
  96.                     return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 String value = request.getParameter( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );

  102.                 if ( attribute.isMandatory( ) && ( ( value == null ) || value.equals( CONSTANT_EMPTY_STRING ) ) )
  103.                 {
  104.                     return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  105.                 }
  106.             }
  107.         }

  108.         return null;
  109.     }

  110.     /**
  111.      * Create the user fields
  112.      *
  113.      * @param user
  114.      *            Adminuser
  115.      * @param request
  116.      *            HttpServletRequest
  117.      * @param locale
  118.      *            locale
  119.      */
  120.     public static void doCreateUserFields( AdminUser user, HttpServletRequest request, Locale locale )
  121.     {
  122.         // Attributes created in the Back-Office
  123.         List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );

  124.         for ( IAttribute attribute : listAttributes )
  125.         {
  126.             List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );

  127.             for ( AdminUserField userField : listUserFields )
  128.             {
  129.                 if ( userField != null )
  130.                 {
  131.                     AdminUserFieldHome.create( userField );
  132.                 }
  133.             }
  134.         }

  135.         // Attributes associated to the plugins
  136.         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
  137.         {
  138.             adminUserFieldListenerService.doCreateUserFields( user, request, locale );
  139.         }
  140.     }

  141.     /**
  142.      * Modify the user fields
  143.      *
  144.      * @param user
  145.      *            AdminUser
  146.      * @param request
  147.      *            HttpServletRequest
  148.      * @param locale
  149.      *            locale
  150.      * @param currentUser
  151.      *            current user
  152.      */
  153.     public static void doModifyUserFields( AdminUser user, HttpServletRequest request, Locale locale, AdminUser currentUser )
  154.     {
  155.         // Attributes created in the Back-Office
  156.         List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );
  157.         Map<Integer, List<AdminUserField>> map = new HashMap<>( );

  158.         for ( IAttribute attribute : listAttributes )
  159.         {
  160.             List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );

  161.             map.put( attribute.getIdAttribute( ), listUserFields );
  162.         }

  163.         // Remove all user fields
  164.         AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
  165.         auFieldFilter.setIdUser( user.getUserId( ) );
  166.         AdminUserFieldHome.removeByFilter( auFieldFilter );

  167.         for ( Entry<Integer, List<AdminUserField>> entry : map.entrySet( ) )
  168.         {
  169.             for ( AdminUserField userField : entry.getValue( ) )
  170.             {
  171.                 if ( userField != null )
  172.                 {
  173.                     AdminUserFieldHome.create( userField );
  174.                 }
  175.             }
  176.         }

  177.         // Attributes associated to the plugins
  178.         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
  179.         {
  180.             adminUserFieldListenerService.doModifyUserFields( user, request, locale, currentUser );
  181.         }
  182.     }

  183.     /**
  184.      * Remove the user fields
  185.      *
  186.      * @param user
  187.      *            Adminuser
  188.      * @param request
  189.      *            HttpServletRequest
  190.      * @param locale
  191.      *            locale
  192.      */
  193.     public static void doRemoveUserFields( AdminUser user, HttpServletRequest request, Locale locale )
  194.     {
  195.         AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
  196.         auFieldFilter.setIdUser( user.getUserId( ) );
  197.         AdminUserFieldHome.removeByFilter( auFieldFilter );

  198.         // Attributes associated to the plugins
  199.         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
  200.         {
  201.             adminUserFieldListenerService.doRemoveUserFields( user, request, locale );
  202.         }
  203.     }

  204.     /**
  205.      * Remove the user fields from a given ID attribute
  206.      *
  207.      * @param nIdAttribute
  208.      *            the ID attribute
  209.      */
  210.     public static void doRemoveUserFieldsByIdAttribute( int nIdAttribute )
  211.     {
  212.         AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
  213.         auFieldFilter.setIdAttribute( nIdAttribute );
  214.         AdminUserFieldHome.removeByFilter( auFieldFilter );
  215.     }

  216.     /**
  217.      * Remove the user fields from a given ID attribute field
  218.      *
  219.      * @param nIdAttributeField
  220.      *            the attribute field ID
  221.      */
  222.     public static void doRemoveUserFieldsByIdField( int nIdAttributeField )
  223.     {
  224.         AdminUserFieldHome.removeUserFieldsFromIdField( nIdAttributeField );
  225.     }

  226.     /**
  227.      * Get the user attribute fields
  228.      *
  229.      * @param nUserId
  230.      *            the user ID
  231.      * @param locale
  232.      *            the {@link Locale}
  233.      * @return a Map of (ID Attribute, Object). The object could be either a File or a list of {@link AdminUserField}
  234.      */
  235.     public static Map<String, Object> getAdminUserFields( int nUserId, Locale locale )
  236.     {
  237.         List<IAttribute> listAttributes = _attributeService.getAllAttributesWithFields( locale );

  238.         return getAdminUserFields( listAttributes, nUserId, locale );
  239.     }

  240.     /**
  241.      * Get the user attribute fields
  242.      *
  243.      * @param listAttributes
  244.      *            the list of attributes
  245.      * @param nUserId
  246.      *            the user ID
  247.      * @param locale
  248.      *            the {@link Locale}
  249.      * @return a Map of (ID Attribute, Object). The object could be either a File or a list of {@link AdminUserField}
  250.      */
  251.     public static Map<String, Object> getAdminUserFields( List<IAttribute> listAttributes, int nUserId, Locale locale )
  252.     {
  253.         Map<String, Object> map = new HashMap<>( );

  254.         for ( IAttribute attribute : listAttributes )
  255.         {
  256.             List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );

  257.             if ( attribute.isAttributeImage( ) )
  258.             {
  259.                 if ( CollectionUtils.isNotEmpty( listUserFields ) )
  260.                 {
  261.                     AdminUserField userField = listUserFields.get( 0 );

  262.                     if ( userField.getFile( ) != null )
  263.                     {
  264.                         map.put( String.valueOf( attribute.getIdAttribute( ) ), userField.getFile( ) );
  265.                     }
  266.                 }
  267.             }
  268.             else
  269.             {
  270.                 if ( CollectionUtils.isEmpty( listUserFields ) )
  271.                 {
  272.                     AdminUserField userField = new AdminUserField( );
  273.                     userField.setValue( StringUtils.EMPTY );
  274.                     listUserFields.add( userField );
  275.                 }

  276.                 map.put( String.valueOf( attribute.getIdAttribute( ) ), listUserFields );
  277.             }
  278.         }

  279.         return map;
  280.     }

  281.     /**
  282.      * Get the admin user field from a given attribute and a given user ID
  283.      *
  284.      * @param attribute
  285.      *            a {@link IAttribute}
  286.      * @param nUserId
  287.      *            the user ID
  288.      * @param locale
  289.      *            the {@link Locale}
  290.      * @return either a File (if the attribute is a type img) or a list of {@link AdminUserField}
  291.      */
  292.     public Object getAdminUserField( IAttribute attribute, int nUserId, Locale locale )
  293.     {
  294.         List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );

  295.         if ( attribute.isAttributeImage( ) )
  296.         {
  297.             if ( CollectionUtils.isNotEmpty( listUserFields ) )
  298.             {
  299.                 AdminUserField userField = listUserFields.get( 0 );

  300.                 if ( userField.getFile( ) != null )
  301.                 {
  302.                     return userField.getFile( );
  303.                 }
  304.             }
  305.         }
  306.         else
  307.         {
  308.             if ( CollectionUtils.isEmpty( listUserFields ) )
  309.             {
  310.                 AdminUserField userField = new AdminUserField( );
  311.                 userField.setValue( StringUtils.EMPTY );
  312.                 listUserFields.add( userField );
  313.             }

  314.             return listUserFields;
  315.         }

  316.         return null;
  317.     }
  318. }