1 64 65 70 package com.jcorporate.expresso.core.utility; 71 72 import com.jcorporate.expresso.core.db.DBConnection; 73 import com.jcorporate.expresso.core.db.DBConnectionPool; 74 import com.jcorporate.expresso.core.db.DBException; 75 import com.jcorporate.expresso.core.misc.ConfigManager; 76 import com.jcorporate.expresso.core.misc.ConfigurationException; 77 import com.jcorporate.expresso.core.misc.FileUtil; 78 import com.jcorporate.expresso.core.misc.StringUtil; 79 80 import java.io.BufferedReader ; 81 import java.io.File ; 82 import java.io.FileReader ; 83 import java.io.FileWriter ; 84 import java.io.IOException ; 85 import java.io.InputStreamReader ; 86 import java.io.PrintWriter ; 87 import java.util.Enumeration ; 88 import java.util.Hashtable ; 89 import java.util.StringTokenizer ; 90 import java.util.Vector ; 91 92 93 100 public class ReNameUtil { 101 private static String thisClass = ("com.jcorporate.expresso.core.utilty." + 102 "ReNameUtil."); 103 104 107 108 private static String suffix = ".tmp"; 109 private static Hashtable translateTable = new Hashtable (3); 110 private static int totalEdits = 0; 111 112 115 public ReNameUtil() { 116 super(); 117 } 118 119 122 public static void processDirs(String fileName) 123 throws IOException { 124 System.out.println("Processing directory " + fileName); 125 126 if (!fileName.endsWith("/")) { 127 fileName = fileName + "/"; 128 } 129 130 Vector contents = getDir(fileName); 131 String oneItem = null; 132 File oneFile = null; 133 134 for (Enumeration e = contents.elements(); e.hasMoreElements();) { 135 oneItem = (String ) e.nextElement(); 136 oneFile = new File (fileName + oneItem); 137 138 139 if (oneFile.isDirectory()) { 140 System.out.println("Processing subdirectory " + fileName + 141 oneItem); 142 143 144 processDirs(fileName + oneItem); 145 146 if (getDir(fileName + oneItem).size() == 0) { 147 oneFile = new File (fileName + oneItem); 148 System.out.println("Empty directory " + oneItem); 149 } 150 151 } else { 152 processFile(fileName + oneItem); 153 } 154 } 155 156 } 157 158 159 165 public static void processFile(String sourceFile) 166 throws IOException { 167 String myName = (thisClass + "processFile(String)"); 168 169 if (sourceFile.equals("")) { 170 throw new IOException (myName + ":File name empty - cannot" + 171 " process."); 172 } 173 if (sourceFile.endsWith(suffix)) { 174 return; 175 } 176 177 String destFile = sourceFile + suffix; 178 179 if (sourceFile.equals(destFile)) { 180 throw new IOException (myName + ":Cannot copy file '" + sourceFile + 181 "' to itself"); 182 } 183 184 File dest = new File (destFile); 185 186 if (dest.exists()) { 187 if (!dest.delete()) { 188 throw new IOException (myName + ":Unable to delete existing " + 189 "destination file '" + destFile + 190 "'. Logged in as " + 191 System.getProperty("user.name")); 192 } 193 } 194 195 File source = new File (sourceFile); 196 197 if (!source.exists()) { 198 throw new IOException (myName + ":Source file '" + sourceFile + 199 "' does not exist. Cannot process. Logged in as " + 200 System.getProperty("user.name")); 201 } 202 203 FileWriter fout = new FileWriter (destFile); 204 PrintWriter bout = new PrintWriter (fout); 205 FileReader fin = new FileReader (sourceFile); 206 BufferedReader bin = new BufferedReader (fin); 207 String oneFrom = null; 208 String oneTo = null; 209 String oneLine = bin.readLine(); 210 int replacements = 0; 211 212 while (oneLine != null) { 213 String newLine = oneLine; 214 215 for (Enumeration e = translateTable.keys(); e.hasMoreElements();) { 216 oneFrom = (String ) e.nextElement(); 217 218 if (oneLine.indexOf(oneFrom) >= 0) { 219 oneTo = (String ) translateTable.get(oneFrom); 220 newLine = StringUtil.replace(newLine, oneFrom, oneTo); 221 replacements++; 222 totalEdits++; 223 } 224 } 225 226 bout.println(newLine); 227 oneLine = bin.readLine(); 228 } 229 230 bout.flush(); 231 bin.close(); 232 fin.close(); 233 System.err.println("Made " + replacements + " in file " + sourceFile); 234 235 if (!dest.exists()) { 236 throw new IOException (myName + 237 ":File processing failed: destination file" + 238 " '" + destFile + 239 "' does not exist after processing."); 240 } 241 242 243 FileUtil.moveFile(destFile, sourceFile); 244 } 245 246 247 254 public static Vector getDir(String dirName) 255 throws IOException { 256 String myName = (thisClass + "getDir(String)"); 257 File dirFile = new File (dirName); 258 259 if (!dirFile.isDirectory()) { 260 throw new IOException (myName + ":'" + dirName + 261 "' is not a directory."); 262 } 263 264 String [] dir = dirFile.list(); 265 266 if (dir == null) { 267 throw new IOException (myName + ":Null array reading directory " + 268 " of " + dirName); 269 } 270 271 Vector fileList = new Vector (1); 272 String oneFileName = null; 273 274 for (int i = 0; i < dir.length; i++) { 275 oneFileName = dir[i].trim(); 276 fileList.addElement(oneFileName); 277 } 278 279 return fileList; 280 } 281 282 283 290 public static String getExtension(String fileName) 291 throws IOException { 292 String tempName = new File (fileName).getName(); 293 StringTokenizer stk = new StringTokenizer (tempName, "."); 294 stk.nextToken(); 295 296 if (stk.hasMoreTokens()) { 297 return stk.nextToken(); 298 } else { 299 return (""); 300 } 301 } 302 303 304 310 public static String getPath(String fileName) { 311 StringBuffer path = new StringBuffer ("/"); 312 String nextToken; 313 StringTokenizer stk = new StringTokenizer (fileName, "/"); 314 315 while (stk.hasMoreTokens()) { 316 nextToken = stk.nextToken(); 317 318 if (stk.hasMoreTokens()) { 319 path.append(nextToken); 320 path.append("/"); 321 } 322 } 323 324 return path.toString(); 325 } 326 327 330 private static void readTransFile(String transFileName) 331 throws IOException { 332 FileReader fin = new FileReader (transFileName); 333 BufferedReader bin = new BufferedReader (fin); 334 String oneFrom = null; 335 String oneTo = null; 336 int currLine = 0; 337 String oneLine = bin.readLine(); 338 339 while (oneLine != null) { 340 currLine++; 341 342 StringTokenizer stk = new StringTokenizer (oneLine, "|"); 343 344 if (!stk.hasMoreTokens()) { 345 throw new IllegalArgumentException ("Translation file must have " + 346 "from|to pairs on each line - from 'from' found on line " + 347 currLine); 348 } 349 350 oneFrom = stk.nextToken(); 351 352 if (!stk.hasMoreTokens()) { 353 throw new IllegalArgumentException ("Translation file must have " + 354 "from|to pairs on each line - no 'to' found on line " + 355 currLine); 356 } 357 358 oneTo = stk.nextToken(); 359 translateTable.put(oneFrom, oneTo); 360 oneLine = bin.readLine(); 361 } 362 363 System.out.println("Read " + currLine + " translation pairs"); 364 } 365 366 367 371 public static void processDB(String configDir, String db) 372 throws IOException , DBException, 373 ConfigurationException { 374 375 ConfigManager.dbInitialize(); 377 378 DBConnectionPool dbp = DBConnectionPool.getInstance(db); 379 DBConnection myConnection = dbp.getConnection("ReNameUtil"); 380 String oneControllerClass = null; 381 String oneGroup = null; 382 String newControllerClass = null; 383 myConnection.execute("SELECT GroupName, ControllerClass FROM CONTROLLERSECURITY"); 384 385 Vector updates = new Vector (); 386 387 while (myConnection.next()) { 388 oneGroup = StringUtil.notNull(myConnection.getString(1)); 389 oneControllerClass = StringUtil.notNull(myConnection.getString(2)); 390 391 String oneFrom = null; 392 String oneTo = null; 393 394 for (Enumeration e = translateTable.keys(); e.hasMoreElements();) { 395 oneFrom = (String ) e.nextElement(); 396 397 if (oneControllerClass.indexOf(oneFrom) >= 0) { 398 oneTo = (String ) translateTable.get(oneFrom); 399 newControllerClass = StringUtil.replace(oneControllerClass, 400 oneFrom, oneTo); 401 totalEdits++; 402 updates.addElement(oneGroup + "|" + oneControllerClass + 403 "|" + newControllerClass); 404 } 405 } 406 } 407 408 409 String oneUpdate = null; 410 411 for (Enumeration ue = updates.elements(); ue.hasMoreElements();) { 412 oneUpdate = (String ) ue.nextElement(); 413 414 StringTokenizer stk = new StringTokenizer (oneUpdate, "|"); 415 oneGroup = stk.nextToken(); 416 oneControllerClass = stk.nextToken(); 417 newControllerClass = stk.nextToken(); 418 myConnection.executeUpdate("UPDATE CONTROLLERSECURITY SET ControllerClass = '" + 419 newControllerClass + "' where ControllerClass = '" + 420 oneControllerClass + "' AND GroupName = '" + oneGroup + 421 "'"); 422 } 423 424 425 updates = new Vector (); 426 427 String oneDBObjectName = null; 428 String oneMethodCode = null; 429 String newDBObjectName = null; 430 myConnection.execute("SELECT GroupName, DBObjectName, MethodCode FROM DBOBJSECURITY"); 431 432 while (myConnection.next()) { 433 oneGroup = StringUtil.notNull(myConnection.getString(1)); 434 oneDBObjectName = StringUtil.notNull(myConnection.getString(2)); 435 oneMethodCode = StringUtil.notNull(myConnection.getString(3)); 436 437 String oneFrom = null; 438 String oneTo = null; 439 440 for (Enumeration e = translateTable.keys(); e.hasMoreElements();) { 441 oneFrom = (String ) e.nextElement(); 442 443 if (oneDBObjectName.indexOf(oneFrom) >= 0) { 444 oneTo = (String ) translateTable.get(oneFrom); 445 newDBObjectName = StringUtil.replace(oneDBObjectName, 446 oneFrom, oneTo); 447 totalEdits++; 448 updates.addElement(oneGroup + "|" + oneDBObjectName + "|" + 449 oneMethodCode + "|" + newDBObjectName); 450 } 451 } 452 } 453 454 for (Enumeration ue2 = updates.elements(); ue2.hasMoreElements();) { 455 oneUpdate = (String ) ue2.nextElement(); 456 457 StringTokenizer stk = new StringTokenizer (oneUpdate, "|"); 458 oneGroup = stk.nextToken(); 459 oneDBObjectName = stk.nextToken(); 460 oneMethodCode = stk.nextToken(); 461 newDBObjectName = stk.nextToken(); 462 myConnection.executeUpdate("UPDATE DBOBJSECURITY SET DBObjectName = '" + 463 newDBObjectName + "' where DBObjectName = '" + 464 oneDBObjectName + "' AND GroupName = '" + oneGroup + 465 "' AND MethodCode = '" + oneMethodCode + "'"); 466 } 467 468 469 updates = new Vector (); 470 oneDBObjectName = null; 471 myConnection.execute("SELECT DBObjectName FROM DBOBJLIMIT"); 472 473 while (myConnection.next()) { 474 oneDBObjectName = StringUtil.notNull(myConnection.getString(1)); 475 476 String oneFrom = null; 477 String oneTo = null; 478 479 for (Enumeration e = translateTable.keys(); e.hasMoreElements();) { 480 oneFrom = (String ) e.nextElement(); 481 482 if (oneDBObjectName.indexOf(oneFrom) >= 0) { 483 oneTo = (String ) translateTable.get(oneFrom); 484 newDBObjectName = StringUtil.replace(oneDBObjectName, 485 oneFrom, oneTo); 486 totalEdits++; 487 updates.addElement(oneDBObjectName + "|" + 488 newDBObjectName); 489 } 490 } 491 } 492 493 for (Enumeration ue3 = updates.elements(); ue3.hasMoreElements();) { 494 oneUpdate = (String ) ue3.nextElement(); 495 496 StringTokenizer stk = new StringTokenizer (oneUpdate, "|"); 497 oneDBObjectName = stk.nextToken(); 498 newDBObjectName = stk.nextToken(); 499 myConnection.executeUpdate("UPDATE DBOBJLIMIT SET DBObjectName = '" + 500 newDBObjectName + "' where DBObjectName = '" + 501 oneDBObjectName + "'"); 502 } 503 504 505 updates = new Vector (); 506 507 String oneSchemaClass = null; 508 String newSchemaClass = null; 509 myConnection.execute("SELECT SchemaClass FROM SETUP"); 510 511 while (myConnection.next()) { 512 oneSchemaClass = StringUtil.notNull(myConnection.getString(1)); 513 514 String oneFrom = null; 515 String oneTo = null; 516 517 for (Enumeration e = translateTable.keys(); e.hasMoreElements();) { 518 oneFrom = (String ) e.nextElement(); 519 520 if (oneSchemaClass.indexOf(oneFrom) >= 0) { 521 oneTo = (String ) translateTable.get(oneFrom); 522 newSchemaClass = StringUtil.replace(oneSchemaClass, 523 oneFrom, oneTo); 524 totalEdits++; 525 updates.addElement(oneSchemaClass + "|" + newSchemaClass); 526 } 527 } 528 } 529 530 for (Enumeration ue3 = updates.elements(); ue3.hasMoreElements();) { 531 oneUpdate = (String ) ue3.nextElement(); 532 533 StringTokenizer stk = new StringTokenizer (oneUpdate, "|"); 534 oneSchemaClass = stk.nextToken(); 535 newSchemaClass = stk.nextToken(); 536 myConnection.executeUpdate("UPDATE SETUP SET SchemaClass = '" + 537 newSchemaClass + 538 "' where SchemaClass = '" + 539 oneSchemaClass + "'"); 540 } 541 } 542 543 544 547 public static void main(String [] args) { 548 System.out.println("ReNameUtil"); 549 550 BufferedReader ds = new BufferedReader (new InputStreamReader (System.in)); 551 552 try { 553 System.out.print("Enter name of translation file:"); 554 555 String transFile = ds.readLine(); 556 System.out.print("Enter config dir:"); 557 558 String configDir = ds.readLine(); 559 System.out.print("Update (d)b or (f)iles?"); 560 561 String whichUpdate = ds.readLine(); 562 563 if (whichUpdate.equalsIgnoreCase("f")) { 564 System.out.print("Enter directory to process:"); 565 566 String dirName = ds.readLine(); 567 ConfigManager.load(configDir); 568 readTransFile(transFile); 569 processDirs(dirName); 570 } else { 571 System.out.print("Enter context/db name:"); 572 573 String db = ds.readLine(); 574 ConfigManager.load(configDir); 575 readTransFile(transFile); 576 processDB(configDir, db); 577 } 578 579 System.out.println("\nAll edits complete. Made " + totalEdits + 580 " changes in total"); 581 } catch (Exception e) { 582 e.printStackTrace(); 583 } 584 } 585 586 } 587 | Popular Tags |