AttributeImage.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.business.user.attribute;

  35. import fr.paris.lutece.portal.business.file.File;
  36. import fr.paris.lutece.portal.business.file.FileHome;
  37. import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
  38. import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
  39. import fr.paris.lutece.portal.business.user.AdminUser;
  40. import fr.paris.lutece.portal.service.fileupload.FileUploadService;
  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.user.attribute.AttributeService;
  44. import fr.paris.lutece.portal.service.util.AppLogService;
  45. import fr.paris.lutece.portal.web.constants.Messages;
  46. import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
  47. import fr.paris.lutece.util.filesystem.FileSystemUtil;
  48. import fr.paris.lutece.util.string.StringUtil;

  49. import org.apache.commons.collections.CollectionUtils;
  50. import org.apache.commons.fileupload.FileItem;
  51. import org.apache.commons.lang3.StringUtils;

  52. import java.io.ByteArrayInputStream;
  53. import java.io.IOException;

  54. import java.util.ArrayList;
  55. import java.util.List;
  56. import java.util.Locale;
  57. import java.util.Optional;

  58. import javax.imageio.ImageIO;

  59. import javax.servlet.http.HttpServletRequest;

  60. /**
  61.  *
  62.  * AttributeComboBox
  63.  *
  64.  */
  65. public class AttributeImage extends AbstractAttribute
  66. {
  67.     // Constants
  68.     private static final String EMPTY_STRING = "";
  69.     private static final String CONSTANT_UNDERSCORE = "_";

  70.     // Parameters
  71.     private static final String PARAMETER_TITLE = "title";
  72.     private static final String PARAMETER_HELP_MESSAGE = "help_message";
  73.     private static final String PARAMETER_MANDATORY = "mandatory";
  74.     private static final String PARAMETER_ATTRIBUTE = "attribute";
  75.     private static final String PARAMETER_WIDTH = "width";
  76.     private static final String PARAMETER_HEIGHT = "height";
  77.     private static final String PARAMETER_UPDATE_ATTRIBUTE = "update_attribute";
  78.     private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
  79.     private static final String PARAMETER_ID_USER = "id_user";

  80.     // Properties
  81.     private static final String PROPERTY_TYPE_IMAGE = "portal.users.attribute.type.image";
  82.     private static final String PROPERTY_CREATE_IMAGE_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeImage";
  83.     private static final String PROPERTY_MODIFY_IMAGE_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeImage";
  84.     private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "portal.users.message.noArithmeticalCharacters";

  85.     // Templates
  86.     private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/image/create_attribute_image.html";
  87.     private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/image/modify_attribute_image.html";
  88.     private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/image/html_code_form_attribute_image.html";
  89.     private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/image/html_code_value_attribute_image.html";
  90.     private static final String REGEX_ID = "-?[0-9]+";

  91.     /**
  92.      * Constructor
  93.      */
  94.     public AttributeImage( )
  95.     {
  96.         setAttributeImage( true );
  97.     }

  98.     /**
  99.      * Get the template create an attribute
  100.      *
  101.      * @return The URL of the template
  102.      */
  103.     @Override
  104.     public String getTemplateCreateAttribute( )
  105.     {
  106.         return TEMPLATE_CREATE_ATTRIBUTE;
  107.     }

  108.     /**
  109.      * Get the template modify an attribute
  110.      *
  111.      * @return The URL of the template
  112.      */
  113.     @Override
  114.     public String getTemplateModifyAttribute( )
  115.     {
  116.         return TEMPLATE_MODIFY_ATTRIBUTE;
  117.     }

  118.     /**
  119.      * Get the template html form attribute
  120.      *
  121.      * @return the template
  122.      */
  123.     @Override
  124.     public String getTemplateHtmlFormAttribute( )
  125.     {
  126.         return TEMPLATE_HTML_FORM_ATTRIBUTE;
  127.     }

  128.     /**
  129.      * Get the template html form search attribute
  130.      *
  131.      * @return the template
  132.      */
  133.     @Override
  134.     public String getTemplateHtmlFormSearchAttribute( )
  135.     {
  136.         return EMPTY_STRING;
  137.     }

  138.     /**
  139.      * Get the template html for the value of the attribute
  140.      *
  141.      * @return the template
  142.      */
  143.     @Override
  144.     public String getTemplateHtmlValue( )
  145.     {
  146.         return TEMPLATE_HTML_VALUE;
  147.     }

  148.     /**
  149.      * Get page title for create page
  150.      *
  151.      * @return page title
  152.      */
  153.     @Override
  154.     public String getPropertyCreatePageTitle( )
  155.     {
  156.         return PROPERTY_CREATE_IMAGE_PAGETITLE;
  157.     }

  158.     /**
  159.      * Get page title for modify page
  160.      *
  161.      * @return page title
  162.      */
  163.     @Override
  164.     public String getPropertyModifyPageTitle( )
  165.     {
  166.         return PROPERTY_MODIFY_IMAGE_PAGETITLE;
  167.     }

  168.     /**
  169.      * Set the data of the attribute
  170.      *
  171.      * @param request
  172.      *            HttpServletRequest
  173.      * @return null if there are no errors
  174.      */
  175.     @Override
  176.     public String setAttributeData( HttpServletRequest request )
  177.     {
  178.         String strTitle = request.getParameter( PARAMETER_TITLE );
  179.         String strHelpMessage = Optional.ofNullable( request.getParameter( PARAMETER_HELP_MESSAGE ) ).map( String::trim ).orElse( null );
  180.         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
  181.         String strWidth = request.getParameter( PARAMETER_WIDTH );
  182.         String strHeight = request.getParameter( PARAMETER_HEIGHT );
  183.         String strShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );

  184.         if ( StringUtils.isBlank( strTitle ) )
  185.         {
  186.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  187.         }

  188.         setTitle( strTitle );
  189.         setHelpMessage( strHelpMessage );
  190.         setMandatory( strMandatory != null );
  191.         setShownInResultList( strShownInResultList != null );
  192.         // Never show an image in the search box
  193.         setShownInSearch( false );

  194.         if ( getListAttributeFields( ) == null )
  195.         {
  196.             List<AttributeField> listAttributeFields = new ArrayList<>( );
  197.             AttributeField attributeField = new AttributeField( );
  198.             listAttributeFields.add( attributeField );
  199.             setListAttributeFields( listAttributeFields );
  200.         }

  201.         if ( ( StringUtils.isNotBlank( strWidth ) && !strWidth.matches( REGEX_ID ) )
  202.                 || ( StringUtils.isNotBlank( strHeight ) && !strHeight.matches( REGEX_ID ) ) )
  203.         {
  204.             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
  205.         }

  206.         if ( StringUtils.isNotBlank( strWidth ) && strWidth.matches( REGEX_ID ) )
  207.         {
  208.             int nWidth = Integer.parseInt( strWidth );
  209.             getListAttributeFields( ).get( 0 ).setWidth( nWidth );
  210.         }
  211.         else
  212.         {
  213.             getListAttributeFields( ).get( 0 ).setWidth( -1 );
  214.         }

  215.         if ( StringUtils.isNotBlank( strHeight ) && strHeight.matches( REGEX_ID ) )
  216.         {
  217.             int nHeight = Integer.parseInt( strHeight );
  218.             getListAttributeFields( ).get( 0 ).setHeight( nHeight );
  219.         }
  220.         else
  221.         {
  222.             getListAttributeFields( ).get( 0 ).setHeight( -1 );
  223.         }

  224.         return null;
  225.     }

  226.     /**
  227.      * Set attribute type
  228.      *
  229.      * @param locale
  230.      *            locale
  231.      */
  232.     @Override
  233.     public void setAttributeType( Locale locale )
  234.     {
  235.         AttributeType attributeType = new AttributeType( );
  236.         attributeType.setLocale( locale );
  237.         attributeType.setClassName( this.getClass( ).getName( ) );
  238.         attributeType.setLabelType( PROPERTY_TYPE_IMAGE );
  239.         setAttributeType( attributeType );
  240.     }

  241.     /**
  242.      * Get the data of the user fields
  243.      *
  244.      * @param request
  245.      *            HttpServletRequest
  246.      * @param user
  247.      *            user
  248.      * @return user field data
  249.      */
  250.     @Override
  251.     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
  252.     {
  253.         String strUpdateAttribute = request.getParameter( PARAMETER_UPDATE_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
  254.         List<AdminUserField> listUserFields = new ArrayList<>( );

  255.         try
  256.         {
  257.             if ( StringUtils.isNotBlank( strUpdateAttribute ) )
  258.             {
  259.                 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  260.                 FileItem fileItem = multipartRequest.getFile( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );

  261.                 if ( ( fileItem != null ) && ( StringUtils.isNotEmpty( fileItem.getName( ) ) ) )
  262.                 {
  263.                     File file = new File( );
  264.                     PhysicalFile physicalFile = new PhysicalFile( );
  265.                     physicalFile.setValue( fileItem.get( ) );
  266.                     file.setTitle( FileUploadService.getFileNameOnly( fileItem ) );
  267.                     file.setSize( (int) fileItem.getSize( ) );
  268.                     file.setPhysicalFile( physicalFile );
  269.                     file.setMimeType( FileSystemUtil.getMIMEType( FileUploadService.getFileNameOnly( fileItem ) ) );

  270.                     // verify that the file is an image
  271.                     ImageIO.read( new ByteArrayInputStream( file.getPhysicalFile( ).getValue( ) ) );

  272.                     AdminUserField userField = new AdminUserField( );
  273.                     userField.setUser( user );
  274.                     userField.setAttribute( this );

  275.                     AttributeService.getInstance( ).setAttributeField( this );

  276.                     if ( CollectionUtils.isNotEmpty( getListAttributeFields( ) ) )
  277.                     {
  278.                         userField.setAttributeField( getListAttributeFields( ).get( 0 ) );
  279.                         userField.setFile( file );
  280.                     }

  281.                     listUserFields.add( userField );
  282.                 }
  283.             }
  284.             else
  285.             {
  286.                 AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
  287.                 auFieldFilter.setIdAttribute( getIdAttribute( ) );

  288.                 String strIdUser = request.getParameter( PARAMETER_ID_USER );

  289.                 if ( StringUtils.isNotBlank( strIdUser ) )
  290.                 {
  291.                     auFieldFilter.setIdUser( StringUtil.getIntValue( strIdUser, 0 ) );
  292.                 }

  293.                 listUserFields = AdminUserFieldHome.findByFilter( auFieldFilter );
  294.                 listUserFields.stream( ).filter( a -> a.getFile( ) != null ).forEach( ( AdminUserField userField ) -> {
  295.                     File file = FileHome.findByPrimaryKey( userField.getFile( ).getIdFile( ) );
  296.                     userField.setFile( file );

  297.                     int nIdPhysicalFile = file.getPhysicalFile( ).getIdPhysicalFile( );
  298.                     PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( nIdPhysicalFile );
  299.                     userField.getFile( ).setPhysicalFile( physicalFile );
  300.                 } );
  301.             }
  302.         }
  303.         catch( IOException e )
  304.         {
  305.             AppLogService.error( e.getMessage( ), e );
  306.         }

  307.         return listUserFields;
  308.     }

  309.     /**
  310.      * Get whether the attribute is anonymizable.
  311.      *
  312.      * @return True if the attribute can be anonymized, false otherwise.
  313.      */
  314.     @Override
  315.     public boolean isAnonymizable( )
  316.     {
  317.         return false;
  318.     }
  319. }