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.plugins.directory.business;
35
36 import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
37 import fr.paris.lutece.plugins.directory.service.DirectoryService;
38 import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
39 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
40 import fr.paris.lutece.portal.service.i18n.I18nService;
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.plugin.Plugin;
44 import fr.paris.lutece.portal.service.plugin.PluginService;
45 import fr.paris.lutece.portal.service.security.LuteceUser;
46 import fr.paris.lutece.portal.service.security.SecurityService;
47 import fr.paris.lutece.portal.service.template.AppTemplateService;
48 import fr.paris.lutece.util.ReferenceItem;
49 import fr.paris.lutece.util.ReferenceList;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import org.apache.commons.lang.StringUtils;
53
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Locale;
59 import java.util.Map;
60
61 import javax.servlet.http.HttpServletRequest;
62
63
64
65
66
67
68 public class EntryTypeMyLuteceUser extends Entry
69 {
70
71 private static final int CONSTANT_POSITION_MYLUTECE_USER_LOGIN = 0;
72 private static final String SPACE = " ";
73 private static final String OPEN_BRACKET = "(";
74 private static final String CLOSED_BRACKET = ")";
75 private static final String TWO_POINTS = ":";
76 private static final String COMMA = ",";
77
78
79 private static final String PROPERTY_USER_LOGIN = "directory.viewing_mylutece_user.labelLogin";
80 private static final String PROPERTY_USER_INFO_PREFIX = "portal.security.";
81
82
83 private static final String TEMPLATE_CREATE = "admin/plugins/directory/entrytypemyluteceuser/create_entry_type_mylutece_user.html";
84 private static final String TEMPLATE_MODIFY = "admin/plugins/directory/entrytypemyluteceuser/modify_entry_type_mylutece_user.html";
85 private static final String TEMPLATE_HTML_FRONT_CODE_FORM_ENTRY = "skin/plugins/directory/entrytypemyluteceuser/html_code_form_entry_type_mylutece_user.html";
86 private static final String TEMPLATE_HTML_FRONT_CODE_ENTRY_VALUE = "skin/plugins/directory/entrytypemyluteceuser/html_code_entry_value_type_mylutece_user.html";
87 private static final String TEMPLATE_HTML_FRONT_CODE_FORM_SEARCH_ENTRY = "skin/plugins/directory/entrytypemyluteceuser/html_code_form_search_entry_type_mylutece_user.html";
88 private static final String TEMPLATE_HTML_CODE_FORM_ENTRY = "admin/plugins/directory/entrytypemyluteceuser/html_code_form_entry_type_mylutece_user.html";
89 private static final String TEMPLATE_HTML_CODE_ENTRY_VALUE = "admin/plugins/directory/entrytypemyluteceuser/html_code_entry_value_type_mylutece_user.html";
90 private static final String TEMPLATE_HTML_CODE_FORM_SEARCH_ENTRY = "admin/plugins/directory/entrytypemyluteceuser/html_code_form_search_entry_type_mylutece_user.html";
91
92
93
94
95
96 @Override
97 public String getTemplateCreate( )
98 {
99 return TEMPLATE_CREATE;
100 }
101
102
103
104
105
106 @Override
107 public String getTemplateModify( )
108 {
109 return TEMPLATE_MODIFY;
110 }
111
112
113
114
115
116 @Override
117 public String getTemplateHtmlFormEntry( boolean isDisplayFront )
118 {
119 if ( isDisplayFront )
120 {
121 return TEMPLATE_HTML_FRONT_CODE_FORM_ENTRY;
122 }
123
124 return TEMPLATE_HTML_CODE_FORM_ENTRY;
125 }
126
127
128
129
130
131 @Override
132 public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
133 {
134 if ( isDisplayFront )
135 {
136 return TEMPLATE_HTML_FRONT_CODE_ENTRY_VALUE;
137 }
138
139 return TEMPLATE_HTML_CODE_ENTRY_VALUE;
140 }
141
142
143
144
145
146 @Override
147 public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
148 {
149 if ( isDisplayFront )
150 {
151 return TEMPLATE_HTML_FRONT_CODE_FORM_SEARCH_ENTRY;
152 }
153
154 return TEMPLATE_HTML_CODE_FORM_SEARCH_ENTRY;
155 }
156
157
158
159
160
161 @Override
162 public String getEntryData( HttpServletRequest request, Locale locale )
163 {
164 String strTitle = request.getParameter( PARAMETER_TITLE );
165 String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
166 String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
167 .trim( ) : null;
168 String strComment = request.getParameter( PARAMETER_COMMENT );
169 String strMandatory = request.getParameter( PARAMETER_MANDATORY );
170 String strIndexed = request.getParameter( PARAMETER_INDEXED );
171 String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
172 String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
173 String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
174 String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
175 String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
176 String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
177 String strIsAllSearch = request.getParameter( PARAMETER_IS_ADD_VALUE_SEARCH_ALL );
178 String strLabelValueAllSearch = request.getParameter( PARAMETER_LABEL_VALUE_SEARCH_ALL );
179
180
181 String strShowFullInfo = request.getParameter( PARAMETER_SHOW_ALL_INFO );
182
183 String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
184 String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
185
186 String strFieldError = DirectoryUtils.EMPTY_STRING;
187
188 if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
189 {
190 strFieldError = FIELD_TITLE;
191 }
192
193 if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
194 {
195 Object [ ] tabRequiredFields = {
196 I18nService.getLocalizedString( strFieldError, locale )
197 };
198
199 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
200 }
201
202 this.setTitle( strTitle );
203 this.setHelpMessage( strHelpMessage );
204 this.setHelpMessageSearch( strHelpMessageSearch );
205 this.setComment( strComment );
206 this.setMandatory( strMandatory != null );
207 this.setIndexed( strIndexed != null );
208 this.setIndexedAsTitle( strIndexedAsTitle != null );
209 this.setIndexedAsSummary( strIndexedAsSummary != null );
210 this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
211 this.setShownInResultList( strShowInResultList != null );
212 this.setShownInResultRecord( strShowInResultRecord != null );
213 this.setFieldInLine( false );
214 this.setShownInHistory( strShowInHistory != null );
215 this.setShownInExport( strShowInExport != null );
216 this.setShownInCompleteness( strShowInCompleteness != null );
217 this.setAddValueAllSearch( strIsAllSearch != null );
218
219 if ( strIsAllSearch != null )
220 {
221 this.setLabelValueAllSearch( strLabelValueAllSearch );
222 }
223 else
224 {
225 this.setLabelValueAllSearch( null );
226 }
227
228 if ( ( this.getFields( ) == null ) || ( this.getFields( ).size( ) == 0 ) )
229 {
230 List<Field> listFields = new ArrayList<Field>( );
231 Field field = new Field( );
232 field.setEntry( this );
233 listFields.add( field );
234 this.setFields( listFields );
235 }
236
237
238
239 this.getFields( ).get( 0 ).setDefaultValue( strShowFullInfo != null );
240
241 return null;
242 }
243
244
245
246
247
248 @Override
249 public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
250 Locale locale ) throws DirectoryErrorException
251 {
252 if ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) )
253 {
254 String strUserLogin = lstValue.get( CONSTANT_POSITION_MYLUTECE_USER_LOGIN );
255 LuteceUser user = SecurityService.getInstance( ).getUser( strUserLogin );
256
257 if ( user != null )
258 {
259 RecordField recordField = new RecordField( );
260 recordField.setEntry( this );
261 recordField.setValue( strUserLogin );
262 listRecordField.add( recordField );
263 }
264 }
265 }
266
267
268
269
270
271
272 public ReferenceList getMyLuteceUsers( )
273 {
274 ReferenceList listMyLuteceUsers = new ReferenceList( );
275 Collection<LuteceUser> listUsers = SecurityService.getInstance( ).getUsers( );
276
277 for ( LuteceUser user : listUsers )
278 {
279 StringBuilder sbUser = new StringBuilder( );
280 String strFamilyName = user.getUserInfo( LuteceUser.NAME_FAMILY );
281 String strGivenName = user.getUserInfo( LuteceUser.NAME_GIVEN );
282
283 if ( ( strFamilyName != null ) && !strFamilyName.equals( DirectoryUtils.EMPTY_STRING ) )
284 {
285 sbUser.append( strFamilyName + SPACE );
286 }
287
288 if ( ( strGivenName != null ) && !strGivenName.equals( DirectoryUtils.EMPTY_STRING ) )
289 {
290 sbUser.append( strGivenName + SPACE );
291 }
292
293 sbUser.append( OPEN_BRACKET + user.getName( ) + CLOSED_BRACKET );
294 listMyLuteceUsers.addItem( user.getName( ), sbUser.toString( ) );
295 }
296
297 return listMyLuteceUsers;
298 }
299
300
301
302
303 @Override
304 public String getHtmlRecordFieldValue( Locale locale, RecordField recordField, boolean isDisplayFront )
305 {
306 if ( getTemplateHtmlRecordFieldValue( isDisplayFront ) != null )
307 {
308 Map<String, Object> model = new HashMap<String, Object>( );
309 model.put( MARK_ENTRY, this );
310 model.put( MARK_RECORD_FIELD, recordField );
311 model.put( MARK_LOCALE, locale );
312
313 if ( showAllInfo( ) )
314 {
315 int nIdRecord = recordField.getRecord( ).getIdRecord( );
316 String strUserGuid = DirectoryService.getInstance( ).getUserGuid( nIdRecord, getIdEntry( ) );
317 ReferenceList listUserInfos = DirectoryService.getInstance( ).getUserInfos( strUserGuid, getIdEntry( ) );
318
319 model.put( MARK_MYLUTECE_USER_INFOS_LIST, listUserInfos );
320 model.put( MARK_MYLUTECE_USER_LOGIN, strUserGuid );
321 }
322
323 HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlRecordFieldValue( isDisplayFront ), locale, model );
324
325 return template.getHtml( );
326 }
327
328 return null;
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343 @Override
344 public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bExportDirectory )
345 {
346 String strValue = StringUtils.EMPTY;
347
348 if ( ( recordField != null ) && StringUtils.isNotBlank( recordField.getValue( ) ) && ( recordField.getRecord( ) != null ) )
349 {
350 if ( bExportDirectory )
351 {
352 int nIdRecord = recordField.getRecord( ).getIdRecord( );
353 String strUserGuid = DirectoryService.getInstance( ).getUserGuid( nIdRecord, getIdEntry( ) );
354 ReferenceList listUserInfos = DirectoryService.getInstance( ).getUserInfos( strUserGuid, getIdEntry( ) );
355
356 if ( showAllInfo( ) && StringUtils.isNotBlank( strUserGuid ) && ( listUserInfos != null ) )
357 {
358 StringBuilder sbValue = new StringBuilder( );
359 sbValue.append( I18nService.getLocalizedString( PROPERTY_USER_LOGIN, locale ) + SPACE + TWO_POINTS + SPACE + strUserGuid );
360
361 for ( ReferenceItem userInfo : listUserInfos )
362 {
363 sbValue.append( COMMA );
364 sbValue.append( I18nService.getLocalizedString( PROPERTY_USER_INFO_PREFIX + userInfo.getCode( ), locale ) );
365 sbValue.append( SPACE + TWO_POINTS + SPACE );
366 sbValue.append( userInfo.getName( ) );
367 }
368
369 strValue = sbValue.toString( );
370 }
371 }
372
373 if ( StringUtils.isBlank( strValue ) )
374 {
375 strValue = recordField.getValue( );
376 }
377 }
378
379 return strValue;
380 }
381
382
383
384
385
386
387
388
389
390
391
392 @Override
393 public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
394 {
395 return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
396 }
397
398
399
400
401 @Override
402 public boolean isSortable( )
403 {
404 return true;
405 }
406
407
408
409
410
411
412 private boolean showAllInfo( )
413 {
414 boolean bShow = false;
415 List<Field> listFields = getFields( );
416
417 if ( ( listFields == null ) || ( listFields.size( ) == 0 ) )
418 {
419 Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
420 listFields = FieldHome.getFieldListByIdEntry( getIdEntry( ), plugin );
421 }
422
423 if ( listFields != null )
424 {
425 Field field = listFields.get( 0 );
426
427 if ( field != null )
428 {
429 bShow = field.isDefaultValue( );
430 }
431 }
432
433 return bShow;
434 }
435
436
437
438
439
440
441 @Override
442 public boolean isAnonymizable( )
443 {
444 return true;
445 }
446 }