1 40 package org.dspace.storage.rdbms; 41 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.Map ; 46 47 import org.dspace.core.ConfigurationManager; 48 49 55 public class TableRow 56 { 57 58 private static final Object NULL_OBJECT = new Object (); 59 60 61 private String table; 62 63 68 private Map data = new HashMap (); 69 70 81 public TableRow(String table, List columns) 82 { 83 this.table = table; 84 nullColumns(columns); 85 } 86 87 93 public String getTable() 94 { 95 return table; 96 } 97 98 105 public boolean hasColumn(String column) 106 { 107 return data.get(canonicalize(column)) != null; 108 } 109 110 117 public boolean isColumnNull(String column) 118 { 119 if (!hasColumn(column)) 120 { 121 throw new IllegalArgumentException ("No such column " + column); 122 } 123 124 return data.get(canonicalize(column)) == NULL_OBJECT; 125 } 126 127 138 public int getIntColumn(String column) 139 { 140 if (!hasColumn(column)) 141 { 142 throw new IllegalArgumentException ("No such column " + column); 143 } 144 145 String name = canonicalize(column); 146 147 if (isColumnNull(name)) 148 { 149 return -1; 150 } 151 152 Object value = data.get(name); 153 154 if (value == null) 155 { 156 throw new IllegalArgumentException ("Column " + column 157 + " not present"); 158 } 159 160 if (!(value instanceof Integer )) 161 { 162 throw new IllegalArgumentException ("Value for " + column 163 + " is not an integer"); 164 } 165 166 return ((Integer ) value).intValue(); 167 } 168 169 179 public long getLongColumn(String column) 180 { 181 if (!hasColumn(column)) 182 { 183 throw new IllegalArgumentException ("No such column " + column); 184 } 185 186 String name = canonicalize(column); 187 188 if (isColumnNull(name)) 189 { 190 return -1; 191 } 192 193 Object value = data.get(name); 194 195 if (value == null) 196 { 197 throw new IllegalArgumentException ("Column " + column 198 + " not present"); 199 } 200 201 if (!(value instanceof Long )) 202 { 203 throw new IllegalArgumentException ("Value is not an long"); 204 } 205 206 return ((Long ) value).longValue(); 207 } 208 209 220 public String getStringColumn(String column) 221 { 222 if (!hasColumn(column)) 223 { 224 throw new IllegalArgumentException ("No such column " + column); 225 } 226 227 String name = canonicalize(column); 228 229 if (isColumnNull(name)) 230 { 231 return null; 232 } 233 234 Object value = data.get(name); 235 236 if (value == null) 237 { 238 throw new IllegalArgumentException ("Column " + column 239 + " not present"); 240 } 241 242 if (!(value instanceof String )) 243 { 244 throw new IllegalArgumentException ("Value is not an string"); 245 } 246 247 return (String ) value; 248 } 249 250 261 public boolean getBooleanColumn(String column) 262 { 263 if (!hasColumn(column)) 264 { 265 throw new IllegalArgumentException ("No such column " + column); 266 } 267 268 String name = canonicalize(column); 269 270 if (isColumnNull(name)) 271 { 272 return false; 273 } 274 275 Object value = data.get(name); 276 277 if (value == null) 279 { 280 throw new IllegalArgumentException ("Column " + column 281 + " not present"); 282 } 283 284 if ((value instanceof Boolean )) 285 { 286 return ((Boolean ) value).booleanValue(); 287 } 288 else if ((value instanceof Integer )) 289 { 290 int i = ((Integer ) value).intValue(); 291 292 if (i == 0) 293 { 294 return false; } 296 297 return true; } 299 else 300 { 301 throw new IllegalArgumentException ( 302 "Value is not a boolean or an integer"); 303 } 304 } 305 306 317 public java.util.Date getDateColumn(String column) 318 { 319 if (!hasColumn(column)) 320 { 321 throw new IllegalArgumentException ("No such column " + column); 322 } 323 324 String name = canonicalize(column); 325 326 if (isColumnNull(name)) 327 { 328 return null; 329 } 330 331 Object value = data.get(name); 332 333 if (value == null) 334 { 335 throw new IllegalArgumentException ("Column " + column 336 + " not present"); 337 } 338 339 if (!(value instanceof java.util.Date )) 340 { 341 throw new IllegalArgumentException ("Value is not a Date"); 342 } 343 344 return (java.util.Date ) value; 345 } 346 347 355 public void setColumnNull(String column) 356 { 357 if (!hasColumn(column)) 358 { 359 throw new IllegalArgumentException ("No such column " + column); 360 } 361 362 setColumnNullInternal(canonicalize(column)); 363 } 364 365 375 public void setColumn(String column, boolean b) 376 { 377 if (!hasColumn(column)) 378 { 379 throw new IllegalArgumentException ("No such column " + column); 380 } 381 382 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 383 { 384 data.put(canonicalize(column), b ? new Integer (1) : new Integer (0)); 386 } 387 else 388 { 389 data.put(canonicalize(column), b ? Boolean.TRUE : Boolean.FALSE); 391 } 392 } 393 394 404 public void setColumn(String column, String s) 405 { 406 if (!hasColumn(column)) 407 { 408 throw new IllegalArgumentException ("No such column " + column); 409 } 410 411 data.put(canonicalize(column), (s == null) ? NULL_OBJECT : s); 412 } 413 414 424 public void setColumn(String column, int i) 425 { 426 if (!hasColumn(column)) 427 { 428 throw new IllegalArgumentException ("No such column " + column); 429 } 430 431 data.put(canonicalize(column), new Integer (i)); 432 } 433 434 444 public void setColumn(String column, long l) 445 { 446 if (!hasColumn(column)) 447 { 448 throw new IllegalArgumentException ("No such column " + column); 449 } 450 451 data.put(canonicalize(column), new Long (l)); 452 } 453 454 465 public void setColumn(String column, java.util.Date d) 466 { 467 if (!hasColumn(column)) 468 { 469 throw new IllegalArgumentException ("No such column " + column); 470 } 471 472 if (d == null) 473 { 474 setColumnNull(canonicalize(column)); 475 476 return; 477 } 478 479 data.put(canonicalize(column), d); 480 } 481 482 486 491 public String toString() 492 { 493 final String NEWLINE = System.getProperty("line.separator"); 494 StringBuffer result; 495 496 if (table==null) 497 { 498 result = new StringBuffer ("no_table"); 499 } 500 else 501 { 502 result = new StringBuffer (table); 503 } 504 505 result.append(NEWLINE); 506 507 for (Iterator iterator = data.keySet().iterator(); iterator.hasNext();) 508 { 509 String column = (String ) iterator.next(); 510 result.append("\t").append(column).append(" = ").append( 511 isColumnNull(column) ? "NULL" : data.get(column)).append( 512 NEWLINE); 513 } 514 515 return result.toString(); 516 } 517 518 523 public int hashCode() 524 { 525 return toString().hashCode(); 526 } 527 528 534 public boolean equals(Object obj) 535 { 536 if (!(obj instanceof TableRow)) 537 { 538 return false; 539 } 540 541 return data.equals(((TableRow) obj).data); 542 } 543 544 551 static String canonicalize(String column) 552 { 553 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 554 { 555 return column.toUpperCase(); 557 } 558 559 return column.toLowerCase(); 561 } 562 563 570 private void nullColumns(List columns) 571 { 572 for (Iterator iterator = columns.iterator(); iterator.hasNext();) 573 { 574 setColumnNullInternal((String ) iterator.next()); 575 } 576 } 577 578 584 private void setColumnNullInternal(String column) 585 { 586 data.put(canonicalize(column), NULL_OBJECT); 587 } 588 } 589 | Popular Tags |