1 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 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; 39 private boolean weakPattern = true; private DateTimePattern pattern = DateTimePattern.DEFAULT; 42 43 46 public DateTimeDataType(String name) { 47 super(name, Date.class); 48 setMin(MIN_VALUE, true); 49 setMax(MAX_VALUE, true); 50 } 51 52 protected String xmlValue(Object 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 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 castToValidate(Object 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 t) { 79 throw new CastException(t); 80 } 81 } 82 83 86 public Date getMin() { 87 Object min = getMinRestriction().getValue(); 88 return min == null ? MIN_VALUE : Casting.toDate(min); 89 } 90 91 94 public Date getMax() { 95 Object max = getMaxRestriction().getValue(); 96 return max == null ? MAX_VALUE : Casting.toDate(max); 97 } 98 99 100 109 public DateTimePattern getPattern() { 110 return pattern; 111 } 112 116 public void setPattern(String 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 clone(String name) { 130 DateTimeDataType clone = (DateTimeDataType) super.clone(name); 131 clone.weakPattern = true; 132 return clone; 133 } 134 135 protected StringBuffer toStringBuffer() { 136 StringBuffer buf = super.toStringBuffer(); 137 buf.append(" " + pattern); 138 return buf; 139 } 140 } 141 | Popular Tags |