5 added + 1 removed + 9 modified, total 15 files
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/AbstractConditionsDatabaseObject.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/AbstractConditionsDatabaseObject.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -7,26 +7,31 @@
import java.util.Map.Entry;
/**
- * The abstract implementation of {@link ConditionsDatabaseObject}.
+ * The abstract implementation of {@link ConditionsObject}.
* @author Jeremy McCormick <[log in to unmask]>
*/
-public abstract class AbstractConditionsDatabaseObject implements ConditionsDatabaseObject {
+public abstract class AbstractConditionsDatabaseObject implements ConditionsObject {
- ConnectionManager _connectionManager = null;
- ConditionsTableMetaData _tableMetaData = null;
- int _rowId = -1;
- int _setId = -1;
- boolean _isDirty = false;
- boolean _isReadOnly = false;
- FieldValueMap _fieldValues = null;
+ private ConnectionManager _connectionManager = null;
+ private ConditionsTableMetaData _tableMetaData = null;
+ protected int _rowId = -1;
+ protected int _collectionId = -1;
+ protected boolean _isDirty = false;
+ protected boolean _isReadOnly = false;
+ protected FieldValueMap _fieldValues = null;
/**
- * Map of field names to their values.
+ * Class that maps field names to values.
*/
public static final class FieldValueMap extends LinkedHashMap<String, Object> {
}
/**
+ * Constructor for sub-classing with no arguments.
+ */
+ protected AbstractConditionsDatabaseObject() {}
+
+ /**
* Constructor for a new object which cannot initially be read only as it must be inserted.
* @param tableMetaData
* @param fieldValues
@@ -50,7 +55,7 @@
}
_connectionManager = connectionManager;
_tableMetaData = tableMetaData;
- _setId = setId;
+ _collectionId = setId;
_rowId = -1;
if (fieldValues != null) {
_fieldValues = fieldValues;
@@ -94,8 +99,8 @@
return _rowId;
}
- public int getSetId() {
- return _setId;
+ public int getCollectionId() {
+ return _collectionId;
}
public boolean isReadOnly() {
@@ -110,24 +115,24 @@
return _isDirty;
}
- public void delete() throws ConditionsDatabaseObjectException {
+ public void delete() throws ConditionsObjectException {
if (isReadOnly()) {
- throw new ConditionsDatabaseObjectException("This object cannot be deleted in read only mode.");
+ throw new ConditionsObjectException("This object cannot be deleted in read only mode.");
}
String query = "DELETE FROM " + _tableMetaData.getTableName() + " WHERE id = " + _rowId;
_connectionManager.update(query);
_rowId = -1;
}
- public void insert() throws ConditionsDatabaseObjectException, SQLException {
+ public void insert() throws ConditionsObjectException, SQLException {
if (!isNew()) {
- throw new ConditionsDatabaseObjectException("Record already exists in database.");
+ throw new ConditionsObjectException("Record already exists in database.");
}
if (isReadOnly()) {
- throw new ConditionsDatabaseObjectException("Cannot insert in read only mode.");
+ throw new ConditionsObjectException("Cannot insert in read only mode.");
}
if (_fieldValues.size() == 0) {
- throw new ConditionsDatabaseObjectException("There are no field values to insert.");
+ throw new ConditionsObjectException("There are no field values to insert.");
}
StringBuffer buff = new StringBuffer();
buff.append("INSERT INTO " + _tableMetaData.getTableName() + "( set_id");
@@ -135,7 +140,7 @@
buff.append(", " + entry.getKey());
}
buff.append(" ) VALUES ( ");
- buff.append(_setId);
+ buff.append(_collectionId);
for (Entry<String, Object> entry : _fieldValues.entrySet()) {
buff.append(", " + entry.getValue());
}
@@ -144,9 +149,9 @@
_rowId = key;
}
- public void select() throws ConditionsDatabaseObjectException, SQLException {
+ public void select() throws ConditionsObjectException, SQLException {
if (isNew()) {
- throw new ConditionsDatabaseObjectException("Record has not been inserted into database yet.");
+ throw new ConditionsObjectException("Record has not been inserted into database yet.");
}
StringBuffer buff = new StringBuffer();
buff.append("SELECT ");
@@ -166,29 +171,64 @@
}
}
- public void update() throws ConditionsDatabaseObjectException {
+ public void update() throws ConditionsObjectException {
if (isReadOnly()) {
- throw new ConditionsDatabaseObjectException("Cannot update in read only mode.");
+ throw new ConditionsObjectException("Cannot update in read only mode.");
}
if (isNew()) {
- throw new ConditionsDatabaseObjectException("Cannot update a new record.");
+ throw new ConditionsObjectException("Cannot update a new record.");
}
if (_fieldValues.size() == 0) {
- throw new ConditionsDatabaseObjectException("No field values to update.");
+ throw new ConditionsObjectException("No field values to update.");
}
StringBuffer buff = new StringBuffer();
buff.append("UPDATE " + _tableMetaData.getTableName() + " SET ");
- for (Entry entry : _fieldValues.entrySet()) {
+ for (Entry<String, Object> entry : _fieldValues.entrySet()) {
buff.append(entry.getKey() + " = '" + entry.getValue() + "', ");
}
buff.delete(buff.length()-2, buff.length()-1);
buff.append(" WHERE id = " + _rowId);
_connectionManager.update(buff.toString());
- _isDirty = false;
+ setIsDirty(false);
}
public void setFieldValue(String key, Object value) {
+ if (_fieldValues == null)
+ _fieldValues = new FieldValueMap();
_fieldValues.put(key, value);
- _isDirty = true;
- }
-}
+ setIsDirty(true);
+ }
+
+ public void setFieldValues(FieldValueMap fieldValues) {
+ _fieldValues = fieldValues;
+ if (!isNew()) {
+ setIsDirty(true);
+ }
+ }
+
+ public <T> T getFieldValue(Class<T> klass, String field) {
+ return klass.cast(_fieldValues.get(field));
+ }
+
+ public void setConnectionManager(ConnectionManager connectionManager) {
+ _connectionManager = connectionManager;
+ }
+
+ public void setTableMetaData(ConditionsTableMetaData tableMetaData) {
+ _tableMetaData = tableMetaData;
+ }
+
+ public void setCollectionId(int collectionId) {
+ _collectionId = collectionId;
+ if (!isNew())
+ setIsDirty(true);
+ }
+
+ public void setIsDirty(boolean isDirty) {
+ _isDirty = isDirty;
+ }
+
+ protected void setIsReadOnly(boolean isReadOnly) {
+ _isReadOnly = isReadOnly;
+ }
+}
\ No newline at end of file
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/BasicConditionsObjectCollection.java (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/BasicConditionsObjectCollection.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -0,0 +1,112 @@
+package org.hps.conditions;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.hps.conditions.ConditionsObject.ConditionsObjectException;
+
+public class BasicConditionsObjectCollection implements ConditionsObjectCollection {
+
+ List<ConditionsObject> objects = new ArrayList<ConditionsObject>();
+ ConditionsTableMetaData _tableMetaData;
+ int _collectionId;
+ boolean _isReadOnly;
+ boolean _isNew;
+ boolean _isDirty;
+
+ /**
+ * Use for existing collection.
+ * @param tableMetaData
+ * @param collectionId
+ * @param isReadOnly
+ */
+ public BasicConditionsObjectCollection(ConditionsTableMetaData tableMetaData, int collectionId, boolean isReadOnly) {
+ _tableMetaData = tableMetaData;
+ _collectionId = collectionId;
+ _isReadOnly = isReadOnly;
+ if (collectionId == -1) {
+ _isNew = true;
+ }
+ }
+
+ /**
+ * Use for new collection.
+ * @param tableMetaData
+ */
+ public BasicConditionsObjectCollection(ConditionsTableMetaData tableMetaData) {
+ _tableMetaData = tableMetaData;
+ _collectionId = -1;
+ _isReadOnly = true;
+ }
+
+ public ConditionsObject get(int index) {
+ return objects.get(index);
+ }
+
+ public List<ConditionsObject> getObjects() {
+ return Collections.unmodifiableList(objects);
+ }
+
+ public void add(ConditionsObject object) {
+ if (objects.contains(object)) {
+ throw new IllegalArgumentException("Collection already contains this object.");
+ }
+ object.setCollectionId(getCollectionId());
+ objects.add(object);
+ if (!isNew())
+ setIsDirty(true);
+ }
+
+ public ConditionsTableMetaData getTableMetaData() {
+ return _tableMetaData;
+ }
+
+ public int getCollectionId() {
+ return _collectionId;
+ }
+
+ public void updateAll() throws ConditionsObjectException {
+ for (ConditionsObject object : objects) {
+ object.update();
+ }
+ setIsDirty(false);
+ }
+
+ public void deleteAll() throws ConditionsObjectException {
+ for (ConditionsObject object : objects) {
+ object.delete();
+ }
+ }
+
+ public void selectAll() throws ConditionsObjectException, SQLException {
+ for (ConditionsObject object : objects) {
+ object.select();
+ }
+ }
+
+ public void insertAll() throws ConditionsObjectException, SQLException {
+ for (ConditionsObject object : objects) {
+ if (object.isNew()) {
+ object.insert();
+ }
+ }
+ }
+
+ public boolean isDirty() {
+ return _isDirty;
+ }
+
+ public boolean isReadOnly() {
+ return _isReadOnly;
+ }
+
+ void setIsDirty(boolean isDirty) {
+ _isDirty = isDirty;
+ }
+
+ public boolean isNew() {
+ return _isNew;
+ }
+}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/BasicConditionsObjectFactory.java (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/BasicConditionsObjectFactory.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -0,0 +1,47 @@
+package org.hps.conditions;
+
+import org.hps.conditions.AbstractConditionsDatabaseObject.FieldValueMap;
+import org.hps.conditions.ConditionsObject.ConditionsObjectException;
+
+
+public class BasicConditionsObjectFactory implements ConditionsObjectFactory {
+
+ private ConnectionManager _connectionManager;
+ private ConditionsTableRegistry _tableRegistry;
+
+ protected BasicConditionsObjectFactory(ConnectionManager connectionManager, ConditionsTableRegistry tableRegistry) {
+ _connectionManager = connectionManager;
+ _tableRegistry = tableRegistry;
+ }
+
+ @SuppressWarnings("unchecked")
+ // FIXME: Not sure this is the best way to accomplish the desired generic behavior, especially
+ // the unchecked cast to the T return type.
+ public ConditionsObject createObject(
+ Class<? extends ConditionsObject> klass,
+ String tableName,
+ int collectionId, // should be -1 if new object
+ FieldValueMap fieldValues) throws ConditionsObjectException {
+ ConditionsObject newObject = null;
+ try {
+ newObject = klass.newInstance();
+ } catch (InstantiationException x) {
+ throw new RuntimeException(x);
+ } catch (IllegalAccessException x) {
+ throw new RuntimeException(x);
+ }
+ newObject.setCollectionId(collectionId);
+ newObject.setFieldValues(fieldValues);
+ newObject.setConnectionManager(_connectionManager);
+ ConditionsTableMetaData tableMetaData = _tableRegistry.getTableMetaData(tableName);
+ if (tableMetaData == null) {
+ throw new ConditionsObjectException("No meta data found for table: " + tableName);
+ }
+ newObject.setTableMetaData(tableMetaData);
+ return newObject;
+ }
+
+ public ConditionsTableRegistry getTableRegistry() {
+ return _tableRegistry;
+ }
+}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsConverterRegister.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsConverterRegister.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -1,6 +1,5 @@
package org.hps.conditions;
-import org.lcsim.conditions.ConditionsManager;
import org.hps.conditions.beam.BeamCurrentConverter;
import org.hps.conditions.ecal.EcalBadChannelConverter;
import org.hps.conditions.ecal.EcalCalibrationConverter;
@@ -15,6 +14,7 @@
import org.hps.conditions.svt.SvtDaqMapConverter;
import org.hps.conditions.svt.SvtGainConverter;
import org.hps.conditions.svt.SvtTimeShiftConverter;
+import org.lcsim.conditions.ConditionsManager;
/**
* This class registers the full set of conditions converters onto the manager.
@@ -28,9 +28,22 @@
*/
static void register(ConditionsManager manager) {
+ // Create the table meta data registry.
+ ConditionsTableRegistry tableRegistry = new ConditionsTableRegistry();
+ tableRegistry.registerDefaultTableMetaData();
+
+ // Create the object factory for SVT.
+ ConditionsObjectFactory factory =
+ new BasicConditionsObjectFactory(ConnectionManager.getConnectionManager(), tableRegistry);
+
// ConditionsRecords with validity meta data.
manager.registerConditionsConverter(new ConditionsRecordConverter());
+ // SVT gains. (TESTING!!!)
+ SvtGainConverter svtGainConverter = new SvtGainConverter();
+ svtGainConverter.setObjectFactory(factory);
+ manager.registerConditionsConverter(svtGainConverter);
+
// SVT channel map.
manager.registerConditionsConverter(new SvtChannelMapConverter());
@@ -39,10 +52,7 @@
// SVT calibrations.
manager.registerConditionsConverter(new SvtCalibrationConverter());
-
- // SVT gains.
- manager.registerConditionsConverter(new SvtGainConverter());
-
+
// SVT bad channels.
manager.registerConditionsConverter(new SvtBadChannelConverter());
@@ -71,6 +81,6 @@
manager.registerConditionsConverter(new EcalConditionsConverter());
// Beam current condition.
- manager.registerConditionsConverter(new BeamCurrentConverter());
+ manager.registerConditionsConverter(new BeamCurrentConverter());
}
}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsDatabaseObject.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsDatabaseObject.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -1,82 +0,0 @@
-package org.hps.conditions;
-
-import java.sql.SQLException;
-
-/**
- * This is an interface for accessing conditions database information by row.
- * @author Jeremy McCormick <[log in to unmask]>
- */
-public interface ConditionsDatabaseObject {
-
- /**
- * Get the database table meta data associated to this object.
- * @return The database table meta data associated to this object.
- */
- ConditionsTableMetaData getTableMetaData();
-
- /**
- * Get the row ID of this object.
- * @return The database row ID.
- */
- int getRowId();
-
- /**
- * Get the set ID of this object identifying its collection.
- * @return The collection ID.
- */
- int getSetId();
-
- /**
- * Update this row in the database using a SQL UPDATE statement.
- */
- void update() throws ConditionsDatabaseObjectException;
-
- /**
- * Delete this object's row in the database using a SQL DELETE statement.
- */
- void delete() throws ConditionsDatabaseObjectException;
-
- /**
- * Insert this object into the database using a SQL INSERT statement.
- */
- void insert() throws ConditionsDatabaseObjectException, SQLException;
-
- /**
- * Select data into this object from the database using a SQL SELECT statement.
- */
- void select() throws ConditionsDatabaseObjectException, SQLException;
-
- /**
- * Return true if this object is read-only.
- * @return True if object is read-only.
- */
- boolean isReadOnly();
-
- /**
- * Return true if this object is new and hasn't been inserted into the database yet.
- * @return True if object is new.
- */
- boolean isNew();
-
- /**
- * Return true if this object's data has been modified without a database update.
- * @return True if object is dirty.
- */
- boolean isDirty();
-
- /**
- * Generic set method. This will set the object to the 'dirty' state.
- * @param fieldName The name of the field.
- * @param fieldValue The field value.
- */
- void setFieldValue(String field, Object value);
-
- /**
- * Exception type throw by methods in this interface.
- */
- public static final class ConditionsDatabaseObjectException extends Exception {
- public ConditionsDatabaseObjectException(String message) {
- super(message);
- }
- }
-}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsObjectCollection.java (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsObjectCollection.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -0,0 +1,35 @@
+package org.hps.conditions;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.hps.conditions.ConditionsObject.ConditionsObjectException;
+
+// TODO: This should be a generic with <T extends ConditionsDatabaseObject>
+// but of course this is a pain in the ass!!!
+public interface ConditionsObjectCollection {
+
+ ConditionsObject get(int index);
+
+ void add(ConditionsObject object);
+
+ List<ConditionsObject> getObjects();
+
+ ConditionsTableMetaData getTableMetaData();
+
+ int getCollectionId();
+
+ void updateAll() throws ConditionsObjectException;
+
+ void deleteAll() throws ConditionsObjectException;
+
+ void selectAll() throws ConditionsObjectException, SQLException;
+
+ void insertAll() throws ConditionsObjectException, SQLException;
+
+ boolean isDirty();
+
+ boolean isReadOnly();
+
+ boolean isNew();
+}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsObjectFactory.java (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsObjectFactory.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -0,0 +1,35 @@
+package org.hps.conditions;
+
+import org.hps.conditions.AbstractConditionsDatabaseObject.FieldValueMap;
+import org.hps.conditions.ConditionsObject.ConditionsObjectException;
+
+/**
+ * This is the primary interface in the API for generically instantiating {@link ConditionsObject} objects.
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
+public interface ConditionsObjectFactory {
+
+ /**
+ * Create a <code>ConditionsObject</code> generically, given a concrete class,
+ * an associated table in the conditions database, a collection ID (which may be -1 for a new object),
+ * and a map of field values.
+ *
+ * @param klass The concrete Class to be instantiated, which must have a zero argument constructor.
+ * @param tableName The name of the table in the conditions database.
+ * @param collectionId The unique collection
+ * @param fieldValues The field values of the object.
+ * @return The new ConditionsObject with concrete type <code>klass</code>.
+ * @throws ConditionsObjectException if there is a problem creating the object.
+ */
+ public ConditionsObject createObject(
+ Class<? extends ConditionsObject> klass,
+ String tableName,
+ int collectionId,
+ FieldValueMap fieldValues) throws ConditionsObjectException;
+
+ /**
+ * Get the registry of table meta data used by this factory.
+ * @return The registry of table meta data.
+ */
+ public ConditionsTableRegistry getTableRegistry();
+}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsTableMetaData.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsTableMetaData.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -18,7 +18,5 @@
String getTableName() {
return _tableName;
- }
-
- // TODO: Add method for getting next set ID.
+ }
}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsTableRegistry.java (rev 0)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsTableRegistry.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -0,0 +1,33 @@
+package org.hps.conditions;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConditionsTableRegistry {
+
+ Map<String, ConditionsTableMetaData> _tableMetaDataMap = new HashMap<String, ConditionsTableMetaData>();
+
+ public ConditionsTableMetaData getTableMetaData(String name) {
+ return _tableMetaDataMap.get(name);
+ }
+
+ void addTableMetaData(ConditionsTableMetaData tableMetaData) {
+ if (_tableMetaDataMap.get(tableMetaData.getTableName()) != null) {
+ throw new IllegalArgumentException("Table data already exists for " + tableMetaData.getTableName());
+ }
+ _tableMetaDataMap.put(tableMetaData.getTableName(), tableMetaData);
+ }
+
+ void registerDefaultTableMetaData() {
+
+ // SVT Gains (as test!!!)
+ Set<String> svtGainFields = new HashSet<String>();
+ svtGainFields.add("gain");
+ svtGainFields.add("offset");
+ ConditionsTableMetaData svtGainTable =
+ new ConditionsTableMetaData(ConditionsConstants.SVT_GAINS, svtGainFields);
+ addTableMetaData(svtGainTable);
+ }
+}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConnectionManager.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConnectionManager.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -158,30 +158,7 @@
cleanup(connection);
}
- /**
- * Setup the object from a properties file.
- */
- /*
- private void setupFromProperties() {
- Object obj = System.getProperties().get("hps.conditions.db.configuration");
- if (obj != null) {
- String config = obj.toString();
- Properties p = new Properties();
- try {
- p.load(new FileInputStream(new File(config)));
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- connectionParameters = ConnectionParameters.fromProperties(p);
- }
- }
- */
public void setupFromProperties(File propertiesFile) {
- //Object obj = System.getProperties().get("hps.conditions.db.configuration");
- //if (obj != null) {
- //String config = obj.toString();
Properties p = new Properties();
try {
p.load(new FileInputStream(propertiesFile));
@@ -191,6 +168,5 @@
throw new RuntimeException(e);
}
connectionParameters = ConnectionParameters.fromProperties(p);
- //}
}
}
java/trunk/conditions/src/main/java/org/hps/conditions
--- java/trunk/conditions/src/main/java/org/hps/conditions/DatabaseConditionsConverter.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/DatabaseConditionsConverter.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -8,14 +8,20 @@
* @author Jeremy McCormick <[log in to unmask]>
*/
public abstract class DatabaseConditionsConverter<T> implements ConditionsConverter<T> {
-
+
+ protected ConditionsObjectFactory _objectFactory;
+
/**
* Get the the {@link ConnectionManager} associated with this converter.
* For now, this calls the singleton method of the ConnectionManager
* to get its instance.
* @return The ConnectionManager of this converter.
*/
- public ConnectionManager getConnectionManager() {
+ protected ConnectionManager getConnectionManager() {
return ConnectionManager.getConnectionManager();
}
+
+ protected void setObjectFactory(ConditionsObjectFactory objectFactory) {
+ _objectFactory = objectFactory;
+ }
}
\ No newline at end of file
java/trunk/conditions/src/main/java/org/hps/conditions/svt
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtConditionsConverter.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtConditionsConverter.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -10,8 +10,9 @@
import java.util.Map.Entry;
+import org.hps.conditions.ConditionsObject;
+import org.hps.conditions.DatabaseConditionsConverter;
import org.lcsim.conditions.ConditionsManager;
-import org.hps.conditions.DatabaseConditionsConverter;
/**
* This class creates an {@link SvtConditions} object from the database,
@@ -59,9 +60,14 @@
// Add gains by channel.
SvtGainCollection gains = manager.getCachedConditions(SvtGainCollection.class, SVT_GAINS).getCachedData();
- for (Entry<Integer,SvtGain> entry : gains.entrySet()) {
- SvtChannel channel = conditions.getChannelMap().get(entry.getKey());
- conditions.getChannelConstants(channel).setGain(entry.getValue());
+ //for (Entry<Integer,SvtGain> entry : gains.entrySet()) {
+ // SvtChannel channel = conditions.getChannelMap().get(entry.getKey());
+ // conditions.getChannelConstants(channel).setGain(entry.getValue());
+ //}
+ for (ConditionsObject object : gains.getObjects()) {
+ int channelId = object.getFieldValue(Integer.class, "svt_channel_id");
+ SvtChannel channel = conditions.getChannelMap().get(channelId);
+ conditions.getChannelConstants(channel).setGain((SvtGain)object);
}
// Set the time shifts by sensor.
java/trunk/conditions/src/main/java/org/hps/conditions/svt
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGain.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGain.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -1,30 +1,19 @@
package org.hps.conditions.svt;
+import org.hps.conditions.AbstractConditionsDatabaseObject;
+
/**
* This class represents gain measurements for a single SVT channel.
* @author Jeremy McCormick <[log in to unmask]>
*/
-public class SvtGain {
-
- double gain = Double.NaN;
- double offset = Double.NaN;
-
+public class SvtGain extends AbstractConditionsDatabaseObject {
+
/**
- * Full qualified class constructor.
- * @param gain The gain of the channel.
- * @param offset The gain's offset.
- */
- SvtGain(double gain, double offset) {
- this.gain = gain;
- this.offset = offset;
- }
-
- /**
* Get the gain.
* @return The gain value.
*/
double getGain() {
- return gain;
+ return getFieldValue(Double.class, "gain");
}
/**
@@ -32,7 +21,7 @@
* @return The offset value.
*/
double getOffset() {
- return offset;
+ return getFieldValue(Double.class, "offset");
}
/**
@@ -40,8 +29,6 @@
* @return This object converted to a string.
*/
public String toString() {
- //return "gain: " + gain + ", offset: " + offset;
- return "" + gain + '\t' + offset;
- }
-
+ return "" + getGain() + '\t' + getOffset();
+ }
}
java/trunk/conditions/src/main/java/org/hps/conditions/svt
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGainCollection.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGainCollection.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -1,13 +1,17 @@
package org.hps.conditions.svt;
-import java.util.LinkedHashMap;
+import org.hps.conditions.BasicConditionsObjectCollection;
+import org.hps.conditions.ConditionsTableMetaData;
+
/**
* This class represents a list of {@link SvtGain} objects associated
* with their SVT channel IDs from the database.
* @author Jeremy McCormick <[log in to unmask]>
*/
-public class SvtGainCollection extends LinkedHashMap<Integer, SvtGain> {
- SvtGainCollection() {
+public class SvtGainCollection extends BasicConditionsObjectCollection {
+
+ public SvtGainCollection(ConditionsTableMetaData tableMetaData, int collectionId, boolean isReadOnly) {
+ super(tableMetaData, collectionId, isReadOnly);
}
}
java/trunk/conditions/src/main/java/org/hps/conditions/svt
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGainConverter.java 2014-03-14 02:01:45 UTC (rev 297)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtGainConverter.java 2014-03-14 02:02:38 UTC (rev 298)
@@ -3,10 +3,14 @@
import java.sql.ResultSet;
import java.sql.SQLException;
-import org.lcsim.conditions.ConditionsManager;
+import org.hps.conditions.AbstractConditionsDatabaseObject.FieldValueMap;
+import org.hps.conditions.ConditionsObject;
+import org.hps.conditions.ConditionsObject.ConditionsObjectException;
import org.hps.conditions.ConditionsRecord;
+import org.hps.conditions.ConditionsTableMetaData;
import org.hps.conditions.ConnectionManager;
import org.hps.conditions.DatabaseConditionsConverter;
+import org.lcsim.conditions.ConditionsManager;
/**
* This class creates a {@link SvtGainCollection} from the conditions database.
@@ -34,17 +38,20 @@
// Get the table name, field name, and field value defining the applicable conditions.
String tableName = record.getTableName();
String fieldName = record.getFieldName();
- int fieldValue = record.getFieldValue();
+ int collectionId = record.getFieldValue();
// Objects for building the return value.
- SvtGainCollection collection = new SvtGainCollection();
+ //SvtGainCollection collection = new SvtGainCollection();
+ ConditionsTableMetaData tableMetaData = _objectFactory.getTableRegistry().getTableMetaData(tableName);
+ SvtGainCollection collection =
+ new SvtGainCollection(tableMetaData, collectionId, true);
// Get the connection manager.
ConnectionManager connectionManager = ConnectionManager.getConnectionManager();
// Construct the query to find matching calibration records using the ID field.
String query = "SELECT svt_channel_id, gain, offset FROM "
- + tableName + " WHERE " + fieldName + " = " + fieldValue
+ + tableName + " WHERE " + fieldName + " = " + collectionId
+ " ORDER BY svt_channel_id ASC";
// Execute the query and get the results.
@@ -52,15 +59,23 @@
try {
// Loop over the gain records.
- while(resultSet.next()) {
- // Create the object with this channel's gain parameters.
- int channelId = resultSet.getInt(1);
- double gain = resultSet.getDouble(2);
- double offset = resultSet.getDouble(3);
- collection.put(channelId, new SvtGain(gain, offset));
+ while(resultSet.next()) {
+
+ // Setup conditions object.
+ FieldValueMap fieldValues = new FieldValueMap();
+ fieldValues.put("svt_channel_id", resultSet.getInt(1));
+ fieldValues.put("gain", resultSet.getDouble(2));
+ fieldValues.put("offset", resultSet.getDouble(3));
+ ConditionsObject newObject = _objectFactory.createObject(
+ SvtGain.class, tableName, -1, fieldValues);
+
+ // Add to collection.
+ collection.add(newObject);
}
- } catch (SQLException x) {
- throw new RuntimeException("Database error.", x);
+ } catch (SQLException x1) {
+ throw new RuntimeException("Database error.", x1);
+ } catch (ConditionsObjectException x2) {
+ throw new RuntimeException("Error converting to SvtGain object.", x2);
}
// Return collection of gain objects to caller.
SVNspam 0.1