| 1 19 package org.openharmonise.rm.resources.metadata.properties.ranges; 20 21 import java.util.Date ; 22 import java.text.*; 23 import java.util.StringTokenizer ; 24 import java.util.logging.*; 25 import java.util.logging.Level ; 26 27 import org.openharmonise.rm.metadata.GeneralPropertyInstance; 28 import org.openharmonise.rm.publishing.*; 29 30 31 39 public class DateRange 40 extends AbstractRange 41 implements Range, Publishable, Cloneable { 42 43 46 public final static String TAG_DATE_RANGE = "DateRange"; 47 48 49 52 private Date m_dtMax = null; 53 54 57 private Date m_dtMin = null; 58 59 63 private boolean m_bIncludeTime = true; 64 65 69 static final private String SEPARATOR = "|"; 70 71 74 static final public String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss G"; 75 76 79 static final public String DATE_FORMAT = "yyyy-MM-dd G"; 80 81 84 private static final Logger m_logger = Logger.getLogger(DateRange.class.getName()); 85 86 90 public DateRange() { 91 super(Date .class.getName()); 92 } 93 94 100 public DateRange(String sDetails) { 101 super(Date .class.getName(), sDetails); 102 } 103 104 107 public boolean isValid(Object obj) { 108 boolean bIsValid = false; 109 110 if((obj instanceof Date ) || (obj instanceof java.util.Date )) { 111 Date date = (Date ) obj; 112 bIsValid = true; 113 114 if(hasMin() == true) { 115 if(getMin().after(date) == true) { 116 bIsValid = false; 117 } 118 } 119 120 if(hasMax() == true) { 121 if(getMax().before(date) == true) { 122 bIsValid = false; 123 } 124 } 125 } 126 127 return bIsValid; 128 } 129 130 133 public boolean equals(Object obj) { 134 boolean bResult = false; 135 136 if (obj instanceof DateRange) { 137 bResult = super.equals(obj); 138 } 139 140 return bResult; 141 } 142 143 148 public void setMin(Date min) { 149 if((m_dtMin == null && min != null) 150 || (m_dtMin != null && m_dtMin.equals(min) == false)) { 151 m_dtMin = min; 152 isChanged(true); 153 } 154 155 } 156 157 162 public void setMax(Date max) { 163 if((m_dtMax == null && max != null) 164 || (m_dtMax != null && m_dtMax.equals(max) == false)) { 165 m_dtMax = max; 166 isChanged(true); 167 } 168 } 169 170 175 public boolean hasMax() { 176 return m_dtMax != null; 177 } 178 179 184 public boolean hasMin() { 185 return m_dtMin != null; 186 } 187 188 193 public Date getMin() { 194 return m_dtMin; 195 } 196 197 202 public Date getMax() { 203 return m_dtMax; 204 } 205 206 211 public void includeTime(boolean bIncludeTime) { 212 if(m_bIncludeTime != bIncludeTime) { 213 m_bIncludeTime = bIncludeTime; 214 isChanged(true); 215 } 216 } 217 218 223 public boolean includeTime() { 224 return m_bIncludeTime; 225 } 226 227 230 public String getDetails() { 231 232 if (isChanged()) { 233 StringBuffer strbuf = new StringBuffer (); 234 235 String sFormat = DATETIME_FORMAT; 236 237 if(m_bIncludeTime == false) { 238 sFormat = DATE_FORMAT; 239 } 240 241 strbuf.append(m_bIncludeTime); 242 strbuf.append(SEPARATOR); 243 SimpleDateFormat date_formatter = 244 new SimpleDateFormat(sFormat); 245 246 if (hasMin() == true) { 247 strbuf.append(date_formatter.format(m_dtMin)); 248 } 249 strbuf.append(SEPARATOR); 250 if (hasMax() == true) { 251 strbuf.append(date_formatter.format(m_dtMax)); 252 } 253 super.setDetails(strbuf.toString()); 254 } 255 256 return super.getDetails(); 257 } 258 259 262 public void setDetails(String sDetails) { 263 if (sDetails != null) { 264 StringTokenizer tokenizer = 265 new StringTokenizer (sDetails, SEPARATOR); 266 SimpleDateFormat formatter = null; 267 268 int i = 0; 269 int nTmp = 0; 270 while (tokenizer.hasMoreTokens()) { 271 String token = tokenizer.nextToken(); 272 273 if (token != null && token.length() > 0) { 274 275 if(i == 0) { 276 m_bIncludeTime = Boolean.valueOf(token).booleanValue(); 277 278 String sFormat = DATETIME_FORMAT; 279 280 if(m_bIncludeTime == false) { 281 sFormat = DATE_FORMAT; 282 } 283 284 formatter = new SimpleDateFormat(sFormat); 285 } else if (i == 1 && formatter != null) { 286 try { 287 m_dtMin = formatter.parse(token); 288 289 290 } catch (ParseException e) { 291 m_dtMin = null; 293 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 294 } 295 } else if(formatter != null) { 296 try { 297 m_dtMax = formatter.parse(token); 298 } catch (ParseException e) { 299 m_dtMax = null; 301 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 302 } 303 } 304 305 } 306 i++; 307 } 308 } 309 310 super.setDetails(sDetails); 311 } 312 313 316 public Class getPropertyInstanceClass() throws ClassNotFoundException { 317 return GeneralPropertyInstance.class; 318 } 319 320 323 public String getTagName() { 324 return TAG_DATE_RANGE; 325 } 326 } 327 | Popular Tags |