1 32 33 package com.knowgate.dataobjs; 34 35 import java.io.IOException ; 36 import java.io.FileNotFoundException ; 37 import java.io.File ; 38 39 import java.sql.SQLException ; 40 import java.sql.CallableStatement ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.Time ; 44 import java.sql.Timestamp ; 45 46 import java.lang.ClassNotFoundException ; 47 import java.lang.IllegalAccessException ; 48 import java.lang.InstantiationException ; 49 50 import java.util.Date ; 51 import java.util.Map ; 52 import java.util.Collection ; 53 import java.util.ArrayList ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 import java.util.LinkedList ; 57 import java.util.ListIterator ; 58 import java.util.Set ; 59 import java.util.Properties ; 60 61 import java.math.BigDecimal ; 62 63 import java.text.ParseException ; 64 import java.text.SimpleDateFormat ; 65 import java.text.DecimalFormat ; 66 import java.text.DecimalFormatSymbols ; 67 68 import org.xml.sax.SAXException ; 69 import org.xml.sax.SAXNotRecognizedException ; 70 import org.xml.sax.SAXNotSupportedException ; 71 import org.xml.sax.SAXParseException ; 72 73 import com.knowgate.debug.*; 74 import com.knowgate.jdc.*; 75 import com.knowgate.math.Money; 76 import com.knowgate.misc.Gadgets; 77 78 93 94 public class DBPersist implements Map { 95 96 102 103 public DBPersist (String sTableName, String sAuditClass) 104 throws IllegalStateException { 105 106 sTable = sTableName; 107 108 sAuditCls = sAuditClass; 109 sAuditUsr = ""; 110 sTransactId = ""; 111 bAllCaps = bHasLongVarBinaryData = false; 112 AllVals = new HashMap (); 113 } 114 115 123 124 public DBPersist (String sTableName, String sAuditClass, boolean bAllValuesUpperCase) 125 throws IllegalStateException { 126 127 sTable = sTableName; 128 129 sAuditCls = sAuditClass; 130 sAuditUsr = ""; 131 sTransactId = ""; 132 bAllCaps = bAllValuesUpperCase; 133 bHasLongVarBinaryData = false; 134 AllVals = new HashMap (); 135 } 136 137 142 public void allcaps(boolean bAllValuesUpperCase) { 143 bAllCaps = bAllValuesUpperCase; 144 } 145 146 151 public boolean allcaps() { 152 return bAllCaps; 153 } 154 155 158 public void setAuditUser (String sAuditUser) { 159 sAuditUsr = sAuditUser; 160 } 161 162 165 166 public void setAuditTransact (String sAuditTransact) { 167 sTransactId = sAuditTransact; 168 } 169 170 174 public boolean isEmpty() { 175 return AllVals.isEmpty(); 176 } 177 178 182 public int size() { 183 return AllVals.size(); 184 } 185 186 190 public void clear() { 191 AllVals.clear(); 192 } 193 194 200 public void clone(DBPersist oSource) { 201 sTable = oSource.getTableName(); 202 sAuditCls = oSource.getAuditClassName(); 203 AllVals = new HashMap (oSource.AllVals); 204 } 205 206 212 public boolean containsValue(Object oKey) { 213 return AllVals.containsValue(oKey); 214 } 215 216 223 public boolean containsKey(Object oKey) 224 throws NullPointerException { 225 if (oKey==null) throw new NullPointerException ("DBPersist.containsKey() field name cannot be null"); 226 return AllVals.containsKey(oKey); 227 } 228 229 235 236 public Object get(String sKey) { 237 return AllVals.get(sKey); 238 } 239 240 246 public Date getCreationDate(JDCConnection oConn) throws SQLException { 247 Date oDt; 248 ResultSet oRSet = null; 249 PreparedStatement oStmt = null; 250 DBTable oTbl = getTable(oConn); 251 LinkedList oList = oTbl.getPrimaryKey(); 252 ListIterator oIter; 253 String sSQL = "SELECT "+DB.dt_created+" FROM "+oTbl.getName()+" WHERE 1=1"; 254 oIter = oList.listIterator(); 255 while (oIter.hasNext()) 256 sSQL += " AND " + oIter.next() + "=?"; 257 try { 258 oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 259 int p=0; 260 oIter = oList.listIterator(); 261 while (oIter.hasNext()) 262 oStmt.setObject(++p, get(oIter.next())); 263 oRSet = oStmt.executeQuery(); 264 if (oRSet.next()) 265 oDt = oRSet.getDate(1); 266 else 267 oDt = null; 268 oRSet.close(); 269 oRSet=null; 270 oStmt.close(); 271 oStmt=null; 272 } catch (Exception xcpt) { 273 if (null!=oRSet) { try { oRSet.close(); } catch (Exception ignore) { } } 274 if (null!=oStmt) { try { oStmt.close(); } catch (Exception ignore) { } } 275 throw new SQLException ("DBPersist.getCreationDate() "+xcpt.getClass().getName()+" "+xcpt.getMessage()); 276 } 277 return oDt; 278 } 280 288 289 public Object get(Object oKey) throws NullPointerException { 290 if (oKey==null) throw new NullPointerException ("DBPersist.get() field name cannot be null"); 291 return AllVals.get(oKey); 292 } 293 294 301 302 public BigDecimal getDecimal(String sKey) 303 throws ClassCastException , NumberFormatException { 304 305 Object oDec = AllVals.get(sKey); 306 307 if (oDec==null) 308 return null; 309 else { 310 if (oDec.getClass().getName().equalsIgnoreCase("java.lang.String")) 311 return new BigDecimal ((String ) oDec); 312 else 313 return (BigDecimal ) oDec; 314 } 315 } 317 318 328 public String getDecimalFormated(String sKey, String sPattern) 329 throws ClassCastException , NumberFormatException ,NullPointerException , 330 IllegalArgumentException { 331 332 BigDecimal oDec = getDecimal(sKey); 333 334 if (oDec==null) 335 return null; 336 else { 337 return new DecimalFormat (sPattern).format(oDec.doubleValue()); 338 } 339 } 341 351 public String getDoubleFormated(String sKey, String sPattern) 352 throws ClassCastException , NumberFormatException ,NullPointerException , 353 IllegalArgumentException { 354 if (isNull(sKey)) 355 return null; 356 else 357 return new DecimalFormat (sPattern).format(getDouble(sKey)); 358 } 359 360 370 public String getFloatFormated(String sKey, String sPattern) 371 throws ClassCastException , NumberFormatException ,NullPointerException , 372 IllegalArgumentException { 373 if (isNull(sKey)) 374 return null; 375 else 376 return new DecimalFormat (sPattern).format(getFloat(sKey)); 377 } 378 379 388 public Money getMoney(String sKey) 389 throws NumberFormatException { 390 Object oVal = AllVals.get(sKey); 391 if (null!=oVal) 392 if (oVal.toString().length()>0) 393 return Money.parse(oVal.toString()); 394 else 395 return null; 396 else 397 return null; 398 } 400 407 public String getString(String sKey) throws NullPointerException { 408 return AllVals.get(sKey).toString(); 409 } 410 411 419 420 public String getStringNull(String sKey, String sDefault) { 421 Object oVal; 422 if (AllVals.containsKey(sKey)) { 423 oVal = AllVals.get(sKey); 424 if (null==oVal) 425 return sDefault; 426 else 427 return oVal.toString(); 428 } 429 else 430 return sDefault; 431 } 432 433 439 public Time getTimeOfDay(String sKey) { 440 Object oTm = AllVals.get(sKey); 441 if (null!=oTm) 442 return (Time ) oTm; 443 else 444 return null; 445 } 447 454 455 public short getShort(String sKey) throws java.lang.NullPointerException { 456 Object oVal = AllVals.get(sKey); 457 458 if (oVal==null) throw new NullPointerException (sKey + " is null"); 459 460 return Short.parseShort(oVal.toString()); 461 } 462 463 471 472 public double getDouble(String sKey) 473 throws NullPointerException , NumberFormatException { 474 Object oVal = AllVals.get(sKey); 475 Class oCls; 476 double dRetVal; 477 478 if (oVal==null) throw new NullPointerException (sKey + " is null"); 479 480 oCls = oVal.getClass(); 481 482 try { 483 if (oCls.equals(Short.TYPE)) 484 dRetVal = (double) ((Short ) oVal).shortValue(); 485 else if (oCls.equals(Integer.TYPE)) 486 dRetVal = (double) ((Integer ) oVal).intValue(); 487 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 488 dRetVal = ((java.math.BigDecimal ) oVal).doubleValue(); 489 else if (oCls.equals(Float.TYPE)) 490 dRetVal = ((Float ) oVal).floatValue(); 491 else if (oCls.equals(Double.TYPE)) 492 dRetVal = ((Double ) oVal).doubleValue(); 493 else 494 dRetVal = new Double (oVal.toString()).floatValue(); 495 } catch (ClassNotFoundException cnfe) { dRetVal = 0d; } 496 497 return dRetVal; 498 } 500 508 509 public float getFloat(String sKey) 510 throws NullPointerException , NumberFormatException { 511 Object oVal = AllVals.get(sKey); 512 Class oCls; 513 float fRetVal; 514 515 if (oVal==null) throw new NullPointerException (sKey + " is null"); 516 517 oCls = oVal.getClass(); 518 519 try { 520 if (oCls.equals(Short.TYPE)) 521 fRetVal = (float) ((Short ) oVal).shortValue(); 522 else if (oCls.equals(Integer.TYPE)) 523 fRetVal = (float) ((Integer ) oVal).intValue(); 524 else if (oCls.equals(Class.forName("java.math.BigDecimal"))) 525 fRetVal = ((java.math.BigDecimal ) oVal).floatValue(); 526 else if (oCls.equals(Float.TYPE)) 527 fRetVal = ((Float ) oVal).floatValue(); 528 else if (oCls.equals(Double.TYPE)) 529 fRetVal = ((Double ) oVal).floatValue(); 530 else 531 fRetVal = new Float (oVal.toString()).floatValue(); 532 } catch (ClassNotFoundException cnfe) { fRetVal = 0f; } 533 534 return fRetVal; 535 536 } 538 546 547 public int getInt(String sKey) 548 throws NullPointerException , NumberFormatException { 549 int iRetVal; 550 Object oInt = AllVals.get(sKey); 551 552 if (Integer.TYPE.equals(oInt.getClass())) 553 iRetVal = ((Integer )(oInt)).intValue(); 554 else 555 iRetVal = Integer.parseInt(oInt.toString()); 556 557 return iRetVal; 558 } 560 566 567 public Integer getInteger(String sKey) throws NumberFormatException { 568 Object oInt = AllVals.get(sKey); 569 570 if (null!=oInt) 571 if (Integer.TYPE.equals(oInt.getClass())) 572 return (Integer ) oInt; 573 else 574 return new Integer (oInt.toString()); 575 else 576 return null; 577 } 579 585 586 public Date getDate(String sKey) throws ClassCastException { 587 Object oDt = AllVals.get(sKey); 588 if (null!=oDt) 589 return (Date ) oDt; 590 else 591 return null; 592 } 594 600 601 public String getDateShort(String sKey) 602 throws ClassCastException { 603 Object oDt = AllVals.get(sKey); 604 if (null!=oDt) { 605 java.util.Date dDt = (java.util.Date ) oDt; 606 int y = dDt.getYear()+1900, m=dDt.getMonth()+1, d=dDt.getDate(); 607 return String.valueOf(y)+"-"+(m<10 ? "0" : "")+String.valueOf(m)+"-"+(d<10 ? "0" : "")+String.valueOf(d); 608 } 609 else 610 return null; 611 } 613 621 622 public String getDateFormated(String sKey, String sFormat) 623 throws ClassCastException { 624 Object oDt = AllVals.get(sKey); 625 SimpleDateFormat oSimpleDate; 626 627 if (null!=oDt) { 628 oSimpleDate = new SimpleDateFormat (sFormat); 629 return oSimpleDate.format((java.util.Date ) oDt); 630 } 631 else 632 return null; 633 } 635 642 public String getDateTime(String sKey) { 643 return getDateFormated(sKey, "yyyy-MM-dd hh:mm:ss"); 644 } 646 653 public String getDateTime24(String sKey) { 654 return getDateFormated(sKey, "yyyy-MM-dd HH:mm:ss"); 655 } 657 658 665 public java.sql.Date getSQLDate(String sKey) 666 throws ClassCastException { 667 java.sql.Date oRetVal; 668 Object oObj = AllVals.get(sKey); 669 670 if (oObj==null) { 671 oRetVal = null; 672 } else { 673 String sCls = oObj.getClass().getName(); 674 if (sCls.equals("java.sql.Date")) 675 oRetVal = (java.sql.Date ) oObj; 676 else if (sCls.equals("java.util.Date")) 677 oRetVal = new java.sql.Date (((java.util.Date )oObj).getTime()); 678 else if (sCls.equals("java.sql.Timestamp")) 679 oRetVal = new java.sql.Date (((java.sql.Timestamp )oObj).getTime()); 680 else 681 throw new ClassCastException ("DBPersist.getSQLDate() Cannot cast "+sCls+" to java.sql.Date"); 682 } 683 return oRetVal; 684 } 686 693 public Time getSQLTime(String sKey) 694 throws ClassCastException { 695 java.sql.Time oRetVal; 696 Object oObj = AllVals.get(sKey); 697 698 if (oObj==null) { 699 oRetVal = null; 700 } else { 701 String sCls = oObj.getClass().getName(); 702 if (sCls.equals("java.sql.Time")) 703 oRetVal = (java.sql.Time ) oObj; 704 else if (sCls.equals("java.util.Date")) 705 oRetVal = new java.sql.Time (((java.util.Date )oObj).getTime()); 706 else if (sCls.equals("java.sql.Timestamp")) 707 oRetVal = new java.sql.Time (((java.sql.Timestamp )oObj).getTime()); 708 else 709 throw new ClassCastException ("DBPersist.getSQLTime() Cannot cast "+sCls+" to java.sql.Time"); 710 } 711 return oRetVal; 712 } 714 720 public String getTime(String sKey) 721 throws ClassCastException { 722 Object oObj = AllVals.get(sKey); 723 724 if (oObj!=null) { 725 java.util.Date oDt = (java.util.Date ) oObj; 726 return (oDt.getHours()<10 ? "0" : "")+String.valueOf(oDt.getHours())+":"+(oDt.getMinutes()<10 ? "0" : "")+String.valueOf(oDt.getMinutes())+(oDt.getSeconds()<10 ? "0" : "")+":"+String.valueOf(oDt.getSeconds()); 727 } 728 else { 729 return null; 730 } 731 } 733 744 public int getIntervalPart(String sKey, String sPart) 745 throws NullPointerException , NumberFormatException , IllegalArgumentException { 746 if (sPart==null) throw new IllegalArgumentException ("DBPersist.getIntervalPart() interval part to get cannot be null"); 747 if (!sPart.equalsIgnoreCase("days")) throw new IllegalArgumentException ("DBPersist.getIntervalPart() interval part to get must be 'days'"); 748 Object oObj = AllVals.get(sKey); 749 if (oObj==null) throw new NullPointerException ("DBPersist.getIntervalPart() value of interval is null"); 750 String sTI = oObj.toString().toLowerCase(); 751 int iMons = sTI.indexOf("mons")<0 ? 0 : sTI.indexOf("mons")+4; 752 int iDays = sTI.indexOf("days"); 753 if (iDays<0) return 0; 754 return Integer.parseInt(Gadgets.removeChars(sTI.substring(iMons,iDays), " ")); 755 } 757 762 763 public Timestamp getTimestamp(String sKey) { 764 Object oDt = AllVals.get(sKey); 765 766 if (null!=oDt) 767 return new Timestamp (((java.util.Date ) oDt).getTime()); 768 else 769 return null; 770 } 772 776 public Set getItems() { 777 return AllVals.keySet(); 778 } 779 780 784 public Set keySet() { 785 return AllVals.keySet(); 786 } 787 788 791 public HashMap getItemMap() { 792 return AllVals; 793 } 794 795 799 public Collection values() { 800 return AllVals.values(); 801 } 802 803 807 public Set entrySet() { 808 return AllVals.entrySet(); 809 } 810 811 814 815 public Iterator iterator() { 816 return AllVals.values().iterator(); 817 } 818 819 823 824 public String getAuditClassName() { 825 return sAuditCls; 826 } 827 828 832 833 public String getTableName() { 834 return sTable; 835 } 836 837 842 843 public DBTable getTable() { 844 if (null==oTable) { 845 oTable = DBBind.getTable(sTable); 846 } 847 return oTable; 848 } 850 862 public DBTable getTable(JDCConnection oConn) 863 throws SQLException , IllegalStateException { 864 865 if (null==oTable) { 866 JDCConnectionPool oPool = oConn.getPool(); 867 868 if (null==oPool) { 869 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) 870 oTable = new DBTable(oConn.getCatalog(), null, sTable, 1); 871 else 872 oTable = new DBTable(oConn.getCatalog(), oConn.getSchemaName(), sTable, 1); 873 874 oTable.readColumns(oConn, oConn.getMetaData()); 875 } 876 else { 877 DBBind oBind = (DBBind) oPool.getDatabaseBinding(); 878 879 if (null==oBind) 880 throw new IllegalStateException ("Connection Pool for " + sAuditCls + " is not binded to the database."); 881 else 882 oTable = oBind.getDBTable(sTable); 883 } 884 } 885 return oTable; 886 } 887 888 894 895 public boolean isNull(String sKey) { 896 boolean bIsNull = !AllVals.containsKey(sKey); 897 if (!bIsNull) 898 bIsNull = AllVals.get(sKey)==null; 899 return bIsNull; 900 } 901 902 910 911 public boolean load(JDCConnection oConn, Object [] PKVals) throws SQLException { 912 if (oTable==null) { 913 oTable = getTable(oConn); 914 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 915 return oTable.loadRegister(oConn, PKVals, AllVals); 916 } else 917 return oTable.loadRegister(oConn, PKVals, AllVals); 918 } 919 920 929 public boolean load(JDCConnection oConn, String sKey) throws SQLException { 930 if (oTable==null) { 931 oTable = getTable(oConn); 932 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 933 return oTable.loadRegister(oConn, new Object []{sKey}, AllVals); 934 } else 935 return oTable.loadRegister(oConn, new Object []{sKey}, AllVals); 936 } 937 938 945 946 public void put(String sKey, String sVal) throws NullPointerException { 947 if (sKey==null) 948 throw new NullPointerException ("DBPersist.put(String,String) field name cannot be null"); 949 if (null==sVal) 950 AllVals.put(sKey, null); 951 else if (bAllCaps) 952 AllVals.put(sKey, sVal.toUpperCase()); 953 else 954 AllVals.put(sKey, sVal); 955 } 956 957 963 964 public void put(String sKey, Object oObj) throws NullPointerException { 965 if (sKey==null) 966 throw new NullPointerException ("DBPersist.put(String,Object) field name cannot be null"); 967 AllVals.put(sKey, oObj); 968 } 969 970 979 980 public Object put(Object sKey, Object oObj) throws NullPointerException { 981 Object oPrevious; 982 if (sKey==null) 983 throw new NullPointerException ("DBPersist.put(Object,Object) field name cannot be null"); 984 if (AllVals.containsKey(sKey)) { 985 oPrevious = AllVals.get(sKey); 986 AllVals.remove(sKey); 987 } else { 988 oPrevious = null; 989 } 990 AllVals.put(sKey, oObj); 991 return oPrevious; 992 } 993 994 999 1000 public void put(String sKey, int iVal) { 1001 AllVals.put(sKey, new Integer (iVal)); 1002 } 1003 1004 1009 1010 public void put(String sKey, short iVal) { 1011 AllVals.put(sKey, new Short (iVal)); 1012 } 1013 1014 1019 1020 public void put(String sKey, Date dtVal) { 1021 AllVals.put(sKey, dtVal); 1022 } 1023 1024 1030 1031 public void put(String sKey, Time tmVal) { 1032 AllVals.put(sKey, tmVal); 1033 } 1034 1035 1042 public void put(String sKey, String sDate, SimpleDateFormat oPattern) 1043 throws ParseException { 1044 AllVals.put(sKey, oPattern.parse(sDate)); 1045 } 1046 1047 1052 1053 public void put(String sKey, double dVal) { 1054 AllVals.put(sKey, new Double (dVal)); 1055 } 1056 1057 1062 1063 public void put(String sKey, BigDecimal oDecVal) { 1064 AllVals.put(sKey, oDecVal); 1065 } 1066 1067 1075 public void put(String sKey, String sDecVal, DecimalFormat oPattern) 1076 throws ParseException { 1077 AllVals.put(sKey, oPattern.parse(sDecVal)); 1078 } 1079 1080 1085 1086 public void put(String sKey, float fVal) { 1087 AllVals.put(sKey, new Float (fVal)); 1088 } 1089 1090 1096 1097 public void put(String sKey, Money mVal) { 1098 if (null==mVal) 1099 AllVals.put(sKey, null); 1100 else 1101 AllVals.put(sKey, mVal.toString()); 1102 } 1103 1104 1110 1111 public void put(String sKey, File oFile) throws FileNotFoundException { 1112 if (!bHasLongVarBinaryData) LongVarBinaryValsLen = new HashMap (); 1113 1114 LongVarBinaryValsLen.put(sKey, new Long (oFile.length())); 1115 1116 AllVals.put(sKey, oFile); 1117 1118 bHasLongVarBinaryData = true; 1119 } 1121 1127 1128 public void put(String sKey, byte[] aBytes) { 1129 if (!bHasLongVarBinaryData) LongVarBinaryValsLen = new HashMap (); 1130 1131 LongVarBinaryValsLen.put(sKey, new Long (aBytes.length)); 1132 1133 AllVals.put(sKey, aBytes); 1134 1135 bHasLongVarBinaryData = true; 1136 } 1138 1144 1145 public void put(String sKey, char[] aChars) { 1146 if (!bHasLongVarBinaryData) LongVarBinaryValsLen = new HashMap (); 1147 1148 LongVarBinaryValsLen.put(sKey, new Long (aChars.length)); 1149 1150 AllVals.put(sKey, aChars); 1151 1152 bHasLongVarBinaryData = true; 1153 } 1155 1166 1167 public void put(String sKey, String sData, int iSQLType) 1168 throws FileNotFoundException , IllegalArgumentException , NullPointerException { 1169 int iDecDot; 1170 1171 if (sKey==null) 1172 throw new NullPointerException ("DBPersist.put(String,String,int) field name cannot be null"); 1173 1174 switch (iSQLType) { 1175 case java.sql.Types.VARCHAR: 1176 case java.sql.Types.LONGVARCHAR: 1177 case java.sql.Types.CHAR: 1178 case java.sql.Types.CLOB: 1179 if (null==sData) 1180 AllVals.put(sKey, null); 1181 else if (bAllCaps) 1182 AllVals.put(sKey, sData.toUpperCase()); 1183 else 1184 AllVals.put(sKey, sData); 1185 break; 1186 case java.sql.Types.INTEGER: 1187 AllVals.put(sKey, new Integer (sData)); 1188 break; 1189 case java.sql.Types.SMALLINT: 1190 AllVals.put(sKey, new Short (sData)); 1191 break; 1192 case java.sql.Types.DATE: 1193 if (null != sData) 1194 AllVals.put(sKey, java.sql.Date.valueOf(sData)); 1195 else 1196 AllVals.put(sKey, null); 1197 break; 1198 case java.sql.Types.TIME: 1199 if (null != sData) 1200 AllVals.put(sKey, java.sql.Time.valueOf(sData)); 1201 else 1202 AllVals.put(sKey, null); 1203 break; 1204 case java.sql.Types.TIMESTAMP: 1205 if (null != sData) 1206 AllVals.put(sKey, new java.sql.Timestamp (java.util.Date.parse(sData))); 1207 else 1208 AllVals.put(sKey, null); 1209 break; 1210 case java.sql.Types.DOUBLE: 1211 case java.sql.Types.FLOAT: 1212 AllVals.put(sKey, new Double (sData)); 1213 break; 1214 case java.sql.Types.REAL: 1215 AllVals.put(sKey, new Float (sData)); 1216 break; 1217 case java.sql.Types.DECIMAL: 1218 case java.sql.Types.NUMERIC: 1219 iDecDot = sData.indexOf("."); 1220 if (iDecDot < 0) iDecDot = sData.indexOf(","); 1221 if (iDecDot < 0) 1222 AllVals.put(sKey, new BigDecimal (sData)); 1223 else 1224 AllVals.put(sKey, 1225 BigDecimal.valueOf(Long.parseLong(sData.substring(0, iDecDot)), 1226 Integer.parseInt(sData.substring(iDecDot + 1227 1)))); 1228 break; 1229 case java.sql.Types.LONGVARBINARY: 1230 case java.sql.Types.BLOB: 1231 if (!bHasLongVarBinaryData) LongVarBinaryValsLen = new HashMap (); 1232 1233 File oFile = new File (sData); 1234 if (oFile.exists()) { 1235 LongVarBinaryValsLen.put(sKey, new Long (oFile.length())); 1236 AllVals.put(sKey, oFile); 1237 } 1238 bHasLongVarBinaryData = true; 1239 break; 1240 case 1111: if (DebugFile.trace) DebugFile.writeln("Binding interval "+sData); 1242 try { 1243 Class cPGIntval = Class.forName("org.postgresql.util.PGInterval"); 1244 java.lang.reflect.Constructor cNewPGInt = null; 1245 Object oPGIntval; 1246 try { 1247 cNewPGInt = cPGIntval.getConstructor(new Class []{String .class}); 1248 } catch (NoSuchMethodException neverthrown) {} 1249 try { 1250 oPGIntval = cNewPGInt.newInstance(new Object []{sData}); 1251 AllVals.put(sKey, oPGIntval); 1252 } catch (InstantiationException neverthrown) {} 1253 catch (IllegalAccessException neverthrown) {} 1254 catch (java.lang.reflect.InvocationTargetException neverthrown) {} 1255 } catch (ClassNotFoundException cnfe) { 1256 throw new IllegalArgumentException ("ClassNotFoundException org.postgresql.util.PGInterval"); 1257 } 1258 if (DebugFile.trace) DebugFile.writeln("Interval successfully binded"); 1259 break; 1260 } } 1263 1266 1267 public void putAll(Properties oPropsCollection) throws FileNotFoundException { 1268 Iterator oIter = this.getTable().getColumns().iterator(); 1269 DBColumn oDBCol; 1270 String sColName; 1271 String sPropValue; 1272 1273 while (oIter.hasNext()) { 1274 oDBCol = (DBColumn) oIter.next(); 1275 sColName = oDBCol.getName(); 1276 sPropValue = oPropsCollection.getProperty(sColName); 1277 1278 if (null!=sPropValue) { 1279 if (sPropValue.trim().length()>0) { 1280 switch(oDBCol.getSqlType()) { 1281 case java.sql.Types.INTEGER: 1282 replace(sColName, new Integer (sPropValue)); 1283 break; 1284 case java.sql.Types.SMALLINT: 1285 replace(sColName, new Short (sPropValue)); 1286 break; 1287 case java.sql.Types.DOUBLE: 1288 case java.sql.Types.REAL: 1289 replace(sColName, new Double (sPropValue)); 1290 break; 1291 case java.sql.Types.FLOAT: 1292 replace(sColName, new Float (sPropValue)); 1293 break; 1294 case java.sql.Types.NUMERIC: 1295 case java.sql.Types.DECIMAL: 1296 replace(sColName, new java.math.BigDecimal (sPropValue)); 1297 break; 1298 default: 1299 put(sColName, sPropValue, oDBCol.getSqlType()); 1300 } } else if (!isNull(sColName)) 1303 replace(sColName, null); 1304 } } } 1308 1314 public void putAll(Map oMap) { 1315 Iterator oIter = oMap.keySet().iterator(); 1316 while (oIter.hasNext()){ 1317 String sKey = oIter.next().toString(); 1318 AllVals.put(sKey, oMap.get(sKey)); 1319 } } 1322 1326 1327 public void remove(String sKey) { 1328 if (AllVals.containsKey(sKey)) AllVals.remove(sKey); 1329 } 1330 1331 1337 1338 public Object remove(Object oKey) { 1339 Object oPrevious; 1340 if (AllVals.containsKey(oKey)) { 1341 oPrevious = AllVals.get(oKey); 1342 AllVals.remove(oKey); 1343 } else { 1344 oPrevious = null; 1345 } 1346 return oPrevious; 1347 } 1348 1349 1354 public void replace(String sKey, Object oObj) { 1355 remove(sKey); 1356 1357 AllVals.put(sKey, oObj); 1358 } 1359 1360 1365 1366 public void replace(String sKey, int iVal) { 1367 Integer oObj = new Integer (iVal); 1368 1369 remove(sKey); 1370 1371 AllVals.put(sKey, oObj); 1372 } 1373 1374 1379 1380 public void replace(String sKey, short iVal) { 1381 Short oObj = new Short (iVal); 1382 1383 remove(sKey); 1384 1385 AllVals.put(sKey, oObj); 1386 } 1387 1388 1393 1394 public void replace(String sKey, float fVal) { 1395 Float oObj = new Float (fVal); 1396 1397 remove(sKey); 1398 1399 AllVals.put(sKey, oObj); 1400 } 1401 1402 1407 1408 public void toLowerCase(String sKey) { 1409 if (!isNull(sKey)) 1410 replace (sKey, getString(sKey).toLowerCase()); 1411 } 1412 1413 1418 1419 public void toUpperCase(String sKey) { 1420 if (!isNull(sKey)) 1421 replace (sKey, getString(sKey).toUpperCase()); 1422 } 1423 1424 1442 1443 public boolean store(JDCConnection oConn) throws SQLException { 1444 boolean bRetVal; 1445 1446 if (bHasLongVarBinaryData) { 1447 try { 1448 if (oTable==null) { 1449 oTable = getTable(oConn); 1450 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 1451 bRetVal = oTable.storeRegisterLong(oConn, AllVals, LongVarBinaryValsLen); 1452 } else 1453 bRetVal = oTable.storeRegisterLong(oConn, AllVals, LongVarBinaryValsLen); 1454 } 1455 catch (IOException ioe) { 1456 throw new SQLException (ioe.getMessage(),"40001",40001); 1457 } 1458 finally { 1459 LongVarBinaryValsLen.clear(); 1460 bHasLongVarBinaryData = false; 1461 } 1462 } 1463 else 1464 if (oTable==null) { 1465 oTable = getTable(oConn); 1466 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 1467 bRetVal = oTable.storeRegister(oConn, AllVals); 1468 } else 1469 bRetVal = oTable.storeRegister(oConn, AllVals); 1470 1471 return bRetVal; 1472 } 1474 1482 1483 public boolean delete(JDCConnection oConn) throws SQLException { 1484 boolean bRetVal; 1485 1486 if (null==oTable) { 1487 oTable = getTable(oConn); 1488 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 1489 bRetVal = oTable.deleteRegister(oConn, AllVals); 1490 } else 1491 bRetVal = oTable.deleteRegister(oConn, AllVals); 1492 1493 return bRetVal; 1494 } 1496 1503 1504 public boolean exists(JDCConnection oConn) throws SQLException { 1505 if (null==oTable) { 1506 oTable = getTable(oConn); 1507 if (null==oTable) throw new SQLException ("Table not found "+sTable,"42S02", 42002); 1508 return oTable.existsRegister(oConn, AllVals); 1509 } else 1510 return oTable.existsRegister(oConn, AllVals); 1511 } 1512 1513 1522 1523 protected String toXML(String sIdent, String sDelim, HashMap oAttrs) 1524 1525 throws IllegalStateException { 1526 1527 if (null==oTable) 1528 throw new IllegalStateException ("DBPersist.toXML() method invoked before load() method was called"); 1529 1530 StringBuffer oBF = new StringBuffer (80*oTable.columnCount()); 1531 ListIterator oIT = oTable.getColumns().listIterator(); 1532 DBColumn oColumn; 1533 Object oColValue; 1534 String sColName; 1535 String sStartElement = sIdent + sIdent + "<"; 1536 String sEndElement = ">" + sDelim; 1537 Class oColClass, ClassString = null, ClassDate = null; 1538 SimpleDateFormat oXMLDate = new SimpleDateFormat ("yyyy-MM-dd'T'hh:mm:ss"); 1539 1540 try { 1541 ClassString = Class.forName("java.lang.String"); 1542 ClassDate = Class.forName("java.util.Date"); 1543 } catch (ClassNotFoundException ignore) { } 1544 1545 if (null==oAttrs) { 1546 oBF.append(sIdent + "<" + sAuditCls + ">" + sDelim); 1547 } else { 1548 oBF.append(sIdent + "<" + sAuditCls); 1549 Iterator oNames = oAttrs.keySet().iterator(); 1550 while (oNames.hasNext()) { 1551 Object oName = oNames.next(); 1552 oBF.append(" "+oName+"=\""+oAttrs.get(oName)+"\""); 1553 } oBF.append(">" + sDelim); 1555 } 1557 while (oIT.hasNext()) { 1558 oColumn = (DBColumn) oIT.next(); 1559 sColName = oColumn.getName(); 1560 oColValue = AllVals.get(sColName); 1561 1562 oBF.append(sStartElement); 1563 oBF.append(sColName); 1564 oBF.append(">"); 1565 if (null!=oColValue) { 1566 oColClass = oColValue.getClass(); 1567 if (oColClass.equals(ClassString)) 1568 oBF.append("<![CDATA[" + oColValue + "]]>"); 1569 else if (oColClass.equals(ClassDate)) 1570 oBF.append(oXMLDate.format((java.util.Date ) oColValue)); 1571 else 1572 oBF.append(oColValue); 1573 } 1574 oBF.append("</"); 1575 oBF.append(sColName); 1576 oBF.append(sEndElement); 1577 } 1579 oBF.append(sIdent + "</" + sAuditCls + ">"); 1580 1581 return oBF.toString(); 1582 } 1584 1591 1592 public String toXML(String sIdent, String sDelim) 1593 throws IllegalStateException { 1594 return toXML(sIdent, sDelim, null); 1595 } 1596 1597 1603 1604 public String toXML(String sIdent) { 1605 return toXML(sIdent, "\n", null); 1606 } 1607 1608 1614 1615 public String toXML() { 1616 return toXML("", "\n", null); 1617 } 1618 1619 1644 1645 public void parseXML(String sXMLFilePath) throws SAXException ,SAXNotRecognizedException ,SAXNotSupportedException ,SAXParseException ,IOException ,ClassNotFoundException ,IllegalAccessException ,InstantiationException { 1646 DBSaxHandler oHandler = new DBSaxHandler(this); 1647 oHandler.parse(sXMLFilePath); 1648 } 1649 1650 1655 protected HashMap changelog(DBPersist oOldInstance) { 1656 Object oKey, oOld, oNew; 1657 HashMap oLog = new HashMap (size()*2); 1658 1659 Iterator oKeys = keySet().iterator(); 1661 while (oKeys.hasNext()) { 1662 oKey = oKeys.next(); 1663 oNew = get(oKey); 1664 oOld = oOldInstance.get(oKey); 1665 if (null!=oNew) { 1666 if (!oNew.equals(oOld)) oLog.put(oKey, oOld); 1667 } else if (oOld!=null) { 1668 oLog.put(oKey, oOld); 1669 } 1670 } 1672 oKeys = oOldInstance.keySet().iterator(); 1674 while (oKeys.hasNext()) { 1675 oKey = oKeys.next(); 1676 if (!containsKey(oKey)) { 1677 oOld = oOldInstance.get(oKey); 1678 oLog.put(oKey, oOld); 1679 } 1680 } return oLog; 1682 } 1684 1695 1696 protected static String getUIdFromName(JDCConnection oConn, Integer iDomainId, String sInstanceNm, String sStoredProc) throws SQLException { 1697 CallableStatement oCall; 1698 PreparedStatement oStmt; 1699 ResultSet oRSet; 1700 1701 String sInstanceId; 1702 1703 if (null==iDomainId) { 1704 if (JDCConnection.DBMS_POSTGRESQL==oConn.getDataBaseProduct()) { 1705 if (DebugFile.trace) 1706 DebugFile.writeln("Connection.prepareStatement(SELECT " + sStoredProc + "('" + sInstanceNm + "')"); 1707 1708 oStmt = oConn.prepareStatement("SELECT " + sStoredProc + "(?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1709 oStmt.setString(1, sInstanceNm); 1710 oRSet = oStmt.executeQuery(); 1711 if (oRSet.next()) 1712 sInstanceId = oRSet.getString(1); 1713 else 1714 sInstanceId = null; 1715 oRSet.close(); 1716 oStmt.close(); 1717 } 1718 else { 1719 if (DebugFile.trace) 1720 DebugFile.writeln("Connection.prepareCall({ call " + sStoredProc + " ('" + sInstanceNm + "',?)})"); 1721 1722 oCall = oConn.prepareCall("{ call " + sStoredProc + " (?,?)}"); 1723 try { 1724 oCall.setQueryTimeout(15); 1725 } 1726 catch (SQLException sqle) {} 1727 1728 oCall.setString(1, sInstanceNm); 1729 oCall.registerOutParameter(2, java.sql.Types.CHAR); 1730 1731 oCall.execute(); 1732 1733 sInstanceId = oCall.getString(2); 1734 1735 if (JDCConnection.DBMS_ORACLE==oConn.getDataBaseProduct() && null!=sInstanceId) 1736 sInstanceId = sInstanceId.trim(); 1737 1738 oCall.close(); 1739 oCall = null; 1740 } 1741 } 1742 else { 1743 if (JDCConnection.DBMS_POSTGRESQL==oConn.getDataBaseProduct()) { 1744 if (DebugFile.trace) 1745 DebugFile.writeln("Connection.prepareStatement(SELECT " + sStoredProc + "(" + iDomainId.toString() + ",'" + sInstanceNm + "')"); 1746 1747 oStmt = oConn.prepareStatement("SELECT " + sStoredProc + "(?,?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1748 oStmt.setInt(1, iDomainId.intValue()); 1749 oStmt.setString(2, sInstanceNm); 1750 oRSet = oStmt.executeQuery(); 1751 if (oRSet.next()) 1752 sInstanceId = oRSet.getString(1); 1753 else 1754 sInstanceId = null; 1755 oRSet.close(); 1756 oStmt.close(); 1757 } 1758 else { 1759 if (DebugFile.trace) 1760 DebugFile.writeln("Connection.prepareCall({ call " + sStoredProc + " (" + iDomainId.toString() + ",'" + sInstanceNm + "',?)})"); 1761 1762 oCall = oConn.prepareCall("{ call " + sStoredProc + " (?,?,?) }"); 1763 try { 1764 oCall.setQueryTimeout(15); 1765 } 1766 catch (SQLException sqle) {} 1767 1768 oCall.setInt(1, iDomainId.intValue()); 1769 oCall.setString(2, sInstanceNm); 1770 oCall.registerOutParameter(3, java.sql.Types.CHAR); 1771 1772 oCall.execute(); 1773 1774 sInstanceId = oCall.getString(3); 1775 1776 if (null!=sInstanceId) sInstanceId = sInstanceId.trim(); 1777 1778 oCall.close(); 1779 oCall = null; 1780 } 1781 } 1782 1783 return sInstanceId; 1784 } 1786 protected HashMap AllVals; 1787 protected String sAuditCls; 1788 protected String sAuditUsr; 1789 protected String sTransactId; 1790 private boolean bAllCaps; 1791 private boolean bHasLongVarBinaryData; 1792 private HashMap LongVarBinaryValsLen; 1793 private DBTable oTable; 1794 private String sTable; 1795}
| Popular Tags
|