1 32 33 package com.knowgate.hipergate; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.FileInputStream ; 38 39 import java.util.Properties ; 40 41 import java.util.LinkedList ; 42 import java.util.ListIterator ; 43 import java.util.NoSuchElementException ; 44 import java.util.StringTokenizer ; 45 46 import java.sql.Connection ; 47 import java.sql.ResultSet ; 48 import java.sql.Statement ; 49 import java.sql.PreparedStatement ; 50 import java.sql.CallableStatement ; 51 import java.sql.SQLException ; 52 import java.sql.DatabaseMetaData ; 53 54 import com.knowgate.debug.DebugFile; 55 import com.knowgate.jdc.JDCConnection; 56 import com.knowgate.dataobjs.DB; 57 import com.knowgate.dataobjs.DBBind; 58 import com.knowgate.dataobjs.DBPersist; 59 import com.knowgate.dataobjs.DBSubset; 60 61 import com.knowgate.misc.Gadgets; 62 import com.knowgate.dfs.FileSystem; 63 64 69 public class Category extends DBPersist { 70 71 74 public Category() { 75 super(DB.k_categories, "Category"); 76 oFS = null; 77 } 78 79 81 86 public Category(String sIdCategory) throws SQLException { 87 super(DB.k_categories,"Category"); 88 89 put(DB.gu_category, sIdCategory); 90 oFS = null; 91 } 92 93 95 101 public Category(JDCConnection oConn, String sIdCategory) throws SQLException { 102 super(DB.k_categories,"Category"); 103 104 Object aCatg[] = { sIdCategory }; 105 106 load (oConn,aCatg); 107 oFS = null; 108 } 109 110 112 protected Category(String sTableName, String sClassName) { 113 super(sTableName, sClassName); 114 oFS = null; 115 } 116 117 119 129 public LinkedList browse (JDCConnection oConn, int iDirection, int iOrder) throws SQLException { 130 String sCatId = getString(DB.gu_category); 131 String sNeighbour; 132 boolean bDoNext; 133 PreparedStatement oStmt; 134 ResultSet oRSet; 135 LinkedList oCatList = new LinkedList (); 136 Category oCatg; 137 138 if (DebugFile.trace) { 139 DebugFile.writeln("Begin Category.browse([Connection], ...)"); 140 DebugFile.incIdent(); 141 } 142 143 if (iDirection==Category.BROWSE_UP) { 144 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_parent_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_child_cat + "=?)"); 145 oStmt = oConn.prepareStatement("SELECT " + DB.gu_parent_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_child_cat + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 146 } 147 else { 148 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_child_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_parent_cat + "=?)"); 149 oStmt = oConn.prepareStatement("SELECT " + DB.gu_child_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_parent_cat + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 150 } 151 152 do { 153 154 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setString(1, " + sCatId + ")"); 155 156 oStmt.setString(1, sCatId); 157 oRSet = oStmt.executeQuery(); 158 bDoNext = oRSet.next(); 159 160 if (bDoNext) 161 sNeighbour = oRSet.getString(1); 162 else 163 sNeighbour = ""; 164 165 oRSet.close(); 166 167 if (bDoNext) { 168 if (sCatId.equals(sNeighbour)) { 169 bDoNext = false; 170 } 171 else { 172 oCatg = new Category(oConn, sNeighbour); 173 174 if (iDirection==Category.BROWSE_UP) 175 if (iOrder==Category.BROWSE_BOTTOMUP) 176 oCatList.addLast(oCatg); 177 else 178 oCatList.addFirst(oCatg); 179 else 180 if (iOrder==Category.BROWSE_BOTTOMUP) 181 oCatList.addFirst(oCatg); 182 else 183 oCatList.addLast(oCatg); 184 185 sCatId = sNeighbour; 186 } } } while (bDoNext); 189 190 oStmt.close(); 191 192 if (DebugFile.trace) { 193 DebugFile.decIdent(); 194 DebugFile.writeln("End Category.browse() : " + String.valueOf(oCatList.size())); 195 } 196 197 return oCatList; 198 } 200 202 215 public String getPath(Connection oConn) throws SQLException { 216 Statement oStmt; 217 ResultSet oRSet; 218 CallableStatement oCall; 219 DatabaseMetaData oMDat; 220 String sPath; 221 String sDBMS; 222 223 if (DebugFile.trace) { 224 DebugFile.writeln("Begin Category.getPath([Connection])" ); 225 DebugFile.incIdent(); 226 DebugFile.writeln("gu_category=" + get(DB.gu_category)); 227 } 228 229 try { 230 oMDat = oConn.getMetaData(); 231 if (null==oMDat) 232 sDBMS = "unknown"; 233 else 234 sDBMS = oConn.getMetaData().getDatabaseProductName(); 235 } 236 catch (NullPointerException npe) { 237 sDBMS = "unknown"; 238 } 239 240 if (sDBMS.equals("PostgreSQL")) { 241 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 242 243 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_get_cat_path('" + getStringNull(DB.gu_category, "null") + "'))"); 244 245 oRSet = oStmt.executeQuery("SELECT k_sp_get_cat_path ('" + getString(DB.gu_category) + "')"); 246 oRSet.next(); 247 sPath = oRSet.getString(1); 248 oRSet.close(); 249 oStmt.close(); 250 } 251 else { 252 if (DebugFile.trace) DebugFile.writeln("{call k_sp_get_cat_path ('" + getStringNull(DB.gu_category, "null") + "',?)}"); 253 254 oCall = oConn.prepareCall("{call k_sp_get_cat_path (?,?)}"); 255 oCall.setString(1, getString(DB.gu_category)); 256 oCall.registerOutParameter(2, java.sql.Types.VARCHAR); 257 oCall.execute(); 258 sPath = oCall.getString(2); 259 oCall.close(); 260 } 261 263 if (DebugFile.trace) { 264 DebugFile.decIdent(); 265 DebugFile.writeln("End Category.getPath() : " + sPath); 266 } 267 268 return sPath; 269 } 271 273 282 public boolean delete(JDCConnection oConn) throws SQLException { 283 try { 284 return Category.delete(oConn, getString(DB.gu_category)); 285 } catch (IOException ioe) { 286 throw new SQLException ("IOException " + ioe.getMessage()); 287 } 288 } 290 292 304 public int addObject(Connection oConn, String sIdObject, int iIdClass, int iAttribs, int iOdPosition) throws SQLException { 305 PreparedStatement oStmt; 306 int iRetVal; 307 308 if (DebugFile.trace) { 309 DebugFile.writeln("Begin Category.addObject([Connection], " + sIdObject + ", ...)" ); 310 DebugFile.incIdent(); 311 } 312 313 oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_cat_objs + " (" + DB.gu_category + "," + DB.gu_object + "," + DB.id_class + "," + DB.bi_attribs + "," + DB.od_position + ") VALUES (?,?,?,?,?)"); 314 oStmt.setString(1, getString(DB.gu_category)); 315 oStmt.setString(2, sIdObject); 316 oStmt.setInt (3, iIdClass); 317 oStmt.setInt (4, iAttribs); 318 oStmt.setInt (5, iOdPosition); 319 iRetVal = oStmt.executeUpdate(); 320 oStmt.close(); 321 322 if (DebugFile.trace) { 323 DebugFile.decIdent(); 324 DebugFile.writeln("End Category.addProduct() : " + iRetVal); 325 } 326 327 return iRetVal; 328 } 330 332 340 public int removeObject(Connection oConn, String sIdObject) throws SQLException { 341 int iRetVal; 342 PreparedStatement oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "=? AND " + DB.gu_object + "=?"); 343 oStmt.setString(1, getString(DB.gu_category)); 344 oStmt.setString(2, sIdObject); 345 iRetVal = oStmt.executeUpdate(); 346 oStmt.close(); 347 return iRetVal; 348 } 350 352 361 public void removeGroupPermissions (Connection oConn, String sIdGroups, short iRecurse, short iObjects) throws SQLException { 362 CallableStatement oStmt; 363 StringTokenizer oUsrTok; 364 int iTokCount; 365 String sIdCategory; 366 367 if (DebugFile.trace) { 368 DebugFile.writeln("Begin Category.removeGroupPermissions([Connection], " + sIdGroups + "," + iRecurse + "," + iObjects + ")" ); 369 DebugFile.incIdent(); 370 DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_del_grp ('" + getStringNull(DB.gu_category, "null") + "',?," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 371 } 372 373 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) 374 oStmt = oConn.prepareCall("{ call k_sp_cat_del_grp ('" + getString(DB.gu_category) + "',?,CAST(" + String.valueOf(iRecurse) + " AS SMALLINT), CAST(" + String.valueOf(iObjects) + " AS SMALLINT)) }"); 375 else 376 oStmt = oConn.prepareCall("{ call k_sp_cat_del_grp ('" + getString(DB.gu_category) + "',?," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 377 378 if (sIdGroups.indexOf(',')>=0) { 379 oUsrTok = new StringTokenizer (sIdGroups, ","); 380 iTokCount = oUsrTok.countTokens(); 381 sIdCategory = getString(DB.gu_category); 382 383 for (int t=0; t<iTokCount; t++) { 384 oStmt.setString(1, oUsrTok.nextToken()); 385 oStmt.execute(); 386 } 388 oStmt.close(); 389 } 390 else { 391 oStmt.setString(1, sIdGroups); 392 oStmt.execute(); 393 oStmt.close(); 394 } 395 396 if (DebugFile.trace) { 397 DebugFile.decIdent(); 398 DebugFile.writeln("End Category.Category.removeGroupPermissions()"); 399 } 400 } 402 404 418 public void setGroupPermissions(Connection oConn, String sIdGroups, int iACLMask, short iRecurse, short iObjects) throws SQLException { 419 PreparedStatement oStmt; 420 CallableStatement oCall; 421 StringTokenizer oUsrTok; 422 String sToken; 423 int iTokCount; 424 425 if (DebugFile.trace) { 426 DebugFile.writeln("Begin Category.setGroupPermissions([Connection], " + sIdGroups + "," + iACLMask + "," + iRecurse + "," + iObjects + ")" ); 427 DebugFile.incIdent(); 428 429 DebugFile.writeln("database product name " + oConn.getMetaData().getDatabaseProductName()); 430 431 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) 432 DebugFile.writeln("Connection.prepareStatement(SELECT k_sp_cat_set_grp ('" + getString(DB.gu_category) + "',?," + String.valueOf(iACLMask) + ", CAST(" + String.valueOf(iRecurse) + " AS SMALLINT), CAST(" + String.valueOf(iObjects) + " AS SMALLINT))"); 433 else 434 DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_set_grp ('" + getStringNull(DB.gu_category, "null") + "',?," + String.valueOf(iACLMask) + "," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 435 } 436 437 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) { 438 oStmt = oConn.prepareStatement("SELECT k_sp_cat_set_grp ('" + getString(DB.gu_category) + "',?," + String.valueOf(iACLMask) + ", CAST(" + String.valueOf(iRecurse) + " AS SMALLINT), CAST(" + String.valueOf(iObjects) + " AS SMALLINT))"); 439 if (sIdGroups.indexOf(',')>0) { 440 oUsrTok = new StringTokenizer (sIdGroups, ","); 441 iTokCount = oUsrTok.countTokens(); 442 for (int t=0; t<iTokCount; t++) { 443 oStmt.setString(1, oUsrTok.nextToken()); 444 oStmt.execute(); 445 } } 447 else { 448 oStmt.setString(1, sIdGroups); 449 oStmt.execute(); 450 } 451 oStmt.close(); 452 } else { 453 oCall = oConn.prepareCall("{ call k_sp_cat_set_grp ('" + getString(DB.gu_category) + "',?," + String.valueOf(iACLMask) + "," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 454 if (sIdGroups.indexOf(',')>0) { 455 oUsrTok = new StringTokenizer (sIdGroups, ","); 456 iTokCount = oUsrTok.countTokens(); 457 for (int t=0; t<iTokCount; t++) { 458 sToken = oUsrTok.nextToken(); 459 if (DebugFile.trace) DebugFile.writeln("CallableStatement.setString(1,"+sToken+")"); 460 oCall.setString(1, sToken); 461 oCall.execute(); 462 } } 464 else { 465 if (DebugFile.trace) DebugFile.writeln("CallableStatement.setString(1,"+sIdGroups+")"); 466 oCall.setString(1, sIdGroups); 467 oCall.execute(); 468 } 469 oCall.close(); 470 } 471 472 if (DebugFile.trace) { 473 String [] aGrps = com.knowgate.misc.Gadgets.split(sIdGroups,','); 474 int iMsk; 475 for (int g=0; g<aGrps.length; g++) { 476 iMsk = getGroupPermissions(oConn,aGrps[g]); 477 if (iMsk!=iACLMask) 478 throw new SQLException ("Procedure k_sp_cat_grp_perm returned a different permissions mask ("+String.valueOf(iMsk)+") for group "+aGrps[g]+" on category " + getStringNull(DB.gu_category,null)+ " than that set by k_sp_cat_set_grp ("+String.valueOf(iACLMask)+")"); 479 } 480 DebugFile.decIdent(); 481 DebugFile.writeln("End Category.Category.setGroupPermissions()"); 482 } 483 } 485 487 502 public int getUserPermissions(Connection oConn, String sIdUser) throws SQLException { 503 int iACLMask; 504 CallableStatement oCall; 505 Statement oStmt; 506 ResultSet oRSet; 507 508 if (DebugFile.trace) { 509 DebugFile.writeln("Begin Category.getUserPermissions([Connection], " + sIdUser + ")" ); 510 DebugFile.incIdent(); 511 } 512 513 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) { 514 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 515 516 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_cat_usr_perm ('" + sIdUser + "','" + getStringNull(DB.gu_category,"null") + "'))"); 517 518 oRSet = oStmt.executeQuery("SELECT k_sp_cat_usr_perm ('" + sIdUser + "','" + getString(DB.gu_category) + "')"); 519 oRSet.next(); 520 iACLMask = oRSet.getInt(1); 521 oRSet.close(); 522 oStmt.close(); 523 } 524 else { 525 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_usr_perm('" + sIdUser + "','" + getStringNull(DB.gu_category,null) + "',?) })"); 526 527 oCall = oConn.prepareCall("{ call k_sp_cat_usr_perm(?,?,?) }"); 528 oCall.setString(1, sIdUser); 529 oCall.setString(2, getString(DB.gu_category)); 530 oCall.registerOutParameter(3, java.sql.Types.INTEGER); 531 oCall.execute(); 532 iACLMask = oCall.getInt(3); 533 oCall.close(); 534 } 535 536 if (DebugFile.trace) { 537 DebugFile.decIdent(); 538 DebugFile.writeln("End Category.getUserPermissions() : " + String.valueOf(iACLMask)); 539 } 540 541 return iACLMask; 542 } 544 546 561 public int getGroupPermissions(Connection oConn, String sIdGroup) throws SQLException { 562 int iACLMask; 563 CallableStatement oCall; 564 Statement oStmt; 565 ResultSet oRSet; 566 567 if (DebugFile.trace) { 568 DebugFile.writeln("Begin Category.getGroupPermissions([Connection], " + sIdGroup + ")" ); 569 DebugFile.incIdent(); 570 } 571 572 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) { 573 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 574 575 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_cat_grp_perm ('" + sIdGroup + "','" + getStringNull(DB.gu_category,"null") + "'))"); 576 577 oRSet = oStmt.executeQuery("SELECT k_sp_cat_grp_perm ('" + sIdGroup + "','" + getString(DB.gu_category) + "')"); 578 oRSet.next(); 579 iACLMask = oRSet.getInt(1); 580 oRSet.close(); 581 oStmt.close(); 582 } 583 else { 584 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_grp_perm('" + sIdGroup + "','" + getStringNull(DB.gu_category,null) + "',?) })"); 585 586 oCall = oConn.prepareCall("{ call k_sp_cat_grp_perm(?,?,?) }"); 587 oCall.setString(1, sIdGroup); 588 oCall.setString(2, getString(DB.gu_category)); 589 oCall.registerOutParameter(3, java.sql.Types.INTEGER); 590 oCall.execute(); 591 iACLMask = oCall.getInt(3); 592 oCall.close(); 593 } 594 595 if (DebugFile.trace) { 596 DebugFile.decIdent(); 597 DebugFile.writeln("End Category.getGroupPermissions() : " + String.valueOf(iACLMask)); 598 } 599 600 return iACLMask; 601 } 603 605 616 public void removeUserPermissions(Connection oConn, String sIdUsers, short iRecurse, short iObjects) throws SQLException { 617 CallableStatement oStmt; 618 StringTokenizer oUsrTok; 619 int iTokCount; 620 621 if (DebugFile.trace) { 622 DebugFile.writeln("Begin Category.removeUserPermissions([Connection], " + sIdUsers + "," + iRecurse + "," + iObjects + ")" ); 623 DebugFile.incIdent(); 624 DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_del_usr ('" + getStringNull(DB.gu_category, "null") + "',?," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 625 } 626 627 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) 628 oStmt = oConn.prepareCall("{ call k_sp_cat_del_usr ('" + getString(DB.gu_category) + "',?, CAST(" + String.valueOf(iRecurse) + " AS SMALLINT), CAST(" + String.valueOf(iObjects) + " AS SMALLINT)) }"); 629 else 630 oStmt = oConn.prepareCall("{ call k_sp_cat_del_usr ('" + getString(DB.gu_category) + "',?," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"); 631 632 if (sIdUsers.indexOf(',')>=0) { 633 oUsrTok = new StringTokenizer (sIdUsers, ","); 634 iTokCount = oUsrTok.countTokens(); 635 636 for (int t=0; t<iTokCount; t++) { 637 oStmt.setString(1, oUsrTok.nextToken()); 638 oStmt.execute(); 639 } 641 oStmt.close(); 642 } 643 else { 644 oStmt.setString(1, sIdUsers); 645 oStmt.execute(); 646 oStmt.close(); 647 } 648 649 if (DebugFile.trace) { 650 DebugFile.decIdent(); 651 DebugFile.writeln("End Category.Category.removeUserPermissions()"); 652 } 653 } 655 657 670 public void setUserPermissions(Connection oConn, String sIdUsers, int iACLMask, short iRecurse, short iObjects) throws SQLException { 671 CallableStatement oStmt; 672 StringTokenizer oUsrTok; 673 String sSQL; 674 String sUserId; 675 int iTokCount; 676 677 if (DebugFile.trace) { 678 DebugFile.writeln("Begin Category.setUserPermissions([Connection], " + sIdUsers + "," + iACLMask + "," + iRecurse + "," + iObjects + ")" ); 679 DebugFile.incIdent(); 680 DebugFile.writeln(" " + DB.gu_category + "=" + getStringNull(DB.gu_category, "null")); 681 } 682 683 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) 684 sSQL = "{ call k_sp_cat_set_usr (?,?," + String.valueOf(iACLMask) + ", CAST(" + String.valueOf(iRecurse) + " AS SMALLINT), CAST(" + String.valueOf(iObjects) + " AS SMALLINT)) }"; 685 else 686 sSQL = "{ call k_sp_cat_set_usr (?,?," + String.valueOf(iACLMask) + "," + String.valueOf(iRecurse) + "," + String.valueOf(iObjects) + ") }"; 687 688 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall(" + sSQL + ")"); 689 690 oStmt = oConn.prepareCall(sSQL); 691 692 if (sIdUsers.indexOf(',')>0) { 693 oUsrTok = new StringTokenizer (sIdUsers, ","); 694 iTokCount = oUsrTok.countTokens(); 695 696 for (int t=0; t<iTokCount; t++) { 697 sUserId = oUsrTok.nextToken(); 698 699 if (DebugFile.trace) DebugFile.writeln("binding user " + String.valueOf(t+1) + " " + sUserId); 700 701 oStmt.setObject(1, getString(DB.gu_category), java.sql.Types.CHAR); 702 oStmt.setObject(2, sUserId, java.sql.Types.CHAR); 703 oStmt.execute(); 704 } 706 } 707 else { 708 if (DebugFile.trace) DebugFile.writeln("binding user " + sIdUsers); 709 710 oStmt.setObject(1, getString(DB.gu_category), java.sql.Types.CHAR); 711 oStmt.setObject(2, sIdUsers, java.sql.Types.CHAR); 712 oStmt.execute(); 713 } 714 715 oStmt.close(); 716 717 if (DebugFile.trace) { 718 DebugFile.decIdent(); 719 DebugFile.writeln("End Category.setUserPermissions()"); 720 } 721 } 723 725 735 public void inheritPermissions(JDCConnection oConn, String sFromCategory, short iRecurse, short iObjects) throws SQLException { 736 int i; 737 int iUsrPerms; 738 int iGrpPerms; 739 String sIdCategory = getString(DB.gu_category); 740 DBSubset oUsrPerms = new DBSubset(DB.k_x_cat_user_acl, DB.gu_user + "," + DB.acl_mask, DB.gu_category + "='" + sFromCategory + "'", 100); 741 DBSubset oGrpPerms = new DBSubset(DB.k_x_cat_group_acl, DB.gu_acl_group + "," + DB.acl_mask, DB.gu_category + "='" + sFromCategory + "'", 100); 742 Statement oDelete = oConn.createStatement(); 743 PreparedStatement oInsert; 744 745 if (DebugFile.trace) { 746 DebugFile.writeln("Begin Category.inheritPermissions([Connection], " + sFromCategory + "," + iRecurse + "," + iObjects + ")" ); 747 DebugFile.incIdent(); 748 } 749 750 if (DebugFile.trace) DebugFile.writeln(" loading user permissions from " + DB.k_x_cat_user_acl); 751 752 iUsrPerms = oUsrPerms.load(oConn); 753 754 if (DebugFile.trace) DebugFile.writeln(" loading group permissions from " + DB.k_x_cat_group_acl); 755 756 iGrpPerms = oGrpPerms.load(oConn); 757 758 if (DebugFile.trace) DebugFile.writeln(" Connection.executeUpdate(" + "DELETE FROM " + DB.k_x_cat_user_acl + " WHERE " + DB.gu_category + "='" + sIdCategory + "')"); 759 760 oDelete.executeUpdate("DELETE FROM " + DB.k_x_cat_user_acl + " WHERE " + DB.gu_category + "='" + sIdCategory + "'"); 761 762 if (DebugFile.trace) DebugFile.writeln(" Connection.executeUpdate(" + "DELETE FROM " + DB.k_x_cat_group_acl + " WHERE " + DB.gu_category + "='" + sIdCategory + "')"); 763 764 oDelete.executeUpdate("DELETE FROM " + DB.k_x_cat_group_acl + " WHERE " + DB.gu_category + "='" + sIdCategory + "'"); 765 766 oDelete.close(); 767 oDelete = null; 768 769 if (DebugFile.trace) DebugFile.writeln(" Connection.prepareStatement(" + "INSERT INTO " + DB.k_x_cat_user_acl + "(" + DB.gu_category + "," + DB.gu_user + "," + DB.acl_mask + ") VALUES (?,?,?))"); 770 771 oInsert = oConn.prepareStatement("INSERT INTO " + DB.k_x_cat_user_acl + "(" + DB.gu_category + "," + DB.gu_user + "," + DB.acl_mask + ") VALUES (?,?,?)"); 772 773 for (i=0; i<iUsrPerms; i++) { 774 oInsert.setString(1, sIdCategory ); 775 oInsert.setString(2, oUsrPerms.getString(0,i) ); 776 oInsert.setInt(3, oUsrPerms.getInt(1,i) ); 777 778 if (DebugFile.trace) DebugFile.writeln(" PreparedStatement.executeUpdate(" + sIdCategory + "," + oUsrPerms.getString(0,i) + "," + oUsrPerms.getInt(1,i) + ")"); 779 oInsert.executeUpdate(); 780 oInsert.close(); 781 } 782 783 if (DebugFile.trace) DebugFile.writeln(" Connection.prepareStatement(" + "INSERT INTO " + DB.k_x_cat_group_acl + "(" + DB.gu_category + "," + DB.gu_acl_group + "," + DB.acl_mask + ") VALUES (?,?,?))"); 784 785 oInsert = oConn.prepareStatement("INSERT INTO " + DB.k_x_cat_group_acl + "(" + DB.gu_category + "," + DB.gu_acl_group + "," + DB.acl_mask + ") VALUES (?,?,?)"); 786 for (i=0; i<iGrpPerms; i++) { 787 oInsert.setString(1, sIdCategory ); 788 oInsert.setString(2, oGrpPerms.getString(0,i) ); 789 oInsert.setInt(3, oGrpPerms.getInt(1,i) ); 790 791 if (DebugFile.trace) DebugFile.writeln(" PreparedStatement.executeUpdate(" + sIdCategory + "," + oGrpPerms.getString(0,i) + "," + oGrpPerms.getInt(1,i) + ")"); 792 oInsert.executeUpdate(); 793 oInsert.close(); 794 } 795 796 if (DebugFile.trace) { 797 DebugFile.decIdent(); 798 DebugFile.writeln("End Category.inheritPermissions()"); 799 } 800 } 802 804 811 public boolean isChildOf(Connection oConn, String sParentCategory) throws SQLException { 812 String sSelfId = getString(DB.gu_category); 813 String sChild; 814 boolean isChild = false; 815 boolean bDoNext; 816 PreparedStatement oStmt = oConn.prepareStatement("SELECT " + DB.gu_child_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_parent_cat + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 817 ResultSet oRSet; 818 819 do { 820 oStmt.setString(1, sParentCategory); 821 oRSet = oStmt.executeQuery(); 822 bDoNext = oRSet.next(); 823 if (bDoNext) 824 sChild = oRSet.getString(1); 825 else 826 sChild = "-1"; 827 oRSet.close(); 828 829 if (bDoNext) { 830 if (sChild.equals(sParentCategory)) { 831 bDoNext = false; 832 } 833 else if (sChild.equals(sSelfId)) { 834 isChild = true; 835 bDoNext = false; 836 } 837 else { 838 sParentCategory = sChild; 839 } 840 } } while (bDoNext); 842 843 oStmt.close(); 844 845 return isChild; 846 } 848 850 857 public boolean isParentOf(Connection oConn, String sChildCategory) throws SQLException { 858 String sSelfId; 859 String sParnt; 860 boolean isParent = false; 861 boolean bDoNext; 862 PreparedStatement oStmt = oConn.prepareStatement("SELECT " + DB.gu_parent_cat + " FROM " + DB.k_cat_tree + " WHERE " + DB.gu_child_cat + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 863 ResultSet oRSet; 864 865 if (DebugFile.trace) { 866 DebugFile.writeln("Begin Category.isParentOf(" + sChildCategory + ")"); 867 DebugFile.incIdent(); 868 } 869 870 sSelfId = getString(DB.gu_category); 871 872 if (DebugFile.trace) DebugFile.writeln(" " + DB.gu_category + " = " + sSelfId); 873 874 do { 875 oStmt.setString(1, sChildCategory); 876 oRSet = oStmt.executeQuery(); 877 bDoNext = oRSet.next(); 878 if (bDoNext) 879 sParnt = oRSet.getString(1); 880 else 881 sParnt = "-1"; 882 oRSet.close(); 883 884 if (DebugFile.trace) DebugFile.writeln(" id_parent = " + sParnt); 885 886 if (bDoNext) { 887 if (sParnt.equals(sChildCategory)) { 888 bDoNext = false; 889 } 890 else if (sParnt.equals(sSelfId)) { 891 isParent = true; 892 bDoNext = false; 893 } 894 else { 895 sChildCategory = sParnt; 896 } 897 } } while (bDoNext); 899 900 oStmt.close(); 901 902 if (DebugFile.trace) { 903 DebugFile.decIdent(); 904 DebugFile.writeln("End Category.isParentOf() : " + isParent); 905 } 906 907 return isParent; 908 } 910 912 920 public int level(JDCConnection oConn) throws SQLException { 921 int iLevel; 922 CallableStatement oCall; 923 Statement oStmt; 924 ResultSet oRSet; 925 926 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 927 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 928 oRSet = oStmt.executeQuery("SELECT k_sp_cat_level('" + getString(DB.gu_category) + "')"); 929 oRSet.next(); 930 iLevel = oRSet.getInt(1); 931 oRSet.close(); 932 oStmt.close(); 933 } 934 else { 935 oCall = oConn.prepareCall("{ call k_sp_cat_level(?,?)}"); 936 oCall.setString(1, getString(DB.gu_category)); 937 oCall.registerOutParameter(2, java.sql.Types.INTEGER); 938 oCall.execute(); 939 iLevel = oCall.getInt(2); 940 oCall.close(); 941 } 942 943 return iLevel; 944 } 946 948 953 public boolean getIsRoot(Connection oConn) throws SQLException { 954 Statement oStmt; 955 ResultSet oRSet; 956 boolean bRoot = false; 957 958 oStmt = oConn.createStatement(); 960 oRSet = oStmt.executeQuery("SELECT " + DB.gu_category + " FROM " + DB.k_cat_root + " WHERE " + DB.gu_category + "='" + getString(DB.gu_category) + "'"); 961 962 bRoot = oRSet.next(); 963 964 oRSet.close(); 965 oStmt.close(); 966 968 return bRoot; 969 } 971 973 980 public void setIsRoot(Connection oConn, boolean bIsRoot) throws SQLException { 981 Statement oStmt; 982 ResultSet oRSet; 983 boolean bIsChild; 984 985 if (DebugFile.trace) { 986 DebugFile.writeln("Begin Category.setIsRoot([Connection], " + String.valueOf(bIsRoot) + ")" ); 987 DebugFile.incIdent(); 988 } 989 990 if (bIsRoot) { 992 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 993 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT NULL FROM " + DB.k_cat_tree + " WHERE " + DB.gu_child_cat + "='" + getStringNull(DB.gu_category,"null") + "')"); 994 oRSet = oStmt.executeQuery("SELECT NULL FROM " + DB.k_cat_tree + " WHERE " + DB.gu_child_cat + "='" + getString(DB.gu_category) + "'"); 995 bIsChild = oRSet.next(); 996 oRSet.close(); 997 oStmt.close(); 998 999 if (bIsChild) 1000 throw new SQLException ("Category cannot be set Root if present as a child at k_cat_tree table"); 1001 } 1002 1003 oStmt = oConn.createStatement(); 1004 1005 if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_cat_root + " WHERE " + DB.gu_category + "='" + getStringNull(DB.gu_category, "null") + "')"); 1006 1007 oStmt.executeUpdate("DELETE FROM " + DB.k_cat_root + " WHERE " + DB.gu_category + "='" + getString(DB.gu_category) + "'"); 1008 1009 if (bIsRoot) { 1010 if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(INSERT INTO " + DB.k_cat_root + "(" + DB.gu_category + ") VALUES ('" + getStringNull(DB.gu_category, "null") + "')"); 1011 1012 oStmt.executeUpdate("INSERT INTO " + DB.k_cat_root + "(" + DB.gu_category + ") VALUES ('" + getString(DB.gu_category) + "')"); 1013 } 1014 1015 oStmt.close(); 1016 1018 if (DebugFile.trace) { 1019 DebugFile.decIdent(); 1020 DebugFile.writeln("End Category.setIsRoot()"); 1021 } 1022 } 1024 1033 public String getLabel(Connection oConn, String sLanguage) throws SQLException { 1034 String sTr; 1035 1036 if (DebugFile.trace) { 1037 DebugFile.writeln("Begin Category.getLabel([Connection], " + sLanguage + ")" ); 1038 DebugFile.incIdent(); 1039 DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.tr_category + " FROM " + DB.k_cat_labels + " WHERE " + DB.gu_category + "='" + get(DB.gu_category) + "' AND " + DB.id_language + "='" + sLanguage + "'"); 1040 } 1041 1042 PreparedStatement oStmt = oConn.prepareStatement("SELECT " + DB.tr_category + " FROM " + DB.k_cat_labels + " WHERE " + DB.gu_category + "=? AND " + DB.id_language + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1043 oStmt.setString(1, getString(DB.gu_category)); 1044 oStmt.setString(2, sLanguage); 1045 1046 ResultSet oRSet = oStmt.executeQuery(); 1047 if (oRSet.next()) 1048 sTr = oRSet.getString(1); 1049 else 1050 sTr = null; 1051 oRSet.close(); 1052 oStmt.close(); 1053 1054 if (DebugFile.trace) { 1055 DebugFile.decIdent(); 1056 DebugFile.writeln("End Category.getLabel() : " + sTr); 1057 } 1058 1059 return sTr; 1060 } 1062 1064 1074 public DBSubset getNames(JDCConnection oConn) throws SQLException { 1075 Object aCatg[] = { get(DB.gu_category) }; 1076 1077 oNames = new DBSubset(DB.k_cat_labels, DB.id_language + "," + DB.tr_category + "," + DB.url_category, DB.gu_category + "=?", 4); 1078 oNames.load (oConn, aCatg); 1079 1080 return oNames; 1081 } 1083 1085 1091 public DBSubset getChilds(JDCConnection oConn) throws SQLException { 1092 Object aCatg[] = { get(DB.gu_category) }; 1093 1094 oChilds = new DBSubset(DB.k_cat_tree, DB.gu_child_cat, DB.gu_parent_cat + "=?",1); 1095 1096 oChilds.load (oConn, aCatg); 1097 1098 return oChilds; 1099 } 1101 1103 1109 public DBSubset getParents(JDCConnection oConn) throws SQLException { 1110 Object aCatg[] = { get(DB.gu_category) }; 1111 1112 oParents = new DBSubset(DB.k_cat_tree, DB.gu_parent_cat, DB.gu_child_cat + "=?", 1); 1113 oParents.load (oConn, aCatg); 1114 1115 return oParents; 1116 } 1118 1120 1129 public DBSubset getObjects(JDCConnection oConn) throws SQLException { 1130 Object aCatg[] = { get(DB.gu_category) }; 1131 DBSubset oObjs; 1132 1133 oObjs = new DBSubset(DB.k_x_cat_objs, DB.gu_object + "," + DB.id_class + "," + DB.bi_attribs, DB.gu_category + "=? ORDER BY " + DB.od_position, 64); 1134 1135 oObjs.load(oConn, new Object []{getString(DB.gu_category)}); 1136 1137 return oObjs; 1138 } 1140 1142 1148 public DBSubset getACLGroups(JDCConnection oConn) throws SQLException { 1149 Object aCatg[] = { get(DB.gu_category) }; 1150 1151 oACLGroups = new DBSubset(DB.k_x_cat_group_acl, DB.gu_acl_group + "," + DB.acl_mask, DB.gu_category + "=?", 50); 1152 oACLGroups.load (oConn, aCatg); 1153 1154 return oACLGroups; 1155 } 1157 1159 1165 public DBSubset getACLUsers(JDCConnection oConn) throws SQLException { 1166 Object aCatg[] = { get("id_category") }; 1167 1168 oACLUsers = new DBSubset(DB.k_x_cat_user_acl, DB.gu_user + "," + DB.acl_mask, DB.gu_category + "=?", 100); 1169 oACLUsers.load (oConn, aCatg); 1170 1171 return oACLUsers; 1172 } 1174 1176 1185 public void setParent(Connection oConn, String sIdParent) throws SQLException { 1186 Statement oStmt = oConn.createStatement(); 1187 ResultSet oRSet; 1188 String sSQL; 1189 boolean bAlreadyExists; 1190 1191 if (DebugFile.trace) { 1192 DebugFile.writeln("Begin Category.setParent([Connection], " + sIdParent + ")" ); 1193 DebugFile.incIdent(); 1194 } 1195 1196 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1197 sSQL = "SELECT NULL FROM " + DB.k_cat_tree + " WHERE " + DB.gu_parent_cat + "='" + sIdParent + "' AND " + DB.gu_child_cat + "='" + getString(DB.gu_category) + "'"; 1198 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSQL + ")"); 1199 oRSet = oStmt.executeQuery(sSQL); 1200 bAlreadyExists = oRSet.next(); 1201 oRSet.close(); 1202 oStmt.close(); 1203 1204 if (!bAlreadyExists) { 1205 oStmt = oConn.createStatement(); 1206 sSQL = "INSERT INTO " + DB.k_cat_tree + " (" + DB.gu_parent_cat + "," + DB.gu_child_cat + ") VALUES ('" + sIdParent + "','" + getString(DB.gu_category) + "')"; 1207 if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")"); 1208 oStmt.execute(sSQL); 1209 oStmt.close(); 1210 } 1211 if (DebugFile.trace) { 1212 DebugFile.decIdent(); 1213 DebugFile.writeln("End Category.setParent()"); 1214 } 1215 } 1217 1219 1226 public void resetParent(Connection oConn, String sIdParent) throws SQLException { 1227 Statement oStmt = oConn.createStatement(); 1228 String sSQL; 1229 1230 if (DebugFile.trace) { 1231 DebugFile.writeln("Begin Category.resetParent([Connection], " + sIdParent + ")" ); 1232 DebugFile.incIdent(); 1233 } 1234 1235 sSQL = "DELETE FROM " + DB.k_cat_tree + " WHERE " + DB.gu_parent_cat + "='" + sIdParent + "' AND " + DB.gu_child_cat + "='" + getString(DB.gu_category) + "'"; 1236 1237 if (DebugFile.trace) DebugFile.writeln("oStmt.executeUpdate(" + sSQL + ")"); 1238 1239 oStmt.executeUpdate(sSQL); 1240 1241 if (DebugFile.trace) { 1242 DebugFile.decIdent(); 1243 DebugFile.writeln("End Category.resetParent()"); 1244 } 1245 } 1247 1249 1256 public boolean store(JDCConnection oConn) throws SQLException { 1257 java.sql.Timestamp dtNow = new java.sql.Timestamp (DBBind.getTime()); 1258 1259 if (!AllVals.containsKey(DB.gu_category)) 1262 put(DB.gu_category, Gadgets.generateUUID()); 1263 1264 replace(DB.dt_modified, dtNow); 1266 1267 return super.store(oConn); 1268 } 1270 1272 1279 public void expand(Connection oConn) throws SQLException { 1280 CallableStatement oStmt; 1281 1282 if (DebugFile.trace) { 1283 DebugFile.writeln("Begin Category.expand([Connection])"); 1284 DebugFile.incIdent(); 1285 DebugFile.writeln("Connection.prepareCall({ call k_sp_cat_expand ('" + getStringNull(DB.gu_category,"null") + "')}"); 1286 } 1287 1288 oStmt = oConn.prepareCall("{ call k_sp_cat_expand ('" + getString(DB.gu_category) + "') }"); 1289 oStmt.execute(); 1290 oStmt.close(); 1291 1292 if (DebugFile.trace) { 1293 DebugFile.decIdent(); 1294 DebugFile.writeln("End Category.expand()"); 1295 } 1296 } 1298 1300 1313 public void storeLabels(JDCConnection oConn, String sNamesTable, 1314 String sRowDelim, String sColDelim) 1315 throws SQLException , NoSuchElementException { 1316 String sName; 1317 String sLanguageId; 1318 String sTrCategory; 1319 int iTokCount; 1320 StringTokenizer oRowTok; 1321 StringTokenizer oColTok; 1322 CategoryLabel oName = new CategoryLabel(); 1323 1324 if (DebugFile.trace) { 1325 DebugFile.writeln("Begin Category.storeLabels([Connection], \"" + sNamesTable + "\",\"" + sRowDelim + "\",\"" + sColDelim + "\")"); 1326 DebugFile.incIdent(); 1327 } 1328 1329 if (sNamesTable.length()>0) { 1330 oName.put (DB.gu_category, getString(DB.gu_category)); 1331 1332 1334 if (DebugFile.trace) DebugFile.writeln("new StringTokenizer(" + sNamesTable + "\"" + sRowDelim + "\""); 1335 1336 oRowTok = new StringTokenizer (sNamesTable, sRowDelim); 1337 1338 iTokCount = oRowTok.countTokens(); 1339 1340 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iTokCount) + " tokens found"); 1341 1342 for (int r=0; r<iTokCount; r++) { 1343 sName = oRowTok.nextToken(); 1345 1346 if (DebugFile.trace) DebugFile.writeln("new StringTokenizer(" + sName + ", \"" + sColDelim + "\""); 1347 1348 oColTok = new StringTokenizer (sName, sColDelim); 1350 1351 if (DebugFile.trace) DebugFile.writeln("StringTokenizer.nextToken(" + String.valueOf(r) + ") for id_language"); 1352 1353 sLanguageId = oColTok.nextToken(); 1354 1355 if (DebugFile.trace) DebugFile.writeln("StringTokenizer.nextToken(" + String.valueOf(r) + ") for tr_category"); 1356 1357 sTrCategory = oColTok.nextToken(); 1358 1359 if (sTrCategory!=null) { 1360 sTrCategory = sTrCategory.trim(); 1361 1362 if (sTrCategory.length()>0) { 1363 oName.replace(DB.id_language, sLanguageId); 1364 oName.replace(DB.tr_category, sTrCategory); 1365 1366 if (DebugFile.trace) DebugFile.writeln("CategoryLabel.store("+ sLanguageId + "," + sTrCategory + ")"); 1367 1368 oName.store(oConn); 1369 } 1370 } } } 1374 if (DebugFile.trace) { 1375 DebugFile.decIdent(); 1376 DebugFile.writeln("End Category.storeLabels()"); 1377 } 1378 } 1380 1382 1396 public void uploadDirectory (JDCConnection oConn, String sSourcePath, String sProtocol, 1397 String sServer, String sTargetPath, String sLanguage) 1398 throws Exception , IOException , SQLException { 1399 File oDir, oFile; 1400 File aFiles[]; 1401 int iFiles; 1402 String sFileName, sBasePath, sTargetChomp, sNewCategoryId, sNewCategoryNm; 1403 Properties oURLProps; 1404 FileInputStream oIOStrm; 1405 PreparedStatement oStmt, oCatg; 1406 ResultSet oRSet; 1407 1408 Category oNewCategory; 1409 Product oProd; 1410 ProductLocation oLoca; 1411 1412 Object aCatValues[]; 1413 Object aLblValues[]; 1414 Short iTrue = new Short ((short)1); 1415 Integer iActive = new Integer (1); 1416 1417 if (DebugFile.trace) { 1418 DebugFile.writeln("Begin Category.uploadDirectory([Connection], " + sSourcePath + ", ..., " + sTargetPath + "," + sLanguage + ")" ); 1419 DebugFile.incIdent(); 1420 } 1421 1422 if (null==oFS) { 1423 if (DebugFile.trace) DebugFile.writeln("new com.knowgate.dfs.FileSystem()"); 1424 oFS = new FileSystem(); 1425 } 1426 1427 sBasePath = sSourcePath.substring(sSourcePath.indexOf("://")+3); 1429 1430 if (DebugFile.trace) DebugFile.writeln("sBasePath=" + sBasePath); 1431 1432 oProd = new Product(); 1433 oProd.put(DB.gu_owner, getString(DB.gu_owner)); 1434 1435 oDir = new File (sBasePath); 1437 aFiles = oDir.listFiles(); 1438 iFiles = aFiles.length; 1439 1440 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iFiles) + " files found"); 1441 1442 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT NULL FROM " + DB.k_products + " p, " + DB.k_x_cat_objs + " o WHERE p." + DB.nm_product + "=? AND o." + DB.gu_category + "=? AND o." + DB.id_class + "=" + String.valueOf(Product.ClassId) + " AND p." + DB.gu_product + "=o." + DB.gu_object); 1444 oStmt = oConn.prepareStatement("SELECT " + DB.gu_product + " FROM " + DB.k_products + " p, " + DB.k_x_cat_objs + " o WHERE p." + DB.nm_product + "=? AND o." + DB.gu_category + "=? AND o." + DB.id_class + "=" + String.valueOf(Product.ClassId) + " AND p." + DB.gu_product + "=o." + DB.gu_object, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1445 1446 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_category + " FROM " + DB.k_cat_tree + " t," + DB.k_cat_labels + " l WHERE t.gu_parent_cat=? AND l.tr_category=? AND t.gu_child_cat=l.gu_category"); 1448 oCatg = oConn.prepareStatement("SELECT " + DB.gu_category + " FROM " + DB.k_cat_tree + " t," + DB.k_cat_labels + " l WHERE t." + DB.gu_parent_cat + "=? AND l." + DB.tr_category + "=? AND t." + DB.gu_child_cat + "=l." + DB.gu_category, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1449 1450 for (int f=0; f<iFiles; f++) { 1451 oFile = aFiles[f]; 1452 if (oFile.isFile()) { 1453 sFileName = oFile.getName(); 1454 1455 if (DebugFile.trace) DebugFile.writeln("nm_product=" + (sFileName.length()<=128 ? sFileName : sFileName.substring(0,128))); 1456 1457 if (sFileName.toLowerCase().endsWith(".url")) 1458 oProd.put(DB.nm_product, (sFileName.length()<=128 ? sFileName.substring(0,sFileName.length()-4) : sFileName.substring(0,128))); 1460 else 1461 oProd.put(DB.nm_product, (sFileName.length()<=128 ? sFileName : sFileName.substring(0,128))); 1463 1464 oStmt.setString(1, oProd.getString(DB.nm_product)); 1466 oStmt.setString(2, getString(DB.gu_category)); 1467 oRSet = oStmt.executeQuery(); 1468 if (oRSet.next()) 1469 oProd.put(DB.gu_product, oRSet.getString(1)); 1470 oRSet.close(); 1471 1472 if (DebugFile.trace) DebugFile.writeln("oProd.store([Connection]);"); 1473 oProd.store(oConn); 1474 1475 if (DebugFile.trace) DebugFile.writeln("oProd.addToCategory([Connection], " + getStringNull(DB.gu_category, "null") + ");"); 1477 oProd.addToCategory(oConn, this.getString(DB.gu_category), 0); 1478 1479 oLoca = new ProductLocation(); 1481 1482 oLoca.put(DB.gu_owner, getString(DB.gu_owner)); 1483 oLoca.put(DB.gu_product, oProd.get(DB.gu_product)); 1484 1485 if (sFileName.toLowerCase().endsWith(".url")) { 1486 oURLProps = new Properties (); 1490 oIOStrm = new FileInputStream (oFile); 1491 oURLProps.load(oIOStrm); 1492 oIOStrm.close(); 1493 1494 if (DebugFile.trace) DebugFile.writeln("URL=" + oURLProps.getProperty("URL", "null")); 1495 1496 oLoca.setURL(oURLProps.getProperty("URL")); 1497 oLoca.remove(DB.xfile); 1498 oLoca.remove(DB.xoriginalfile); 1499 1500 oLoca.put(DB.id_cont_type, oLoca.getContainerType()); 1501 oLoca.setLength(0); 1502 if (DebugFile.trace) DebugFile.writeln("oLoca.store([Connection])"); 1503 oLoca.store(oConn); 1504 1505 try { oConn.commit(); } catch (SQLException ignore) { } 1506 } 1507 else { 1508 oLoca.setPath (sProtocol, sServer, sTargetPath, sFileName, sFileName); 1510 oLoca.put(DB.id_cont_type, oLoca.getContainerType()); 1511 oLoca.setLength(new Long (oFile.length()).intValue()); 1512 1513 if (DebugFile.trace) DebugFile.writeln("oLoca.store([Connection])"); 1514 oLoca.store(oConn); 1515 1516 try { oConn.commit(); } catch (SQLException ignore) { } 1517 1518 oLoca.upload(oConn, oFS, sSourcePath, sFileName, sProtocol + sServer + sTargetPath, sFileName); 1521 } 1522 oLoca = null; 1523 1524 oProd.remove(DB.gu_product); 1525 oProd.remove(DB.nm_product); 1526 } 1527 else if (oFile.isDirectory()) { 1528 sFileName = oFile.getName(); 1529 1530 if (sProtocol.startsWith("file://")) 1531 sTargetChomp = (sTargetPath.endsWith(System.getProperty("file.separator")) ? sTargetPath : sTargetPath + System.getProperty("file.separator")); 1532 else 1533 sTargetChomp = (sTargetPath.endsWith("/") ? sTargetPath : sTargetPath + "/"); 1534 1535 oCatg.setString(1, getString(DB.gu_category)); 1536 oCatg.setString(2, sFileName); 1537 oRSet = oCatg.executeQuery(); 1538 if (oRSet.next()) { 1539 sNewCategoryId = oRSet.getString(1); 1540 oNewCategory = new Category(oConn, sNewCategoryId); 1541 sNewCategoryNm = oNewCategory.getString(DB.nm_category); 1542 1543 oFS.mkdirs(sProtocol + sServer + sTargetChomp + sNewCategoryNm); 1545 } 1546 else { 1547 1548 sNewCategoryNm = Category.makeName(oConn, sFileName); 1550 1551 if (DebugFile.trace) DebugFile.writeln("sNewCategoryNm=" + sNewCategoryNm); 1552 1553 aCatValues = new Object [] { getString(DB.gu_category), getString(DB.gu_owner), sNewCategoryNm, iTrue, iActive, "folderclosed_16x16.gif", "folderopen_16x16.gif"}; 1555 sNewCategoryId = Category.create(oConn, aCatValues); 1556 1557 aLblValues = new Object [] { sNewCategoryId, sLanguage, sFileName, null }; 1559 CategoryLabel.create(oConn, aLblValues); 1560 1561 try { oConn.commit(); } catch (SQLException ignore) { } 1562 1563 oFS.mkdirs(sProtocol + sServer + sTargetChomp + sNewCategoryNm); 1565 1566 oNewCategory = new Category(oConn, sNewCategoryId); 1568 oNewCategory.inheritPermissions(oConn, getString(DB.gu_category), (short)1, (short)1); 1569 } 1570 oRSet.close(); 1571 1572 oNewCategory.uploadDirectory(oConn, "file://" + oFile.getAbsolutePath(), sProtocol, sServer, sTargetChomp + sNewCategoryNm, sLanguage); 1573 } 1574 } 1575 1576 if (DebugFile.trace) { 1577 DebugFile.decIdent(); 1578 DebugFile.writeln("End Category.uploadDirectory()"); 1579 } 1580 1581 } 1583 1586 1604 public static String store(JDCConnection oConn, String sCategoryId, String sParentId, String sCategoryName, short iIsActive, int iDocStatus, String sOwner, String sIcon1, String sIcon2 ) throws SQLException { 1605 Category oCatg = new Category (); 1606 boolean isParentOfParent = false; 1607 Object aCatg[] = { null }; 1608 DBSubset oNames; 1609 1610 if (DebugFile.trace) { 1611 DebugFile.writeln("Begin Category.store([Connection], " + sCategoryId + ", " + sParentId + ", sCategoryName" + ", ...)" ); 1612 DebugFile.incIdent(); 1613 } 1614 1615 oCatg.put (DB.gu_owner, sOwner); 1616 oCatg.put (DB.nm_category, sCategoryName); 1617 oCatg.put (DB.bo_active, iIsActive); 1618 oCatg.put (DB.id_doc_status, iDocStatus); 1619 1620 if (null!=sIcon1) oCatg.put (DB.nm_icon, sIcon1); 1621 if (null!=sIcon2) oCatg.put (DB.nm_icon2, sIcon2); 1622 1623 if (null!=sCategoryId) { 1624 oCatg.put (DB.gu_category, sCategoryId); 1625 1626 if (null!=sParentId) { 1628 if (sCategoryId.equalsIgnoreCase(sParentId)) { 1629 if (DebugFile.trace) DebugFile.writeln("ERROR: Category " + sCategoryName + " is its own parent"); 1630 throw new SQLException ("Category tree circular reference"); 1631 } 1633 isParentOfParent = oCatg.isParentOf(oConn, sParentId); 1637 } 1639 } 1641 if (isParentOfParent) { 1642 if (DebugFile.trace) DebugFile.writeln("ERROR: Category " + sCategoryName + " has a circular parentship relationship"); 1643 throw new SQLException ("Category tree circular reference"); 1644 } 1645 1646 if (null!=sCategoryId) { 1649 if (DebugFile.trace) DebugFile.writeln("Clearing labels..."); 1650 aCatg[0] = oCatg.getString(DB.gu_category); 1651 oNames = new DBSubset (DB.k_cat_labels, DB.id_language+","+DB.tr_category+","+DB.url_category, DB.gu_category+"=?",1); 1652 oNames.clear (oConn, aCatg); 1653 if (DebugFile.trace) DebugFile.writeln("Labels cleared."); 1654 } 1655 else 1656 oCatg.remove(DB.gu_category); 1657 1658 oCatg.store(oConn); 1662 1663 if (null==sParentId) 1665 oCatg.setIsRoot(oConn, true); 1666 else 1667 oCatg.setParent(oConn, sParentId); 1668 1669 if (DebugFile.trace) { 1670 DebugFile.decIdent(); 1671 DebugFile.writeln("End Category.store() : " + oCatg.getString(DB.gu_category)); 1672 } 1673 1674 return oCatg.getString(DB.gu_category); 1676 } 1678 1680 1690 public static boolean delete(JDCConnection oConn, String sCategoryGUID) 1691 throws SQLException , IOException { 1692 1693 if (DebugFile.trace) { 1694 DebugFile.writeln("Begin Category.delete([Connection], " + sCategoryGUID + ")"); 1695 DebugFile.incIdent(); 1696 } 1697 1698 Category oCat = new Category(sCategoryGUID); 1699 1700 DBSubset oChlds = oCat.getChilds(oConn); 1703 int iChilds = oChlds.getRowCount(); 1704 for (int c=0; c<iChilds; c++) 1705 Category.delete(oConn, oChlds.getString(0,c)); 1706 1707 Statement oStmt; 1708 Product oProd; 1709 DBSubset oObjs = oCat.getObjects(oConn); 1710 int iObjs = oObjs.getRowCount(); 1711 boolean bRetVal; 1712 1713 for (int o=0; o<iObjs; o++) { 1715 switch (oObjs.getInt(1, o)) { 1716 case com.knowgate.hipergate.Product.ClassId: 1717 oProd = new Product(oConn, oObjs.getString(0, o)); 1718 oProd.delete(oConn); 1719 break; 1720 case com.knowgate.crm.Company.ClassId: 1721 com.knowgate.crm.Company.delete(oConn, oObjs.getString(0, o)); 1722 break; 1723 case com.knowgate.forums.NewsGroup.ClassId: 1724 com.knowgate.forums.NewsGroup.delete(oConn, oObjs.getString(0, o)); 1725 break; 1726 case com.knowgate.hipergate.Image.ClassId: 1727 Image oImg = new com.knowgate.hipergate.Image(oConn, oObjs.getString(0, o)); 1728 oImg.delete(oConn); 1729 break; 1730 case com.knowgate.hipermail.DBMimeMessage.ClassId: 1731 com.knowgate.hipermail.DBMimeMessage.delete(oConn, sCategoryGUID, oObjs.getString(0, o)); 1732 break; 1733 } 1734 } 1736 oObjs = null; 1737 1738 if (DBBind.exists(oConn, DB.k_mime_msgs, "U")) { 1739 oObjs = new DBSubset(DB.k_mime_msgs, DB.gu_mimemsg, DB.gu_category + "='" + sCategoryGUID + "'", 1000); 1740 iObjs = oObjs.load(oConn); 1741 1742 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 1743 PreparedStatement oDlte = oConn.prepareStatement("SELECT k_sp_del_mime_msg(?)"); 1744 ResultSet oRSet; 1745 for (int m=0; m<iObjs; m++) { 1746 oDlte.setString(1, oObjs.getString(0,m)); 1747 oRSet = oDlte.executeQuery(); 1748 oRSet.close(); 1749 } 1750 oDlte.close(); 1751 } 1752 else { 1753 CallableStatement oCall = oConn.prepareCall("{ call k_sp_del_mime_msg(?) }"); 1754 for (int m=0; m<iObjs; m++) { 1755 oCall.setString(1, oObjs.getString(0,m)); 1756 oCall.execute(); 1757 } 1758 oCall.close(); 1759 } 1760 } 1762 oObjs = null; 1763 1764 if (DBBind.exists(oConn, DB.k_x_company_prods, "U")) { 1765 1766 oStmt = oConn.createStatement(); 1767 1768 if (DebugFile.trace) 1769 DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_company_prods + " WHERE " + DB.gu_category + "='" + sCategoryGUID + "')"); 1770 1771 oStmt.executeUpdate("DELETE FROM " + DB.k_x_company_prods + " WHERE " + DB.gu_category + "='" + sCategoryGUID + "'"); 1772 1773 oStmt.close(); 1774 } 1776 if (DBBind.exists(oConn, DB.k_x_contact_prods, "U")) { 1777 1778 oStmt = oConn.createStatement(); 1779 1780 if (DebugFile.trace) 1781 DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_contact_prods + " WHERE " + DB.gu_category + "='" + sCategoryGUID + "')"); 1782 1783 oStmt.executeUpdate("DELETE FROM " + DB.k_x_contact_prods + " WHERE " + DB.gu_category + "='" + sCategoryGUID + "'"); 1784 1785 oStmt.close(); 1786 } 1788 LinkedList oChilds = oCat.browse(oConn, BROWSE_DOWN, BROWSE_BOTTOMUP); 1790 ListIterator oIter = oChilds.listIterator(); 1791 1792 while (oIter.hasNext()) { 1793 oCat = (Category) oIter.next(); 1794 oCat.delete(oConn); 1795 } 1797 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 1798 if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_del_category_r ('" + sCategoryGUID + "'))"); 1799 oStmt = oConn.createStatement(); 1800 oStmt.executeQuery("SELECT k_sp_del_category_r ('" + sCategoryGUID + "')"); 1801 oStmt.close(); 1802 bRetVal = true; 1803 } else { 1804 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({call k_sp_del_category_r('" + sCategoryGUID + "')})"); 1805 CallableStatement oCall = oConn.prepareCall("{call k_sp_del_category_r ('" + sCategoryGUID + "')}"); 1806 bRetVal = oCall.execute(); 1807 oCall.close(); 1808 } 1809 1810 if (DebugFile.trace) { 1811 DebugFile.decIdent(); 1812 DebugFile.writeln("End Category.delete() : " + String.valueOf(bRetVal)); 1813 } 1814 return bRetVal; 1815 } 1817 1819 1828 public static String create(JDCConnection oConn, Object [] Values) throws SQLException { 1829 Category oCatg = new Category (); 1830 1831 oCatg.put (DB.gu_owner, Values[1]); 1832 oCatg.put (DB.nm_category, Values[2]); 1833 oCatg.put (DB.bo_active, Values[3]); 1834 oCatg.put (DB.id_doc_status, Values[4]); 1835 oCatg.put (DB.nm_icon, Values[5]); 1836 if (Values[6]!=null) 1837 oCatg.put (DB.nm_icon2, Values[6]); 1838 else 1839 oCatg.put (DB.nm_icon2, Values[5]); 1840 1841 oCatg.store(oConn); 1844 1845 if (null!=Values[0]) 1847 oCatg.setParent(oConn, Values[0].toString()); 1848 else 1849 oCatg.setIsRoot(oConn, true); 1850 1851 return oCatg.getString(DB.gu_category); 1853 } 1855 1857 1866 public static String getIdFromName(JDCConnection oConn, String sCategoryNm) throws SQLException { 1867 if (DebugFile.trace) { 1868 DebugFile.writeln("Begin Category.getIdFromName([Connection], " + sCategoryNm + ")" ); 1869 DebugFile.incIdent(); 1870 } 1871 1872 String sCatId = DBPersist.getUIdFromName(oConn, null, sCategoryNm, "k_sp_get_cat_id"); 1873 1874 if (DebugFile.trace) { 1875 DebugFile.decIdent(); 1876 DebugFile.writeln("End Category.getIdFromName() : " + sCatId); 1877 } 1878 1879 return sCatId; 1880 } 1882 1884 1902 public static synchronized String makeName(JDCConnection oConn, String sCategoryNm) throws SQLException { 1903 String sCatNm; 1904 int iChurriguito; 1905 String sCatIndex; 1906 1907 if (DebugFile.trace) { 1908 DebugFile.writeln("Begin Category.makeName([Connection], " + sCategoryNm + ")" ); 1909 DebugFile.incIdent(); 1910 } 1911 1912 String sShortCategoryNm = (sCategoryNm.length()>18 ? sCategoryNm.substring(0, 18) : sCategoryNm); 1913 sShortCategoryNm = Gadgets.ASCIIEncode(sShortCategoryNm); 1914 sShortCategoryNm.replace(' ', '_'); 1915 sShortCategoryNm.replace(',', '_'); 1916 sShortCategoryNm.replace(';', '_'); 1917 sShortCategoryNm.replace('"', 'q'); 1918 sShortCategoryNm.replace('|', '_'); 1919 1920 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DBBind.Functions.ISNULL + "(MAX(" + DB.nm_category + "),'" + sShortCategoryNm + "~00000000') FROM " + DB.k_categories + " WHERE " + DB.nm_category + " LIKE '" + sShortCategoryNm + "%')"); 1922 1923 PreparedStatement oStmt = oConn.prepareStatement("SELECT " + DBBind.Functions.ISNULL + "(MAX(" + DB.nm_category + "),'" + sShortCategoryNm + "~00000000') FROM " + DB.k_categories + " WHERE " + DB.nm_category + " LIKE ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1924 1925 oStmt.setString(1, sShortCategoryNm + "%"); 1926 ResultSet oRSet = oStmt.executeQuery(); 1927 if (oRSet.next()) { 1928 sCatNm = oRSet.getString(1); 1929 } 1930 else 1931 sCatNm = sShortCategoryNm + "~00000000"; 1932 oRSet.close(); 1933 oStmt.close(); 1934 1935 iChurriguito = sCatNm.indexOf("~"); 1937 if (iChurriguito>0) 1938 sCatIndex = String.valueOf(Integer.parseInt(sCatNm.substring(iChurriguito+1)) + 1); 1939 else 1940 sCatIndex = "00000001"; 1941 1942 for (int z=0; z<8-sCatIndex.length(); z++) sCatIndex = "0" + sCatIndex; 1944 1945 sShortCategoryNm += "~" + sCatIndex; 1946 1947 if (DebugFile.trace) { 1948 DebugFile.decIdent(); 1949 DebugFile.writeln("End Category.makeName() : " + sShortCategoryNm); 1950 } 1951 1952 return sShortCategoryNm; 1953 } 1955 1958 public static final int BROWSE_UP = 0; 1959 public static final int BROWSE_DOWN = 1; 1960 1961 public static final int BROWSE_TOPDOWN = 0; 1962 public static final int BROWSE_BOTTOMUP = 1; 1963 1964 public static final short ClassId = 10; 1965 1966 1969 private DBSubset oParents; 1970 private DBSubset oChilds; 1971 private DBSubset oNames; 1972 private DBSubset oACLGroups; 1973 private DBSubset oACLUsers; 1974 private FileSystem oFS; 1975} 1976 | Popular Tags |