1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package integration.forms;
24 
25 import com.sun.star.form.binding.*;
26 
27 /**
28  *
29  * @author  fs@openoffice.org
30  */
31 public class DateValidator extends integration.forms.ControlValidator
32 {
33 
34     /** Creates a new instance of NumericValidator */
DateValidator( )35     public DateValidator( )
36     {
37     }
38 
explainInvalid( Object Value )39     public String explainInvalid( Object Value )
40     {
41         try
42         {
43             if ( isVoid( Value ) )
44                 return "empty input";
45 
46             com.sun.star.util.Date dateValue = (com.sun.star.util.Date)Value;
47             if ( isDedicatedInvalidDate( dateValue ) )
48                 return "this is no valid date";
49 
50             if ( !isNextMonthsDate( dateValue ) )
51                 return "date must denote a day in the current month";
52         }
53         catch( java.lang.Exception e )
54         {
55             return "oops. What did you enter for this to happen?";
56         }
57         return "";
58     }
59 
isValid( Object Value )60     public boolean isValid( Object Value )
61     {
62         try
63         {
64             if ( isVoid( Value ) )
65                 return false;
66 
67             com.sun.star.util.Date dateValue = (com.sun.star.util.Date)
68                 com.sun.star.uno.AnyConverter.toObject(
69                     com.sun.star.util.Date.class, Value);
70             if ( isDedicatedInvalidDate( dateValue ) )
71                 return false;
72 
73             if ( !isNextMonthsDate( dateValue ) )
74                 return false;
75             return true;
76         }
77         catch( java.lang.Exception e )
78         {
79             e.printStackTrace( System.err );
80         }
81         return false;
82     }
83 
isDedicatedInvalidDate( com.sun.star.util.Date dateValue )84     private boolean isDedicatedInvalidDate( com.sun.star.util.Date dateValue )
85     {
86         return ( dateValue.Day == 0 ) && ( dateValue.Month == 0 ) && ( dateValue.Year == 0 );
87     }
88 
isNextMonthsDate( com.sun.star.util.Date dateValue )89     private boolean isNextMonthsDate( com.sun.star.util.Date dateValue )
90     {
91         int overallMonth = dateValue.Year * 12 + dateValue.Month - 1;
92 
93         int todaysMonth = new java.util.Date().getMonth();
94         int todaysYear = new java.util.Date().getYear() + 1900;
95         int todaysOverallMonth = todaysYear * 12 + todaysMonth;
96 
97         return overallMonth == todaysOverallMonth;
98     }
99 }
100