KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > evaluator > compare > DatePropertyValueComparator


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.evaluator.compare;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Date JavaDoc;
21
22 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
23 import org.alfresco.service.cmr.action.ActionServiceException;
24 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
25
26 /**
27  * Date property value comparator
28  *
29  * @author Roy Wetherall
30  */

31 public class DatePropertyValueComparator implements PropertyValueComparator
32 {
33     /**
34      * I18N message ids
35      */

36     private static final String JavaDoc MSGID_INVALID_OPERATION = "date_property_value_comparator.invalid_operation";
37     
38     /**
39      * @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#compare(java.io.Serializable, java.io.Serializable, org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation)
40      */

41     public boolean compare(Serializable JavaDoc propertyValue,
42             Serializable JavaDoc compareValue, ComparePropertyValueOperation operation)
43     {
44         boolean result = false;
45         
46         if (operation == null)
47         {
48             operation = ComparePropertyValueOperation.EQUALS;
49         }
50         
51         Date JavaDoc propertyDate = (Date JavaDoc)propertyValue;
52         Date JavaDoc compareDate = (Date JavaDoc)compareValue;
53         
54         switch (operation)
55         {
56             case EQUALS:
57             {
58                 result = propertyDate.equals(compareDate);
59                 break;
60             }
61             case LESS_THAN:
62             {
63                 result = propertyDate.before(compareDate);
64                 break;
65             }
66             case LESS_THAN_EQUAL:
67             {
68                 result = (propertyDate.equals(compareDate) || propertyDate.before(compareDate));
69                 break;
70             }
71             case GREATER_THAN:
72             {
73                 result = propertyDate.after(compareDate);
74                 break;
75             }
76             case GREATER_THAN_EQUAL:
77             {
78                 result = (propertyDate.equals(compareDate) || propertyDate.after(compareDate));
79                 break;
80             }
81             default:
82             {
83                 // Raise an invalid operation exception
84
throw new ActionServiceException(
85                         MSGID_INVALID_OPERATION,
86                         new Object JavaDoc[]{operation.toString()});
87             }
88         }
89         
90         return result;
91     }
92
93     /**
94      * @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#registerComparator(org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator)
95      */

96     public void registerComparator(ComparePropertyValueEvaluator evaluator)
97     {
98         evaluator.registerComparator(DataTypeDefinition.DATE, this);
99         evaluator.registerComparator(DataTypeDefinition.DATETIME, this);
100     }
101
102 }
103
Popular Tags