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.template.AppTemplateService;
46  import fr.paris.lutece.util.ReferenceItem;
47  import fr.paris.lutece.util.ReferenceList;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import java.util.ArrayList;
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Locale;
56  import java.util.Map;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  /**
61   *
62   * class EntryTypeRemoteMyLuteceUser
63   *
64   */
65  public class EntryTypeRemoteMyLuteceUser extends Entry
66  {
67      // CONSTANTS
68      private static final int CONSTANT_POSITION_MYLUTECE_USER_LOGIN = 0;
69      private static final String SPACE = " ";
70      private static final String TWO_POINTS = ":";
71      private static final String COMMA = ",";
72  
73      // PROPERTIES
74      private static final String PROPERTY_USER_LOGIN = "directory.viewing_mylutece_user.labelLogin";
75      private static final String PROPERTY_USER_INFO_PREFIX = "portal.security.";
76  
77      // TEMPLATES
78      private static final String TEMPLATE_CREATE = "admin/plugins/directory/entrytyperemotemyluteceuser/create_entry_type_remote_mylutece_user.html";
79      private static final String TEMPLATE_MODIFY = "admin/plugins/directory/entrytyperemotemyluteceuser/modify_entry_type_remote_mylutece_user.html";
80      private static final String TEMPLATE_HTML_FRONT_CODE_FORM_ENTRY = "skin/plugins/directory/entrytyperemotemyluteceuser/html_code_form_entry_type_remote_mylutece_user.html";
81      private static final String TEMPLATE_HTML_FRONT_CODE_ENTRY_VALUE = "skin/plugins/directory/entrytyperemotemyluteceuser/html_code_entry_value_type_remote_mylutece_user.html";
82      private static final String TEMPLATE_HTML_FRONT_CODE_FORM_SEARCH_ENTRY = "skin/plugins/directory/entrytyperemotemyluteceuser/html_code_form_search_entry_type_remote_mylutece_user.html";
83      private static final String TEMPLATE_HTML_CODE_FORM_ENTRY = "admin/plugins/directory/entrytyperemotemyluteceuser/html_code_form_entry_type_remote_mylutece_user.html";
84      private static final String TEMPLATE_HTML_CODE_ENTRY_VALUE = "admin/plugins/directory/entrytyperemotemyluteceuser/html_code_entry_value_type_remote_mylutece_user.html";
85      private static final String TEMPLATE_HTML_CODE_FORM_SEARCH_ENTRY = "admin/plugins/directory/entrytyperemotemyluteceuser/html_code_form_search_entry_type_remote_mylutece_user.html";
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public String getTemplateCreate( )
92      {
93          return TEMPLATE_CREATE;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public String getTemplateModify( )
101     {
102         return TEMPLATE_MODIFY;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String getTemplateHtmlFormEntry( boolean isDisplayFront )
110     {
111         if ( isDisplayFront )
112         {
113             return TEMPLATE_HTML_FRONT_CODE_FORM_ENTRY;
114         }
115 
116         return TEMPLATE_HTML_CODE_FORM_ENTRY;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
124     {
125         if ( isDisplayFront )
126         {
127             return TEMPLATE_HTML_FRONT_CODE_ENTRY_VALUE;
128         }
129 
130         return TEMPLATE_HTML_CODE_ENTRY_VALUE;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
138     {
139         if ( isDisplayFront )
140         {
141             return TEMPLATE_HTML_FRONT_CODE_FORM_SEARCH_ENTRY;
142         }
143 
144         return TEMPLATE_HTML_CODE_FORM_SEARCH_ENTRY;
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public String getEntryData( HttpServletRequest request, Locale locale )
152     {
153         String strTitle = request.getParameter( PARAMETER_TITLE );
154         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
155         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
156                 .trim( ) : null;
157         String strComment = request.getParameter( PARAMETER_COMMENT );
158         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
159         String strIndexed = request.getParameter( PARAMETER_INDEXED );
160         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
161         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
162         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
163         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
164         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
165         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
166         String strIsAllSearch = request.getParameter( PARAMETER_IS_ADD_VALUE_SEARCH_ALL );
167         String strLabelValueAllSearch = request.getParameter( PARAMETER_LABEL_VALUE_SEARCH_ALL );
168 
169         // Check if we have to show every information of the users or not
170         String strShowFullInfo = request.getParameter( PARAMETER_SHOW_ALL_INFO );
171 
172         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
173         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
174 
175         String strFieldError = DirectoryUtils.EMPTY_STRING;
176 
177         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
178         {
179             strFieldError = FIELD_TITLE;
180         }
181 
182         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
183         {
184             Object [ ] tabRequiredFields = {
185                 I18nService.getLocalizedString( strFieldError, locale )
186             };
187 
188             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
189         }
190 
191         this.setTitle( strTitle );
192         this.setHelpMessage( strHelpMessage );
193         this.setHelpMessageSearch( strHelpMessageSearch );
194         this.setComment( strComment );
195         this.setMandatory( strMandatory != null );
196         this.setIndexed( strIndexed != null );
197         this.setIndexedAsTitle( strIndexedAsTitle != null );
198         this.setIndexedAsSummary( strIndexedAsSummary != null );
199         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
200         this.setShownInResultList( strShowInResultList != null );
201         this.setShownInResultRecord( strShowInResultRecord != null );
202         this.setFieldInLine( false );
203         this.setShownInHistory( strShowInHistory != null );
204         this.setShownInExport( strShowInExport != null );
205         this.setShownInCompleteness( strShowInCompleteness != null );
206         this.setAddValueAllSearch( strIsAllSearch != null );
207 
208         if ( strIsAllSearch != null )
209         {
210             this.setLabelValueAllSearch( strLabelValueAllSearch );
211         }
212         else
213         {
214             this.setLabelValueAllSearch( null );
215         }
216 
217         if ( ( this.getFields( ) == null ) || ( this.getFields( ).size( ) == 0 ) )
218         {
219             List<Field> listFields = new ArrayList<Field>( );
220             Field field = new Field( );
221             field.setEntry( this );
222             listFields.add( field );
223             this.setFields( listFields );
224         }
225 
226         // IMPORTANT !!!
227         // The flag to show every information is stored in the database directory_field.DEFAULT_value
228         this.getFields( ).get( 0 ).setDefaultValue( strShowFullInfo != null );
229 
230         return null;
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     @Override
237     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
238             Locale locale ) throws DirectoryErrorException
239     {
240         if ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) )
241         {
242             String strUserLogin = lstValue.get( CONSTANT_POSITION_MYLUTECE_USER_LOGIN );
243 
244             RecordField recordField = new RecordField( );
245             recordField.setEntry( this );
246             recordField.setValue( strUserLogin );
247             listRecordField.add( recordField );
248         }
249     }
250 
251     /**
252      * {@inheritDoc}
253      */
254     @Override
255     public String getHtmlRecordFieldValue( Locale locale, RecordField recordField, boolean isDisplayFront )
256     {
257         if ( getTemplateHtmlRecordFieldValue( isDisplayFront ) != null )
258         {
259             Map<String, Object> model = new HashMap<String, Object>( );
260             model.put( MARK_ENTRY, this );
261             model.put( MARK_RECORD_FIELD, recordField );
262             model.put( MARK_LOCALE, locale );
263 
264             if ( showAllInfo( ) )
265             {
266                 int nIdRecord = recordField.getRecord( ).getIdRecord( );
267                 String strUserGuid = DirectoryService.getInstance( ).getUserGuid( nIdRecord, getIdEntry( ) );
268                 ReferenceList listUserInfos = DirectoryService.getInstance( ).getUserInfos( strUserGuid, getIdEntry( ) );
269 
270                 model.put( MARK_MYLUTECE_USER_INFOS_LIST, listUserInfos );
271                 model.put( MARK_MYLUTECE_USER_LOGIN, strUserGuid );
272             }
273 
274             HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlRecordFieldValue( isDisplayFront ), locale, model );
275 
276             return template.getHtml( );
277         }
278 
279         return null;
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bExportDirectory )
287     {
288         String strValue = StringUtils.EMPTY;
289 
290         if ( ( recordField != null ) && StringUtils.isNotBlank( recordField.getValue( ) ) && ( recordField.getRecord( ) != null ) )
291         {
292             if ( bExportDirectory )
293             {
294                 int nIdRecord = recordField.getRecord( ).getIdRecord( );
295                 String strUserGuid = DirectoryService.getInstance( ).getUserGuid( nIdRecord, getIdEntry( ) );
296                 ReferenceList listUserInfos = DirectoryService.getInstance( ).getUserInfos( strUserGuid, getIdEntry( ) );
297 
298                 if ( showAllInfo( ) && StringUtils.isNotBlank( strUserGuid ) && ( listUserInfos != null ) )
299                 {
300                     StringBuilder sbValue = new StringBuilder( );
301                     sbValue.append( I18nService.getLocalizedString( PROPERTY_USER_LOGIN, locale ) + SPACE + TWO_POINTS + SPACE + strUserGuid );
302 
303                     for ( ReferenceItem userInfo : listUserInfos )
304                     {
305                         sbValue.append( COMMA );
306                         sbValue.append( I18nService.getLocalizedString( PROPERTY_USER_INFO_PREFIX + userInfo.getCode( ), locale ) );
307                         sbValue.append( SPACE + TWO_POINTS + SPACE );
308                         sbValue.append( userInfo.getName( ) );
309                     }
310 
311                     strValue = sbValue.toString( );
312                 }
313             }
314 
315             if ( StringUtils.isBlank( strValue ) )
316             {
317                 strValue = recordField.getValue( );
318             }
319         }
320 
321         return strValue;
322     }
323 
324     /**
325      * 
326      * {@inheritDoc}
327      */
328     @Override
329     public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
330     {
331         return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
332     }
333 
334     /**
335      *
336      * {@inheritDoc}
337      */
338     @Override
339     public boolean isSortable( )
340     {
341         return true;
342     }
343 
344     /**
345      * Check if it must show every information of the user or not
346      * 
347      * @return true if it must show every information, false otherwise
348      */
349     private boolean showAllInfo( )
350     {
351         boolean bShow = false;
352         List<Field> listFields = getFields( );
353 
354         if ( ( listFields == null ) || listFields.isEmpty( ) )
355         {
356             Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
357             listFields = FieldHome.getFieldListByIdEntry( getIdEntry( ), plugin );
358         }
359 
360         if ( listFields != null )
361         {
362             Field field = listFields.get( 0 );
363 
364             if ( field != null )
365             {
366                 bShow = field.isDefaultValue( );
367             }
368         }
369 
370         return bShow;
371     }
372 }