package journal.action; import java.io.FileReader; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; import jargs.gnu.CmdLineParser; import journal.reader.ActionType; import journal.reader.DataJournalEntry; import journal.reader.JournalEntry; import journal.reader.Options; import journal.schema.TableVersion; public class Renamer extends BaseAction { boolean patch; boolean renameUser; boolean renameClient; String fileName = ""; Map<String, String> map = new HashMap<String, String>(); private static class Counter { int clients = 0; int users = 0; void incrementClient() { clients++; } void incrementUser() { users++; } public String toString() { return "clients = " + clients + " users = " + users; } } Map<String, Counter> counter = new HashMap<String, Counter>(); private static class Columns { boolean hasClient = false; boolean hasUser = false; boolean hasClientFile = false; } Map<TableVersion, Columns> hasColumns = new HashMap<TableVersion, Renamer.Columns>(); @Override public void start(Options options) throws Exception { super.start(options); // need to rethink the whole option vs command line arguments issue. } @Override public void help() { System.err.println("\t--action journal.action.Renamer -- <options>"); System.err.println("\t\tOptions are\n"); System.err.println("\t\t-c --client"); System.err.println("\t\t-u --user"); System.err.println("\t\t-o --oldName[=]<old-name> -n --newName[=]<new-name>"); System.err.println("\t\t-f --fileName[=]<file-name> # format: oldname=newname"); System.err.println("\t\t-p --patch"); } @Override public void finish() throws Exception { System.err.println("\nRenamed the following : "); for (Map.Entry<String, Counter> entry : counter.entrySet()) { System.err.println(entry.getKey() + " -> " + map.get(entry.getKey()) + " : " + entry.getValue()); } } @Override public String[] parseArgs(String[] args) { CmdLineParser parser = new CmdLineParser(); CmdLineParser.Option oldNameOption = parser.addStringOption('o', "oldName"); CmdLineParser.Option newNameOption = parser.addStringOption('n', "newName"); CmdLineParser.Option fileNameOption = parser.addStringOption('f', "fileName"); CmdLineParser.Option patchOption = parser.addBooleanOption('p', "patch"); CmdLineParser.Option userOption = parser.addBooleanOption('u', "user"); CmdLineParser.Option clientOption = parser.addBooleanOption('c', "client"); try { parser.parse(args); } catch ( CmdLineParser.OptionException e ) { System.err.println(e.getMessage()); help(); System.exit(2); } String oldName = (String) parser.getOptionValue(oldNameOption, ""); String newName = (String) parser.getOptionValue(newNameOption, ""); fileName = (String) parser.getOptionValue(fileNameOption, fileName); this.patch = (Boolean) parser.getOptionValue(patchOption, Boolean.FALSE); this.renameUser = (Boolean) parser.getOptionValue(userOption, Boolean.FALSE); this.renameClient = (Boolean) parser.getOptionValue(clientOption, Boolean.FALSE); // verify that the correct options match up if( !renameUser && !renameClient ) { throw new IllegalArgumentException("Need to specify at least one of [--user|--client]"); } if( fileName.equals("") ) { if( oldName.equals("") || newName.equals("")) { throw new IllegalArgumentException("Need to specify either filename or old and new name"); } map.put(oldName, newName); } else { try { readFile(fileName); } catch(IOException e) { throw new IllegalArgumentException("Illegal filename", e); } } for (String name : map.keySet()) { counter.put(name, new Counter()); } return parser.getRemainingArgs(); } private void readFile(final String fileName) throws IOException { Properties p = new Properties(); FileReader r = new FileReader(fileName); p.load(r); for (Enumeration<?> propNames = p.propertyNames(); propNames.hasMoreElements(); ) { String name = (String) propNames.nextElement(); map.put(name, p.getProperty(name)); } } private Columns getTableColumns(TableVersion tableVersion) { if( hasColumns.containsKey(tableVersion)) { return hasColumns.get(tableVersion); } else { Columns c = new Columns(); hasColumns.put(tableVersion, c); c.hasClient = tableVersion.getAttributeByName("client") != null; c.hasUser = tableVersion.getAttributeByName("user") != null; c.hasClientFile = tableVersion.getAttributeByName("cfile") != null; return c; } } @Override public void putValue(JournalEntry entry) throws Exception { DataJournalEntry data = (DataJournalEntry) entry; boolean renamed = false; boolean deleted = false; TableVersion version = data.getTableVersion(); Columns c = getTableColumns(version); if( renameClient && c.hasClient ) { String client = (String) data.getValue("client"); if( map.containsKey(client) ) { if( patch ) { out.println(data.toJournalString(ActionType.DELETE_VALUE)); deleted = true; } renamed = true; counter.get(client).incrementClient(); client = map.get(client); data.setValue("client", client); } } if( renameClient && c.hasClientFile ) { String clientFile = (String) data.getValue("cfile"); String client = clientFile.substring(2, clientFile.indexOf('/', 2)); if( map.containsKey(client) ) { if( patch && !deleted ) { out.println(data.toJournalString(ActionType.DELETE_VALUE)); deleted = true; } renamed = true; counter.get(client).incrementClient(); String newClient = map.get(client); String newClientFile = clientFile.replaceFirst("//"+client+"/", "//" + newClient + "/"); data.setValue("cfile", newClientFile); } } if( renameUser && c.hasUser ) { String user = (String) data.getValue("user"); if( map.containsKey(user) ) { if( patch && !deleted) { out.println(data.toJournalString(ActionType.DELETE_VALUE)); } renamed = true; counter.get(user).incrementUser(); user = map.get(user); data.setValue("user", user); } } if( renamed || !patch ) { out.println(data.toJournalString()); } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 8012 | Sven Erik Knop | Rename Renamer to ClientUserRenamer for clarification. | ||
#3 | 7823 | Sven Erik Knop |
Updated the Renamer tool to solve the problem that owners of specs such as labels and client workspaces wouldn't get updated. Jar file with updated version submitted as well. |
||
#2 | 7626 | Sven Erik Knop |
Removed obsolete imports. Added jar file manifest to build.xml |
||
#1 | 7527 | Sven Erik Knop |
JournalReader, now in its proper place. Documentation to follow. |