/* copyright Spry Software, Inc. 2006-2007 */ package spry.ui; import spry.model.*; import spry.output.*; import java.io.*; import java.sql.Connection; import java.util.*; public class DataFileLoader { String odbcName; String tableFileName; List inputFileNames; boolean quiet; boolean clearTables; public DataFileLoader() { odbcName = ""; quiet = false; clearTables = false; } /** * @return Returns the quiet. */ public boolean isQuiet() { return quiet; } /** * @param quiet The quiet to set. */ public void setQuiet(boolean quiet) { this.quiet = quiet; } /** * @return Returns the clearTables. */ public boolean isClearTables() { return clearTables; } /** * @param clearTables The clearTables to set. */ public void setClearTables(boolean clearTables) { this.clearTables = clearTables; } /** * @return Returns the odbcName. */ public String getOdbcName() { return odbcName; } /** * @param odbcName The odbcName to set. */ public void setOdbcName(String odbcName) { this.odbcName = odbcName; } /** * @return Returns the inputFileName. */ public List getInputFileNames() { return inputFileNames; } /** * @param intputFileName The intputFileName to set. */ public void setInputFileNames(List inputFileNames) { this.inputFileNames = inputFileNames; } /** * @return Returns the outputFileName. */ public String getTableFileName() { return tableFileName; } /** * @param outputFileName The outputFileName to set. */ public void setTableFileName(String value) { this.tableFileName = value; } private void loadTables(Connection conn, DataStore dataStore, Properties tableMap) { spry.output.DatabaseOutput output = new spry.output.DatabaseOutput(conn, tableMap); output.setClearTables(clearTables); Iterator iter = tableMap.keySet().iterator(); while (iter.hasNext()) { String modelDescr = (String)iter.next(); Class modelClass = (Class) spry.ui.util.FactoryManager.getManager().getMember("Tables", modelDescr); if (modelClass == null) System.out.println("Could not find class for "+modelDescr); else { try { DataStoreBasedTableModel model = (DataStoreBasedTableModel)modelClass.newInstance(); model.setDataStore(dataStore); output.setTableForModel(model, tableMap.getProperty(modelDescr)); if (!quiet) System.out.println("Loading " + modelDescr + "..."); output.outputTabularData(model); } catch (Exception e) { e.printStackTrace(); } } } } /** @returns 2 for input file does not exist * * */ public int runLoad() { java.sql.Connection conn = DatabaseConnectionManager.getInstance().getODBCConnection(odbcName); Properties tableMap = new Properties(); if (tableFileName != null) { java.io.File tableFile = new File(tableFileName); if (tableFile.exists()) { try { java.io.InputStream input = new java.io.FileInputStream(tableFile); tableMap.load(input); } catch (Exception e) { e.printStackTrace(); } } } Iterator nameIterator = inputFileNames.iterator(); while (nameIterator.hasNext()) { String inputFileName = (String)nameIterator.next(); File dataFile = new File(inputFileName); if (!dataFile.exists()) System.err.println("Non-existent input file "+inputFileName); else { try { if (!quiet) System.out.println("Reading " + inputFileName + "..."); DataStore dataStore = DataStoreFactory.getInstance().loadDataFile(dataFile, null); loadTables(conn, dataStore, tableMap); //totalStore.addDataStore(dataStore); } catch (Exception e) { e.printStackTrace(); } } } if (conn != null) { DatabaseConnectionManager.getInstance().closeODBCConnection("avago"); } return 0; } private static void printUsage() { System.out.println( "Usage: java spry.ui.DataFileLoader [switches] datafile1 datafile2 datafile3 ..."); System.out.println(" the switches currently available are "); System.out.println("--tablefile (table mapping model names to db tables) "); System.out.println("--odbcname "); System.out.println("--quiet (minimizes output)"); System.out.println("--clear (remove existing data from tables being loaded)"); } static { DataStoreFactory.getInstance(); new TableView(); //force class to load } public static void main(String[] args) { if (args.length == 0 || args[0].equals("--help")) { printUsage(); System.exit(1); } int argsIndex = 0; String tableFile = null; LinkedList inputFileNames = new LinkedList(); String odbcName = null; boolean quiet = false; boolean clear = false; while (argsIndex < args.length) { if (args[argsIndex].equals("--tablefile")) { argsIndex++; tableFile = args[argsIndex]; } else if (args[argsIndex].equals("--odbcname")) { argsIndex++; if (argsIndex < args.length) odbcName = args[argsIndex]; } else if (args[argsIndex].equals("--quiet")) quiet = true; else if (args[argsIndex].equals("--clear")) clear = true; else { inputFileNames.add(args[argsIndex]); } argsIndex++; } if (inputFileNames.size() == 0) { System.err.println("Error: No input file name provided"); System.exit(2); } else if (tableFile == null) { System.err.println("Error: No table file provided"); System.exit(3); } DataFileLoader loader = new DataFileLoader(); loader.setTableFileName(tableFile); loader.setInputFileNames(inputFileNames); loader.setQuiet(quiet); loader.setClearTables(clear); if (odbcName != null) loader.setOdbcName(odbcName); else System.out.println("You haven't set an odbc connection, this probably won't work"); int loadReturn = loader.runLoad(); System.exit(loadReturn); } }