ImportAdminUserService.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.admin;

  35. import fr.paris.lutece.portal.business.user.AdminUserHome;
  36. import fr.paris.lutece.portal.service.csv.CSVMessageDescriptor;
  37. import fr.paris.lutece.portal.service.csv.CSVMessageLevel;
  38. import fr.paris.lutece.portal.service.csv.CSVReaderService;
  39. import fr.paris.lutece.portal.service.i18n.I18nService;
  40. import fr.paris.lutece.portal.service.util.AppPropertiesService;

  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.Locale;

  44. /**
  45.  * Class to import Admin Users from CSV files.
  46.  */
  47. public abstract class ImportAdminUserService extends CSVReaderService
  48. {
  49.     private static final String MESSAGE_ACCESS_CODE_ALREADY_USED = "portal.users.message.user.accessCodeAlreadyUsed";
  50.     private static final String MESSAGE_EMAIL_ALREADY_USED = "portal.users.message.user.accessEmailUsed";
  51.     private static final String MESSAGE_ERROR_MIN_NUMBER_COLUMNS = "portal.users.import_users_from_file.messageErrorMinColumnNumber";
  52.     private static final String PROPERTY_IMPORT_EXPORT_USER_SEPARATOR = "lutece.importExportUser.defaultSeparator";
  53.     private static final String CONSTANT_DEFAULT_IMPORT_EXPORT_USER_SEPARATOR = ":";
  54.     private static final String MESSAGE_USERS_IMPORTED = "portal.users.import_users_from_file.usersImported";

  55.     private Character _strAttributesSeparator;
  56.     private boolean _bUpdateExistingUsers;

  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     @Override
  61.     protected List<CSVMessageDescriptor> checkLineOfCSVFile( String [ ] strLineDataArray, int nLineNumber, Locale locale )
  62.     {
  63.         List<CSVMessageDescriptor> listMessages = new ArrayList<>( );

  64.         int nbMinColumns = getNbMinColumns( );
  65.         if ( ( strLineDataArray == null ) || ( strLineDataArray.length < nbMinColumns ) )
  66.         {
  67.             int nNbCol;

  68.             if ( strLineDataArray == null )
  69.             {
  70.                 nNbCol = 0;
  71.             }
  72.             else
  73.             {
  74.                 nNbCol = strLineDataArray.length;
  75.             }

  76.             Object [ ] args = {
  77.                     nNbCol, nbMinColumns
  78.             };
  79.             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_MIN_NUMBER_COLUMNS, args, locale );
  80.             CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strErrorMessage );
  81.             listMessages.add( error );

  82.             return listMessages;
  83.         }

  84.         if ( !getUpdateExistingUsers( ) )
  85.         {
  86.             String strAccessCode = getAccessCode( strLineDataArray );
  87.             String strEmail = getEmail( strLineDataArray );

  88.             if ( AdminUserHome.checkAccessCodeAlreadyInUse( strAccessCode ) > 0 )
  89.             {
  90.                 String strMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_CODE_ALREADY_USED, locale );
  91.                 CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strMessage );
  92.                 listMessages.add( error );
  93.             }
  94.             else
  95.             {
  96.                 if ( AdminUserHome.checkEmailAlreadyInUse( strEmail ) > 0 )
  97.                 {
  98.                     String strMessage = I18nService.getLocalizedString( MESSAGE_EMAIL_ALREADY_USED, locale );
  99.                     CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strMessage );
  100.                     listMessages.add( error );
  101.                 }
  102.             }
  103.         }

  104.         return listMessages;
  105.     }

  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     @Override
  110.     protected List<CSVMessageDescriptor> getEndOfProcessMessages( int nNbLineParses, int nNbLinesWithoutErrors, Locale locale )
  111.     {
  112.         List<CSVMessageDescriptor> listMessages = new ArrayList<>( );
  113.         Object [ ] args = {
  114.                 nNbLineParses, nNbLinesWithoutErrors
  115.         };
  116.         String strMessageContent = I18nService.getLocalizedString( MESSAGE_USERS_IMPORTED, args, locale );
  117.         CSVMessageDescriptor message = new CSVMessageDescriptor( CSVMessageLevel.INFO, 0, strMessageContent );
  118.         listMessages.add( message );

  119.         return listMessages;
  120.     }

  121.     /**
  122.      * Get the separator used for attributes of admin users.
  123.      *
  124.      * @return The separator
  125.      */
  126.     public Character getAttributesSeparator( )
  127.     {
  128.         if ( _strAttributesSeparator == null )
  129.         {
  130.             _strAttributesSeparator = AppPropertiesService.getProperty( PROPERTY_IMPORT_EXPORT_USER_SEPARATOR, CONSTANT_DEFAULT_IMPORT_EXPORT_USER_SEPARATOR )
  131.                     .charAt( 0 );
  132.         }

  133.         return _strAttributesSeparator;
  134.     }

  135.     /**
  136.      * Get the update users flag
  137.      *
  138.      * @return True if existing users should be updated, false if they should be ignored.
  139.      */
  140.     public boolean getUpdateExistingUsers( )
  141.     {
  142.         return _bUpdateExistingUsers;
  143.     }

  144.     /**
  145.      * Set the update users flag
  146.      *
  147.      * @param bUpdateExistingUsers
  148.      *            True if existing users should be updated, false if they should be ignored.
  149.      */
  150.     public void setUpdateExistingUsers( boolean bUpdateExistingUsers )
  151.     {
  152.         _bUpdateExistingUsers = bUpdateExistingUsers;
  153.     }

  154.     /**
  155.      * Get the HTML template for importing users from file
  156.      *
  157.      * @return the template path
  158.      */
  159.     public abstract String getImportFromFileTemplate( );

  160.     /**
  161.      * Get the number min of CSV columns
  162.      *
  163.      * @return the min number of columns
  164.      */
  165.     public abstract int getNbMinColumns( );

  166.     /**
  167.      * Get the access code from data array
  168.      *
  169.      * @param strLineDataArray
  170.      *            the data array
  171.      * @return the access code
  172.      */
  173.     public abstract String getAccessCode( String [ ] strLineDataArray );

  174.     /**
  175.      * Get the email from CSV columns
  176.      *
  177.      * @param strLineDataArray
  178.      *            the data array
  179.      * @return the email
  180.      */
  181.     public abstract String getEmail( String [ ] strLineDataArray );

  182.     /**
  183.      * {@inheritDoc}
  184.      */
  185.     // Reexport this method for visibility in this package
  186.     @Override
  187.     protected abstract List<CSVMessageDescriptor> readLineOfCSVFile( String [ ] strLineDataArray, int nLineNumber, Locale locale, String strBaseUrl );

  188. }