LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  March 2015

HPS-SVN March 2015

Subject:

r2362 - /java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Sun, 8 Mar 2015 01:07:13 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (170 lines)

Author: [log in to unmask]
Date: Sat Mar  7 17:07:08 2015
New Revision: 2362

Log:
Improvements to add command argument parsing and handling.

Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/AddCommand.java	Sat Mar  7 17:07:08 2015
@@ -6,6 +6,7 @@
 import org.hps.conditions.api.ConditionsObjectException;
 import org.hps.conditions.api.ConditionsRecord;
 import org.hps.conditions.api.FieldValueMap;
+import org.hps.conditions.database.DatabaseConditionsManager;
 
 /**
  * This is a command for the conditions CLI that will add a conditions record,
@@ -19,52 +20,114 @@
     PrintStream ps = System.out;
     
     AddCommand() {
-        super("add", "Add a conditions record");
-        options.addOption("r", true, "The starting run number");
+        super("add", "Add a conditions record to associate a collection to a run range");
+        options.addOption("r", true, "The starting run number (required)");
         options.getOption("r").setRequired(true);
-        options.addOption("e", true, "The ending run number");
-        options.addOption("k", true, "The conditions key");
-        options.addOption("t", true, "The table name");
+        options.addOption("e", true, "The ending run number (default is starting run number)");
+        options.addOption("t", true, "The table name (required)");
         options.getOption("t").setRequired(true);
-        options.addOption("T", true, "A tag value");
-        options.addOption("u", true, "Your user name");
-        options.addOption("c", true, "The collection ID");
+        options.addOption("c", true, "The collection ID (required)");
         options.getOption("c").setRequired(true);
-        options.addOption("m", true, "The notes");
+        options.addOption("T", true, "A tag value (optional)");
+        options.addOption("u", true, "Your user name (optional)");
+        options.addOption("m", true, "The notes about this conditions set (optional)");
     }
        
     void execute(String[] arguments) {
         super.execute(arguments);
-                
-        int runStart = Integer.parseInt(commandLine.getOptionValue("r"));
+        
+        // This command has 3 required options.
+        if (commandLine.getOptions().length == 0) {
+            this.printUsage();
+            System.exit(1);
+        }
+
+        // Run start (required).
+        int runStart;        
+        if (commandLine.hasOption("r")) {
+            runStart = Integer.parseInt(commandLine.getOptionValue("r"));
+        } else {
+            throw new RuntimeException("Missing required -r option with run number.");
+        }
+        
+        // Run end.
         int runEnd = runStart;
         if (commandLine.hasOption("e")) {
             runEnd = Integer.parseInt(commandLine.getOptionValue("e"));
         }
-        
-        String tableName = commandLine.getOptionValue("t");
+
+        // Name of table (required).
+        String tableName;
+        if (commandLine.hasOption("t")) {
+            tableName = commandLine.getOptionValue("t");
+        } else {
+            throw new RuntimeException("Missing required -t argument with table name");
+        }        
         String name = tableName;
-        if (commandLine.hasOption("k")) {
-            name = commandLine.getOptionValue("k");
+
+        // Collection ID (required).
+        int collectionId;
+        if (commandLine.hasOption("c")) {
+            collectionId = Integer.parseInt(commandLine.getOptionValue("c"));
+        } else {
+            throw new RuntimeException("Missing required -c argument with collection ID");
         }
         
+        // User name.
         String createdBy = System.getProperty("user.name");
         if (commandLine.hasOption("u")) {
             createdBy = commandLine.getOptionValue("u");
         }
         
+        // Tag to assign (optional).
         String tag = null;
         if (commandLine.hasOption("T")) {
             tag = commandLine.getOptionValue("T");
         }
         
+        // Notes (optional).
         String notes = null;
         if (commandLine.hasOption("m")) {
             notes = commandLine.getOptionValue("m");
         }
         
-        int collectionId = Integer.parseInt(commandLine.getOptionValue("c"));
+        // Create the conditions record to insert.
+        ConditionsRecord conditionsRecord = createConditionsRecord(
+                runStart, 
+                runEnd, 
+                tableName, 
+                name, 
+                collectionId, 
+                createdBy, 
+                tag, 
+                notes);       
+        try {
+            boolean createdConnection = false;
+            DatabaseConditionsManager manager = DatabaseConditionsManager.getInstance();
+            if (!DatabaseConditionsManager.getInstance().isConnected()) {
+                createdConnection = manager.openConnection();
+            }
+            conditionsRecord.insert();
+            manager.closeConnection(createdConnection);
+        } catch (ConditionsObjectException e) {
+            throw new RuntimeException("An error occurred while adding a conditions record.", e);
+        }
+        ps.println("successfully added conditions record ...");
+        ps.println(conditionsRecord);
+    }
 
+    /**
+     * @param runStart
+     * @param runEnd
+     * @param tableName
+     * @param name
+     * @param collectionId
+     * @param createdBy
+     * @param tag
+     * @param notes
+     * @return
+     */
+    private ConditionsRecord createConditionsRecord(int runStart, int runEnd, String tableName, String name, int collectionId, String createdBy, String tag, String notes) {
         ConditionsRecord conditionsRecord = new ConditionsRecord();
         FieldValueMap fieldValues = new FieldValueMap();
         fieldValues.put("run_start", runStart);
@@ -81,13 +144,6 @@
         }
         conditionsRecord.setFieldValues(fieldValues);
         fieldValues.put("created", new Date());
-        try {
-            conditionsRecord.insert();
-        } catch (ConditionsObjectException e) {
-            throw new RuntimeException("An error occurred while adding a conditions record.", e);
-        }
-        ps.println("successfully added conditions record ...");
-        ps.println(conditionsRecord);
+        return conditionsRecord;
     }
-
-}
+}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use