View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.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   * class EntryTypeMyLuteceUser
66   *
67   */
68  public class EntryTypeMyLuteceUser extends Entry
69  {
70      // CONSTANTS
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      // PROPERTIES
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      // TEMPLATES
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       * {@inheritDoc}
95       */
96      @Override
97      public String getTemplateCreate( )
98      {
99          return TEMPLATE_CREATE;
100     }
101 
102     /**
103      *
104      * {@inheritDoc}
105      */
106     @Override
107     public String getTemplateModify( )
108     {
109         return TEMPLATE_MODIFY;
110     }
111 
112     /**
113      *
114      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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         // Check if we have to show every information of the users or not
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         // IMPORTANT !!!
238         // The flag to show every information is stored in the database directory_field.DEFAULT_value
239         this.getFields( ).get( 0 ).setDefaultValue( strShowFullInfo != null );
240 
241         return null;
242     }
243 
244     /**
245      *
246      * {@inheritDoc}
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      * Get all mylutece users
269      * 
270      * @return mylutece users
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      * {@inheritDoc}
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      * Convert the record field value to string
333      * 
334      * @param recordField
335      *            the record field
336      * @param locale
337      *            Locale
338      * @param bDisplayFront
339      *            true if it is displayed in front
340      * @param bExportDirectory
341      *            true if it is exported
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      * Convert the record field value to string
384      * 
385      * @param recordField
386      *            the record field
387      * @param locale
388      *            Locale
389      * @param bDisplayFront
390      *            true if it is displayed in front
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      * {@inheritDoc}
400      */
401     @Override
402     public boolean isSortable( )
403     {
404         return true;
405     }
406 
407     /**
408      * Check if it must show every information of the user or not
409      * 
410      * @return true if it must show every information, false otherwise
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      * Check if entries of this type are anonymizable or not.
438      * 
439      * @return True if the entry type is anonymizable, false otherwise
440      */
441     @Override
442     public boolean isAnonymizable( )
443     {
444         return true;
445     }
446 }