Search in sources :

Example 16 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DefaultValueTransformerFactory method getJavaType.

// TODO: calculating Java type of ObjAttribute may become unneeded per
// CAY-1752, as DbAttribute will have it.
protected String getJavaType(DbAttribute a) {
    DbEntity dbEntity = a.getEntity();
    DataMap dataMap = dbEntity.getDataMap();
    Collection<String> javaTypes = new HashSet<>();
    for (ObjEntity objEntity : dataMap.getMappedEntities(dbEntity)) {
        for (ObjAttribute oa : objEntity.getAttributes()) {
            // TODO: this won't pick up flattened attributes
            if (a.getName().equals(oa.getDbAttributePath())) {
                javaTypes.add(oa.getType());
            }
        }
    }
    if (javaTypes.size() != 1) {
        String javaType = TypesMapping.getJavaBySqlType(a.getType());
        String attributeName = dbEntity.getName() + "." + a.getName();
        String msg = javaTypes.size() > 1 ? "ObjAttributes with different java types" : "No ObjAttributes";
        // Warn user about this problem as there is nothing else we can do
        logger.warn(msg + " bound to DbAttribute '" + attributeName + "', " + javaType + " type will be used.");
        return javaType;
    }
    return javaTypes.iterator().next();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DataMap(org.apache.cayenne.map.DataMap) HashSet(java.util.HashSet)

Example 17 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelectOverrideModifiedCached.

@Test
public void testOnQuerySelectOverrideModifiedCached() {
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    EntityResolver resolver = new EntityResolver(entities);
    CayenneContext context = new CayenneContext();
    context.setEntityResolver(resolver);
    ObjectId oid = new ObjectId("test_entity", "x", "y");
    MockPersistentObject o1 = new MockPersistentObject(oid);
    o1.setPersistenceState(PersistenceState.MODIFIED);
    context.getGraphManager().registerNode(oid, o1);
    assertSame(o1, context.getGraphManager().getNode(oid));
    // another object with the same GID ... we must merge it with cached and return
    // cached object instead of the one fetched
    MockPersistentObject o2 = new MockPersistentObject(oid);
    MockClientConnection connection = new MockClientConnection(new GenericResponse(Arrays.asList(o2)));
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    context.setChannel(channel);
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    assertEquals(1, response.size());
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertTrue("Expected cached object, got: " + list, list.contains(o1));
    assertSame(o1, context.getGraphManager().getNode(oid));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 18 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelect.

@Test
public void testOnQuerySelect() {
    final MockPersistentObject o1 = new MockPersistentObject();
    ObjectId oid1 = new ObjectId("test_entity");
    o1.setObjectId(oid1);
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(o1));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    CayenneContext context = new CayenneContext(channel);
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    Persistent o1_1 = (Persistent) list.get(0);
    assertEquals(o1.getObjectId(), o1_1.getObjectId());
    // ObjectContext must be injected
    assertEquals(context, o1_1.getObjectContext());
    assertSame(o1_1, context.getGraphManager().getNode(oid1));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) Persistent(org.apache.cayenne.Persistent) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockPersistentObject(org.apache.cayenne.MockPersistentObject) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 19 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class EntityMergeSupport method addMissingRelationship.

private void addMissingRelationship(ObjEntity entity, DbRelationship dbRelationship) {
    // getting DataMap from DbRelationship's source entity. This is the only object in our arguments that
    // is guaranteed to be a part of the map....
    DataMap dataMap = dbRelationship.getSourceEntity().getDataMap();
    DbEntity targetEntity = dbRelationship.getTargetEntity();
    Collection<ObjEntity> mappedObjEntities = dataMap.getMappedEntities(targetEntity);
    if (mappedObjEntities.isEmpty()) {
        if (targetEntity == null) {
            targetEntity = new DbEntity(dbRelationship.getTargetEntityName());
        }
        if (dbRelationship.getTargetEntityName() != null) {
            boolean needGeneratedEntity = createObjRelationship(entity, dbRelationship, nameGenerator.objEntityName(targetEntity));
            if (needGeneratedEntity) {
                LOGGER.warn("Can't find ObjEntity for " + dbRelationship.getTargetEntityName());
                LOGGER.warn("Db Relationship (" + dbRelationship + ") will have GUESSED Obj Relationship reflection. ");
            }
        }
    } else {
        for (Entity mappedTarget : mappedObjEntities) {
            createObjRelationship(entity, dbRelationship, mappedTarget.getName());
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DataMap(org.apache.cayenne.map.DataMap)

Example 20 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DefaultDbImportAction method execute.

@Override
public void execute(DbImportConfiguration config) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DB connection: " + config.getDataSourceInfo());
        logger.debug(String.valueOf(config));
    }
    boolean hasChanges = false;
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
    DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource);
    DataMap sourceDataMap;
    try (Connection connection = dataSource.getConnection()) {
        sourceDataMap = load(config, adapter, connection);
    }
    DataMap targetDataMap = existingTargetMap(config);
    if (targetDataMap == null) {
        String path = config.getTargetDataMap() == null ? "null" : config.getTargetDataMap().getAbsolutePath() + "'";
        logger.info("");
        logger.info("Map file does not exist. Loaded db model will be saved into '" + path);
        hasChanges = true;
        targetDataMap = newTargetDataMap(config);
    }
    // transform source DataMap before merging
    transformSourceBeforeMerge(sourceDataMap, targetDataMap, config);
    MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
    DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
    Collection<MergerToken> tokens = DataMapMerger.builder(mergerTokenFactory).filters(loaderConfig.getFiltersConfig()).skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading()).skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading()).build().createMergeTokens(targetDataMap, sourceDataMap);
    tokens = log(sort(reverse(mergerTokenFactory, tokens)));
    hasChanges |= syncDataMapProperties(targetDataMap, config);
    hasChanges |= applyTokens(targetDataMap, tokens, config);
    hasChanges |= syncProcedures(targetDataMap, sourceDataMap, loaderConfig.getFiltersConfig());
    if (hasChanges) {
        saveLoaded(targetDataMap, config);
    }
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Connection(java.sql.Connection) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)

Aggregations

DataMap (org.apache.cayenne.map.DataMap)233 Test (org.junit.Test)88 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)48 DbEntity (org.apache.cayenne.map.DbEntity)48 ObjEntity (org.apache.cayenne.map.ObjEntity)45 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)22 URL (java.net.URL)21 URLResource (org.apache.cayenne.resource.URLResource)20 ArrayList (java.util.ArrayList)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)19 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)17 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)16 DataMapEvent (org.apache.cayenne.configuration.event.DataMapEvent)15 File (java.io.File)14 Procedure (org.apache.cayenne.map.Procedure)14 Embeddable (org.apache.cayenne.map.Embeddable)13 Injector (org.apache.cayenne.di.Injector)12 DbAttribute (org.apache.cayenne.map.DbAttribute)11 EntityResolver (org.apache.cayenne.map.EntityResolver)11 Entity (org.apache.cayenne.map.Entity)10