From aedc34d55462a75e329bbf342251ff6504cd117e Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Sun, 19 May 2024 17:56:33 -0400 Subject: Initial import from SVN --- .../java/net/wotonomy/access/EOAdaptorChannel.java | 244 +++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAdaptorChannel.java (limited to 'projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAdaptorChannel.java') diff --git a/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAdaptorChannel.java b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAdaptorChannel.java new file mode 100644 index 0000000..09b8a6d --- /dev/null +++ b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAdaptorChannel.java @@ -0,0 +1,244 @@ +/* + Wotonomy: OpenStep design patterns for pure Java applications. + Copyright (C) 2001 Michael Powers + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see http://www.gnu.org + */ +package net.wotonomy.access; + +import net.wotonomy.control.EOFetchSpecification; +import net.wotonomy.control.EOQualifier; +import net.wotonomy.foundation.NSArray; +import net.wotonomy.foundation.NSDictionary; +import net.wotonomy.foundation.NSMutableDictionary; + +/** +* +* @author ezamudio@nasoft.com +* @author $Author: cgruber $ +* @version $Revision: 894 $ +*/ +public abstract class EOAdaptorChannel { + + protected EOAdaptorContext _context; + + public EOAdaptorChannel(EOAdaptorContext context) { + super(); + _context = context; + } + + public EOAdaptorContext adaptorContext() { + return _context; + } + + public void addStoredProceduresNamed(NSArray names, EOModel model) { + } + + public abstract NSArray attributesToFetch(); + + public abstract void cancelFetch(); + + public abstract void closeChannel(); + + public abstract NSArray describeResults(); + + public abstract int deleteRowsDescribedByQualifier(EOQualifier q, EOEntity entity); + + public abstract void evaluateExpression(EOSQLExpression sql); + + public abstract void executeStoredProcedure(EOStoredProcedure proc, NSDictionary values); + + public abstract NSMutableDictionary fetchRow(); + + public abstract void insertRow(NSDictionary row, EOEntity entity); + + public abstract boolean isFetchInProgress(); + + public abstract boolean isOpen(); + + public abstract void openChannel(); + + public abstract NSDictionary returnValuesForLastStoredProcedureInvocation(); + + public abstract void selectAttributes(NSArray atts, EOFetchSpecification fspec, boolean lock, EOEntity entity); + + public abstract void setAttributesToFetch(NSArray atts); + + public abstract int updateValuesInRowsDescribedByQualifier(NSDictionary row, EOQualifier q, EOEntity entity); + + public void deleteRowDescribedByQualifier(EOQualifier q, EOEntity entity) { + adaptorContext().beginTransaction(); + int count = deleteRowsDescribedByQualifier(q, entity); + if (count != 1) { + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("Qualifier deleted " + count + " rows instead of exactly one."); + } + adaptorContext().commitTransaction(); + } + + public EOModel describeModelWithTableNames(NSArray names) { + return null; + } + + public NSArray describeStoredProcedureNames() { + return NSArray.EmptyArray; + } + + public NSArray describeTableNames() { + return NSArray.EmptyArray; + } + + public NSMutableDictionary dictionaryWithObjectsForAttributes(Object[] values, NSArray attributes) { + Object[] keys = new Object[attributes.count()]; + for (int i = 0; i < attributes.count(); i++) + keys[i] = ((EOAttribute)attributes.objectAtIndex(i)).name(); + return new NSMutableDictionary(values, keys); + } + + public void lockRowComparingAttributes(NSArray atts, EOEntity entity, EOQualifier q, NSDictionary snapshot) { + EOFetchSpecification fspec = new EOFetchSpecification(entity.name(), q, null); + adaptorContext().beginTransaction(); + selectAttributes(atts, fspec, true, entity); + if (isFetchInProgress()) { + NSDictionary row = fetchRow(); + if (row == null) { + cancelFetch(); + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("Cannot obtain row to lock. Probably modified from the outside."); + } + if (isFetchInProgress()) { + if (fetchRow() != null) { + cancelFetch(); + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("Qualifier returns more than one row."); + } + } + java.util.Enumeration enumeration = snapshot.keyEnumerator(); + while (enumeration.hasMoreElements()) { + Object key = enumeration.nextElement(); + Object svalue = snapshot.objectForKey(key); + Object rvalue = row.objectForKey(key); + if (rvalue == null) { + cancelFetch(); + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("Value for key " + key + " not found in locked row."); + } + if (!rvalue.equals(svalue)) { + cancelFetch(); + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("Value for key " + key + " differes from snapshot."); + } + } + adaptorContext().commitTransaction(); + return; + } + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("A fetch was never generated."); + } + + public void performAdaptorOperation(EOAdaptorOperation operation) { + int opcode = operation.adaptorOperator(); + switch (opcode) { + case EODatabaseOperation.AdaptorLockOperator: + if (operation.entity() == null) + throw new EOGeneralAdaptorException("A lock operation must have an entity assigned to it.", + new NSDictionary(operation, "operation")); + if (operation.qualifier() == null) + throw new EOGeneralAdaptorException("A lock operation must have a qualifier assigned to it.", + new NSDictionary(operation, "operation")); + if (operation.qualifier() == null) + throw new EOGeneralAdaptorException("A lock operation must have changedValues assigned to it.", + new NSDictionary(operation, "operation")); + lockRowComparingAttributes(operation.attributes(), operation.entity(), operation.qualifier(), operation.changedValues()); + break; + case EODatabaseOperation.AdaptorInsertOperator: + if (operation.entity() == null) + throw new EOGeneralAdaptorException("An insert operation must have an entity assigned to it.", + new NSDictionary(operation, "operation")); + if (operation.changedValues() == null) + throw new EOGeneralAdaptorException("An insert operation must have changedValues assigned to it.", + new NSDictionary(operation, "operation")); + insertRow(operation.changedValues(), operation.entity()); + break; + case EODatabaseOperation.AdaptorUpdateOperator: + if (operation.entity() == null) + throw new EOGeneralAdaptorException("An update operation must have an entity assigned to it.", + new NSDictionary(operation, "operation")); + if (operation.changedValues() == null) + throw new EOGeneralAdaptorException("An update operation must have changedValues assigned to it.", + new NSDictionary(operation, "operation")); + updateValuesInRowsDescribedByQualifier(operation.changedValues(), operation.qualifier(), operation.entity()); + break; + case EODatabaseOperation.AdaptorDeleteOperator: + if (operation.entity() == null) + throw new EOGeneralAdaptorException("A delete operation must have an entity assigned to it.", + new NSDictionary(operation, "operation")); + deleteRowsDescribedByQualifier(operation.qualifier(), operation.entity()); + break; + case EODatabaseOperation.AdaptorStoredProcedureOperator: + if (operation.storedProcedure() == null) + throw new EOGeneralAdaptorException("A stored procedure operation must have a stored procedure assigned to it.", + new NSDictionary(operation, "operation")); + executeStoredProcedure(operation.storedProcedure(), operation.changedValues()); + break; + default: + throw new EOGeneralAdaptorException("I don't know how to perform an operation with code " + opcode, + new NSDictionary(operation, "operation")); + } + } + + public void performAdaptorOperations(NSArray ops) { + for (int i = 0; i < ops.count(); i++) { + EOAdaptorOperation adop = (EOAdaptorOperation)ops.objectAtIndex(i); + performAdaptorOperation(adop); + } + } + + public NSArray primaryKeysForNewRowsWithEntity(int count, EOEntity entity) { + NSDictionary[] keys = new NSDictionary[count]; + for (int i = 0; i < count; i++) + keys[i] = NSDictionary.EmptyDictionary; + return new NSArray(keys); + } + + public void updateValuesInRowDescribedByQualifier(NSDictionary row, EOQualifier q, EOEntity entity) { + adaptorContext().beginTransaction(); + int count = updateValuesInRowsDescribedByQualifier(row, q, entity); + if (count != 1) { + adaptorContext().rollbackTransaction(); + throw new EOGeneralAdaptorException("The qualifier should describe exactly one row (updated " + count + " rows)"); + } + adaptorContext().commitTransaction(); + } + +} +/* + * $Log$ + * Revision 1.2 2006/02/16 16:47:14 cgruber + * Move some classes in to "internal" packages and re-work imports, etc. + * + * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions. + * + * Revision 1.1 2006/02/16 13:19:57 cgruber + * Check in all sources in eclipse-friendly maven-enabled packages. + * + * Revision 1.2 2005/05/11 15:21:53 cgruber + * Change enum to enumeration, since enum is now a keyword as of Java 5.0 + * + * A few other comments in the code. + * + * Revision 1.1 2003/08/13 00:37:45 chochos + * an almost complete implementation of the abstract adaptor-layer classes + * + */ \ No newline at end of file -- cgit v1.2.3