KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > DateCompareMethod


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.template;
18
19 import java.util.Date JavaDoc;
20 import java.util.List JavaDoc;
21
22 import freemarker.ext.beans.BeanModel;
23 import freemarker.template.TemplateDateModel;
24 import freemarker.template.TemplateMethodModelEx;
25 import freemarker.template.TemplateModelException;
26 import freemarker.template.TemplateNumberModel;
27
28 /**
29  * @author Kevin Roast
30  *
31  * Custom FreeMarker Template language method.
32  * <p>
33  * Compare two dates to see if they differ by the specified number of miliseconds
34  * <p>
35  * Usage:
36  * dateCompare(dateA, dateB) - 1 if dateA if greater than dateB
37  * dateCompare(dateA, dateB, millis) - 1 if dateA is greater than dateB by at least millis, else 0
38  */

39 public final class DateCompareMethod implements TemplateMethodModelEx
40 {
41     /**
42      * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
43      */

44     public Object JavaDoc exec(List JavaDoc args) throws TemplateModelException
45     {
46         int result = 0;
47         
48         if (args.size() >= 2)
49         {
50             Object JavaDoc arg0 = args.get(0);
51             Object JavaDoc arg1 = args.get(1);
52             long diff = 0;
53             if (args.size() == 3)
54             {
55                Object JavaDoc arg2 = args.get(2);
56                if (arg2 instanceof TemplateNumberModel)
57                {
58                   Number JavaDoc number = ((TemplateNumberModel)arg2).getAsNumber();
59                   diff = number.longValue();
60                }
61             }
62             if (arg0 instanceof TemplateDateModel && arg1 instanceof TemplateDateModel)
63             {
64                 Date JavaDoc dateA = (Date JavaDoc)((TemplateDateModel)arg0).getAsDate();
65                 Date JavaDoc dateB = (Date JavaDoc)((TemplateDateModel)arg1).getAsDate();
66                 if (dateA.getTime() > (dateB.getTime() - diff))
67                 {
68                    result = 1;
69                 }
70             }
71         }
72         
73         return result;
74     }
75 }
76
Popular Tags