Commit in java/trunk/conditions/src on MAIN
main/java/org/hps/conditions/ConditionsSeries.java+46added 604
                            /ConditionsSeriesConverter.java+100added 604
test/java/org/hps/conditions/ConditionsSeriesConverterTest.java+32added 604
+178
3 added files
Preliminary version of multi collection API for conditions system.

java/trunk/conditions/src/main/java/org/hps/conditions
ConditionsSeries.java added at 604
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsSeries.java	                        (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsSeries.java	2014-05-19 20:37:07 UTC (rev 604)
@@ -0,0 +1,46 @@
+package org.hps.conditions;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * This class represents a series of collections containing <tt>ConditionsObjects</tt>.  It 
+ * is used to conditions collections when there are multiple ones with the same key that
+ * are valid for the current run. 
+ *  
+ * @author Jeremy McCormick <[log in to unmask]>
+ *
+ * @param <CollectionType> The specific type of the <tt>ConditionsObjectCollection</tt> the series contains.
+ */
+public class ConditionsSeries<CollectionType extends ConditionsObjectCollection> implements Iterable<CollectionType> {
+    
+    List<CollectionType> collections = new ArrayList<CollectionType>();
+    
+    CollectionType getCollection(int series) {
+        return collections.get(series);
+    }
+        
+    int addCollection(CollectionType collection) {
+        if (collections.contains(collection))
+            throw new IllegalArgumentException("The collection is already registered with this object.");
+        collections.add(collection);
+        return collections.indexOf(collection);
+    }
+    
+    int getNumberOfCollections() {
+        return collections.size();
+    }
+    
+    CollectionType findCollection(ConditionsRecord record) {
+        for (CollectionType collection : collections) {
+            if (collection.conditionsRecord == record)
+                return collection;
+        }
+        return null;
+    }
+    
+    public Iterator<CollectionType> iterator() {
+        return collections.iterator();
+    }
+}

java/trunk/conditions/src/main/java/org/hps/conditions
ConditionsSeriesConverter.java added at 604
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsSeriesConverter.java	                        (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsSeriesConverter.java	2014-05-19 20:37:07 UTC (rev 604)
@@ -0,0 +1,100 @@
+package org.hps.conditions;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.hps.conditions.ConditionsRecord.ConditionsRecordCollection;
+
+/**
+ * <p>
+ * This converter creates a <tt>ConditionsSeries</tt> which is a set of <tt>ConditionsObjectCollection</tt>
+ * objects with the same type.  This can be used to retrieve sets of conditions that may overlap in 
+ * time validity, such as sets of bad channels .
+ * </p>
+ * <p>
+ * Since type inference from the target variable is used in the {@link #createSeries(String)} method
+ * signature, there only needs to be one of these converters per {@link DatabaseConditionsManager}.
+ * The creation of the specific types is also done automatically, so each type of conditions object
+ * does not need its own converter class.
+ * </p>
+ * 
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
+public class ConditionsSeriesConverter {
+
+    DatabaseConditionsManager conditionsManager = null;
+    
+    ConditionsSeriesConverter(DatabaseConditionsManager conditionsManager) {
+        if (conditionsManager == null)
+            throw new RuntimeException("The conditionsManager is null.");
+        this.conditionsManager = conditionsManager;
+    }
+        
+    /**
+     * Create a <tt>ConditionsSeries</tt> which is a series of <tt>ConditionsObjectCollections</tt> of the same type,
+     * each of which have their own <tt>ConditionsRecord</tt>.  This should be used for overlapping conditions, such as
+     * sets of bad channels that are combined together as in the test run.
+     * 
+     * @param conditionsKey The name of the conditions key to retrieve from the conditions table.
+     * @return The <tt>ConditionsSeries</tt> matching <tt>conditionsKey</tt> which type inferred from target variable.
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public <CollectionType extends ConditionsObjectCollection> ConditionsSeries<CollectionType> createSeries(String conditionsKey) {
+
+        // Get the table meta data from the key given by the caller.
+        TableMetaData tableMetaData = conditionsManager.findTableMetaData(conditionsKey);
+        if (tableMetaData == null)
+            throw new RuntimeException("Table meta data for " + conditionsKey + " was not found.");
+
+        ConditionsSeries<CollectionType> series = new ConditionsSeries<CollectionType>();
+
+        // Get the ConditionsRecord with the meta-data, which will use the current run
+        // number from the manager.
+        ConditionsRecordCollection conditionsRecords = conditionsManager.findConditionsRecords(conditionsKey);
+
+        // Loop over conditions records. This will usually just be one record.
+        for (ConditionsRecord conditionsRecord : conditionsRecords.getObjects()) {
+            
+            ConditionsObjectCollection collection = ConditionsObjectUtil.createCollection(tableMetaData);
+
+            try {
+                collection.setCollectionId(conditionsRecord.getCollectionId());
+                collection.setConditionsRecord(conditionsRecords.get(0));
+            } catch (ConditionsObjectException e) {
+                throw new RuntimeException(e);
+            }
+            collection.setTableMetaData(tableMetaData);
+
+            // Get the table name.
+            String tableName = conditionsRecord.getTableName();
+
+            // Get the collection ID.
+            int collectionId = conditionsRecord.getCollectionId();
+
+            // Build a select query.
+            String query = QueryBuilder.buildSelect(tableName, collectionId, tableMetaData.getFieldNames(), "id ASC");
+
+            // Query the database.
+            ResultSet resultSet = conditionsManager.selectQuery(query);
+
+            try {
+                // Loop over rows.
+                while (resultSet.next()) {
+                    // Create new ConditionsObject.
+                    ConditionsObject newObject = ConditionsObjectUtil.createConditionsObject(resultSet, tableMetaData);
+
+                    // Add new object to collection, which will also assign it a
+                    // collection ID if applicable.
+                    collection.add(newObject);                                       
+                }
+            } catch (SQLException | ConditionsObjectException e) {
+                throw new RuntimeException(e);
+            }
+            
+            series.addCollection((CollectionType)collection);
+        }
+
+        // Return new collection.
+        return series;
+    }
+}

java/trunk/conditions/src/test/java/org/hps/conditions
ConditionsSeriesConverterTest.java added at 604
--- java/trunk/conditions/src/test/java/org/hps/conditions/ConditionsSeriesConverterTest.java	                        (rev 0)
+++ java/trunk/conditions/src/test/java/org/hps/conditions/ConditionsSeriesConverterTest.java	2014-05-19 20:37:07 UTC (rev 604)
@@ -0,0 +1,32 @@
+package org.hps.conditions;
+
+import junit.framework.TestCase;
+
+import org.hps.conditions.svt.SvtBadChannel;
+import org.hps.conditions.svt.SvtBadChannel.SvtBadChannelCollection;
+
+
+public class ConditionsSeriesConverterTest extends TestCase {
+    
+    DatabaseConditionsManager conditionsManager;
+
+    public void setUp() {
+        conditionsManager = new DefaultTestSetup().configure().setup();
+    }
+    
+    public void testConditionsSeries() {
+        
+        ConditionsSeries<SvtBadChannelCollection> series = 
+                conditionsManager.getConditionsSeries(TableConstants.SVT_BAD_CHANNELS);
+        
+        for (SvtBadChannelCollection collection : series) {
+            System.out.println("collection " + collection.getCollectionId() + " has " + collection.getObjects().size() + " objects");
+            System.out.println("conditions record ...");
+            System.out.println(collection.getConditionsRecord().toString());
+            for (SvtBadChannel badChannel : collection) {
+                System.out.println("  channel #" + badChannel.getChannelId());
+            }
+        }
+    }
+
+}
SVNspam 0.1