use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextExtrasIT method testIdObjectFromDataRow.
@Test
public void testIdObjectFromDataRow() {
DataRow row = new DataRow(10);
row.put("ARTIST_ID", 100000);
DataObject obj = context.objectFromDataRow(Artist.class, row);
assertNotNull(obj);
assertTrue(context.getGraphManager().registeredNodes().contains(obj));
assertEquals(PersistenceState.HOLLOW, obj.getPersistenceState());
assertNull(context.getObjectStore().getCachedSnapshot(obj.getObjectId()));
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextExtrasIT method testFullObjectFromDataRow.
@Test
public void testFullObjectFromDataRow() {
DataRow row = new DataRow(10);
row.put("ARTIST_ID", 123456);
row.put("ARTIST_NAME", "ArtistXYZ");
row.put("DATE_OF_BIRTH", new Date());
Artist obj = context.objectFromDataRow(Artist.class, row);
assertTrue(context.getGraphManager().registeredNodes().contains(obj));
assertEquals(PersistenceState.COMMITTED, obj.getPersistenceState());
assertNotNull(context.getObjectStore().getCachedSnapshot(obj.getObjectId()));
assertEquals("ArtistXYZ", obj.getArtistName());
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testShouldMergeChanges.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store.
*/
@Test
public void testShouldMergeChanges() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldMergeChanges(DataObject object, DataRow snapshotInStore) {
methodInvoked[0] = true;
return true;
}
};
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertNotSame(altArtist, artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
context1.setDelegate(delegate);
// Update and save artist in peer context
artist.setArtistName("version2");
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class FlushObserver method nextGeneratedRows.
/**
* Processes generated keys.
*/
@Override
@SuppressWarnings("unchecked")
public void nextGeneratedRows(Query query, ResultIterator<?> keysIterator, List<ObjectId> idsToUpdate) {
// read and close the iterator before doing anything else
List<DataRow> keys;
try {
keys = (List<DataRow>) keysIterator.allRows();
} finally {
keysIterator.close();
}
if (!(query instanceof InsertBatchQuery)) {
throw new CayenneRuntimeException("Generated keys only supported for InsertBatchQuery, instead got %s", query);
}
if (keys.size() != idsToUpdate.size()) {
throw new CayenneRuntimeException("Mismatching number of generated PKs: expected %d, instead got %d", idsToUpdate.size(), keys.size());
}
for (int i = 0; i < keys.size(); i++) {
DataRow key = keys.get(i);
// empty key?
if (key.size() == 0) {
throw new CayenneRuntimeException("Empty key generated.");
}
ObjectId idToUpdate = idsToUpdate.get(i);
if (idToUpdate == null || !idToUpdate.isTemporary()) {
// why would this happen?
return;
}
BatchQuery batch = (BatchQuery) query;
for (DbAttribute attribute : batch.getDbEntity().getGeneratedAttributes()) {
// DB DEFAULT values. Ignore those.
if (attribute.isPrimaryKey()) {
Object value = key.get(attribute.getName());
// identity result sets, so a data row may contain garbage labels.
if (value == null) {
value = key.values().iterator().next();
}
// Log the generated PK
logger.logGeneratedKey(attribute, value);
// I guess we should override any existing value,
// as generated key is the latest thing that exists in the DB.
idToUpdate.getReplacementIdMap().put(attribute.getName(), value);
break;
}
}
}
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class PostprocessVisitor method processObjectChange.
private void processObjectChange(DbRowOp dbRow) {
if (dbRow.getChangeId().getEntityName().startsWith(ASTDbPath.DB_PREFIX)) {
return;
}
DataRow dataRow = context.currentSnapshot(dbRow.getObject());
if (dbRow.getObject() instanceof DataObject) {
DataObject dataObject = (DataObject) dbRow.getObject();
dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
dataObject.setSnapshotVersion(dataRow.getVersion());
}
if (updatedSnapshots == null) {
updatedSnapshots = new HashMap<>();
}
updatedSnapshots.put(dbRow.getObject().getObjectId(), dataRow);
// update Map reverse relationships
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(dbRow.getChangeId().getEntityName());
for (ArcProperty arc : descriptor.getMapArcProperties()) {
ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
// must resolve faults... hopefully for to-one this will not cause extra fetches...
Object source = arc.readProperty(dbRow.getObject());
if (source != null && !reverseArc.isFault(source)) {
remapTarget(reverseArc, source, dbRow.getObject());
}
}
}
Aggregations