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.service.admin;
35  
36  import fr.paris.lutece.portal.business.user.AdminUserHome;
37  import fr.paris.lutece.portal.service.csv.CSVMessageDescriptor;
38  import fr.paris.lutece.portal.service.csv.CSVMessageLevel;
39  import fr.paris.lutece.portal.service.csv.CSVReaderService;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Locale;
46  
47  /**
48   * Class to import Admin Users from CSV files.
49   */
50  public abstract class ImportAdminUserService extends CSVReaderService
51  {
52      private static final String MESSAGE_ACCESS_CODE_ALREADY_USED = "portal.users.message.user.accessCodeAlreadyUsed";
53      private static final String MESSAGE_EMAIL_ALREADY_USED = "portal.users.message.user.accessEmailUsed";
54      private static final String MESSAGE_ERROR_MIN_NUMBER_COLUMNS = "portal.users.import_users_from_file.messageErrorMinColumnNumber";
55      private static final String PROPERTY_IMPORT_EXPORT_USER_SEPARATOR = "lutece.importExportUser.defaultSeparator";
56      private static final String CONSTANT_DEFAULT_IMPORT_EXPORT_USER_SEPARATOR = ":";
57      private static final String MESSAGE_USERS_IMPORTED = "portal.users.import_users_from_file.usersImported";
58  
59      private Character _strAttributesSeparator;
60      private boolean _bUpdateExistingUsers;
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      protected List<CSVMessageDescriptor> checkLineOfCSVFile( String [ ] strLineDataArray, int nLineNumber, Locale locale )
67      {
68          List<CSVMessageDescriptor> listMessages = new ArrayList<>( );
69  
70          int nbMinColumns = getNbMinColumns( );
71          if ( ( strLineDataArray == null ) || ( strLineDataArray.length < nbMinColumns ) )
72          {
73              int nNbCol;
74  
75              if ( strLineDataArray == null )
76              {
77                  nNbCol = 0;
78              }
79              else
80              {
81                  nNbCol = strLineDataArray.length;
82              }
83  
84              Object [ ] args = {
85                      nNbCol, nbMinColumns
86              };
87              String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_MIN_NUMBER_COLUMNS, args, locale );
88              CSVMessageDescriptorCSVMessageDescriptor.html#CSVMessageDescriptor">CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strErrorMessage );
89              listMessages.add( error );
90  
91              return listMessages;
92          }
93  
94          if ( !getUpdateExistingUsers( ) )
95          {
96              String strAccessCode = getAccessCode( strLineDataArray );
97              String strEmail = getEmail( strLineDataArray );
98  
99              if ( AdminUserHome.checkAccessCodeAlreadyInUse( strAccessCode ) > 0 )
100             {
101                 String strMessage = I18nService.getLocalizedString( MESSAGE_ACCESS_CODE_ALREADY_USED, locale );
102                 CSVMessageDescriptorCSVMessageDescriptor.html#CSVMessageDescriptor">CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strMessage );
103                 listMessages.add( error );
104             }
105             else
106             {
107                 if ( AdminUserHome.checkEmailAlreadyInUse( strEmail ) > 0 )
108                 {
109                     String strMessage = I18nService.getLocalizedString( MESSAGE_EMAIL_ALREADY_USED, locale );
110                     CSVMessageDescriptorCSVMessageDescriptor.html#CSVMessageDescriptor">CSVMessageDescriptor error = new CSVMessageDescriptor( CSVMessageLevel.ERROR, nLineNumber, strMessage );
111                     listMessages.add( error );
112                 }
113             }
114         }
115 
116         return listMessages;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     protected List<CSVMessageDescriptor> getEndOfProcessMessages( int nNbLineParses, int nNbLinesWithoutErrors, Locale locale )
124     {
125         List<CSVMessageDescriptor> listMessages = new ArrayList<>( );
126         Object [ ] args = {
127                 nNbLineParses, nNbLinesWithoutErrors
128         };
129         String strMessageContent = I18nService.getLocalizedString( MESSAGE_USERS_IMPORTED, args, locale );
130         CSVMessageDescriptorVMessageDescriptor.html#CSVMessageDescriptor">CSVMessageDescriptor message = new CSVMessageDescriptor( CSVMessageLevel.INFO, 0, strMessageContent );
131         listMessages.add( message );
132 
133         return listMessages;
134     }
135 
136     /**
137      * Get the separator used for attributes of admin users.
138      * 
139      * @return The separator
140      */
141     public Character getAttributesSeparator( )
142     {
143         if ( _strAttributesSeparator == null )
144         {
145             _strAttributesSeparator = AppPropertiesService.getProperty( PROPERTY_IMPORT_EXPORT_USER_SEPARATOR, CONSTANT_DEFAULT_IMPORT_EXPORT_USER_SEPARATOR )
146                     .charAt( 0 );
147         }
148 
149         return _strAttributesSeparator;
150     }
151 
152     /**
153      * Get the update users flag
154      * 
155      * @return True if existing users should be updated, false if they should be ignored.
156      */
157     public boolean getUpdateExistingUsers( )
158     {
159         return _bUpdateExistingUsers;
160     }
161 
162     /**
163      * Set the update users flag
164      * 
165      * @param bUpdateExistingUsers
166      *            True if existing users should be updated, false if they should be ignored.
167      */
168     public void setUpdateExistingUsers( boolean bUpdateExistingUsers )
169     {
170         _bUpdateExistingUsers = bUpdateExistingUsers;
171     }
172 
173     /**
174      * Get the HTML template for importing users from file
175      * 
176      * @return the template path
177      */
178     public abstract String getImportFromFileTemplate( );
179 
180     /**
181      * Get the number min of CSV columns
182      * 
183      * @return the min number of columns
184      */
185     public abstract int getNbMinColumns( );
186 
187     /**
188      * Get the access code from data array
189      * 
190      * @param strLineDataArray
191      *            the data array
192      * @return the access code
193      */
194     public abstract String getAccessCode( String [ ] strLineDataArray );
195 
196     /**
197      * Get the email from CSV columns
198      * 
199      * @param strLineDataArray
200      *            the data array
201      * @return the email
202      */
203     public abstract String getEmail( String [ ] strLineDataArray );
204 
205     /**
206      * {@inheritDoc}
207      */
208     // Reexport this method for visibility in this package
209     @Override
210     protected abstract List<CSVMessageDescriptor> readLineOfCSVFile( String [ ] strLineDataArray, int nLineNumber, Locale locale, String strBaseUrl );
211 
212 }