Archive for January, 2009|Monthly archive page

JDBC and Oracle dblinks

I came across a small problem when trying to access a dblink in my SQL using java.sql.PreparedStatement .. I tried using java.sql.Statement instead to build my query and it worked fine.

Ruby : automated re-naming script

I recently had a chance to work on a project which required massive amounts of file and folder restructring. I could’ve done this manually to minimize errors but I chose not to due to time constraints and the fact that most of the errors would be caught during UAT.

The script takes a parameter from the command line which sets the language. Depending on the language the script initializes the proper old -> new mappings for files and folders.  It then replaces any references to the old file with new file name.

require “ftools”

class FileRename

# Define old – > new mappings for english
@@hashEn = Hash.new
@@hashEn["old-name.jsp"] = “new-name.jsp”

# Define old – > new mappings for french
@@hashFr = Hash.new
@@hashFr["old-name.jsp"] = “new-name.jsp”

@@changes = 0

# Initialize hash of old – new file names for jsp’s
def initialize(lang)
if lang.eql? “en”
@oldPrefix = “ie-en”
@newPrefix = “eng”
@oldNewHash = @@hashEn
elsif lang.eql? “fr”
@oldPrefix = “ie-fr”
@newPrefix = “fra”
@oldNewHash = @@hashFr
end
end

# Copy contents of old files to new files
def renameFiles
@logFile = File.new(“file_renaming.log”, “w”)
@oldNewHash.each do |key, value|
oldFile = File.new(@oldPrefix + “/” + key, “r”)        # Open old file in READ-ONLY mode
newFile = File.new(@newPrefix + “/” + value, “w”)    # Open new file in WRITE mode
oldFile.each_line do |line|
newFile.puts line            # Copy each line in old file to new file
end
newFile.close                    # Close new file
oldFile.close                    # Close old file
@logFile.puts “Rename ” + key + ” to ” + value
File.delete(oldFile.path)        # Delete old file
end
@logFile.close
end

# Replace references to old file names with new file names in the newly created jsp’s
def replaceOldFileNames(line)
newLine = String.new
newLine.replace(line)            # create a new copy of the line passed
@oldNewHash.each do |key, value|
newLine.gsub!(@oldPrefix + “/” + key, @newPrefix + “/” + value)        # replace absolute references to old jsp names such as /ie-en/old.jsp with /eng/new.jsp
newLine.gsub!(“/” + key, “/” + value)    # replace <a href=”../old.jsp” with <a href=”../new.jsp”
newLine.gsub!(“\”" + key, “\”" + value)    #replace <a href=”old.jsp” with <a href=”new.jsp”
newLine.gsub!(key, value) # replace old.jsp with new.jsp, ONLY needed for .properties files, comment out otherwise
end
newLine            # return the new string
end

# Replace links to old jsp’s in files in current directory and its sub-folders
def replaceOldLinks(ext)
@logFile = File.new(“file_renaming.log”, “a”)
validFiles = File.join(“**”, ext)        # File filter to recursilve extract all files for the given extension
Dir.glob(validFiles) do |path|            # Scan every file
puts “Fixing paths in ” + path.to_s
@logFile.puts “Fixing paths in ” + path.to_s
aFile = File.new(path, “r”)        # open the file in read-only
lines = aFile.readlines            # Read all lines from file
aFile.close

aFile = File.new(path, “w”)        # open the file in write now and scan each line for old links and write it back
lineNumber = 1
lines.each do |line|
newLine = replaceOldFileNames(line)
unless newLine.eql? line
@logFile.puts “Changed line number ” + lineNumber.to_s + ” in ” + path.to_s
@@changes += 1
end
aFile.puts newLine
lineNumber += 1
end
aFile.close
puts “Done fixing paths in ” + path.to_s
@logFile.puts “Done fixing paths in ” + path.to_s
end
@logFile.puts “Total changes made : ” + @@changes.to_s
@logFile.close
end
end

langCode = ARGV[0] == nil ? “en” : ARGV[0]
fr = FileRename.new(langCode)
fr.renameFiles
fr.replaceOldLinks(“*.html”)
fr.replaceOldLinks(“*.htm”)
fr.replaceOldLinks(“*.jspf”)
fr.replaceOldLinks(“*.jsp”)
fr.replaceOldLinks(“*.properties”)