1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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
138
139
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
154
155
156
157 public boolean getUpdateExistingUsers( )
158 {
159 return _bUpdateExistingUsers;
160 }
161
162
163
164
165
166
167
168 public void setUpdateExistingUsers( boolean bUpdateExistingUsers )
169 {
170 _bUpdateExistingUsers = bUpdateExistingUsers;
171 }
172
173
174
175
176
177
178 public abstract String getImportFromFileTemplate( );
179
180
181
182
183
184
185 public abstract int getNbMinColumns( );
186
187
188
189
190
191
192
193
194 public abstract String getAccessCode( String [ ] strLineDataArray );
195
196
197
198
199
200
201
202
203 public abstract String getEmail( String [ ] strLineDataArray );
204
205
206
207
208
209 @Override
210 protected abstract List<CSVMessageDescriptor> readLineOfCSVFile( String [ ] strLineDataArray, int nLineNumber, Locale locale, String strBaseUrl );
211
212 }