Search in sources :

Example 26 with DataRow

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()));
}
Also used : DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 27 with DataRow

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 28 with DataRow

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 29 with DataRow

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;
            }
        }
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) BatchQuery(org.apache.cayenne.query.BatchQuery) DataRow(org.apache.cayenne.DataRow)

Example 30 with DataRow

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());
        }
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5