Author: [log in to unmask] Date: Tue Nov 25 01:48:21 2014 New Revision: 1579 Log: Simplify conditions object API by removing unnecessary methods. 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 java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.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 Tue Nov 25 01:48:21 2014 @@ -19,8 +19,6 @@ private TableMetaData tableMetaData = null; protected int rowId = -1; protected int collectionId = -1; - protected boolean isDirty = false; - protected boolean isReadOnly = false; protected FieldValueMap fieldValues; /** @@ -42,40 +40,24 @@ return collectionId; } - public boolean isReadOnly() { - return isReadOnly; - } - public boolean isNew() { return rowId == -1; } - public boolean isDirty() { - return isDirty; - } - public void delete() throws ConditionsObjectException { - if (isReadOnly()) { - throw new ConditionsObjectException("This object is set to read only."); - } - if (isNew()) { - throw new ConditionsObjectException("This object is not in the database and so cannot be deleted."); - } 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 (!isNew()) - throw new ConditionsObjectException("Record already exists in database and cannot be inserted."); - if (isReadOnly()) - throw new ConditionsObjectException("This object is set to read only mode."); if (fieldValues.size() == 0) throw new ConditionsObjectException("There are no field values to insert."); - if (!hasValidCollection()) + if (collectionId == -1) throw new ConditionsObjectException("The object's collection ID is not valid."); - String query = QueryBuilder.buildInsert(getTableMetaData().getTableName(), getCollectionId(), getTableMetaData().getFieldNames(), fieldValues.valuesToArray()); + // 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) { @@ -88,6 +70,7 @@ 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); @@ -107,30 +90,20 @@ } public void update() throws ConditionsObjectException { - if (isReadOnly()) { - throw new ConditionsObjectException("This object is set to read only.", this); - } - if (isNew()) { - throw new ConditionsObjectException("Cannot call update on a new record.", this); - } 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); - setIsDirty(false); } public void setFieldValue(String key, Object value) { fieldValues.put(key, value); - setIsDirty(true); } public void setFieldValues(FieldValueMap fieldValues) { this.fieldValues = fieldValues; - if (!isNew()) { - setIsDirty(true); - } } public <T> T getFieldValue(Class<T> klass, String field) { @@ -158,22 +131,11 @@ this.collectionId = collectionId; } - public void setIsDirty(boolean isDirty) { - this.isDirty = isDirty; - } - - public void setIsReadOnly() { - isReadOnly = true; - } - public void setRowId(int rowId) throws ConditionsObjectException { - if (this.rowId != -1) + if (!isNew()) { throw new ConditionsObjectException("The row ID cannot be reassigned on an existing object."); + } this.rowId = rowId; - } - - boolean hasValidCollection() { - return collectionId != -1; } public String toString() { @@ -186,6 +148,4 @@ } 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 Tue Nov 25 01:48:21 2014 @@ -4,10 +4,11 @@ /** * This is an ORM interface for accessing conditions database information by - * row. It can handle new or existing records. The ID values for new records are + * row. It can handle new or existing records. The row ID values for new records are * -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 { /** @@ -50,26 +51,6 @@ void select() throws ConditionsObjectException; /** - * 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 for field values. This will set the object to the * 'dirty' state. * @param fieldName The name of the field. @@ -95,7 +76,7 @@ * @param field The field value. * @return The field value casted to type T. */ - public <T> T getFieldValue(Class<T> klass, String field); + public <T> T getFieldValue(Class<T> type, String field); /** * Get a field value with implicit return type. @@ -127,10 +108,11 @@ * @throws ConditionsObjectException if already set */ public void setRowId(int rowId) throws ConditionsObjectException; - + /** - * Set the object to read only mode. This cannot be changed back once it is - * set. + * Return true if this object is new, e.g. it does not have a valid row ID. + * This means that it does not have a database record in its table. + * @return True if record is new. */ - void setIsReadOnly(); + public boolean isNew(); } Modified: java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java ============================================================================= --- java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java (original) +++ java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsObjectCollection.java Tue Nov 25 01:48:21 2014 @@ -6,6 +6,7 @@ import java.util.LinkedHashSet; import java.util.Set; +import org.hps.conditions.database.DatabaseConditionsManager; import org.hps.conditions.database.TableMetaData; public class ConditionsObjectCollection<ObjectType extends ConditionsObject> implements Iterable<ObjectType> { @@ -13,25 +14,14 @@ protected Set<ObjectType> objects = new LinkedHashSet<ObjectType>(); protected TableMetaData tableMetaData; protected int collectionId = -1; - protected boolean isReadOnly; - protected boolean isDirty; - protected boolean isNew; protected ConditionsRecord conditionsRecord; protected ConditionsObjectCollection() { } - public ConditionsObjectCollection(TableMetaData tableMetaData, int collectionId, boolean isReadOnly) { + public ConditionsObjectCollection(TableMetaData tableMetaData, int collectionId) { this.tableMetaData = tableMetaData; this.collectionId = collectionId; - this.isReadOnly = isReadOnly; - if (collectionId == -1) { - this.isNew = true; - } - } - - public ConditionsRecord getConditionsRecord() { - return conditionsRecord; } public ObjectType get(int index) { @@ -70,8 +60,6 @@ if (object.getTableMetaData() == null && tableMetaData != null) object.setTableMetaData(tableMetaData); objects.add(object); - if (!isNew()) - setIsDirty(true); } public TableMetaData getTableMetaData() { @@ -88,82 +76,53 @@ for (ConditionsObject object : objects) { object.update(); } - setIsDirty(false); } - // TODO: This does not need to loop. It should just call delete on the - // collection ID value. + // TODO: This does not need to loop. It should just call delete on the collection ID value. public void delete() throws ConditionsObjectException { - if (isReadOnly()) { - throw new ConditionsObjectException("Collection is read only."); - } + // TODO: Replace with call to a deleteCollection DatabaseConditionsManager method. for (ConditionsObject object : objects) { object.delete(); } } - // TODO: This should not loop. It should select all the objects with a - // matching collection ID from the database. + // TODO: This should not loop. It should select all the objects with a matching collection ID + // from the database replacing the current contents of the collection (if any). public void select() throws ConditionsObjectException, SQLException { + // TODO: Replace with call to a selectCollection DatabaseConditionsManager method. for (ConditionsObject object : objects) { object.select(); } } - // TODO: This method needs to get the next collection ID from the conditions - // manager. - // TODO: This operation should lock the table. public void insert() throws ConditionsObjectException, SQLException { - if (!isNew()) { - throw new ConditionsObjectException("Collection already exists in the database."); - } - for (ConditionsObject object : objects) { - // if (object.isNew()) { - object.insert(); - // } - } - isNew = false; - } - - public boolean isDirty() { - return isDirty; - } - - public boolean isReadOnly() { - return isReadOnly; - } - - void setIsDirty(boolean isDirty) { - this.isDirty = isDirty; - } - - // TODO: This can probably just check if collection ID is not valid e.g. equals -1. - public boolean isNew() { - return isNew; + DatabaseConditionsManager.getInstance().insertCollection(this); } public void setCollectionId(int collectionId) throws ConditionsObjectException { if (this.collectionId != -1) throw new ConditionsObjectException("The collection ID is already set."); this.collectionId = collectionId; + for (ConditionsObject object : this.objects) { + object.setCollectionId(this.collectionId); + } } public void setTableMetaData(TableMetaData tableMetaData) { this.tableMetaData = tableMetaData; } - public void setIsReadOnly(boolean isReadOnly) { - this.isReadOnly = isReadOnly; - } - - public void setConditionsRecord(ConditionsRecord conditionsRecord) throws ConditionsObjectException { - if (this.conditionsRecord != null) - throw new ConditionsObjectException("The ConditionsRecord is already set on this collection."); - this.conditionsRecord = conditionsRecord; - } - @Override public Iterator<ObjectType> iterator() { return objects.iterator(); } -} + + public String toString() { + StringBuffer buffer = new StringBuffer(); + for (ConditionsObject object : this.getObjects()) { + buffer.append(object.toString()); + buffer.append('\n'); + } + return buffer.toString(); + } +}