Search in sources :

Example 1 with DataMapLoader

use of org.apache.cayenne.configuration.DataMapLoader in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithoutChanges.

@Test
public void testImportWithoutChanges() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.getTargetDataMap()).thenReturn(FILE_STUB);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap dataMap = new DataMap();
            new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory()));
            return dataMap;
        }
    };
    FileProjectSaver projectSaver = mock(FileProjectSaver.class);
    doNothing().when(projectSaver).save(any(Project.class));
    DataMapLoader mapLoader = mock(DataMapLoader.class);
    when(mapLoader.load(any(Resource.class))).thenReturn(new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory())).build());
    DefaultDbImportAction action = buildDbImportAction(projectSaver, mapLoader, dbLoader);
    action.execute(config);
    // no changes - we still
    verify(projectSaver, never()).save(any(Project.class));
    verify(mapLoader, times(1)).load(any(Resource.class));
}
Also used : Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 2 with DataMapLoader

use of org.apache.cayenne.configuration.DataMapLoader in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithFieldChanged.

@Test
public void testImportWithFieldChanged() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.getTargetDataMap()).thenReturn(FILE_STUB);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap dataMap = new DataMap();
            new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("NAME_01").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME")));
            return dataMap;
        }
    };
    final boolean[] haveWeTriedToSave = { false };
    DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {

        @Override
        public void save(Project project) {
            haveWeTriedToSave[0] = true;
            // Validation phase
            DataMap rootNode = (DataMap) project.getRootNode();
            assertEquals(1, rootNode.getObjEntities().size());
            assertEquals(1, rootNode.getDbEntityMap().size());
            DbEntity entity = rootNode.getDbEntity("ARTGROUP");
            assertNotNull(entity);
            assertEquals(4, entity.getAttributes().size());
            assertNotNull(entity.getAttribute("NAME_01"));
        }
    }, new DataMapLoader() {

        @Override
        public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
            return new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME"))).build();
        }
    }, dbLoader);
    action.execute(config);
    assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Also used : FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) DbEntity(org.apache.cayenne.map.DbEntity) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Example 3 with DataMapLoader

use of org.apache.cayenne.configuration.DataMapLoader in project cayenne by apache.

the class CompatibilityDataMapLoaderIT method testLoad.

@Test
public void testLoad() throws Exception {
    Injector injector = getInjector();
    DataMapLoader loader = injector.getInstance(DataMapLoader.class);
    assertTrue(loader instanceof CompatibilityDataMapLoader);
    URL resourceUrl = getClass().getResource("../../project/compatibility/test-map-v6.map.xml");
    Resource resource = new URLResource(resourceUrl);
    DataMap dataMap = loader.load(resource);
    assertNotNull(dataMap);
    assertEquals(1, dataMap.getDbEntities().size());
    assertEquals(1, dataMap.getObjEntities().size());
    assertNotNull(dataMap.getObjEntity("Artist"));
    assertNotNull(dataMap.getDbEntity("Artist"));
    assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 4 with DataMapLoader

use of org.apache.cayenne.configuration.DataMapLoader in project cayenne by apache.

the class ImportDataMapAction method importDataMap.

protected void importDataMap() {
    File dataMapFile = selectDataMap(Application.getFrame());
    if (dataMapFile == null) {
        return;
    }
    DataMap newMap;
    try {
        URL url = dataMapFile.toURI().toURL();
        DataMapLoader loader = application.getInjector().getInstance(DataMapLoader.class);
        newMap = loader.load(new URLResource(url));
        ConfigurationNode root = getProjectController().getProject().getRootNode();
        newMap.setName(NameBuilder.builder(newMap, root).baseName(newMap.getName()).name());
        Resource baseResource = ((DataChannelDescriptor) root).getConfigurationSource();
        if (baseResource != null) {
            Resource dataMapResource = baseResource.getRelativeResource(nameMapper.configurationLocation(newMap));
            newMap.setConfigurationSource(dataMapResource);
        }
        getProjectController().addDataMap(this, newMap);
    } catch (Exception ex) {
        logObj.info("Error importing DataMap.", ex);
        JOptionPane.showMessageDialog(Application.getFrame(), "Error reading DataMap: " + ex.getMessage(), "Can't Open DataMap", JOptionPane.OK_OPTION);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) File(java.io.File) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap)

Example 5 with DataMapLoader

use of org.apache.cayenne.configuration.DataMapLoader in project cayenne by apache.

the class CayenneGeneratorMapLoaderAction method getMainDataMap.

DataMap getMainDataMap() throws MalformedURLException {
    if (mainDataMap == null) {
        DataMapLoader loader = createLoader();
        DataMap mainDataMap = loadDataMap(loader, mainDataMapFile);
        if (additionalDataMapFiles != null) {
            EntityResolver entityResolver = new EntityResolver();
            entityResolver.addDataMap(mainDataMap);
            mainDataMap.setNamespace(entityResolver);
            for (File additionalDataMapFile : additionalDataMapFiles) {
                DataMap dataMap = loadDataMap(loader, additionalDataMapFile);
                entityResolver.addDataMap(dataMap);
                dataMap.setNamespace(entityResolver);
            }
        }
        this.mainDataMap = mainDataMap;
    }
    return mainDataMap;
}
Also used : DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) EntityResolver(org.apache.cayenne.map.EntityResolver) File(java.io.File) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)7 URLResource (org.apache.cayenne.resource.URLResource)6 DataMap (org.apache.cayenne.map.DataMap)5 Resource (org.apache.cayenne.resource.Resource)5 Test (org.junit.Test)5 URL (java.net.URL)3 DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)3 FileProjectSaver (org.apache.cayenne.project.FileProjectSaver)3 Project (org.apache.cayenne.project.Project)3 File (java.io.File)2 DataMapBuilder (org.apache.cayenne.dbsync.merge.builders.DataMapBuilder)2 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)2 DefaultModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate)2 SQLException (java.sql.SQLException)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 ConfigurationNode (org.apache.cayenne.configuration.ConfigurationNode)1 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)1 DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)1 Injector (org.apache.cayenne.di.Injector)1 DbEntity (org.apache.cayenne.map.DbEntity)1