View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.service.attributes;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
37  import fr.paris.lutece.portal.service.i18n.I18nService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.util.date.DateUtil;
40  
41  import java.text.ParseException;
42  import java.text.SimpleDateFormat;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.Date;
47  import java.util.List;
48  import java.util.Locale;
49  
50  
51  /**
52   * Manager for Date Attribute
53   */
54  public class DateManager extends DefaultManager
55  {
56      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/document/attributes/create_date.html";
57      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/document/attributes/modify_date.html";
58      private static final String TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/create_parameters_date.html";
59      private static final String TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/modify_parameters_date.html";
60      private static final String PROPERTY_MESSAGE_ERROR_FORMAT_DATE = "document.message.errorDateFormat";
61      private static final String[] DATE_FORMAT = {"yyyy-MM-dd HH:mm:ss", "yyyy", "MM", "MM/yyyy", "dd/MM/yyyy" };
62      private static final String CHECK_ON = "on";
63  
64      /**
65       * Gets the template to enter the attribute value
66       * @return The template to enter the attribute value
67       */
68      protected String getCreateTemplate(  )
69      {
70          return TEMPLATE_CREATE_ATTRIBUTE;
71      }
72  
73      /**
74       * Gets the template to modify the attribute value
75       * @return The template to modify the attribute value
76       */
77      protected String getModifyTemplate(  )
78      {
79          return TEMPLATE_MODIFY_ATTRIBUTE;
80      }
81  
82      /**
83       * Gets the template to enter the parameters of the attribute value
84       * @return The template to enter the parameters of the attribute value
85       */
86      protected String getCreateParametersTemplate(  )
87      {
88          return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
89      }
90  
91      /**
92       * Gets the template to modify the parameters of the attribute value
93       * @return The template to modify the parameters of the attribute value
94       */
95      protected String getModifyParametersTemplate(  )
96      {
97          return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
98      }
99  
100     /**
101      * Validate the value for the attribute
102      * @param nAttributeId The attribute identifier
103      * @param strValue The value to check
104      * @param locale The current Locale
105      * @return null if valid, otherwise error message
106      */
107     public String validateValue( int nAttributeId, String strValue, Locale locale )
108     {
109         String strValidateValue = super.validateValue( nAttributeId, strValue, locale );
110 
111         if ( strValidateValue != null )
112         {
113             return strValidateValue;
114         }
115 
116         String strValidateDate = validateDate( strValue, locale );
117 
118         if ( strValidateDate != null )
119         {
120             return I18nService.getLocalizedString( strValidateDate, locale );
121         }
122 
123         return null;
124     }
125 
126     /**
127      * Validate the value for the parameters
128      * @param listParameters The list of parameters to check
129      * @param locale The current Locale
130      * @return null if valid, otherwise message property
131      */
132     public String validateValueParameters( List<AttributeTypeParameter> listParameters, Locale locale )
133     {
134         String strValue;
135         String strReturn = null;
136 
137         for ( AttributeTypeParameter parameter : listParameters )
138         {
139             strValue = parameter.getValueList(  ).iterator(  ).hasNext(  )
140                 ? parameter.getValueList(  ).iterator(  ).next(  ) : "";
141 
142             // if current date is checked, return null
143             if ( strValue.equals( CHECK_ON ) )
144             {
145                 strReturn = null;
146             }
147             else
148             {
149                 // if validation failed, fill strReturn
150                 String strValidate = validateDate( strValue, locale );
151 
152                 if ( strValidate != null )
153                 {
154                     strReturn = strValidate;
155                 }
156             }
157         }
158 
159         return strReturn;
160     }
161 
162     /**
163      * Gets parameters values for the attribute
164      * @param nAttributeId The attribute Id
165      * @param locale The current Locale
166      * @return The collection of attribute type parameters
167      */
168     public Collection<AttributeTypeParameter> getAttributeParametersValues( int nAttributeId, Locale locale )
169     {
170         Collection<AttributeTypeParameter> listParameters = super.getAttributeParametersValues( nAttributeId, locale );
171 
172         for ( AttributeTypeParameter parameter : listParameters )
173         {
174             if ( parameter.getValueList(  ).iterator(  ).hasNext(  ) &&
175                     parameter.getValueList(  ).iterator(  ).next(  ).equals( CHECK_ON ) )
176             {
177                 //replace CHECK_ON by current date
178                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( DATE_FORMAT[3] );
179                 Date date = new Date(  );
180                 ArrayList<String> listValues = new ArrayList<String>(  );
181                 listValues.add( simpleDateFormat.format( date ) );
182                 parameter.setValueList( listValues );
183             }
184         }
185 
186         return listParameters;
187     }
188 
189     /**
190      * Validate the date for the attribute
191      * @param strDate The date to check
192      * @param locale The current Locale
193      * @return null if valid, otherwise error message
194      */
195     private String validateDate( String strDate, Locale locale )
196     {
197         Date date = null;
198         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(  );
199         int i = 0;
200 
201         if ( ( strDate == null ) || strDate.equals( "" ) )
202         {
203             return null;
204         }
205 
206         while ( ( date == null ) && ( i < DATE_FORMAT.length ) )
207         {
208             simpleDateFormat.applyPattern( DATE_FORMAT[i] );
209 
210             if ( strDate.length(  ) == DATE_FORMAT[i].length(  ) )
211             {
212                 try
213                 {
214                     date = simpleDateFormat.parse( strDate );
215                 }
216                 catch ( ParseException e )
217                 {
218                     AppLogService.debug( "Bad date format" );
219                 }
220             }
221 
222             i++;
223         }
224 
225         if ( date == null )
226         {
227             date = DateUtil.formatDate( strDate, locale );
228         }
229         else if ( !simpleDateFormat.format( date ).equals( strDate ) )
230         {
231             return PROPERTY_MESSAGE_ERROR_FORMAT_DATE;
232         }
233 
234         if ( date == null )
235         {
236             return PROPERTY_MESSAGE_ERROR_FORMAT_DATE;
237         }
238 
239         return null;
240     }
241 }