001: package net.sourceforge.jaxor;
002:
003: import net.sourceforge.jaxor.api.EntityInterface;
004: import net.sourceforge.jaxor.api.FieldAdapter;
005: import net.sourceforge.jaxor.api.JaxorContext;
006: import net.sourceforge.jaxor.api.ValueChangeListener;
007: import net.sourceforge.jaxor.impl.FirstTimeValueChangeListener;
008: import net.sourceforge.jaxor.impl.UpdateEntityChange;
009: import net.sourceforge.jaxor.impl.DeleteEntityChange;
010: import net.sourceforge.jaxor.impl.InsertEntityChange;
011: import net.sourceforge.jaxor.util.ObjectUtils;
012: import net.sourceforge.jaxor.util.ForeignFieldLazyLoader;
013:
014: import java.sql.Connection;
015: import java.sql.SQLException;
016: import java.util.List;
017: import java.util.Iterator;
018:
019: public abstract class AbstractEntity implements EntityInterface,
020: ValueChangeListener {
021:
022: private MetaRow _metaRow;
023: //flag to protect against circular references during deletion
024: private boolean _isCascadingDelete;
025: private boolean _isRegisteredForDelete;
026:
027: public void setJaxorContext(JaxorContext conn) {
028: getFields().setJaxorContext(conn);
029: }
030:
031: public JaxorContext getJaxorContext() {
032: return getFields().getJaxorContext();
033: }
034:
035: public boolean registerChange(Object newValue, Object oldValue,
036: FieldAdapter mapper) {
037: if (!ObjectUtils.equals(newValue, oldValue)
038: && getJaxorContext() != null) {
039: registerChange();
040: return true;
041: }
042: return false;
043: }
044:
045: public void registerChange() {
046: getJaxorContext().registerUpdate(this );
047: }
048:
049: public void registerLoad() {
050: getFields()
051: .registerLoad(new FirstTimeValueChangeListener(this ));
052: }
053:
054: public void unregister() {
055: getFields().unregisterListener(this );
056: }
057:
058: /**
059: * Registers this entity for deletion with the UnitOfWork.
060: */
061: public void registerDelete() {
062: if (!_isRegisteredForDelete) {
063: getJaxorContext().registerDelete(this );
064: _isRegisteredForDelete = true;
065: }
066: }
067:
068: /**
069: * Registers this entity and all reference foreign entities for deletion.
070: */
071: public void registerCascadingDelete() {
072: if (_isCascadingDelete)//recursive call
073: return;
074: registerDelete();
075: _isCascadingDelete = true;
076: try {
077: List foreignFields = getFields().getForeignFields();
078: for (Iterator iterator = foreignFields.iterator(); iterator
079: .hasNext();) {
080: ForeignFieldLazyLoader foreign = (ForeignFieldLazyLoader) iterator
081: .next();
082: List entities = foreign.getFieldAsList();
083: for (Iterator entityIterator = entities.iterator(); entityIterator
084: .hasNext();) {
085: EntityInterface entity = (EntityInterface) entityIterator
086: .next();
087: entity.registerCascadingDelete();
088: }
089: }
090: } finally {
091: _isCascadingDelete = false;
092: }
093: }
094:
095: public void validate() {
096: getFields().validate();
097: }
098:
099: public String toString() {
100: return getFields().toString();
101: }
102:
103: public int hashCode() {
104: return getPrimaryKeySet().hashCode();
105: }
106:
107: public boolean equals(Object obj) {
108: if (obj == this )
109: return true;
110: if (obj instanceof EntityInterface)
111: return equals((EntityInterface) obj);
112: return false;
113: }
114:
115: /**
116: * We are using dynamic proxies, so if we compare classes we will get the proxy class.
117: * We need the actual class instance.
118: * @return
119: */
120: public Class getImplementationClass() {
121: return getClass();
122: }
123:
124: public boolean equals(EntityInterface enity) {
125: if (!getImplementationClass().equals(
126: enity.getImplementationClass()))
127: return false;
128:
129: PrimaryKeySet pkset = getPrimaryKeySet();
130: if (pkset.equals(enity.getPrimaryKeySet()))
131: return true;
132: return enity == this ;
133: }
134:
135: public PrimaryKeySet getPrimaryKeySet() {
136: return getFields().getPrimaryKey();
137: }
138:
139: public void insert() {
140: execute(new InsertEntityChange(this ));
141: }
142:
143: public void delete() {
144: execute(new DeleteEntityChange(this ));
145: }
146:
147: public void update() {
148: execute(new UpdateEntityChange(this ));
149: }
150:
151: private void execute(EntityChange entityChange) {
152: Connection connection = getJaxorContext().getConnection();
153: try {
154: entityChange.executeSingleChange(connection);
155: } finally {
156: try {
157: connection.close();
158: } catch (SQLException e) {
159: e.printStackTrace();
160: }
161: }
162: }
163:
164: public void setMetaRow(MetaRow meta) {
165: _metaRow = meta;
166: }
167:
168: public MetaRow getMetaRow() {
169: return _metaRow;
170: }
171:
172: }
|