Search in sources :

Example 1 with ObjectContext

use of org.apache.cayenne.ObjectContext in project CRMWebApp by jshioya0506.

the class Object2DBTable method registerdTable.

/**
 * テーブルにデータを登録する
 * @param tableName テーブル名
 * @param dataMaps データマップ
 */
public void registerdTable(String tableName, List<Map<String, Object>> dataMaps) {
    // DBの設定情報を取得
    ServerRuntime cayenneRuntime = new ServerRuntime("cayenne-NexusCRM.xml");
    ObjectContext context = cayenneRuntime.getContext();
    // テーブルオブジェクトにデータを設定し、テーブルにデータを登録
    for (Map<String, Object> dataMap : dataMaps) {
        // データ登録対象のテーブルオブジェクトを取得
        CayenneDataObject dataObj = CayenneDataObjectUtil.findDataObject(context, tableName);
        // テーブルオブジェクトにデータを設定
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            dataObj.writeProperty(entry.getKey(), entry.getValue());
        }
        // データ登録
        context.commitChanges();
    }
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) CayenneDataObject(org.apache.cayenne.CayenneDataObject) ObjectContext(org.apache.cayenne.ObjectContext) Map(java.util.Map)

Example 2 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ClientServerChannelIT method testOnQuery.

@Test
public void testOnQuery() {
    final boolean[] genericDone = new boolean[1];
    MockDataChannel parent = new MockDataChannel(new EntityResolver()) {

        @Override
        public QueryResponse onQuery(ObjectContext context, Query query) {
            genericDone[0] = true;
            return super.onQuery(context, query);
        }
    };
    DataContext context = (DataContext) runtime.newContext(parent);
    QueryMessage message = new QueryMessage(new MockQuery());
    new ClientServerChannel(context).onQuery(null, message.getQuery());
    assertTrue(genericDone[0]);
}
Also used : QueryMessage(org.apache.cayenne.remote.QueryMessage) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) EntityResolver(org.apache.cayenne.map.EntityResolver) ObjectContext(org.apache.cayenne.ObjectContext) MockQuery(org.apache.cayenne.query.MockQuery) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Example 3 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedSimpleProperty.

@Test
public void testPeerObjectUpdatedSimpleProperty() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    context.commitChanges();
    ObjectContext peer1 = runtime.newContext(context);
    Artist a1 = peer1.localObject(a);
    final ObjectContext peer2 = runtime.newContext(context);
    final Artist a2 = peer2.localObject(a);
    a1.setArtistName("Y");
    assertEquals("X", a2.getArtistName());
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals("Y", a2.getArtistName());
            assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 4 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedTempOID.

@Test
public void testPeerObjectUpdatedTempOID() throws Exception {
    ObjectContext peer1 = runtime.newContext(context);
    final Artist a1 = peer1.newObject(Artist.class);
    a1.setArtistName("Y");
    ObjectId a1TempId = a1.getObjectId();
    assertTrue(a1TempId.isTemporary());
    ObjectContext peer2 = runtime.newContext(context);
    final Artist a2 = peer2.localObject(a1);
    assertEquals(a1TempId, a2.getObjectId());
    peer1.commitChanges();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertFalse(a1.getObjectId().isTemporary());
            assertFalse(a2.getObjectId().isTemporary());
            assertEquals(a2.getObjectId(), a1.getObjectId());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 5 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.

@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    Painting px = context.newObject(Painting.class);
    px.setToArtist(a);
    px.setPaintingTitle("PX");
    Painting py = context.newObject(Painting.class);
    py.setPaintingTitle("PY");
    context.commitChanges();
    // pause to let the context events propagate
    Thread.sleep(500);
    ObjectContext peer1 = runtime.newContext(context);
    Painting py1 = peer1.localObject(py);
    Artist a1 = peer1.localObject(a);
    final ObjectContext peer2 = runtime.newContext(context);
    final Painting py2 = peer2.localObject(py);
    final Artist a2 = peer2.localObject(a);
    a1.addToPaintingArray(py1);
    assertEquals(1, a2.getPaintingArray().size());
    assertFalse(a2.getPaintingArray().contains(py2));
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals(2, a2.getPaintingArray().size());
            assertTrue(a2.getPaintingArray().contains(py2));
            assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)127 Test (org.junit.Test)116 Artist (org.apache.cayenne.testdo.testmap.Artist)35 Painting (org.apache.cayenne.testdo.testmap.Painting)14 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 Table1 (org.apache.cayenne.crypto.db.Table1)10 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 ObjectId (org.apache.cayenne.ObjectId)8 GraphDiff (org.apache.cayenne.graph.GraphDiff)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 IvImpl (org.apache.cayenne.testdo.inheritance_vertical.IvImpl)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 QueryResponse (org.apache.cayenne.QueryResponse)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4