KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > DateTimeDataType


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes;
11
12 import java.util.*;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.util.Casting;
16 import org.mmbase.util.DynamicDate;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * The date-time datatype further describes {@link java.util.Date} objects. The date can be
21  * restricted to a certain period (using {@link #setMin}, {@link #setMax}, and {@link
22  * org.mmbase.util.Casting#toDate}. The presentation logic can be specified using a pattern, see
23  * {@link #getPattern}.
24  *
25  * @author Pierre van Rooden
26  * @author Michiel Meeuwissen
27  * @version $Id: DateTimeDataType.java,v 1.34 2006/07/18 12:58:40 michiel Exp $
28  * @since MMBase-1.8
29  */

30 public class DateTimeDataType extends ComparableDataType {
31
32     public static final Date MIN_VALUE = new Date(Long.MIN_VALUE);
33     public static final Date MAX_VALUE = new Date(Long.MAX_VALUE);
34
35     private static final Logger log = Logging.getLoggerInstance(DateTimeDataType.class);
36
37     private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
38

39     // see javadoc of DateTimeFormat
40
private boolean weakPattern = true; // means, may not be changed, must be cloned before changing something
41
private DateTimePattern pattern = DateTimePattern.DEFAULT;
42
43     /**
44      * Constructor for DateTime field.
45      */

46     public DateTimeDataType(String JavaDoc name) {
47         super(name, Date.class);
48         setMin(MIN_VALUE, true);
49         setMax(MAX_VALUE, true);
50     }
51
52     protected String JavaDoc xmlValue(Object JavaDoc value) {
53         if (value instanceof DynamicDate) {
54             return ((DynamicDate) value).getFormat();
55         } else {
56             return super.xmlValue(value);
57         }
58     }
59
60     public void setDefaultValue(Object JavaDoc o) {
61         super.setDefaultValue(Casting.toDate(o));
62     }
63
64     protected void inheritProperties(BasicDataType origin) {
65         super.inheritProperties(origin);
66         if (origin instanceof DateTimeDataType) {
67             DateTimeDataType dataType = (DateTimeDataType)origin;
68             if (weakPattern) {
69                 pattern = dataType.pattern;
70             }
71         }
72     }
73
74     protected Object JavaDoc castToValidate(Object JavaDoc value, Node node, Field field) throws CastException {
75         if (value == null) return null;
76         try {
77             return DynamicDate.eval(Casting.toDate(value));
78         } catch (Throwable JavaDoc t) {
79             throw new CastException(t);
80         }
81     }
82
83     /**
84      * @return the minimum value as an <code>Date</code>, or very very long ago if there is no minimum.
85      */

86     public Date getMin() {
87         Object JavaDoc min = getMinRestriction().getValue();
88         return min == null ? MIN_VALUE : Casting.toDate(min);
89     }
90
91     /**
92      * @return the maximum value as an <code>Date</code>, or a very very in the future if there is no maximum.
93      */

94     public Date getMax() {
95         Object JavaDoc max = getMaxRestriction().getValue();
96         return max == null ? MAX_VALUE : Casting.toDate(max);
97     }
98
99
100     /**
101      * The 'pattern' of a 'DateTime' value gives a <code>DateTimePattern</code> object which can be used as an
102      * indication for presentation.
103      *
104      * Basicly, this can indicate whether the objects present e.g. only a date, only a time and whether e.g. this time includes seconds or not.
105      *
106      * <code>DateTimePattern</code> is actually a wrapper arround a pattern, and that is used here.
107      *
108      */

109     public DateTimePattern getPattern() {
110         return pattern;
111     }
112     /**
113      * Set the pattern for a certain Locale.
114      * See also {@link #getPattern}.
115      */

116     public void setPattern(String JavaDoc p, Locale locale) {
117         if (weakPattern) {
118             pattern = new DateTimePattern(p);
119             weakPattern = false;
120         } else {
121             if (locale == null || locale.equals(Locale.US)) {
122                 pattern.set(p);
123             }
124         }
125         pattern.set(p, locale);
126     }
127
128
129     public Object JavaDoc clone(String JavaDoc name) {
130         DateTimeDataType clone = (DateTimeDataType) super.clone(name);
131         clone.weakPattern = true;
132         return clone;
133     }
134
135     protected StringBuffer JavaDoc toStringBuffer() {
136         StringBuffer JavaDoc buf = super.toStringBuffer();
137         buf.append(" " + pattern);
138         return buf;
139     }
140 }
141
Popular Tags