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.genericattributes.service.entrytype;
35  
36  import java.text.SimpleDateFormat;
37  import java.util.Date;
38  import java.util.List;
39  import java.util.Locale;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
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.MandatoryError;
49  import fr.paris.lutece.plugins.genericattributes.business.Response;
50  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.message.AdminMessage;
53  import fr.paris.lutece.portal.service.message.AdminMessageService;
54  import fr.paris.lutece.util.date.DateUtil;
55  import fr.paris.lutece.util.string.StringUtil;
56  
57  /**
58   * Abstract entry type for dates
59   */
60  public abstract class AbstractEntryTypeDate extends EntryTypeService
61  {
62      private static final String MESSAGE_ILLOGICAL_DATE = "genericattributes.message.illogicalDate";
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
69      {
70          initCommonRequestData( entry, request );
71          String strTitle = request.getParameter( PARAMETER_TITLE );
72          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
73          String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
74          String strComment = request.getParameter( PARAMETER_COMMENT );
75          String strValue = request.getParameter( PARAMETER_VALUE );
76          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
77          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
78          String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
79          String strIndexed = request.getParameter( PARAMETER_INDEXED );
80          String strPlaceholder = request.getParameter( PARAMETER_PLACEHOLDER );
81  
82          String strFieldError = StringUtils.EMPTY;
83  
84          if ( StringUtils.isBlank( strTitle ) )
85          {
86              strFieldError = ERROR_FIELD_TITLE;
87          }
88  
89          if ( StringUtils.isNotBlank( strFieldError ) )
90          {
91              Object [ ] tabRequiredFields = {
92                      I18nService.getLocalizedString( strFieldError, locale )
93              };
94  
95              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
96          }
97  
98          Date dDateValue = null;
99  
100         if ( StringUtils.isNotBlank( strValue ) )
101         {
102             dDateValue = DateUtil.parseIsoDate( strValue );
103             if ( dDateValue == null )
104             {
105                 Object [ ] messageArgs = new Object [ ] {
106                         DateUtil.ISO_PATTERN_DATE
107                 };
108                 return AdminMessageService.getMessageUrl( request, MESSAGE_ILLOGICAL_DATE, messageArgs, AdminMessage.TYPE_STOP );
109             }
110         }
111 
112         entry.setCode( strCode );
113         entry.setTitle( strTitle );
114         entry.setHelpMessage( strHelpMessage );
115         entry.setComment( strComment );
116         entry.setCSSClass( strCSSClass );
117 
118         GenericAttributesUtils.createOrUpdateField( entry, FIELD_PLACEHOLDER, null, strPlaceholder != null ? strPlaceholder : StringUtils.EMPTY );
119         Field field = GenericAttributesUtils.createOrUpdateField( entry, FIELD_DATE_VALUE, null, null );
120         field.setValueTypeDate( dDateValue );
121 
122         entry.setMandatory( strMandatory != null );
123         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
124         entry.setIndexed( strIndexed != null );
125 
126         return null;
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
134     {
135         String strValueEntry = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) ).trim( );
136         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
137         response.setEntry( entry );
138 
139         if ( strValueEntry == null )
140         {
141             return null;
142         }
143 
144         Date tDateValue = DateUtil.parseIsoDate( strValueEntry );
145 
146         if ( tDateValue != null )
147         {
148             response.setResponseValue( String.valueOf( tDateValue.getTime( ) ) );
149         }
150 
151         if ( StringUtils.isNotBlank( response.getResponseValue( ) ) )
152         {
153             response.setToStringValueResponse( getResponseValueForRecap( entry, request, response, locale ) );
154         }
155         else
156         {
157             response.setToStringValueResponse( StringUtils.EMPTY );
158         }
159 
160         response.setIterationNumber( getResponseIterationValue( request ) );
161         listResponse.add( response );
162 
163         // Checks if the entry value contains XSS characters
164         if ( StringUtil.containsXssCharacters( strValueEntry ) )
165         {
166             GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
167             error.setMandatoryError( false );
168             error.setTitleQuestion( entry.getTitle( ) );
169             error.setErrorMessage( I18nService.getLocalizedString( MESSAGE_XSS_FIELD, request.getLocale( ) ) );
170 
171             return error;
172         }
173 
174         if ( entry.isMandatory( ) && StringUtils.isBlank( strValueEntry ) )
175         {
176             return new MandatoryError( entry, locale );
177         }
178 
179         if ( StringUtils.isNotBlank( strValueEntry ) && ( tDateValue == null ) )
180         {
181             SimpleDateFormat sdf = (SimpleDateFormat) DateUtil.getDateFormat( locale );
182             Object [ ] messageArgs = new Object [ ] {
183                     sdf.toPattern( )
184             };
185 
186             String strError = I18nService.getLocalizedString( MESSAGE_ILLOGICAL_DATE, messageArgs, locale );
187             GenericAttributeErrortributes/business/GenericAttributeError.html#GenericAttributeError">GenericAttributeError error = new GenericAttributeError( );
188             error.setTitleQuestion( entry.getTitle( ) );
189             error.setMandatoryError( false );
190             error.setErrorMessage( strError );
191 
192             return error;
193         }
194         return null;
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
202     {
203         return getResponseValueForRecap( entry, request, response, locale );
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
211     {
212         String strValueEntry = response.getResponseValue( );
213         if ( StringUtils.isEmpty( strValueEntry ) )
214         {
215             return StringUtils.EMPTY;
216         }
217         Date date = new Date( Long.valueOf( strValueEntry ) );
218         return DateUtil.getDateString( date, locale );
219     }
220 
221     /**
222      * {@inheritDoc}
223      */
224     @Override
225     public void setResponseToStringValue( Entry entry, Response response, Locale locale )
226     {
227         String strValueEntry = response.getResponseValue( );
228         if ( StringUtils.isNotEmpty( strValueEntry ) )
229         {
230             Date tDateValue = DateUtil.formatDate( response.getResponseValue( ), locale );
231             if ( tDateValue != null )
232             {
233                 response.setResponseValue( String.valueOf( tDateValue.getTime( ) ) );
234             }
235         }
236     }
237 }