1 19 package org.openharmonise.vfs.metadata.range; 20 21 import java.text.*; 22 import java.text.ParseException ; 23 import java.util.Date ; 24 25 import org.openharmonise.commons.xml.*; 26 import org.openharmonise.vfs.metadata.*; 27 import org.openharmonise.vfs.metadata.value.*; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.w3c.dom.Text ; 33 34 35 42 public class DateTimeRange extends AbstractRange implements Range { 43 44 47 public static final String XSD_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G"; 48 49 52 private static SimpleDateFormat df = 53 new java.text.SimpleDateFormat (XSD_DATE_FORMAT); 54 55 58 private Date m_dMinInclusive = null; 59 60 63 private Date m_dMaxInclusive = null; 64 65 68 private Date m_dMinExclusive = null; 69 70 73 private Date m_dMaxExclusive = null; 74 75 78 public DateTimeRange() { 79 super(); 80 } 81 82 88 public Date getMinimum() { 89 if(this.m_dMinInclusive!=null) { 90 return this.m_dMinInclusive; 91 } else { 92 return this.m_dMinExclusive; 93 } 94 } 95 96 102 public Date getMaximum() { 103 if(this.m_dMinInclusive!=null) { 104 return this.m_dMaxInclusive; 105 } else { 106 return this.m_dMaxExclusive; 107 } 108 } 109 110 115 public void setMinimum(Date dt) { 116 this.m_dMinInclusive = dt; 117 } 118 119 124 public void setMaximum(Date dt) { 125 this.m_dMaxInclusive = dt; 126 } 127 128 131 public ValidationResult validate(ValueInstance value) { 132 boolean bIsValid = true; 133 Date maxDt = getMaximum(); 134 Date minDt = getMinimum(); 135 Date curDate = getDate(((DateTimeValue)value).getValue()); 136 137 if(maxDt != null && curDate.after(maxDt)) { 138 bIsValid = false; 139 } 140 141 if(bIsValid == true && minDt != null && curDate.before(minDt)) { 142 bIsValid = false; 143 } 144 145 return new ValidationResult(bIsValid,""); 146 } 147 148 151 public void instantiate(Element elRange) { 152 Element elRestriction = XMLUtils.getFirstElementChild(elRange); 153 NodeList nl = elRestriction.getChildNodes(); 154 for(int i=0; i<nl.getLength();i++) { 155 Node node = nl.item(i); 156 if(node.getNodeType()==Node.ELEMENT_NODE) { 157 Element element = (Element )node; 158 if(element.getLocalName().equalsIgnoreCase("minInclusive")) { 159 Node node2 = element.getFirstChild(); 160 if( node2.getNodeType()==Node.TEXT_NODE) { 161 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 162 XSD_DATE_FORMAT); 163 try { 164 this.m_dMinInclusive = df.parse( ((Text )node2).getNodeValue() ); 165 } catch (DOMException e) { 166 e.printStackTrace(); 167 } catch (ParseException e) { 168 e.printStackTrace(); 169 } 170 } 171 } else if(element.getLocalName().equalsIgnoreCase("maxInclusive")) { 172 Node node2 = element.getFirstChild(); 173 if( node2.getNodeType()==Node.TEXT_NODE) { 174 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 175 XSD_DATE_FORMAT); 176 try { 177 this.m_dMaxInclusive = df.parse( ((Text )node2).getNodeValue() ); 178 } catch (DOMException e) { 179 e.printStackTrace(); 180 } catch (ParseException e) { 181 e.printStackTrace(); 182 } 183 } 184 } else if(element.getLocalName().equalsIgnoreCase("minExclusive")) { 185 Node node2 = element.getFirstChild(); 186 if( node2.getNodeType()==Node.TEXT_NODE) { 187 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 188 XSD_DATE_FORMAT); 189 try { 190 this.m_dMinExclusive = df.parse( ((Text )node2).getNodeValue() ); 191 } catch (DOMException e) { 192 e.printStackTrace(); 193 } catch (ParseException e) { 194 e.printStackTrace(); 195 } 196 } 197 } else if(element.getLocalName().equalsIgnoreCase("maxExclusive")) { 198 Node node2 = element.getFirstChild(); 199 if( node2.getNodeType()==Node.TEXT_NODE) { 200 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 201 XSD_DATE_FORMAT); 202 try { 203 this.m_dMaxExclusive = df.parse( ((Text )node2).getNodeValue() ); 204 } catch (DOMException e) { 205 e.printStackTrace(); 206 } catch (ParseException e) { 207 e.printStackTrace(); 208 } 209 } 210 } 211 } 212 } 213 } 214 215 public String toString() { 216 StringBuffer sBuff = new StringBuffer (); 217 218 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ( 219 XSD_DATE_FORMAT); 220 221 sBuff.append("DateTimeRange:\n"); 222 if(this.m_dMinInclusive!=null) { 223 sBuff.append("minInclusive: ").append(df.format(this.m_dMinInclusive)).append("\n"); 224 } 225 if(this.m_dMaxInclusive!=null) { 226 sBuff.append("maxInclusive: ").append(df.format(this.m_dMaxInclusive)).append("\n"); 227 } 228 if(this.m_dMinExclusive!=null) { 229 sBuff.append("minExclusive: ").append(df.format(this.m_dMinExclusive)).append("\n"); 230 } 231 if(this.m_dMaxExclusive!=null) { 232 sBuff.append("maxExclusive: ").append(df.format(this.m_dMaxExclusive)).append("\n"); 233 } 234 235 return sBuff.toString(); 236 } 237 238 244 private Date getDate(String sValue) { 245 Date date = null; 246 247 try { 248 if(sValue != null && sValue.length() > 0) { 249 date = df.parse(sValue); 250 } 251 } catch (ParseException e) { 252 e.printStackTrace(); 253 } 254 255 return date; 256 } 257 258 } 259 | Popular Tags |