View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.directories.service.entrytype;
35  
36  import fr.paris.lutece.plugins.directories.util.DirectoriesConstants;
37  import fr.paris.lutece.plugins.genericattributes.business.Entry;
38  import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeText;
39  import fr.paris.lutece.plugins.genericattributes.business.Field;
40  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
41  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
42  import fr.paris.lutece.plugins.genericattributes.business.Response;
43  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
44  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
45  import fr.paris.lutece.portal.service.i18n.I18nService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
49  import fr.paris.lutece.util.string.StringUtil;
50  import java.util.List;
51  import java.util.Locale;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  import org.apache.commons.collections.CollectionUtils;
56  import org.apache.commons.lang.StringUtils;
57  
58  /**
59   * class EntryTypeText
60   * 
61   * @author
62   * 
63   */
64  public class EntryTypeMyLuteceUserPicker extends AbstractEntryTypeText
65  {
66      private static final String TEMPLATE_MODIFY = "admin/plugins/directories/entries/modify_entry_type_mylutece_user_picker.html";
67      private static final String TEMPLATE_HTML_CODE_ADMIN = "admin/plugins/directories/entries/fill_entry_type_mylutece_user_picker.html";
68      private static final String TEMPLATE_HTML_CODE = "skin/plugins/directories/entries/fill_entry_type_mylutece_user_picker.html";
69      private static final String TEMPLATE_CREATE = "admin/plugins/directories/entries/create_entry_type_mylutece_user_picker.html";
70      private static final String TEMPLATE_READONLY_FRONTOFFICE = "skin/plugins/directories/entries/readonly_entry_type_mylutece_user_picker.html";
71      private static final String TEMPLATE_READONLY_BACKOFFICE = "admin/plugins/directories/entries/readonly_entry_type_mylutece_user_picker.html";
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
78      {
79          return bDisplayFront ? TEMPLATE_HTML_CODE : TEMPLATE_HTML_CODE_ADMIN;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public String getTemplateModify( Entry entry, boolean bDisplayFront )
87      {
88          return TEMPLATE_MODIFY;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String getTemplateCreate( Entry entry, boolean bDisplayFront )
96      {
97          return TEMPLATE_CREATE;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
105     {
106         initCommonRequestData( entry, request );
107         String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
108         String strTitle = request.getParameter( PARAMETER_TITLE );
109         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
110         String strComment = request.getParameter( PARAMETER_COMMENT );
111         String strValue = request.getParameter( PARAMETER_VALUE );
112         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
113         String strWidth = request.getParameter( PARAMETER_WIDTH );
114         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
115         String strConfirmField = request.getParameter( PARAMETER_CONFIRM_FIELD );
116         String strConfirmFieldTitle = request.getParameter( PARAMETER_CONFIRM_FIELD_TITLE );
117         String strUnique = request.getParameter( PARAMETER_UNIQUE );
118         String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
119         String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
120         String strEditableBack = request.getParameter( PARAMETER_EDITABLE_BACK );
121         String strErrorMessage = request.getParameter( PARAMETER_ERROR_MESSAGE );
122         String strIndexed = request.getParameter( PARAMETER_INDEXED );
123 
124         int nWidth = -1;
125         int nMaxSizeEnter = -1;
126 
127         String strFieldError = StringUtils.EMPTY;
128 
129         if ( StringUtils.isBlank( strTitle ) )
130         {
131             strFieldError = ERROR_FIELD_TITLE;
132         }
133 
134         else
135             if ( StringUtils.isBlank( strWidth ) )
136             {
137                 strFieldError = ERROR_FIELD_WIDTH;
138             }
139 
140         if ( ( strConfirmField != null ) && StringUtils.isBlank( strConfirmFieldTitle ) )
141         {
142             strFieldError = FIELD_CONFIRM_FIELD_TITLE;
143         }
144 
145         if ( StringUtils.isNotBlank( strFieldError ) )
146         {
147             Object [ ] tabRequiredFields = {
148                     I18nService.getLocalizedString( strFieldError, locale )
149             };
150 
151             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
152         }
153 
154         try
155         {
156             nWidth = Integer.parseInt( strWidth );
157         }
158         catch( NumberFormatException ne )
159         {
160             strFieldError = ERROR_FIELD_WIDTH;
161         }
162 
163         try
164         {
165             if ( StringUtils.isNotBlank( strMaxSizeEnter ) )
166             {
167                 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
168             }
169         }
170         catch( NumberFormatException ne )
171         {
172             strFieldError = FIELD_MAX_SIZE_ENTER;
173         }
174 
175         if ( StringUtils.isNotBlank( strFieldError ) )
176         {
177             Object [ ] tabRequiredFields = {
178                     I18nService.getLocalizedString( strFieldError, locale )
179             };
180 
181             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
182         }
183 
184         entry.setTitle( strTitle );
185         entry.setHelpMessage( strHelpMessage );
186         entry.setComment( strComment );
187         entry.setCSSClass( strCSSClass );
188         entry.setIndexed( strIndexed != null );
189         entry.setErrorMessage( strErrorMessage );
190         entry.setCode( strCode );
191 
192         GenericAttributesUtils.createOrUpdateField( entry, FIELD_TEXT_CONF, null, strValue );
193         GenericAttributesUtils.createOrUpdateField( entry, FIELD_WIDTH, null, String.valueOf( nWidth ) );
194         GenericAttributesUtils.createOrUpdateField( entry, FIELD_MAX_SIZE, null, String.valueOf( nMaxSizeEnter ) );
195         GenericAttributesUtils.createOrUpdateField( entry, DirectoriesConstants.FIELD_FILTER, null,
196                 request.getParameter( DirectoriesConstants.PARAMETER_FILTER ) );
197 
198         entry.setMandatory( strMandatory != null );
199         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
200         entry.setEditableBack( strEditableBack != null );
201 
202         boolean confirm = false;
203         String fieldTitle = null;
204         if ( strConfirmField != null )
205         {
206             confirm = true;
207             fieldTitle = strConfirmFieldTitle;
208         }
209 
210         GenericAttributesUtils.createOrUpdateField( entry, FIELD_CONFIRM, fieldTitle, String.valueOf( confirm ) );
211         entry.setUnique( strUnique != null );
212         return null;
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     @Override
219     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
220     {
221         String strValueEntry = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) ).trim( );
222         Field confirmField = entry.getFieldByCode( FIELD_CONFIRM );
223 
224         boolean bConfirmField = confirmField != null && Boolean.valueOf( confirmField.getValue( ) );
225 
226         String strValueEntryConfirmField = null;
227 
228         if ( bConfirmField )
229         {
230             strValueEntryConfirmField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) + SUFFIX_CONFIRM_FIELD ).trim( );
231         }
232 
233         List<RegularExpression> listRegularExpression = entry.getFieldByCode( FIELD_TEXT_CONF ).getRegularExpressionList( );
234         Response response = new Response( );
235         response.setEntry( entry );
236 
237         if ( strValueEntry == null )
238         {
239             return null;
240         }
241 
242         response.setResponseValue( strValueEntry );
243 
244         if ( StringUtils.isNotBlank( response.getResponseValue( ) ) )
245         {
246             response.setToStringValueResponse( getResponseValueForRecap( entry, request, response, locale ) );
247         }
248         else
249         {
250             response.setToStringValueResponse( StringUtils.EMPTY );
251         }
252 
253         response.setIterationNumber( getResponseIterationValue( request ) );
254 
255         listResponse.add( response );
256 
257         String strFilter = entry.getFieldByCode( DirectoriesConstants.FIELD_FILTER ).getValue( );
258         response = new Response( );
259         response.setEntry( entry );
260         response.setResponseValue( strFilter );
261         response.setIterationNumber( getResponseIterationValue( request ) );
262         listResponse.add( response );
263 
264         return checkEntryErrors( entry, confirmField, strValueEntry, strValueEntryConfirmField, listRegularExpression, bConfirmField, locale );
265     }
266 
267     /**
268      * {@inheritDoc}
269      */
270     private GenericAttributeError checkEntryErrors( Entry entry, Field confirmField, String strValueEntry, String strValueEntryConfirmField,
271             List<RegularExpression> listRegularExpression, boolean bConfirmField, Locale locale )
272     {
273         // Checks if the entry value contains XSS characters
274         if ( StringUtil.containsXssCharacters( strValueEntry ) )
275         {
276             GenericAttributeError error = new GenericAttributeError( );
277             error.setMandatoryError( false );
278             error.setTitleQuestion( entry.getTitle( ) );
279             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_XSS_FIELD, locale ) );
280 
281             return error;
282         }
283 
284         if ( entry.isMandatory( ) && StringUtils.isBlank( strValueEntry ) )
285         {
286             if ( StringUtils.isNotEmpty( entry.getErrorMessage( ) ) )
287             {
288                 GenericAttributeError error = new GenericAttributeError( );
289                 error.setMandatoryError( true );
290                 error.setErrorMessage( entry.getErrorMessage( ) );
291 
292                 return error;
293             }
294 
295             return new MandatoryError( entry, locale );
296         }
297 
298         if ( ( !strValueEntry.equals( StringUtils.EMPTY ) ) && CollectionUtils.isNotEmpty( listRegularExpression )
299                 && RegularExpressionService.getInstance( ).isAvailable( ) )
300         {
301             for ( RegularExpression regularExpression : listRegularExpression )
302             {
303                 if ( !RegularExpressionService.getInstance( ).isMatches( strValueEntry, regularExpression ) )
304                 {
305                     GenericAttributeError error = new GenericAttributeError( );
306                     error.setMandatoryError( false );
307                     error.setTitleQuestion( entry.getTitle( ) );
308                     error.setErrorMessage( regularExpression.getErrorMessage( ) );
309 
310                     return error;
311                 }
312             }
313         }
314 
315         if ( bConfirmField && ( ( strValueEntryConfirmField == null ) || !strValueEntry.equals( strValueEntryConfirmField ) ) )
316         {
317             GenericAttributeError error = new GenericAttributeError( );
318             error.setMandatoryError( false );
319             error.setTitleQuestion( confirmField.getTitle( ) );
320             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_CONFIRM_FIELD, new String [ ] {
321                     entry.getTitle( )
322             }, locale ) );
323 
324             return error;
325         }
326         return null;
327     }
328 
329     /**
330      * {@inheritDoc}
331      */
332     @Override
333     public String getTemplateEntryReadOnly( boolean bDisplayFront )
334     {
335         if ( bDisplayFront )
336         {
337             return TEMPLATE_READONLY_FRONTOFFICE;
338         }
339 
340         return TEMPLATE_READONLY_BACKOFFICE;
341     }
342 
343 }