View Javadoc
1   /*
2    * Copyright (C) 2003-2009 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.validator;
18  
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  
23  import org.exoplatform.web.application.ApplicationMessage;
24  import org.exoplatform.webui.application.WebuiRequestContext;
25  import org.exoplatform.webui.core.UIComponent;
26  import org.exoplatform.webui.exception.MessageException;
27  import org.exoplatform.webui.form.UIForm;
28  import org.exoplatform.webui.form.UIFormDateTimeInput;
29  import org.exoplatform.webui.form.UIFormInput;
30  import org.exoplatform.webui.form.validator.DateTimeValidator;
31  
32  
33  /**
34   * Created by The eXo Platform SAS
35   * Author : Phan Le Thanh Chuong
36   *          chuong_phan@exoplatform.com, phan.le.thanh.chuong@gmail.com
37   * Oct 15, 2009
38   */
39  public class DateValidator extends DateTimeValidator {
40  
41    @SuppressWarnings("unchecked")
42    public void validate(UIFormInput uiInput) throws Exception {
43      String inputValue = ((String)uiInput.getValue());
44      if (inputValue.trim().indexOf(" ") > 0) {
45        super.validate(uiInput);
46      } else {
47        validateDateFormat(uiInput);
48      }
49    }
50    
51    private void validateDateFormat(UIFormInput uiInput) throws Exception {
52      
53      String inputValue = ((String)uiInput.getValue());
54      try {
55        WebuiRequestContext requestContext = WebuiRequestContext.getCurrentInstance();
56        DateFormat dateFormat_ = SimpleDateFormat.getDateInstance(DateFormat.SHORT, requestContext.getLocale());
57        dateFormat_.setLenient(false);
58        dateFormat_.parse(inputValue);
59        ((UIFormDateTimeInput)uiInput).setValue(inputValue + " 00:00:00");
60      } catch (ParseException parseEx) {
61        //get label of datetime field
62        UIComponent uiComponent = (UIComponent) uiInput ;
63        UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class) ;
64        String label;
65        try{
66          label = uiForm.getLabel(uiInput.getName());
67        } catch(Exception ex) {
68          label = uiInput.getName();
69        }
70        label = label.trim();
71        if(label.charAt(label.length() - 1) == ':') label = label.substring(0, label.length() - 1);
72        
73        //throw exception with msg
74        throw new MessageException(new ApplicationMessage("DateTimeValidator.msg.Invalid-input",
75                                                          new Object[] { label, inputValue }));
76      }
77    }
78  
79  }