001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.datatypes;
011:
012: import java.util.*;
013:
014: import org.mmbase.bridge.*;
015: import org.mmbase.util.Casting;
016: import org.mmbase.util.DynamicDate;
017: import org.mmbase.util.logging.*;
018:
019: /**
020: * The date-time datatype further describes {@link java.util.Date} objects. The date can be
021: * restricted to a certain period (using {@link #setMin}, {@link #setMax}, and {@link
022: * org.mmbase.util.Casting#toDate}. The presentation logic can be specified using a pattern, see
023: * {@link #getPattern}.
024: *
025: * @author Pierre van Rooden
026: * @author Michiel Meeuwissen
027: * @version $Id: DateTimeDataType.java,v 1.38 2007/12/10 12:27:01 michiel Exp $
028: * @since MMBase-1.8
029: */
030: public class DateTimeDataType extends ComparableDataType {
031:
032: public static final Date MIN_VALUE = new Date(Long.MIN_VALUE);
033: public static final Date MAX_VALUE = new Date(Long.MAX_VALUE);
034:
035: private static final Logger log = Logging
036: .getLoggerInstance(DateTimeDataType.class);
037:
038: private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
039:
040: // see javadoc of DateTimeFormat
041: private boolean weakPattern = true; // means, may not be changed, must be cloned before changing something
042: private DateTimePattern pattern = DateTimePattern.DEFAULT;
043:
044: /**
045: * Constructor for DateTime field.
046: */
047: public DateTimeDataType(String name) {
048: super (name, Date.class);
049: setMin(MIN_VALUE, true);
050: setMax(MAX_VALUE, true);
051: }
052:
053: protected void xmlValue(org.w3c.dom.Element el, Object value) {
054: if (value instanceof DynamicDate) {
055: el.setAttribute("value", ((DynamicDate) value).getFormat());
056: el.setAttribute("eval", Casting.toString(value));
057: } else {
058: super .xmlValue(el, value);
059: }
060: }
061:
062: public void setDefaultValue(Object o) {
063: log.debug("Setting default value " + o);
064: //super.setDefaultValue(Casting.toDate(o));
065: super .setDefaultValue(o == null ? null : Casting.toDate(o));
066: }
067:
068: protected void inheritProperties(BasicDataType origin) {
069: super .inheritProperties(origin);
070: if (origin instanceof DateTimeDataType) {
071: DateTimeDataType dataType = (DateTimeDataType) origin;
072: if (weakPattern) {
073: pattern = dataType.pattern;
074: }
075: }
076: }
077:
078: protected Object castToValidate(Object value, Node node, Field field)
079: throws CastException {
080: if (value == null)
081: return null;
082: try {
083: return DynamicDate.eval(Casting.toDate(value));
084: } catch (Throwable t) {
085: throw new CastException(t);
086: }
087: }
088:
089: /**
090: * @return the minimum value as an <code>Date</code>, or very very long ago if there is no minimum.
091: */
092: public Date getMin() {
093: Object min = getMinRestriction().getValue();
094: return min == null ? MIN_VALUE : Casting.toDate(min);
095: }
096:
097: /**
098: * @return the maximum value as an <code>Date</code>, or a very very in the future if there is no maximum.
099: */
100: public Date getMax() {
101: Object max = getMaxRestriction().getValue();
102: return max == null ? MAX_VALUE : Casting.toDate(max);
103: }
104:
105: /**
106: * The 'pattern' of a 'DateTime' value gives a <code>DateTimePattern</code> object which can be used as an
107: * indication for presentation.
108: *
109: * 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.
110: *
111: * <code>DateTimePattern</code> is actually a wrapper arround a pattern, and that is used here.
112: *
113: */
114: public DateTimePattern getPattern() {
115: return pattern;
116: }
117:
118: /**
119: * Set the pattern for a certain Locale.
120: * See also {@link #getPattern}.
121: */
122: public void setPattern(String p, Locale locale) {
123: if (weakPattern) {
124: pattern = new DateTimePattern(p);
125: weakPattern = false;
126: } else {
127: if (locale == null || locale.equals(Locale.US)) {
128: pattern.set(p);
129: }
130: }
131: pattern.set(p, locale);
132: }
133:
134: public DataType clone(String name) {
135: DateTimeDataType clone = (DateTimeDataType) super .clone(name);
136: clone.weakPattern = true;
137: return clone;
138: }
139:
140: protected StringBuilder toStringBuilder() {
141: StringBuilder buf = super .toStringBuilder();
142: buf.append(" " + pattern);
143: return buf;
144: }
145: }
|