Copy the observable aspect of GenericEntity. Includes unit tests. Also added some FIXME comments.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1470676 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian Crum
2013-04-22 19:38:06 +00:00
parent eb60f61759
commit bdc155f896
3 changed files with 29 additions and 2 deletions
@@ -207,6 +207,7 @@ public class GenericEntity implements Map<String, Object>, LocalizedMap<Object>,
if (value.fields != null) this.fields.putAll(value.fields);
this.delegatorName = value.delegatorName;
this.internalDelegator = value.internalDelegator;
this.observable = new Observable(value.observable);
}
public void reset() {
@@ -232,11 +233,13 @@ public class GenericEntity implements Map<String, Object>, LocalizedMap<Object>,
if (!thisPK.equals(newPK)) {
throw new GenericEntityException("Could not refresh value, new value did not have the same primary key; this PK=" + thisPK + ", new value PK=" + newPK);
}
// FIXME: This is dangerous - two instances sharing a common field Map is a bad idea.
this.fields = newValue.fields;
this.setDelegator(newValue.getDelegator());
this.generateHashCode = newValue.generateHashCode;
this.cachedHashCode = newValue.cachedHashCode;
this.modified = false;
this.observable = new Observable(newValue.observable);
}
public boolean isModified() {
@@ -46,9 +46,11 @@ public class GenericValue extends GenericEntity {
public static final GenericValue NULL_VALUE = new NullGenericValue();
/** Map to cache various related entity collections */
// FIXME: This is a bad idea. Another process could change the related values after they are added to the Map.
public transient Map<String, List<GenericValue>> relatedCache = null;
/** Map to cache various related cardinality one entity collections */
// FIXME: This is a bad idea. Another process could change the related values after they are added to the Map.
public transient Map<String, GenericValue> relatedOneCache = null;
/** This Map will contain the original field values from the database iff
@@ -146,6 +148,7 @@ public class GenericValue extends GenericEntity {
* values from the Db.
*/
public void copyOriginalDbValues() {
// FIXME: There is no guarantee this.fields was not modified.
this.originalDbValues = new HashMap<String, Object>();
this.originalDbValues.putAll(this.fields);
}
@@ -29,6 +29,8 @@ import java.util.List;
import java.util.Map;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.Observable;
import org.ofbiz.base.util.Observer;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilXml;
@@ -95,9 +97,17 @@ public class EntityTestSuite extends EntityTestCase {
// retrieve a sample GenericValue, make sure it's correct
GenericValue testValue = delegator.findOne("TestingType", false, "testingTypeId", "TEST-1");
assertEquals("Retrieved value has the correct description", "Testing Type #1", testValue.getString("description"));
// now update and store it
// Test Observable aspect
TestObserver observer = new TestObserver();
testValue.addObserver(observer);
testValue.put("description", "New Testing Type #1");
assertEquals("Observer called with original GenericValue field name", "description", observer.arg);
observer.observable = null;
observer.arg = null;
GenericValue clonedValue = (GenericValue) testValue.clone();
clonedValue.put("description", "New Testing Type #1");
assertEquals("Observer called with cloned GenericValue field name", "description", observer.arg);
// now store it
testValue.store();
// now retrieve it again and make sure that the updated value is correct
@@ -848,4 +858,15 @@ public class EntityTestSuite extends EntityTestCase {
testType = delegator.findOne("TestingType", UtilMisc.toMap("testingTypeId", "JUNIT-TEST2"), false);
assertNull("Delete TestingType 2", testType);
}
private final class TestObserver implements Observer {
private Observable observable;
private Object arg;
@Override
public void update(Observable observable, Object arg) {
this.observable = observable;
this.arg = arg;
}
}
}