1 30 package com.genimen.djeneric.tools.export; 31 32 import java.io.BufferedInputStream ; 33 import java.io.BufferedOutputStream ; 34 import java.io.File ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.ObjectInputStream ; 39 import java.io.ObjectOutputStream ; 40 import java.io.OutputStream ; 41 import java.io.StreamCorruptedException ; 42 import java.io.UnsupportedEncodingException ; 43 import java.math.BigDecimal ; 44 import java.sql.ResultSet ; 45 import java.sql.SQLException ; 46 import java.sql.Timestamp ; 47 import java.sql.Types ; 48 import java.util.HashMap ; 49 50 import com.genimen.djeneric.language.Messages; 51 import com.genimen.djeneric.repository.DjDomain; 52 import com.genimen.djeneric.repository.DjExtent; 53 import com.genimen.djeneric.repository.DjProperty; 54 import com.genimen.djeneric.repository.exceptions.DjenericException; 55 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 56 import com.genimen.djeneric.repository.rdbms.RdbmsExtent; 57 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager; 58 import com.genimen.djeneric.repository.rdbms.RdbmsSession; 59 import com.genimen.djeneric.repository.rdbms.SqlStatement; 60 import com.genimen.djeneric.util.DjLogger; 61 import com.genimen.djeneric.util.DjProgressDisplayer; 62 63 public class Dumper 64 { 65 private HashMap _ignoreList = new HashMap (); 66 67 private static final String INVALID_FILE_MSG = Messages.getString("Dumper.ImportFileInvalid"); 68 private static final String CP1252 = "Cp1252"; 69 private static final String UTF8 = "UTF8"; 70 71 public final static String HEADER = "DJENERIC_EXPORT_DUMP"; 72 73 RdbmsPersistenceManager _manager; 74 DjProgressDisplayer _progressMonitor; 75 RdbmsSession _session = null; 76 77 public Dumper(DjProgressDisplayer progressMonitor, RdbmsPersistenceManager manager) 78 { 79 _progressMonitor = progressMonitor; 80 _manager = manager; 81 } 82 83 public void ignoreExtent(String extentName) 84 { 85 _ignoreList.put(extentName, extentName); 86 } 87 88 public void setContext(String context) throws ObjectNotDefinedException, DjenericException 89 { 90 _manager.setCurrentContext(context); 91 } 92 93 public RdbmsPersistenceManager getManager() 94 { 95 return _manager; 96 } 97 98 public RdbmsSession getSession() throws DjenericException 99 { 100 if (_session == null) _session = (RdbmsSession) _manager.createSession(); 101 return _session; 102 } 103 104 public void close() 105 { 106 if (_manager != null) _manager.close(); 107 } 108 109 public void println(String msg) 110 { 111 _progressMonitor.displayProgress(msg); 112 } 113 114 public long exportDump(File outFile) throws IOException , DjenericException, SQLException 115 { 116 OutputStream outputStream = new FileOutputStream (outFile); 117 ObjectOutputStream oos = new ObjectOutputStream (new BufferedOutputStream (outputStream)); 118 119 oos.write(HEADER.getBytes(UTF8)); 120 byte[] modelBytes = _manager.getModel().getBytes(UTF8); 121 oos.writeInt(modelBytes.length); 122 oos.write(modelBytes); 123 124 DjExtent extent[] = _manager.getExtents(); 125 long counter = 0; 126 for (int i = 0; i < extent.length; i++) 127 { 128 if (!_ignoreList.containsKey(extent[i].getName())) counter += exportExtent(oos, (RdbmsExtent) extent[i]); 129 } 130 131 oos.close(); 132 _progressMonitor.displayProgress(Messages.getString("Dumper.ExportDone", String.valueOf(counter))); 133 close(); 134 135 return counter; 136 } 137 138 protected long exportExtent(ObjectOutputStream oos, RdbmsExtent extent) throws DjenericException, SQLException , 139 IOException 140 { 141 _progressMonitor.displayProgress(Messages.getString("Dumper.ExportingExtent", extent.getName())); 142 byte[] extentAlias = extent.getInternalCode().getBytes(UTF8); 143 144 StringBuffer sqlStmt = new StringBuffer (100); 145 sqlStmt.append("select "); 146 DjProperty props[] = extent.getProperties(); 147 148 for (int i = 0; i < props.length; i++) 149 { 150 if (i != 0) sqlStmt.append(", "); 151 sqlStmt.append(props[i].getName()); 152 } 153 sqlStmt.append(" from "); 154 sqlStmt.append(extent.getName()); 155 156 RdbmsSession session = (RdbmsSession) _manager.createSession(); 157 SqlStatement stmt = session.getSqlStatement(sqlStmt.toString()); 158 ResultSet rs = stmt.executeQuery(); 159 long counter = 0; 160 161 while (rs.next()) 162 { 163 counter++; 164 oos.writeInt(extentAlias.length); 165 oos.write(extentAlias); 166 for (int i = 0; i < props.length; i++) 167 { 168 int typeCode = props[i].getTypeCode(); 169 170 switch (typeCode) 171 { 172 case DjDomain.BIGDECIMAL_TYPE : 173 writeBigDecimal(oos, rs, i); 174 break; 175 case DjDomain.BYTE_TYPE : 176 writeBytes(oos, props, rs, i); 177 break; 178 case DjDomain.DATE_TYPE : 179 writeDate(oos, rs, i); 180 break; 181 case DjDomain.INT_TYPE : 182 writeInt(oos, rs, i); 183 break; 184 case DjDomain.LONG_TYPE : 185 writeLong(oos, rs, i); 186 break; 187 case DjDomain.STRING_TYPE : 188 writeString(oos, props, rs, i); 189 break; 190 default : 191 throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode))); 192 } 193 } 194 } 195 rs.close(); 196 stmt.close(); 197 String name = (counter == 1 ? extent.getNameSingular() : extent.getNamePlural()); 198 _progressMonitor 199 .displayProgress(" " + Messages.getString("Dumper.exported", String.valueOf(counter), name) + "\n"); 200 return counter; 201 202 } 203 204 protected void writeString(ObjectOutputStream oos, DjProperty[] props, ResultSet rs, int i) throws SQLException , 205 IOException , UnsupportedEncodingException 206 { 207 if (props[i].isMappedToLong()) 208 { 209 byte[] b = SqlStatement.getBlob(rs, props[i].getName()); 210 oos.writeBoolean(b == null); 211 if (b != null) 212 { 213 oos.writeInt(b.length); 214 oos.write(b); 215 } 216 } 217 else 218 { 219 String s = rs.getString(i + 1); 220 boolean wasNull = rs.wasNull(); 221 oos.writeBoolean(wasNull); 222 if (!wasNull) 223 { 224 byte[] b = s.getBytes(CP1252); 225 oos.writeInt(b.length); 226 oos.write(b); 227 } 228 } 229 } 230 231 protected void writeLong(ObjectOutputStream oos, ResultSet rs, int i) throws SQLException , IOException 232 { 233 long l = rs.getLong(i + 1); 234 boolean wasNull = rs.wasNull(); 235 oos.writeBoolean(wasNull); 236 if (!wasNull) oos.writeLong(l); 237 } 238 239 protected void writeInt(ObjectOutputStream oos, ResultSet rs, int i) throws SQLException , IOException 240 { 241 int integer = rs.getInt(i + 1); 242 boolean wasNull = rs.wasNull(); 243 oos.writeBoolean(wasNull); 244 if (!wasNull) oos.writeInt(integer); 245 } 246 247 protected void writeDate(ObjectOutputStream oos, ResultSet rs, int i) throws SQLException , IOException 248 { 249 Timestamp date = rs.getTimestamp(i + 1); 250 boolean wasNull = rs.wasNull(); 251 oos.writeBoolean(wasNull); 252 if (!wasNull) oos.writeLong(date.getTime()); 253 } 254 255 protected void writeBytes(ObjectOutputStream oos, DjProperty[] props, ResultSet rs, int i) throws SQLException , 256 IOException 257 { 258 if (props[i].isMappedToLong()) 259 { 260 byte[] b = SqlStatement.getBlob(rs, props[i].getName()); 261 boolean wasNull = rs.wasNull(); 262 oos.writeBoolean(wasNull); 263 if (!wasNull) 264 { 265 oos.writeInt(b.length); 266 oos.write(b); 267 } 268 } 269 else 270 { 271 String s = rs.getString(i + 1); 272 boolean wasNull = rs.wasNull(); 273 oos.writeBoolean(wasNull); 274 if (!wasNull) 275 { 276 byte[] b = s.getBytes(CP1252); 277 oos.writeInt(b.length); 278 oos.write(b); 279 } 280 } 281 } 282 283 protected void writeBigDecimal(ObjectOutputStream oos, ResultSet rs, int i) throws SQLException , IOException 284 { 285 BigDecimal bd = rs.getBigDecimal(i + 1); 286 boolean wasNull = rs.wasNull(); 287 oos.writeBoolean(wasNull); 288 if (!wasNull) oos.writeFloat(bd.floatValue()); 289 } 290 291 public HashMap determineNewIds(InputStream inputStream) throws IOException , DjenericException 292 { 293 _progressMonitor.displayProgress(Messages.getString("Dumper.DetermineNewIds")); 294 HashMap newids = new HashMap (); 295 296 ObjectInputStream ois = new ObjectInputStream (new BufferedInputStream (inputStream)); 297 298 byte[] header = new byte[HEADER.length()]; 299 300 ois.readFully(header); 301 if (!new String (header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG); 302 int modelSize = ois.readInt(); 303 304 byte[] modelBytes = new byte[modelSize]; 305 306 ois.readFully(modelBytes); 307 String sourceModel = new String (modelBytes, UTF8); 308 309 ImportPersistenceManager srcMgr = new ImportPersistenceManager(); 310 srcMgr.setModelManually(sourceModel); 311 srcMgr.loadModel(); 312 313 while (ois.available() > 0) 314 { 315 determineNewId(ois, newids, srcMgr); 316 } 317 318 ois.close(); 319 return newids; 320 } 321 322 protected void determineNewId(ObjectInputStream ois, HashMap newIds, ImportPersistenceManager srcMgr) 323 throws IOException , DjenericException 324 { 325 int length = ois.readInt(); 326 byte[] extentAliasBuffer = new byte[length]; 327 ois.readFully(extentAliasBuffer); 328 String extentAlias = new String (extentAliasBuffer, UTF8); 329 330 DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias); 331 DjProperty idProperty = extent.getIdProperty(); 332 DjProperty props[] = extent.getProperties(); 333 334 for (int i = 0; i < props.length; i++) 335 { 336 int typeCode = props[i].getTypeCode(); 337 338 switch (typeCode) 339 { 340 case DjDomain.BIGDECIMAL_TYPE : 341 readBigDecimal(ois); 342 break; 343 case DjDomain.BYTE_TYPE : 344 readBytes(ois); 345 break; 346 case DjDomain.DATE_TYPE : 347 readDate(ois); 348 break; 349 case DjDomain.INT_TYPE : 350 readInt(ois); 351 break; 352 case DjDomain.LONG_TYPE : 353 Long someLong = readLong(ois); 354 if (props[i] == idProperty) 355 { 356 newIds.put(someLong, new Long (_manager.getNextObjectId())); 357 } 358 break; 359 case DjDomain.STRING_TYPE : 360 readString(ois); 361 break; 362 default : 363 throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode))); 364 } 365 } 366 } 367 368 protected void checkModel(InputStream inputStream) throws StreamCorruptedException , IOException , DjenericException 369 { 370 ObjectInputStream oos = new ObjectInputStream (new BufferedInputStream (inputStream)); 371 372 byte[] header = new byte[HEADER.length()]; 373 374 oos.readFully(header); 375 if (!new String (header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG); 376 int modelSize = oos.readInt(); 377 378 byte[] modelBytes = new byte[modelSize]; 379 380 oos.readFully(modelBytes); 381 String sourceModel = new String (modelBytes, UTF8); 382 383 if (_manager.getExtents().length == 0) 386 { 387 _manager.setModel(sourceModel); 388 _manager.saveModel(); 389 println(Messages.getString("Dumper.EmptyReposModelImported")); 390 } 391 oos.close(); 392 } 393 394 protected long importDump(InputStream inputStream, HashMap newids) throws IOException , SQLException , 395 DjenericException 396 { 397 _progressMonitor.displayProgress(Messages.getString("Dumper.ImportFirstPass")); 398 ObjectInputStream oos = new ObjectInputStream (new BufferedInputStream (inputStream)); 399 400 byte[] header = new byte[HEADER.length()]; 401 402 oos.readFully(header); 403 if (!new String (header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG); 404 int modelSize = oos.readInt(); 405 406 byte[] modelBytes = new byte[modelSize]; 407 408 oos.readFully(modelBytes); 409 String sourceModel = new String (modelBytes, UTF8); 410 411 if (_manager.getExtents().length == 0) 414 { 415 _manager.setModel(sourceModel); 416 _manager.saveModel(); 417 println(Messages.getString("Dumper.EmptyReposModelImported")); 418 } 419 420 ImportPersistenceManager srcMgr = new ImportPersistenceManager(); 421 srcMgr.setModelManually(sourceModel); 422 srcMgr.loadModel(); 423 424 long counter = 0; 425 while (oos.available() > 0) 426 { 427 importRecordPass1(oos, srcMgr, newids); 428 counter++; 429 if (counter % 125 == 0) _progressMonitor.displayProgress(Messages.getString("Dumper.ImportProgress", String 430 .valueOf(counter))); 431 } 432 _progressMonitor.displayProgress(Messages.getString("Dumper.FirstPassDone", String.valueOf(counter))); 433 434 srcMgr.close(); 435 436 oos.close(); 437 return counter; 438 } 439 440 protected long importRelations(InputStream inputStream, HashMap newids) throws IOException , SQLException , 441 DjenericException 442 { 443 _progressMonitor.displayProgress(Messages.getString("Dumper.ImportRelations")); 444 445 ObjectInputStream oos = new ObjectInputStream (new BufferedInputStream (inputStream)); 446 447 byte[] header = new byte[HEADER.length()]; 448 449 oos.readFully(header); 450 if (!new String (header, UTF8).equals(HEADER)) throw new DjenericException(INVALID_FILE_MSG); 451 int modelSize = oos.readInt(); 452 453 byte[] modelBytes = new byte[modelSize]; 454 455 oos.readFully(modelBytes); 456 String sourceModel = new String (modelBytes, UTF8); 457 458 ImportPersistenceManager srcMgr = new ImportPersistenceManager(); 459 srcMgr.setModelManually(sourceModel); 460 srcMgr.loadModel(); 461 462 long counter = 0; 463 while (oos.available() > 0) 464 { 465 if (importRecordPass2(oos, srcMgr, newids)) counter++; 466 if (counter % 125 == 0 && counter > 0) _progressMonitor.displayProgress(Messages 467 .getString("Dumper.ImportAssocProgress", String.valueOf(counter))); 468 } 469 _progressMonitor.displayProgress(Messages.getString("Dumper.ImportAssocDone", String.valueOf(counter))); 470 srcMgr.close(); 471 472 oos.close(); 473 return counter; 474 } 475 476 HashMap _stmts = new HashMap (); 477 478 protected void importRecordPass1(ObjectInputStream oos, ImportPersistenceManager srcMgr, HashMap newids) 479 throws IOException , SQLException , DjenericException 480 { 481 int length = oos.readInt(); 482 byte[] extentAliasBuffer = new byte[length]; 483 oos.readFully(extentAliasBuffer); 484 String extentAlias = new String (extentAliasBuffer, UTF8); 485 486 DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias); 487 DjProperty props[] = extent.getProperties(); 488 489 String theStmt = (String ) _stmts.get(extent.getInternalCode()); 490 if (theStmt == null) 491 { 492 theStmt = buildInsertStmt(extent, props); 493 _stmts.put(extent.getInternalCode(), theStmt); 494 } 495 496 DjProperty idproperty = extent.getIdProperty(); 497 498 SqlStatement stmt = getSession().getSqlStatement(theStmt); 499 for (int i = 0; i < props.length; i++) 500 { 501 String colName = props[i].getName(); 502 503 int typeCode = props[i].getTypeCode(); 504 505 switch (typeCode) 506 { 507 case DjDomain.BIGDECIMAL_TYPE : 508 BigDecimal bd = readBigDecimal(oos); 509 if (bd != null) stmt.setBigDecimal(colName, bd); 510 else stmt.setNull(colName, Types.DECIMAL); 511 break; 512 case DjDomain.BYTE_TYPE : 513 byte[] bytes = readBytes(oos); 514 if (bytes != null) stmt.setBytes(colName, bytes); 515 else stmt.setNull(colName, Types.BINARY); 516 break; 517 case DjDomain.DATE_TYPE : 518 Timestamp date = readDate(oos); 519 if (date != null) stmt.setTimestamp(colName, date); 520 else stmt.setNull(colName, Types.TIMESTAMP); 521 break; 522 case DjDomain.INT_TYPE : 523 Integer ii = readInt(oos); 524 if (ii != null) stmt.setInt(colName, ii.intValue()); 525 else stmt.setNull(colName, Types.INTEGER); 526 break; 527 case DjDomain.LONG_TYPE : 528 529 Long ll = readLong(oos); 530 if (props[i] == idproperty) 532 { 533 stmt.setLong(colName, ((Long ) newids.get(ll)).longValue()); 534 } 535 else 536 if (props[i].isMappedToRelation()) 538 { 539 stmt.setNull(colName, Types.BIGINT); 540 } 541 else 542 { if (ll != null) stmt.setLong(colName, ll.longValue()); 544 else stmt.setNull(colName, Types.BIGINT); 545 } 546 break; 547 case DjDomain.STRING_TYPE : 548 String s = readString(oos); 549 if (s != null) stmt.setString(colName, s); 550 else stmt.setNull(colName, Types.VARCHAR); 551 break; 552 default : 553 throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode))); 554 } 555 } 556 if (!_ignoreList.containsKey(extent.getName())) stmt.executeUpdate(); 557 } 558 559 HashMap _updStmts = new HashMap (); 560 HashMap _skipExtents = new HashMap (); 561 562 protected boolean importRecordPass2(ObjectInputStream oos, ImportPersistenceManager srcMgr, HashMap newids) 563 throws IOException , SQLException , DjenericException 564 { 565 int length = oos.readInt(); 566 byte[] extentAliasBuffer = new byte[length]; 567 oos.readFully(extentAliasBuffer); 568 String extentAlias = new String (extentAliasBuffer, UTF8); 569 570 DjExtent extent = srcMgr.getExtentByInternalCode(extentAlias); 571 DjProperty props[] = extent.getProperties(); 572 DjProperty idproperty = extent.getIdProperty(); 573 574 Boolean shouldSkip = (Boolean ) _skipExtents.get(extent.getInternalCode()); 575 if (shouldSkip == null) 576 { 577 shouldSkip = containsNoRels(props); 578 _skipExtents.put(extent.getInternalCode(), shouldSkip); 579 } 580 581 boolean result = false; 582 if (shouldSkip.booleanValue()) 583 { 584 skipRecord(oos, props); 585 } 586 else 587 { 588 String theStmt = (String ) _updStmts.get(extent.getInternalCode()); 589 if (theStmt == null) 590 { 591 theStmt = buildUpdateStmt(extent, props, idproperty); 592 _updStmts.put(extent.getInternalCode(), theStmt); 593 } 594 595 SqlStatement stmt = getSession().getSqlStatement(theStmt); 596 597 for (int i = 0; i < props.length; i++) 598 { 599 String colName = props[i].getName(); 600 601 int typeCode = props[i].getTypeCode(); 602 603 switch (typeCode) 604 { 605 case DjDomain.BIGDECIMAL_TYPE : 606 readBigDecimal(oos); 607 break; 608 case DjDomain.BYTE_TYPE : 609 readBytes(oos); 610 break; 611 case DjDomain.DATE_TYPE : 612 readDate(oos); 613 break; 614 case DjDomain.INT_TYPE : 615 readInt(oos); 616 break; 617 case DjDomain.LONG_TYPE : 618 619 Long ll = readLong(oos); 620 if (props[i] == idproperty || props[i].isMappedToRelation()) 622 { 623 if (ll != null) 624 { 625 Long lo = (Long ) newids.get(ll); 626 if (lo != null) stmt.setLong(colName, lo.longValue()); 627 else 628 { 629 DjLogger.log(props[i].getName() + " set to null"); 630 stmt.setNull(colName, Types.BIGINT); 631 } 632 } 633 else stmt.setNull(colName, Types.BIGINT); 634 } 635 break; 636 case DjDomain.STRING_TYPE : 637 readString(oos); 638 break; 639 default : 640 throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode))); 641 } 642 } 643 if (!_ignoreList.containsKey(extent.getName())) 644 { 645 stmt.executeUpdate(); 646 result = true; 647 } 648 } 649 return result; 650 } 651 652 protected Boolean containsNoRels(DjProperty[] props) 653 { 654 Boolean shouldSkip; 655 boolean ss = true; 656 for (int i = 0; i < props.length; i++) 657 { 658 if (props[i].isMappedToRelation()) 659 { 660 ss = false; 661 break; 662 } 663 } 664 shouldSkip = new Boolean (ss); 665 return shouldSkip; 666 } 667 668 protected DjExtent resolveDestinationExtent(DjExtent srcExtent) throws ObjectNotDefinedException 669 { 670 DjExtent extent = null; 671 672 try 673 { 674 extent = _manager.getExtentByInternalCode(srcExtent.getInternalCode()); 675 } 676 catch (ObjectNotDefinedException onde) 677 { 678 } 679 680 try 681 { 682 if (extent == null) extent = _manager.getExtentByObjectType(srcExtent.getObjectType()); 683 } 684 catch (ObjectNotDefinedException onde) 685 { 686 } 687 688 if (extent == null) extent = _manager.getExtent(srcExtent.getName()); 689 690 return extent; 691 } 692 693 protected String buildInsertStmt(DjExtent srcExtent, DjProperty[] props) throws ObjectNotDefinedException 694 { 695 DjExtent extent = resolveDestinationExtent(srcExtent); 696 697 String theStmt; 698 StringBuffer sqlStmt = new StringBuffer (100); 699 sqlStmt.append("insert into "); 700 sqlStmt.append(extent.getName()); 701 sqlStmt.append("("); 702 703 for (int i = 0; i < props.length; i++) 704 { 705 if (i != 0) sqlStmt.append(", "); 706 sqlStmt.append(props[i].getName()); 707 } 708 sqlStmt.append(") values ("); 709 for (int i = 0; i < props.length; i++) 710 { 711 if (i != 0) sqlStmt.append(", "); 712 sqlStmt.append(":"); 713 sqlStmt.append(props[i].getName()); 714 } 715 sqlStmt.append(")"); 716 717 theStmt = sqlStmt.toString(); 718 return theStmt; 719 } 720 721 protected String buildUpdateStmt(DjExtent srcExtent, DjProperty[] props, DjProperty idproperty) 722 throws ObjectNotDefinedException 723 { 724 DjExtent extent = resolveDestinationExtent(srcExtent); 725 726 String theStmt; 727 StringBuffer sqlStmt = new StringBuffer (100); 728 sqlStmt.append("update "); 729 sqlStmt.append(extent.getName()); 730 sqlStmt.append(" set "); 731 732 boolean first = true; 733 for (int i = 0; i < props.length; i++) 734 { 735 if (!props[i].isMappedToRelation()) continue; 736 737 if (!first) sqlStmt.append(", "); 738 first = false; 739 sqlStmt.append(props[i].getName() + " = :" + props[i].getName()); 740 } 741 sqlStmt.append(" where " + idproperty.getName() + " = :" + idproperty.getName()); 742 743 theStmt = sqlStmt.toString(); 744 return theStmt; 745 } 746 747 protected void skipRecord(ObjectInputStream oos, DjProperty[] props) throws DjenericException, IOException 748 { 749 for (int i = 0; i < props.length; i++) 750 { 751 int typeCode = props[i].getTypeCode(); 752 753 switch (typeCode) 754 { 755 case DjDomain.BIGDECIMAL_TYPE : 756 readBigDecimal(oos); 757 break; 758 case DjDomain.BYTE_TYPE : 759 readBytes(oos); 760 break; 761 case DjDomain.DATE_TYPE : 762 readDate(oos); 763 break; 764 case DjDomain.INT_TYPE : 765 readInt(oos); 766 break; 767 case DjDomain.LONG_TYPE : 768 readLong(oos); 769 break; 770 case DjDomain.STRING_TYPE : 771 readString(oos); 772 break; 773 default : 774 throw new DjenericException(Messages.getString("global.PropUnsupported", String.valueOf(typeCode))); 775 } 776 } 777 } 778 779 protected String readString(ObjectInputStream oos) throws IOException , UnsupportedEncodingException 780 { 781 String result = null; 782 783 boolean wasNull = oos.readBoolean(); 784 if (!wasNull) 785 { 786 int xx = oos.readInt(); 787 byte[] buffer = new byte[xx]; 788 oos.readFully(buffer); 789 result = new String (buffer, CP1252); 790 } 791 return result; 792 } 793 794 protected Long readLong(ObjectInputStream oos) throws IOException 795 { 796 Long result = null; 797 798 boolean wasNull = oos.readBoolean(); 799 if (!wasNull) 800 { 801 result = new Long (oos.readLong()); 802 } 803 return result; 804 } 805 806 protected Integer readInt(ObjectInputStream oos) throws IOException 807 { 808 Integer result = null; 809 810 boolean wasNull = oos.readBoolean(); 811 if (!wasNull) 812 { 813 result = new Integer (oos.readInt()); 814 } 815 return result; 816 } 817 818 protected Timestamp readDate(ObjectInputStream oos) throws IOException 819 { 820 Timestamp result = null; 821 boolean wasNull = oos.readBoolean(); 822 if (!wasNull) 823 { 824 result = new Timestamp (oos.readLong()); 825 } 826 return result; 827 } 828 829 protected byte[] readBytes(ObjectInputStream oos) throws IOException 830 { 831 byte[] result = null; 832 boolean wasNull = oos.readBoolean(); 833 if (!wasNull) 834 { 835 int xx = oos.readInt(); 836 byte[] buffer = new byte[xx]; 837 oos.readFully(buffer); 838 result = buffer; 839 } 840 return result; 841 } 842 843 protected BigDecimal readBigDecimal(ObjectInputStream oos) throws IOException 844 { 845 BigDecimal result = null; 846 boolean wasNull = oos.readBoolean(); 847 if (!wasNull) 848 { 849 result = new BigDecimal (oos.readFloat()); 850 } 851 return result; 852 } 853 } | Popular Tags |