View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.forms.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.commons.lang3.math.NumberUtils;
43  
44  import fr.paris.lutece.plugins.forms.util.FormsConstants;
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.Field;
47  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
48  import fr.paris.lutece.plugins.genericattributes.business.Response;
49  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeService;
50  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
51  import fr.paris.lutece.portal.service.html.HtmlCleanerService;
52  import fr.paris.lutece.portal.service.i18n.I18nService;
53  import fr.paris.lutece.portal.service.message.AdminMessage;
54  import fr.paris.lutece.portal.service.message.AdminMessageService;
55  
56  /**
57   * This class is a service for the entry type Terms of service
58   *
59   */
60  public class EntryTypeTermsOfService extends EntryTypeService implements IResponseComparator
61  {
62      // Fields
63      public static final String FIELD_TOS_CODE = "tos";
64  
65      // Messages
66      private static final String MESSAGE_ENTRY_LINK_TITLE = "forms.createEntry.termsOfService.labelLink";
67      private static final String MESSAGE_ENTRY_TOS_TITLE = "forms.createEntry.termsOfService.labelTermsOfService";
68      private static final String MESSAGE_ENTRY_MANDATORY = "forms.message.mandatory.termsOfService";
69  
70      // Templates
71      private static final String TEMPLATE_CREATE = "admin/plugins/forms/entries/create_entry_type_terms_of_service.html";
72      private static final String TEMPLATE_MODIFY = "admin/plugins/forms/entries/modify_entry_type_terms_of_service.html";
73      private static final String TEMPLATE_READONLY_BACKOFFICE = "admin/plugins/forms/entries/readonly_entry_type_terms_of_service.html";
74      private static final String TEMPLATE_EDITION_BACKOFFICE = "admin/plugins/forms/entries/fill_entry_type_terms_of_service.html";
75      private static final String TEMPLATE_EDITION_FRONTOFFICE = "skin/plugins/forms/entries/fill_entry_type_terms_of_service.html";
76      private static final String TEMPLATE_READONLY_FRONTOFFICE = "skin/plugins/forms/entries/readonly_entry_type_terms_of_service.html";
77  
78      // Field codes
79      private static final String FIELD_LINK_CODE = "link";
80      public static final String FIELD_AGREEMENT_CODE = "agreement";
81  
82      // Parameters
83      private static final String PARAMETER_LINK = FIELD_LINK_CODE;
84      private static final String PARAMETER_TOS = FIELD_TOS_CODE;
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
91      {
92          if ( bDisplayFront )
93          {
94              return TEMPLATE_EDITION_FRONTOFFICE;
95          }
96  
97          return TEMPLATE_EDITION_BACKOFFICE;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public String getTemplateCreate( Entry entry, boolean bDisplayFront )
105     {
106         return TEMPLATE_CREATE;
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public String getTemplateModify( Entry entry, boolean bDisplayFront )
114     {
115         return TEMPLATE_MODIFY;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getTemplateEntryReadOnly( boolean bDisplayFront )
123     {
124         if ( bDisplayFront )
125         {
126             return TEMPLATE_READONLY_FRONTOFFICE;
127         }
128         return TEMPLATE_READONLY_BACKOFFICE;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
136     {
137         initCommonRequestData( entry, request );
138         String strTitle = request.getParameter( PARAMETER_TITLE );
139         String strLinkText = request.getParameter( PARAMETER_LINK );
140         String strTermsOfService = request.getParameter( PARAMETER_TOS );
141 
142         String strFieldInError = validateConfig( strTitle, strLinkText, strTermsOfService );
143 
144         if ( StringUtils.isNotEmpty( strFieldInError ) )
145         {
146             return buildErrorUrl( strFieldInError, request, locale );
147         }
148 
149         String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
150 
151         entry.setIndexed( request.getParameter( PARAMETER_INDEXED ) != null );
152         entry.setTitle( strTitle );
153         entry.setCSSClass( strCSSClass );
154         GenericAttributesUtils.createOrUpdateField( entry, FIELD_LINK_CODE, null, strLinkText );
155         GenericAttributesUtils.createOrUpdateField( entry, FIELD_TOS_CODE, null, strTermsOfService );
156         GenericAttributesUtils.createOrUpdateField( entry, FIELD_AGREEMENT_CODE, null, Boolean.FALSE.toString( ) );
157         return null;
158     }
159 
160     /**
161      * Validates the configuration of the entry type
162      * 
163      * @param strTitle
164      *            the title to validate
165      * @param strLinkText
166      *            the link text to validate
167      * @param strTermsOfService
168      *            the terms of service to validate
169      * @return the field in error, or empty if all the fields are valid
170      */
171     private String validateConfig( String strTitle, String strLinkText, String strTermsOfService )
172     {
173         if ( StringUtils.isBlank( strTitle ) )
174         {
175             return ERROR_FIELD_TITLE;
176         }
177 
178         if ( StringUtils.isBlank( strLinkText ) )
179         {
180             return MESSAGE_ENTRY_LINK_TITLE;
181         }
182 
183         if ( StringUtils.isBlank( strTermsOfService ) )
184         {
185             return MESSAGE_ENTRY_TOS_TITLE;
186         }
187 
188         return StringUtils.EMPTY;
189     }
190 
191     /**
192      * Builds the error URL
193      * 
194      * @param strFieldInError
195      *            the field in error
196      * @param request
197      *            the request
198      * @param locale
199      *            the locale
200      * @return the error URL
201      */
202     private String buildErrorUrl( String strFieldInError, HttpServletRequest request, Locale locale )
203     {
204         Object [ ] tabRequiredFields = {
205                 I18nService.getLocalizedString( strFieldInError, locale ),
206         };
207 
208         return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
216     {
217         int nIdFieldAgreement = NumberUtils.toInt( request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) ), FormsConstants.DEFAULT_ID_VALUE );
218         Response responseAgreement = createResponse( entry, request );
219         listResponse.add( responseAgreement );
220 
221         GenericAttributeError error = validateUserResponse( entry, nIdFieldAgreement, locale );
222 
223         if ( error != null )
224         {
225             return error;
226         }
227 
228         Field fieldAgreement = GenericAttributesUtils.findFieldByIdInTheList( nIdFieldAgreement, entry.getFields( ) );
229 
230         // this field is not null because its presence has been validated
231         fieldAgreement.setValue( Boolean.TRUE.toString( ) );
232 
233         responseAgreement.setResponseValue( fieldAgreement.getValue( ) );
234         responseAgreement.setField( fieldAgreement );
235 
236         listResponse.add( createResponseForAcceptedTermsOfService( entry, request ) );
237 
238         return null;
239     }
240 
241     /**
242      * Creates a response
243      * 
244      * @param entry
245      *            the entry linked to the response
246      * @param request
247      *            the request
248      * @return the created response
249      */
250     private Response createResponse( Entry entry, HttpServletRequest request )
251     {
252         Response response = new Response( );
253         response.setEntry( entry );
254         response.setIterationNumber( getResponseIterationValue( request ) );
255 
256         return response;
257     }
258 
259     /**
260      * Validates the user response
261      * 
262      * @param entry
263      *            the entry to validate
264      * @param nIdFieldAgreement
265      *            the field id to validate
266      * @param locale
267      *            the locale
268      * @return a {@code GenericAttributeError} if there is a validation error, {@code null} otherwise
269      */
270     private GenericAttributeError validateUserResponse( Entry entry, int nIdFieldAgreement, Locale locale )
271     {
272         GenericAttributeError error = null;
273 
274         if ( nIdFieldAgreement == FormsConstants.DEFAULT_ID_VALUE )
275         {
276             error = new GenericAttributeError( );
277             error.setMandatoryError( true );
278             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_ENTRY_MANDATORY, locale ) );
279         }
280 
281         Field fieldAgreement = GenericAttributesUtils.findFieldByIdInTheList( nIdFieldAgreement, entry.getFields( ) );
282 
283         if ( fieldAgreement == null || !FIELD_AGREEMENT_CODE.equals( fieldAgreement.getCode( ) ) )
284         {
285             error = new GenericAttributeError( );
286             error.setMandatoryError( true );
287             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_ENTRY_MANDATORY, locale ) );
288         }
289 
290         return error;
291     }
292 
293     /**
294      * Creates a response for the terms of service accepted by the user
295      * 
296      * @param entry
297      *            the entry
298      * @param request
299      *            the request
300      * @return the created response
301      */
302     private Response createResponseForAcceptedTermsOfService( Entry entry, HttpServletRequest request )
303     {
304         Field fieldAcceptedTermsOfService = entry.getFieldByCode( FIELD_TOS_CODE );
305 
306         Response response = createResponse( entry, request );
307         response.setResponseValue( fieldAcceptedTermsOfService.getValue( ) );
308         response.setField( fieldAcceptedTermsOfService );
309 
310         return response;
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     @Override
317     public boolean isResponseChanged( List<Response> listResponseReference, List<Response> listResponseNew )
318     {
319         return false;
320     }
321 
322     @Override
323     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
324     {
325         if ( StringUtils.isEmpty( response.getResponseValue( ) ) )
326         {
327             return StringUtils.EMPTY;
328         }
329         return HtmlCleanerService.text( response.getResponseValue( ) );
330     }
331 }