001: package net.sourceforge.jaxor;
002:
003: import net.sourceforge.jaxor.api.EntityInterface;
004: import net.sourceforge.jaxor.impl.InsertEntityChange;
005: import net.sourceforge.jaxor.impl.UpdateEntityChange;
006: import net.sourceforge.jaxor.impl.DeleteEntityChange;
007:
008: import java.io.Serializable;
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.SQLException;
012:
013: /*
014: * User: Mike
015: * Date: Jan 9, 2003
016: * Time: 7:58:59 PM
017: */
018:
019: public abstract class EntityChange implements Comparable, Serializable {
020:
021: private final int changeNumber;
022: private final EntityInterface _entity;
023: private final SqlAdapter _sqlAdapter;
024:
025: public EntityChange(EntityInterface entity) {
026: this (-1, entity);
027: }
028:
029: public EntityChange(int changeNumber, EntityInterface entity) {
030: this .changeNumber = changeNumber;
031: _entity = entity;
032: _sqlAdapter = new SqlAdapter(_entity);
033: }
034:
035: public SqlAdapter getSqlAdapter() {
036: return _sqlAdapter;
037: }
038:
039: public static EntityChange update(int changeNumber,
040: final EntityInterface entity) {
041: return new UpdateEntityChange(changeNumber, entity);
042: }
043:
044: public static EntityChange insert(int changeNumber,
045: final EntityInterface entity) {
046: return new InsertEntityChange(changeNumber, entity);
047: }
048:
049: public static EntityChange delete(int changeNumber,
050: final EntityInterface entity) {
051: return new DeleteEntityChange(changeNumber, entity);
052: }
053:
054: public int compareTo(Object o) {
055: EntityChange entityChange = ((EntityChange) o);
056: if (equals(o))
057: return 0;
058: return changeNumber - entityChange.changeNumber;
059: }
060:
061: public boolean equals(Object o) {
062: EntityChange entityChange = ((EntityChange) o);
063: return equalClasses(o)
064: && entityChange.getEntity().equals(this .getEntity());
065: }
066:
067: private boolean equalClasses(Object o) {
068: return getClass().equals(o.getClass());
069: }
070:
071: public EntityInterface getEntity() {
072: return _entity;
073: }
074:
075: public boolean canBatchWithLast(EntityChange last) {
076: if (last == null)
077: return false;
078: if (!isBatchable())
079: return false;
080: if (!equalClasses(last))
081: return false;
082: return getEntity().getImplementationClass().equals(
083: last.getEntity().getImplementationClass());
084: }
085:
086: public abstract void beforeFlush();
087:
088: public abstract void flush(Connection conn);
089:
090: public abstract void afterFlush();
091:
092: public abstract PreparedStatement execute(Connection conn)
093: throws SQLException;
094:
095: public abstract boolean isBatchable();
096:
097: public abstract void addToBatch(PreparedStatement stmt)
098: throws SQLException;
099:
100: public String toString() {
101: return _entity.toString();
102: }
103:
104: /**
105: * Used to execute an insert,update, or delete for a single entity and
106: * not within a unit of work.
107: * @param connection
108: */
109: public void executeSingleChange(Connection connection) {
110: beforeFlush();
111: flush(connection);
112: afterFlush();
113: }
114:
115: }
|