Author: [log in to unmask]
Date: Thu Dec 11 11:32:57 2014
New Revision: 1691
Log:
Simplify conditions object API. Database operations will go through the collection interface instead.
Modified:
java/trunk/conditions/src/main/java/org/hps/conditions/api/AbstractConditionsObject.java
java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObject.java
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/api/AbstractConditionsObject.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/api/AbstractConditionsObject.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/api/AbstractConditionsObject.java Thu Dec 11 11:32:57 2014
@@ -1,14 +1,6 @@
package org.hps.conditions.api;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.util.List;
-
-import org.hps.conditions.database.DatabaseConditionsManager;
-import org.hps.conditions.database.DatabaseUtilities;
-import org.hps.conditions.database.QueryBuilder;
-import org.hps.conditions.database.TableMetaData;
+import java.util.Map.Entry;
/**
* The abstract implementation of {@link ConditionsObject}.
@@ -16,88 +8,25 @@
*/
public abstract class AbstractConditionsObject implements ConditionsObject {
- private TableMetaData tableMetaData = null;
protected int rowId = -1;
- protected int collectionId = -1;
protected FieldValueMap fieldValues;
- /**
+ /**
* Constructor for sub-classing.
*/
protected AbstractConditionsObject() {
fieldValues = new FieldValueMap();
}
- public TableMetaData getTableMetaData() {
- return tableMetaData;
- }
-
public int getRowId() {
return rowId;
- }
-
- public int getCollectionId() {
- return collectionId;
}
public boolean isNew() {
return rowId == -1;
}
- public void delete() throws ConditionsObjectException {
- String query = QueryBuilder.buildDelete(tableMetaData.getTableName(), rowId);
- // TODO: Replace this with a method that takes a conditions object.
- DatabaseConditionsManager.getInstance().updateQuery(query);
- rowId = -1;
- }
-
- public void insert() throws ConditionsObjectException {
- if (fieldValues.size() == 0)
- throw new ConditionsObjectException("There are no field values to insert.");
- if (collectionId == -1)
- throw new ConditionsObjectException("The object's collection ID is not valid.");
- // TODO: Replace this with a method that takes a conditions object.
- String query = QueryBuilder.buildInsert(getTableMetaData().getTableName(), getCollectionId(), getTableMetaData().getFieldNames(), fieldValues.valuesToArray());
- System.out.println(query);
- List<Integer> keys = DatabaseConditionsManager.getInstance().updateQuery(query);
- if (keys.size() != 1) {
- throw new ConditionsObjectException("SQL insert returned wrong number of keys: " + keys.size());
- }
- rowId = keys.get(0);
- }
-
- public void select() throws ConditionsObjectException {
- if (isNew()) {
- throw new ConditionsObjectException("Record has not been inserted into the database yet.");
- }
- // TODO: Replace this with method that takes a conditions object.
- String query = QueryBuilder.buildSelect(getTableMetaData().getTableName(), collectionId, fieldValues.fieldsToArray(), "id ASC");
- DatabaseConditionsManager manager = DatabaseConditionsManager.getInstance();
- ResultSet resultSet = manager.selectQuery(query);
- try {
- ResultSetMetaData metadata = resultSet.getMetaData();
- int ncolumns = metadata.getColumnCount();
- if (resultSet.next()) {
- for (int i = 1; i <= ncolumns; i++) {
- fieldValues.put(metadata.getColumnName(i), resultSet.getObject(i));
- }
- }
- } catch (SQLException e) {
- throw new ConditionsObjectException(e.getMessage(), this);
- }
- DatabaseUtilities.close(resultSet);
- resultSet = null;
- }
-
- public void update() throws ConditionsObjectException {
- if (fieldValues.size() == 0) {
- throw new ConditionsObjectException("No field values to update.", this);
- }
- // TODO: Replace this with method that takes a conditions object.
- String query = QueryBuilder.buildUpdate(tableMetaData.getTableName(), rowId, fieldValues.fieldsToArray(), fieldValues.valuesToArray());
- DatabaseConditionsManager.getInstance().updateQuery(query);
- }
-
+
public void setFieldValue(String key, Object value) {
fieldValues.put(key, value);
}
@@ -119,18 +48,6 @@
return (T) fieldValues.get(field);
}
- public void setTableMetaData(TableMetaData tableMetaData) throws ConditionsObjectException {
- if (this.tableMetaData != null)
- throw new ConditionsObjectException("The table meta data cannot be reset on an object.", this);
- this.tableMetaData = tableMetaData;
- }
-
- public void setCollectionId(int collectionId) throws ConditionsObjectException {
- if (this.collectionId != -1)
- throw new ConditionsObjectException("The collection ID cannot be reassigned once set.", this);
- this.collectionId = collectionId;
- }
-
public void setRowId(int rowId) throws ConditionsObjectException {
if (!isNew()) {
throw new ConditionsObjectException("The row ID cannot be reassigned on an existing object.");
@@ -142,8 +59,8 @@
StringBuffer sb = new StringBuffer();
sb.append(this.getRowId());
sb.append('\t');
- for (String fieldName : getTableMetaData().getFieldNames()) {
- sb.append(getFieldValue(fieldName));
+ for (Entry<String, Object> entries : this.getFieldValues().entrySet()) {
+ sb.append(entries.getValue());
sb.append('\t');
}
return sb.toString();
Modified: java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObject.java
=============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObject.java (original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObject.java Thu Dec 11 11:32:57 2014
@@ -8,47 +8,19 @@
* -1 which indicates they are not in the database yet.
* @author Jeremy McCormick <[log in to unmask]>
*/
-// TODO: The collection ID should be a regular field in the FieldValueMap.
public interface ConditionsObject {
/**
* Get the database table meta data associated to this object.
* @return The database table meta data associated to this object.
*/
- TableMetaData getTableMetaData();
+ //TableMetaData getTableMetaData();
/**
* Get the row ID of this object.
* @return The database row ID.
*/
int getRowId();
-
- /**
- * Get the collection ID of this object identifying its unique collection.
- * @return The collection ID.
- */
- int getCollectionId();
-
- /**
- * Update this row in the database using a SQL UPDATE statement.
- */
- void update() throws ConditionsObjectException;
-
- /**
- * Delete this object's row in the database using a SQL DELETE statement.
- */
- void delete() throws ConditionsObjectException;
-
- /**
- * Insert this object into the database using a SQL INSERT statement.
- */
- void insert() throws ConditionsObjectException;
-
- /**
- * Select data into this object from the database using a SQL SELECT
- * statement.
- */
- void select() throws ConditionsObjectException;
/**
* Generic set method for field values. This will set the object to the
@@ -86,22 +58,6 @@
public <T> T getFieldValue(String field);
/**
- * Set the ConditionsTableMetaData of this object. This cannot be reset once
- * set.
- * @param tableMetaData The ConditionsTableMetaData.
- * @throws ConditionsObjectException if already set
- */
- void setTableMetaData(TableMetaData tableMetaData) throws ConditionsObjectException;
-
- /**
- * Set the collection ID of this object. This cannot be reset once set to a
- * valid ID (e.g. not -1).
- * @param collectionId The collection ID.
- * @throws ConditionsObjectException if already set
- */
- void setCollectionId(int collectionId) throws ConditionsObjectException;
-
- /**
* Set the row ID of this object. This cannot be reset once set to a valid
* ID (e.g. not -1).
* @param rowId The object's row ID.
|