Author: [log in to unmask] Date: Tue Apr 21 16:40:21 2015 New Revision: 2777 Log: Add log and description fields to collections table. Remove option for supplying a collection ID to load command. Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java java/trunk/conditions/src/test/java/org/hps/conditions/database/CollectionIdTest.java Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java ============================================================================= --- java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java (original) +++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java Tue Apr 21 16:40:21 2015 @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; @@ -44,9 +45,9 @@ private static final Options OPTIONS = new Options(); static { OPTIONS.addOption(new Option("h", false, "Show help for load command")); - OPTIONS.addOption(new Option("t", true, "Set the name of the target table in the database")); - OPTIONS.addOption(new Option("c", true, "Set the collection ID of this conditions set")); - OPTIONS.addOption(new Option("f", true, "Set the input data file")); + OPTIONS.addOption(new Option("t", true, "Name of the target table in the database")); + OPTIONS.addOption(new Option("f", true, "Input data file")); + OPTIONS.addOption(new Option("d", true, "Description of collection data")); } /** @@ -64,7 +65,7 @@ @Override public void execute(final String[] arguments) { - final CommandLine commandLine = parse(arguments); + final CommandLine commandLine = this.parse(arguments); final String fileName = commandLine.getOptionValue("f"); if (fileName == null) { @@ -86,30 +87,32 @@ openedConnection = conditionsManager.openConnection(); } - int collectionID; - if (commandLine.getOptionValue("c") != null) { - collectionID = Integer.parseInt(commandLine.getOptionValue("c")); - if (conditionsManager.collectionExists(tableName, collectionID)) { - throw new IllegalArgumentException("The user supplied collection ID " + collectionID - + " already exists in this table."); - } - } else { - collectionID = conditionsManager.getNextCollectionID(tableName); + String collectionDescription = null; + if (commandLine.hasOption("d")) { + collectionDescription = commandLine.getOptionValue("d"); + } + + int collectionId; + try { + collectionId = conditionsManager.addCollection(tableName, + "loaded with command line client by " + System.getProperty("user.name"), collectionDescription); + } catch (final SQLException e) { + throw new RuntimeException("Error getting new collection ID.", e); } final List<String> columnNames = new ArrayList<String>(); final List<List<String>> rows = new ArrayList<List<String>>(); - parseFile(fileName, columnNames, rows); + this.parseFile(fileName, columnNames, rows); - final String insertSql = QueryBuilder.buildInsert(tableName, collectionID, columnNames, rows); - if (getVerbose()) { + final String insertSql = QueryBuilder.buildInsert(tableName, collectionId, columnNames, rows); + if (this.getVerbose()) { LOGGER.info(insertSql); } // FIXME: This call should go through an object API like ConditionsObjectCollection.insert rather than the // manager directly. final List<Integer> ids = conditionsManager.updateQuery(insertSql); LOGGER.info("Inserted " + ids.size() + " new rows into table " + tableName + " with collection_id " - + collectionID); + + collectionId); conditionsManager.closeConnection(openedConnection); } Modified: java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java ============================================================================= --- java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java (original) +++ java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java Tue Apr 21 16:40:21 2015 @@ -285,7 +285,8 @@ * @return the collection's ID * @throws SQLException */ - public synchronized int addCollection(final String tableName, final String comment) throws SQLException { + public synchronized int addCollection(final String tableName, final String log, final String description) + throws SQLException { if (tableName == null) { throw new IllegalArgumentException("The tableName argument is null."); } @@ -295,16 +296,21 @@ int collectionId = -1; try { statement = this.connection.prepareStatement( - "INSERT INTO collections (table_name, comment, created) VALUES (?, ?, ?)", + "INSERT INTO collections (table_name, log, description, created) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); statement.setString(1, tableName); - if (comment == null) { + if (log == null) { statement.setNull(2, java.sql.Types.VARCHAR); } else { - statement.setString(2, comment); - } - statement.setDate(3, new java.sql.Date(Calendar.getInstance().getTime().getTime())); - final boolean result = statement.execute(); + statement.setString(2, log); + } + if (description == null) { + statement.setNull(3, java.sql.Types.VARCHAR); + } else { + statement.setString(3, description); + } + statement.setDate(4, new java.sql.Date(Calendar.getInstance().getTime().getTime())); + statement.execute(); resultSet = statement.getGeneratedKeys(); resultSet.next(); collectionId = resultSet.getInt(1); Modified: java/trunk/conditions/src/test/java/org/hps/conditions/database/CollectionIdTest.java ============================================================================= --- java/trunk/conditions/src/test/java/org/hps/conditions/database/CollectionIdTest.java (original) +++ java/trunk/conditions/src/test/java/org/hps/conditions/database/CollectionIdTest.java Tue Apr 21 16:40:21 2015 @@ -43,10 +43,10 @@ * @throws SQLException if there is an error executing SQL queries */ public void testCollectionId() throws SQLException { - int collectionId = this.manager.addCollection("dummy", "foo bar baz"); + int collectionId = this.manager.addCollection("dummy", "test add", "foo bar baz"); System.out.println("created new collection " + collectionId); - collectionId = this.manager.addCollection("dummy", null); + collectionId = this.manager.addCollection("dummy", null, null); System.out.println("created new collection " + collectionId); } }