1 32 33 package com.knowgate.hipergate; 34 35 import java.lang.System ; 36 import java.io.IOException ; 37 import java.io.File ; 38 39 import java.sql.PreparedStatement ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 43 import java.net.URL ; 44 import java.net.MalformedURLException ; 45 46 import com.knowgate.debug.DebugFile; 47 import com.knowgate.jdc.JDCConnection; 48 import com.knowgate.dataobjs.DB; 49 import com.knowgate.dataobjs.DBPersist; 50 import com.knowgate.dfs.FileSystem; 51 import com.knowgate.misc.Gadgets; 52 53 65 public class ProductLocation extends DBPersist { 66 67 public ProductLocation() { 68 super(DB.k_prod_locats, "ProductLocation"); 69 } 70 71 77 public ProductLocation(JDCConnection oConn, String iIdLocation) throws SQLException { 78 super(DB.k_prod_locats, "ProductLocation"); 79 80 Object aProd[] = { iIdLocation }; 81 82 load(oConn, aProd); 83 } 84 85 87 94 public String getPath() { 95 String sPath; 96 String sXProtocol = getString(DB.xprotocol); 97 String sXPath = getStringNull(DB.xpath, ""); 98 String sFileSep = sXProtocol.startsWith("file") ? System.getProperty("file.separator") : "/"; 99 100 if (!sXProtocol.endsWith("://")) sXProtocol += "://"; 101 102 sPath = sXProtocol; 103 104 if (!sXProtocol.startsWith("file")) sPath += getString("xhost"); 105 106 if (!isNull(DB.xport)) sPath += ":" + getShort(DB.xport); 107 108 if (!sPath.endsWith(sFileSep) && !sXPath.startsWith(sFileSep)) sPath += sFileSep; 109 110 sPath += sXPath; 111 112 return sPath; 113 } 115 117 121 122 public String getURL() { 123 String sURL; 124 String sXProtocol = getString(DB.xprotocol); 125 String sFileSep = sXProtocol.startsWith("file") ? System.getProperty("file.separator") : "/"; 126 127 sURL = getPath(); 128 129 if (null!=sURL) { 130 sURL = sURL.trim(); 131 132 if (!sURL.endsWith(sFileSep) && !isNull(DB.xfile)) sURL += sFileSep; 133 134 if (!isNull(DB.xfile)) sURL += getString(DB.xfile); 135 if (!isNull(DB.xanchor)) sURL += "#" + getString(DB.xanchor); 136 } 137 138 return sURL; 139 } 141 143 159 public int getContainerType() { 160 161 if (DebugFile.trace) { 162 DebugFile.writeln("Begin ProductLocation.getContainerType()"); 163 DebugFile.incIdent(); 164 DebugFile.writeln("protocol="+getStringNull(DB.xprotocol,"null").toLowerCase()); 165 } 166 167 String sProtocol = getString(DB.xprotocol).toLowerCase(); 168 int iProdType = 0; 169 170 if (sProtocol.startsWith("file:")) 171 iProdType = 1; 172 else if (sProtocol.startsWith("http:")) 173 iProdType = 2; 174 else if (sProtocol.startsWith("https:")) 175 iProdType = 3; 176 else if (sProtocol.startsWith("ftp:")) 177 iProdType = 4; 178 else if (sProtocol.startsWith("odbc:")) 179 iProdType = 5; 180 else if (sProtocol.startsWith("lotus:")) 181 iProdType = 6; 182 else if (sProtocol.startsWith("jdbc:")) 183 iProdType = 7; 184 else if (sProtocol.startsWith("ware:")) 185 iProdType = 100; 186 187 if (DebugFile.trace) { 188 DebugFile.decIdent(); 189 DebugFile.writeln("End ProductLocation.getContainerType() : " + String.valueOf(iProdType)); 190 } 191 192 return iProdType; 193 } 195 197 205 private String removeQueryString(String sType) { 206 int iLastQuery, iNextQuery; 207 208 if (sType.length()<=4) return sType; 210 211 iLastQuery = iNextQuery = sType.indexOf("?"); 212 213 iNextQuery = sType.indexOf("#"); 214 if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1)) 215 iLastQuery = iNextQuery; 216 217 iNextQuery = sType.indexOf(","); 218 if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1)) 219 iLastQuery = iNextQuery; 220 221 iNextQuery = sType.indexOf("/"); 222 if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1)) 223 iLastQuery = iNextQuery; 224 225 iNextQuery = sType.indexOf("&"); 226 if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1)) 227 iLastQuery = iNextQuery; 228 229 if (iLastQuery<0) iLastQuery=4; 230 231 return sType.substring(0, iLastQuery); 232 } 234 236 253 254 public String getProductType() { 255 String sFile; 256 String sType; 257 int iLast; 258 259 sFile = isNull(DB.xfile) ? getStringNull(DB.xpath,"") : getStringNull(DB.xfile,""); 260 261 if (sFile.length()==0) { 262 if (isNull(DB.id_cont_type)) 263 return "?"; 264 else 265 switch (getInt(DB.id_cont_type)) { 266 case CONTAINER_HTTP : 267 case CONTAINER_HTTPS: 268 return "HTML"; 269 default: 270 return "?"; 271 } 272 } 273 else { 274 iLast = sFile.lastIndexOf("."); 275 if (iLast > 0) { 276 sType = removeQueryString(sFile.substring(++iLast).toUpperCase()); 277 278 return sType; 279 } 280 else { 281 if (isNull(DB.id_cont_type)) 282 return "?"; 283 else 284 switch (getInt(DB.id_cont_type)) { 285 case CONTAINER_HTTP: 286 case CONTAINER_HTTPS: 287 return "HTML"; 288 default: 289 return "?"; 290 } 291 } 292 } 293 } 295 297 301 302 public void setURL (String sURL) throws MalformedURLException { 303 String sURI = sURL.toLowerCase(); 304 URL oURL; 305 306 if (DebugFile.trace) { 307 DebugFile.writeln("Begin ProductLocation.setURL(" + sURL + ")" ); 308 DebugFile.incIdent(); 309 } 310 311 if (!sURI.startsWith("http://") && !sURI.startsWith("https://") && !sURI.startsWith("ftp://") && 312 !sURI.startsWith("file://") && !sURI.startsWith("odbc:") && !sURI.startsWith("jdbc:") && 313 !sURI.startsWith("lotus://") && !sURI.startsWith("ware://")) 314 sURI = "http://" + sURI; 315 316 if (sURI.startsWith("https://")) 317 oURL = new URL ("http" + sURI.substring(5)); 318 else 319 oURL = new URL (sURI); 320 321 replace(DB.xprotocol, sURI.substring(0, sURI.indexOf("://")+3)); 322 323 replace(DB.xhost, oURL.getHost()); 324 325 if (-1!=oURL.getPort()) 326 replace(DB.xport, oURL.getPort()); 327 else 328 remove(DB.xport); 329 330 String sPath = oURL.getPath(); 331 if (null!=sPath) { 332 if (sPath.equals("/")) 333 remove(DB.xpath); 334 else 335 replace(DB.xpath, sPath); 336 } 337 else 338 remove(DB.xpath); 339 340 String sFile = oURL.getFile(); 341 if (null!=sFile) { 342 if (sPath.equals("/")) { 343 remove(DB.xfile); 344 remove(DB.xoriginalfile); 345 } 346 else { 347 replace(DB.xfile, sFile); 348 replace(DB.xoriginalfile, sFile); 349 } 350 } 351 else { 352 remove(DB.xfile); 353 remove(DB.xoriginalfile); 354 } 355 356 if (null!=oURL.getRef()) 357 replace(DB.xanchor, oURL.getRef()); 358 else 359 remove(DB.xanchor); 360 361 if (DebugFile.trace) { 362 DebugFile.decIdent(); 363 DebugFile.writeln("End ProductLocation.setURL() : " + oURL.toExternalForm()); 364 } 365 366 } 368 370 376 public void setLength (long lLen) throws IllegalArgumentException { 377 378 if (lLen>(long)2147483647) 379 throw new IllegalArgumentException ("Maximum allowed file length is 2Gb"); 380 381 replace (DB.len_file, new Long (lLen).intValue()); 382 } 383 384 386 390 public void setLength (int iLen) { 391 replace (DB.len_file, iLen); 392 } 393 394 396 405 public void setPath (String sProtocol, String sHost, String sPath, String sFile, String sOriginalFile) { 406 if (DebugFile.trace) { 407 DebugFile.writeln("Begin ProductLocation.setPath(" + sProtocol + "," + sHost + "," + sPath + "," + sFile + "," + sOriginalFile + ")"); 408 DebugFile.incIdent(); 409 } 410 411 replace(DB.xprotocol,sProtocol); 412 replace(DB.xhost,sHost); 413 replace(DB.xpath,sPath); 414 replace(DB.xfile,sFile); 415 replace(DB.xoriginalfile,sOriginalFile); 416 417 if (DebugFile.trace) { 418 DebugFile.decIdent(); 419 DebugFile.writeln("End ProductLocation.setPath()"); 420 } 421 } 423 425 435 public void setPath (String sProtocol, String sHost, String sPath, String sFile, String sOriginalFile, String sAnchor) { 436 if (DebugFile.trace) { 437 DebugFile.writeln("Begin ProductLocation.setPath(" + sProtocol + "," + sHost + "," + sPath + "," + sFile + "," + sOriginalFile + "," + sAnchor + ")"); 438 DebugFile.incIdent(); 439 } 440 441 replace(DB.xprotocol,sProtocol); 442 replace(DB.xhost,sHost); 443 replace(DB.xpath,sPath); 444 replace(DB.xfile,sFile); 445 replace(DB.xoriginalfile,sOriginalFile); 446 replace(DB.xanchor,sAnchor); 447 448 if (DebugFile.trace) { 449 DebugFile.decIdent(); 450 DebugFile.writeln("End ProductLocation.setPath()"); 451 } 452 } 454 456 464 465 public void setPath (String sHost, String sLocalPath) { 466 final String sFileSep = System.getProperty("file.separator"); 467 String sPath = sLocalPath; 468 int iLast; 469 470 if (DebugFile.trace) { 471 DebugFile.writeln("Begin ProductLocation.setPath(" + sHost + "," + sLocalPath + ")"); 472 DebugFile.incIdent(); 473 } 474 475 if (!sPath.startsWith("file://")) sPath = "file://" + sPath; 476 477 iLast = sPath.lastIndexOf(sFileSep); 478 479 replace(DB.xprotocol, "file"); 480 replace(DB.xhost, sHost); 481 482 if (DebugFile.trace) DebugFile.writeln(DB.xpath + "=" + sPath.substring(7, iLast)); 483 484 replace(DB.xpath, sPath.substring(7, iLast)); 485 486 if (iLast<sPath.length()-1) { 487 if (DebugFile.trace) DebugFile.writeln(DB.xfile + "=" + sPath.substring(iLast+1)); 488 489 replace(DB.xfile, sPath.substring(iLast+1)); 490 491 if (DebugFile.trace) DebugFile.writeln(DB.xoriginalfile + "=" + sPath.substring(iLast+1)); 492 493 replace(DB.xoriginalfile, sPath.substring(iLast+1)); 494 } 495 else { 496 remove(DB.xfile); 497 remove(DB.xoriginalfile); 498 } 499 500 remove(DB.xport); 501 remove(DB.xanchor); 502 503 if (DebugFile.trace) { 504 DebugFile.decIdent(); 505 DebugFile.writeln("End ProductLocation.setPath()"); 506 } 507 } 509 511 519 public boolean store(JDCConnection oConn) throws SQLException { 520 boolean iRetVal; 521 String sProtocol; 522 String sProdType; 523 PreparedStatement oStmt; 524 ResultSet oRSet; 525 Object oNextLoca; 526 final String EmptyStr = ""; 527 java.util.Date dtNow = new java.util.Date (); 528 java.sql.Timestamp dtSQL = new java.sql.Timestamp (dtNow.getTime()); 529 530 if (DebugFile.trace) { 531 DebugFile.writeln("Begin ProductLocation.store([Connection])" ); 532 DebugFile.incIdent(); 533 } 534 535 if (!AllVals.containsKey(DB.gu_location)) 536 put(DB.gu_location, Gadgets.generateUUID()); 537 538 if (!AllVals.containsKey(DB.dt_uploaded)) 539 put (DB.dt_uploaded, dtSQL); 540 541 if (!AllVals.containsKey(DB.dt_modified)) 542 put (DB.dt_modified, dtSQL); 543 544 if (!AllVals.containsKey(DB.id_cont_type)) { 545 if (DebugFile.trace) DebugFile.writeln(DB.id_cont_type + "=" + String.valueOf(getContainerType())); 546 547 put (DB.id_cont_type, new Integer (getContainerType())); 548 } 549 550 if (!AllVals.containsKey(DB.id_prod_type)) { 551 sProdType = getProductType(); 552 553 if (DebugFile.trace) DebugFile.writeln(DB.id_prod_type + "=" + sProdType); 554 555 put(DB.id_prod_type, sProdType.length()>0 ? sProdType : "?"); 556 } 557 else { 558 sProdType = (String ) AllVals.get(DB.id_prod_type); 559 } 560 561 if (!AllVals.containsKey(DB.xhost)) 562 put (DB.xhost, "localhost"); 563 else if (EmptyStr.equals(AllVals.get(DB.xhost))) 564 put (DB.xhost, "localhost"); 565 566 PreparedStatement oStTp = oConn.prepareStatement("SELECT NULL FROM "+DB.k_lu_prod_types+" WHERE "+DB.id_prod_type+"=?",ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 567 oStTp.setString(1, sProdType.length()>0 ? sProdType : "?"); 568 ResultSet oRsTp = oStTp.executeQuery(); 569 boolean bRegistredType = oRsTp.next(); 570 oRsTp.close(); 571 oStTp.close(); 572 if (!bRegistredType) { 573 oStTp = oConn.prepareStatement("INSERT INTO "+DB.k_lu_prod_types+" ("+DB.id_prod_type+","+DB.de_prod_type+","+DB.nm_icon+") VALUES (?,'"+sProdType+" file',NULL)"); 574 oStTp.setString(1, sProdType); 575 oStTp.executeUpdate(); 576 oStTp.close(); 577 } 578 579 if (AllVals.containsKey(DB.xprotocol)) 580 replace(DB.xprotocol, getString(DB.xprotocol).toLowerCase()); 581 582 if (!AllVals.containsKey(DB.pg_prod_locat)) { 583 if (DebugFile.trace) 584 DebugFile.writeln("Connection.prepareStatement(SELECT MAX(" + DB.pg_prod_locat + ")+1 FROM " + DB.k_prod_locats + " WHERE " + DB.gu_product + "='" + getStringNull(DB.gu_product, "") + "')"); 585 586 oStmt = oConn.prepareStatement("SELECT MAX(" + DB.pg_prod_locat + ")+1 FROM " + DB.k_prod_locats + " WHERE " + DB.gu_product + "=?"); 587 oStmt.setString(1, getString(DB.gu_product)); 588 589 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()"); 590 591 oRSet = oStmt.executeQuery(); 592 if (oRSet.next()) { 593 oNextLoca = oRSet.getObject(1); 594 if (oRSet.wasNull()) 595 put(DB.pg_prod_locat, new Integer (1)); 596 else 597 put(DB.pg_prod_locat, new Integer (oNextLoca.toString())); 598 } 599 else 600 put(DB.pg_prod_locat, new Integer (1)); 601 oRSet.close(); 602 oStmt.close(); 603 oStmt = null; 604 605 if (DebugFile.trace) 606 DebugFile.writeln(DB.pg_prod_locat + "=" + String.valueOf(getInt(DB.pg_prod_locat))); 607 } 608 609 if (AllVals.containsKey(DB.status)) 610 replace(DB.status, 1); 611 612 iRetVal = super.store(oConn); 613 614 if (DebugFile.trace) { 615 DebugFile.decIdent(); 616 DebugFile.writeln("End ProductLocation.store() : " + String.valueOf(iRetVal)); 617 } 618 619 return iRetVal; 620 } 622 624 637 public boolean rename(JDCConnection oConn, FileSystem oFileSys, String sNewFile) 638 throws Exception ,IOException ,SQLException ,SecurityException { 639 String sPath; 640 String sOldFile; 641 java.util.Date dtNow = new java.util.Date (); 642 java.sql.Timestamp dtSQL = new java.sql.Timestamp (dtNow.getTime()); 643 644 sOldFile = getString(DB.xfile); 645 646 if (getString(DB.xprotocol).equalsIgnoreCase("file://")) { 647 sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xpath), File.separator); 648 } 649 else { 650 if (isNull(DB.xport)) 651 sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xhost)+"/"+getString(DB.xpath), '/'); 652 else 653 sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xhost)+":"+String.valueOf(getInt(DB.xport))+"/"+getString(DB.xpath), '/'); 654 } 655 656 replace(DB.xfile, sNewFile); 657 replace(DB.id_prod_type, getProductType()); 658 replace(DB.dt_modified, dtSQL); 659 660 store(oConn); 661 662 return oFileSys.rename(sPath+sOldFile, sPath+sNewFile); 663 } 665 667 683 public void upload(JDCConnection oConn, FileSystem oFileSys, String sSourcePath, String sSourceFile, String sTargetPath, String sTargetFile) throws Exception ,IOException ,SQLException { 684 String sFileSep = System.getProperty("file.separator"); 685 686 if (DebugFile.trace) { 687 DebugFile.writeln("Begin ProductLocation.upload([Connection], [FileSystem], " + sSourcePath + "," + sSourceFile + sTargetPath + "," + sTargetFile + ")" ); 688 DebugFile.incIdent(); 689 } 690 691 if (!sSourcePath.endsWith(sFileSep)) sSourcePath += sFileSep; 692 if (!sTargetPath.endsWith(sFileSep)) sTargetPath += sFileSep; 693 694 oFileSys.move(sSourcePath+sSourceFile, sTargetPath+sTargetFile); 695 696 if (DebugFile.trace) { 697 DebugFile.decIdent(); 698 DebugFile.writeln("End ProductLocation.upload()"); 699 } 700 } 702 704 712 public boolean delete(JDCConnection oConn) throws SQLException { 713 boolean bRetVal; 714 FileSystem oFS = new FileSystem(); 715 716 if (DebugFile.trace) { 717 DebugFile.writeln("Begin ProductLocation.delete(" + getString(DB.gu_location) + ")" ); 718 DebugFile.incIdent(); 719 } 720 721 try { 722 if (getInt(DB.id_cont_type)==ProductLocation.CONTAINER_FILE || getInt(DB.id_cont_type)==ProductLocation.CONTAINER_FTP) { 723 724 oFS.delete(getURL()); 725 } } 727 catch (IOException ioe) { 728 if (ioe.getMessage().indexOf("No such file or directory")<0) 729 throw new SQLException (ioe.getMessage()); 730 } 731 732 oFS = null; 733 734 bRetVal = super.delete(oConn); 735 736 if (DebugFile.trace) { 737 DebugFile.decIdent(); 738 DebugFile.writeln("End Product.delete() : " + String.valueOf(bRetVal)); 739 } 740 741 return bRetVal; 742 } 744 746 public static final short ClassId = 16; 747 748 750 public static final int CONTAINER_FILE = 1; 751 public static final int CONTAINER_HTTP = 2; 752 public static final int CONTAINER_HTTPS = 3; 753 public static final int CONTAINER_FTP = 4; 754 public static final int CONTAINER_ODBC = 5; 755 public static final int CONTAINER_LOTUS = 6; 756 public static final int CONTAINER_JDBC = 7; 757 public static final int CONTAINER_WARE = 100; 758 759 } 760 | Popular Tags |