Author: [log in to unmask]
Date: Mon Jun 22 14:11:27 2015
New Revision: 3180
Log:
Add CSV import interface and other improvements. HPSJAVA-550
Modified:
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObject.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObjectCollection.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectUtilities.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableMetaData.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableRegistry.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java
java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/database/ConverterRegistry.java
java/branches/HPSJAVA-488/conditions/src/test/java/org/hps/conditions/dummy/DummyConditionsObjectCollectionTest.java
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObject.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObject.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObject.java Mon Jun 22 14:11:27 2015
@@ -326,9 +326,10 @@
resultSet = statement.executeQuery(sql);
selected = resultSet.next();
if (selected) {
- for (int columnIndex = 1; columnIndex <= this.tableMetaData.getFieldNames().length; columnIndex++) {
- this.setFieldValue(this.tableMetaData.getFieldNames()[columnIndex - 1],
- resultSet.getObject(columnIndex));
+ int columnIndex = 1;
+ for (String fieldName : this.tableMetaData.getFieldNames()) {
+ this.setFieldValue(fieldName, resultSet.getObject(columnIndex));
+ ++columnIndex;
}
}
} finally {
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObjectCollection.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObjectCollection.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/BaseConditionsObjectCollection.java Mon Jun 22 14:11:27 2015
@@ -1,5 +1,9 @@
package org.hps.conditions.api;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -11,7 +15,12 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
/**
* Implementation of the {@link ConditionsObjectCollection} interface.
@@ -326,7 +335,7 @@
}
sb.setLength(sb.length() - 2);
sb.append(") VALUES (");
- for (int fieldIndex = 0; fieldIndex < this.getTableMetaData().getFieldNames().length; fieldIndex++) {
+ for (String fieldName : this.getTableMetaData().getFieldNames()) {
sb.append("?, ");
}
sb.setLength(sb.length() - 2);
@@ -335,10 +344,11 @@
try {
insertObjects = this.connection.prepareStatement(updateStatement, Statement.RETURN_GENERATED_KEYS);
for (final ObjectType object : this) {
- for (int fieldIndex = 0; fieldIndex < this.getTableMetaData().getFieldNames().length; fieldIndex++) {
- final String fieldName = this.getTableMetaData().getFieldNames()[fieldIndex];
- insertObjects.setObject(fieldIndex + 1,
+ int fieldIndex = 1;
+ for (String fieldName : this.getTableMetaData().getFieldNames()) {
+ insertObjects.setObject(fieldIndex,
object.getFieldValue(this.getTableMetaData().getFieldType(fieldName), fieldName));
+ fieldIndex++;
}
insertObjects.executeUpdate();
final ResultSet resultSet = insertObjects.getGeneratedKeys();
@@ -403,6 +413,84 @@
@Override
public final Iterator<ObjectType> iterator() {
return this.objects.iterator();
+ }
+
+ /**
+ * Load data from a CSV file.
+ *
+ * @param file the CSV file
+ */
+ @Override
+ public void loadCsv(final File file) throws IOException, FileNotFoundException, ConditionsObjectException {
+
+ // Clear the objects from the collection.
+ this.objects.clear();
+
+ // Unset the collection ID.
+ this.collectionId = BaseConditionsObject.UNSET_COLLECTION_ID;
+
+ // Check if the table info exists.
+ if (this.getTableMetaData() == null) {
+ // Table name is invalid.
+ throw new RuntimeException("The table meta data is not set.");
+ }
+
+ // Read in the CSV records.
+ final FileReader reader = new FileReader(file);
+ final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());
+ final List<CSVRecord> records = parser.getRecords();
+
+ // Get the database field names from the table info.
+ final Set<String> fields = this.getTableMetaData().getFieldNames();
+
+ // Get the text file column headers from the parser.
+ final Map<String, Integer> headerMap = parser.getHeaderMap();
+
+ // Get the headers that were read in from CSV.
+ final Set<String> headers = headerMap.keySet();
+
+ // Make sure the headers are actually valid column names in the database.
+ for (final String header : headerMap.keySet()) {
+ if (!fields.contains(header)) {
+ // The field name does not match a table column.
+ throw new RuntimeException("Header " + header + " from CSV is not a column in the "
+ + this.getTableMetaData().getTableName() + " table.");
+ }
+ }
+
+ // Get the class of the objects contained in this collection.
+ final Class<? extends ConditionsObject> objectClass = this.getTableMetaData().getObjectClass();
+
+ // Iterate over the CSV records.
+ for (final CSVRecord record : records) {
+
+ // Create a new conditions object.
+ final ObjectType object;
+ try {
+ // Create a new conditions object and cast to correct type for adding to collection.
+ object = (ObjectType) objectClass.newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException("Error creating conditions object.", e);
+ }
+
+ // Set the field values on the object.
+ for (final String header : headers) {
+ // Set the value of a field in the object based on the header name, converting to the correct type.
+ object.setFieldValue(
+ header,
+ ConditionsObjectUtilities.convertValue(this.getTableMetaData().getFieldType(header), record.get(header)));
+ }
+
+ // Add the object to the collection.
+ this.add(object);
+ }
+
+ // Close the CSV parser and reader.
+ parser.close();
+ reader.close();
+
+ // Flag collection as dirty (since it is read from text it is not explicitly in the database).
+ this.isDirty = true;
}
/**
@@ -434,9 +522,10 @@
newObject.setTableMetaData(this.tableMetaData);
final int id = resultSet.getInt(1);
((BaseConditionsObject) newObject).setRowId(id);
- for (int fieldIndex = 0; fieldIndex < this.tableMetaData.getFieldNames().length; fieldIndex++) {
- final String fieldName = this.tableMetaData.getFieldNames()[fieldIndex];
- newObject.setFieldValue(fieldName, resultSet.getObject(fieldIndex + 2));
+ int fieldIndex = 2;
+ for (String fieldName : this.tableMetaData.getFieldNames()) {
+ newObject.setFieldValue(fieldName, resultSet.getObject(fieldIndex));
+ ++fieldIndex;
}
try {
this.add(newObject);
@@ -585,4 +674,18 @@
}
return updated;
}
+
+ /**
+ * Convert object to string.
+ *
+ * @return this object converted to a string
+ */
+ public String toString() {
+ StringBuffer buff = new StringBuffer();
+ for (ConditionsObject object : this.getObjects()) {
+ buff.append(object);
+ buff.append('\n');
+ }
+ return buff.toString();
+ }
}
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java Mon Jun 22 14:11:27 2015
@@ -1,5 +1,8 @@
package org.hps.conditions.api;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.Comparator;
/**
@@ -51,6 +54,16 @@
int getCollectionId();
/**
+ * Load collection from a CSV file.
+ *
+ * @param file the input CSV file
+ * @throws IOException if there is an error closing the reader
+ * @throws FileNotFoundException if the input file does not exist
+ * @throws ConditionsObjectException if there is an error creating a conditions object
+ */
+ void loadCsv(File file) throws IOException, FileNotFoundException, ConditionsObjectException;
+
+ /**
* Set the collection ID.
*
* @param collectionId the new collection ID
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectUtilities.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectUtilities.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectUtilities.java Mon Jun 22 14:11:27 2015
@@ -1,15 +1,10 @@
package org.hps.conditions.api;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import java.util.HashSet;
-import java.util.Set;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
-import javassist.Modifier;
-
-import org.hps.conditions.database.Field;
-import org.hps.conditions.database.Table;
-import org.reflections.Reflections;
+import org.hps.conditions.database.DatabaseConditionsManager;
/**
* This is a collection of utility methods for {@link ConditionsObject}.
@@ -17,94 +12,72 @@
* @author Jeremy McCormick, SLAC
*/
public final class ConditionsObjectUtilities {
+
+ /**
+ * Static instance of conditions manager.
+ */
+ private static final DatabaseConditionsManager MANAGER = DatabaseConditionsManager.getInstance();
/**
- * Find all available classes that extend ConditionsObject.
+ * Default input date format from text data.
+ */
+ private static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ /**
+ * Convert from a raw string into a specific type.
*
- * @return The set of all available classes that extend ConditionsObject.
+ * @param type the target type
+ * @param value the raw value
+ * @return the value converter to the given type
*/
- public static Set<Class<? extends ConditionsObject>> findConditionsObjectTypes() {
- final Reflections reflections = new Reflections("org.hps.conditions");
- final Set<Class<? extends ConditionsObject>> objectTypes = new HashSet<Class<? extends ConditionsObject>>();
- for (final Class<? extends ConditionsObject> objectType : reflections.getSubTypesOf(ConditionsObject.class)) {
- if (Modifier.isAbstract(objectType.getModifiers())) {
- continue;
+ public static Object convertValue(final Class<?> type, final String value) throws ConditionsObjectException {
+ if (Integer.class.equals(type)) {
+ return Integer.parseInt(value);
+ } else if (Double.class.equals(type)) {
+ return Double.parseDouble(value);
+ } else if (Float.class.equals(type)) {
+ return Float.parseFloat(value);
+ } else if (Boolean.class.equals(type)) {
+ return Boolean.parseBoolean(value);
+ } else if (Date.class.equals(type)) {
+ try {
+ return DEFAULT_DATE_FORMAT.parse(value);
+ } catch (ParseException e) {
+ throw new ConditionsObjectException("Error parsing date.", e);
}
- if (objectType.getAnnotation(Table.class) == null) {
- continue;
- }
- objectTypes.add(objectType);
- }
- return objectTypes;
- }
-
- /**
- * Get the class for the collection of the ConditionsObject type.
- *
- * @param type the class of the ConditionsObject
- * @return the class of the collection
- */
- @SuppressWarnings("unchecked")
- public static Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>> getCollectionType(
- final Class<? extends ConditionsObject> type) {
- final String collectionClassName = type.getCanonicalName() + "$" + type.getSimpleName() + "Collection";
- Class<?> rawCollectionClass;
- try {
- rawCollectionClass = Class.forName(collectionClassName);
- } catch (final ClassNotFoundException e) {
- throw new RuntimeException("The type does not define a nested collection class.", e);
- }
- if (!BaseConditionsObjectCollection.class.isAssignableFrom(rawCollectionClass)) {
- throw new RuntimeException("The class " + rawCollectionClass.getSimpleName()
- + " does not extend ConditionsObjectCollection.");
- }
- return (Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>>) rawCollectionClass;
- }
-
- /**
- * Get the list of database field names for the class.
- *
- * @param type the class
- * @return the list of field names
- */
- public static Set<String> getFieldNames(final Class<? extends ConditionsObject> type) {
- final Set<String> fieldNames = new HashSet<String>();
- for (final Method method : type.getMethods()) {
- if (!method.getReturnType().equals(Void.TYPE)) {
- for (final Annotation annotation : method.getAnnotations()) {
- if (annotation.annotationType().equals(Field.class)) {
- if (!Modifier.isPublic(method.getModifiers())) {
- throw new RuntimeException("The method " + type.getName() + "." + method.getName()
- + " has a Field annotation, but it is not public.");
- }
- final Field field = (Field) annotation;
- for (final String fieldName : field.names()) {
- if (fieldName != null && !"".equals(fieldName)) {
- fieldNames.add(fieldName);
- }
- }
- }
- }
- }
- }
- return fieldNames;
- }
-
- /**
- * Get the list of table names for the class.
- *
- * @param type the class
- * @return the list of table names
- */
- public static String[] getTableNames(final Class<? extends ConditionsObject> type) {
- final Table tableAnnotation = type.getAnnotation(Table.class);
- if (tableAnnotation != null) {
- return tableAnnotation.names();
} else {
- return new String[] {};
+ return value;
}
}
-
+
+ /**
+ * Create a new conditions collection from the table name.
+ *
+ * @param tableName the name of the table
+ * @return the new conditions collection
+ * @throws ConditionsObjectException if there is an error creating the collection
+ */
+ public static ConditionsObjectCollection<?> newCollection(String tableName) throws ConditionsObjectException {
+ TableMetaData tableInfo = TableRegistry.getTableRegistry().findByTableName(tableName);
+ ConditionsObjectCollection<?> collection = tableInfo.newCollection();
+ collection.setConnection(MANAGER.getConnection());
+ return collection;
+ }
+
+ /**
+ * Create a new conditions object from the table name.
+ *
+ * @param tableName the name of the table
+ * @return the new conditions object
+ * @throws ConditionsObjectException if there is an error creating the object
+ */
+ public static ConditionsObject newObject(String tableName) throws ConditionsObjectException {
+ TableMetaData tableInfo = TableRegistry.getTableRegistry().findByTableName(tableName);
+ ConditionsObject object = tableInfo.newObject();
+ object.setConnection(MANAGER.getConnection());
+ return object;
+ }
+
/**
* Do not allow class to be instantiated.
*/
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableMetaData.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableMetaData.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableMetaData.java Mon Jun 22 14:11:27 2015
@@ -127,12 +127,12 @@
}
/**
- * Get the names of the fields. Types are implied from the database tables.
- *
- * @return the names of the fields
- */
- public String[] getFieldNames() {
- return this.fieldNames.toArray(new String[] {});
+ * Get the field names.
+ *
+ * @return the set of field names
+ */
+ public Set<String> getFieldNames() {
+ return this.fieldNames;
}
/**
@@ -230,4 +230,36 @@
buff.append('\n');
return buff.toString();
}
+
+ /**
+ * Create a new collection instance from the table meta data's collection type.
+ *
+ * @return the new object collection
+ * @throws ConditionsObjectException if there is an error creating a new collection
+ */
+ ConditionsObjectCollection<?> newCollection() throws ConditionsObjectException {
+ try {
+ ConditionsObjectCollection<?> collection = this.getCollectionClass().newInstance();
+ collection.setTableMetaData(this);
+ return collection;
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new ConditionsObjectException("Error creating new conditions object collection.", e);
+ }
+ }
+
+ /**
+ * Create a new object instance from the table meta data's object type.
+ *
+ * @return the new conditions object
+ * @throws ConditionsObjectException if there is an error creating a new object
+ */
+ ConditionsObject newObject() throws ConditionsObjectException {
+ try {
+ ConditionsObject object = this.getObjectClass().newInstance();
+ object.setTableMetaData(this);
+ return object;
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new ConditionsObjectException("Error creating new conditions object collection.", e);
+ }
+ }
}
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableRegistry.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableRegistry.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/TableRegistry.java Mon Jun 22 14:11:27 2015
@@ -4,11 +4,16 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import javassist.Modifier;
+
import org.hps.conditions.database.Field;
+import org.hps.conditions.database.Table;
+import org.reflections.Reflections;
/**
* This is a registry providing a map between tables and their meta-data.
@@ -68,14 +73,13 @@
*/
private static TableRegistry create() {
final TableRegistry registry = new TableRegistry();
- for (final Class<? extends ConditionsObject> objectType : ConditionsObjectUtilities.findConditionsObjectTypes()) {
+ for (final Class<? extends ConditionsObject> objectType : findConditionsObjectTypes()) {
// Get the collection type.
- final Class<? extends BaseConditionsObjectCollection<?>> collectionType = ConditionsObjectUtilities
- .getCollectionType(objectType);
+ final Class<? extends BaseConditionsObjectCollection<?>> collectionType = getCollectionType(objectType);
// Get the list of field names.
- final Set<String> fieldNames = ConditionsObjectUtilities.getFieldNames(objectType);
+ final Set<String> fieldNames = getFieldNames(objectType);
// Create map of fields to their types.
final Map<String, Class<?>> fieldTypes = new HashMap<String, Class<?>>();
@@ -92,7 +96,7 @@
}
}
- for (final String name : ConditionsObjectUtilities.getTableNames(objectType)) {
+ for (final String name : getTableNames(objectType)) {
// Create a meta data mapping for each table name in the class description.
final TableMetaData data = new TableMetaData(name, name, objectType, collectionType, fieldNames,
fieldTypes);
@@ -103,6 +107,30 @@
}
return registry;
}
+
+ /**
+ * Get the class for the collection of the ConditionsObject type.
+ *
+ * @param type the class of the ConditionsObject
+ * @return the class of the collection
+ */
+ @SuppressWarnings("unchecked")
+ public static Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>> getCollectionType(
+ final Class<? extends ConditionsObject> type) {
+ final String collectionClassName = type.getCanonicalName() + "$" + type.getSimpleName() + "Collection";
+ Class<?> rawCollectionClass;
+ try {
+ rawCollectionClass = Class.forName(collectionClassName);
+ } catch (final ClassNotFoundException e) {
+ throw new RuntimeException("The type does not define a nested collection class.", e);
+ }
+ if (!BaseConditionsObjectCollection.class.isAssignableFrom(rawCollectionClass)) {
+ throw new RuntimeException("The class " + rawCollectionClass.getSimpleName()
+ + " does not extend ConditionsObjectCollection.");
+ }
+ return (Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>>) rawCollectionClass;
+ }
+
/**
* Get the global static instance of the registry.
@@ -134,6 +162,50 @@
*/
private TableRegistry() {
}
+
+ /**
+ * Get the list of database field names for the class.
+ *
+ * @param type the class
+ * @return the list of field names
+ */
+ private static Set<String> getFieldNames(final Class<? extends ConditionsObject> type) {
+ final Set<String> fieldNames = new HashSet<String>();
+ for (final Method method : type.getMethods()) {
+ if (!method.getReturnType().equals(Void.TYPE)) {
+ for (final Annotation annotation : method.getAnnotations()) {
+ if (annotation.annotationType().equals(Field.class)) {
+ if (!Modifier.isPublic(method.getModifiers())) {
+ throw new RuntimeException("The method " + type.getName() + "." + method.getName()
+ + " has a Field annotation, but it is not public.");
+ }
+ final Field field = (Field) annotation;
+ for (final String fieldName : field.names()) {
+ if (fieldName != null && !"".equals(fieldName)) {
+ fieldNames.add(fieldName);
+ }
+ }
+ }
+ }
+ }
+ }
+ return fieldNames;
+ }
+
+ /**
+ * Get the list of table names for the class.
+ *
+ * @param type the class
+ * @return the list of table names
+ */
+ private static String[] getTableNames(final Class<? extends ConditionsObject> type) {
+ final Table tableAnnotation = type.getAnnotation(Table.class);
+ if (tableAnnotation != null) {
+ return tableAnnotation.names();
+ } else {
+ return new String[] {};
+ }
+ }
/**
* Find meta data by collection type.
@@ -164,6 +236,26 @@
public TableMetaData findByTableName(final String name) {
return this.get(name);
}
+
+ /**
+ * Find all available classes that extend ConditionsObject.
+ *
+ * @return The set of all available classes that extend ConditionsObject.
+ */
+ public static Set<Class<? extends ConditionsObject>> findConditionsObjectTypes() {
+ final Reflections reflections = new Reflections("org.hps.conditions");
+ final Set<Class<? extends ConditionsObject>> objectTypes = new HashSet<Class<? extends ConditionsObject>>();
+ for (final Class<? extends ConditionsObject> objectType : reflections.getSubTypesOf(ConditionsObject.class)) {
+ if (Modifier.isAbstract(objectType.getModifiers())) {
+ continue;
+ }
+ if (objectType.getAnnotation(Table.class) == null) {
+ continue;
+ }
+ objectTypes.add(objectType);
+ }
+ return objectTypes;
+ }
/**
* Convert this object to a string.
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/cli/LoadCommand.java Mon Jun 22 14:11:27 2015
@@ -7,8 +7,8 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
+import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -20,6 +20,7 @@
import org.hps.conditions.api.ConditionsObject;
import org.hps.conditions.api.ConditionsObjectCollection;
import org.hps.conditions.api.ConditionsObjectException;
+import org.hps.conditions.api.ConditionsObjectUtilities;
import org.hps.conditions.api.DatabaseObjectException;
import org.hps.conditions.api.TableMetaData;
import org.hps.conditions.database.DatabaseConditionsManager;
@@ -75,27 +76,6 @@
}
/**
- * Convert from a raw string into a specific type.
- *
- * @param type the target type
- * @param value the raw value
- * @return the value converter to the given type
- */
- Object convertValue(final Class<?> type, final String value) {
- if (Integer.class.equals(type)) {
- return Integer.parseInt(value);
- } else if (Double.class.equals(type)) {
- return Double.parseDouble(value);
- } else if (Float.class.equals(type)) {
- return Float.parseFloat(value);
- } else if (Boolean.class.equals(type)) {
- return Boolean.parseBoolean(value);
- } else {
- return value;
- }
- }
-
- /**
* Execute the <i>load</i> command with the given arguments.
*
* @param arguments the command arguments
@@ -205,7 +185,7 @@
final Class<? extends ConditionsObject> objectClass = tableMetaData.getObjectClass();
// Get the field names from the table info.
- final List<String> fieldNames = new ArrayList<String>(Arrays.asList(tableMetaData.getFieldNames()));
+ final Set<String> fieldNames = tableMetaData.getFieldNames();
fieldNames.remove("collection_id");
// Check that the column names which were read in from the header row are valid.
@@ -259,7 +239,7 @@
LOGGER.info("value: " + value);
// Convert the value to a specific type and set the value on the object.
- object.setFieldValue(columnNames.get(i), this.convertValue(columnType, value));
+ object.setFieldValue(columnNames.get(i), ConditionsObjectUtilities.convertValue(columnType, value));
// Add the object to the collection.
LOGGER.info("adding conditions object: " + object);
Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/database/ConverterRegistry.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/database/ConverterRegistry.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/database/ConverterRegistry.java Mon Jun 22 14:11:27 2015
@@ -9,7 +9,7 @@
import org.hps.conditions.api.AbstractConditionsObjectConverter;
import org.hps.conditions.api.BaseConditionsObjectCollection;
import org.hps.conditions.api.ConditionsObject;
-import org.hps.conditions.api.ConditionsObjectUtilities;
+import org.hps.conditions.api.TableRegistry;
import org.reflections.Reflections;
/**
@@ -58,8 +58,7 @@
}
}
- final Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>> collectionType = ConditionsObjectUtilities
- .getCollectionType(objectType);
+ final Class<? extends BaseConditionsObjectCollection<? extends ConditionsObject>> collectionType = TableRegistry.getCollectionType(objectType);
AbstractConditionsObjectConverter converter = null;
if (converterClass == null) {
Modified: java/branches/HPSJAVA-488/conditions/src/test/java/org/hps/conditions/dummy/DummyConditionsObjectCollectionTest.java
=============================================================================
--- java/branches/HPSJAVA-488/conditions/src/test/java/org/hps/conditions/dummy/DummyConditionsObjectCollectionTest.java (original)
+++ java/branches/HPSJAVA-488/conditions/src/test/java/org/hps/conditions/dummy/DummyConditionsObjectCollectionTest.java Mon Jun 22 14:11:27 2015
@@ -1,9 +1,12 @@
package org.hps.conditions.dummy;
+import java.io.File;
import java.sql.Connection;
import junit.framework.TestCase;
+import org.hps.conditions.api.ConditionsObjectCollection;
+import org.hps.conditions.api.ConditionsObjectUtilities;
import org.hps.conditions.api.TableMetaData;
import org.hps.conditions.api.TableRegistry;
import org.hps.conditions.database.DatabaseConditionsManager;
@@ -63,4 +66,16 @@
System.out.println("deleting objects from collection " + collection.getCollectionId());
collection.delete();
}
+
+ public void testCsv() throws Exception {
+
+ // Create an object collection.
+ final ConditionsObjectCollection<?> collection = ConditionsObjectUtilities.newCollection("dummy");
+
+ // Load CSV data.
+ collection.loadCsv(new File("dummy.txt"));
+
+ System.out.println("loaded dummy collection ...");
+ System.out.println(collection.toString());
+ }
}
|