use of org.apache.cayenne.map.DataMap in project cayenne by apache.
the class DefaultDbImportAction method saveLoaded.
/**
* Save imported data.
* This can create DataMap and/or Project files.
*/
protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException {
ConfigurationTree<ConfigurationNode> projectRoot;
if (config.getCayenneProject() == null) {
// Old version of cdbimport, no Cayenne project, need to save only DataMap
projectRoot = new ConfigurationTree<>(dataMap);
} else {
// Cayenne project is present
DataChannelDescriptor dataChannelDescriptor;
if (config.getCayenneProject().exists()) {
// Cayenne project file exists, need to read it and push DataMap inside
URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL());
ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource);
if (!configurationTree.getLoadFailures().isEmpty()) {
throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription());
}
dataChannelDescriptor = configurationTree.getRootNode();
// remove old copy of DataMap if it's there
DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName());
if (oldDataMap != null) {
dataChannelDescriptor.getDataMaps().remove(oldDataMap);
}
} else {
// No project file yet, can simply create empty project with resulting DataMap
dataChannelDescriptor = new DataChannelDescriptor();
dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName()));
dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL()));
logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath());
}
dataChannelDescriptor.getDataMaps().add(dataMap);
projectRoot = new ConfigurationTree<>(dataChannelDescriptor);
}
Project project = new Project(projectRoot);
projectSaver.save(project);
logger.info("");
logger.info("All changes saved.");
}
use of org.apache.cayenne.map.DataMap in project cayenne by apache.
the class DefaultDbImportAction method existingTargetMap.
protected DataMap existingTargetMap(DbImportConfiguration configuration) throws IOException {
File file = configuration.getTargetDataMap();
if (file != null && file.exists() && file.canRead()) {
URLResource configurationResource = new URLResource(file.toURI().toURL());
DataMap dataMap = mapLoader.load(configurationResource);
dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
dataMap.setConfigurationSource(configurationResource);
return dataMap;
}
return null;
}
use of org.apache.cayenne.map.DataMap in project cayenne by apache.
the class DataMapMergerTest method testAddRelationship1.
@Test
public void testAddRelationship1() throws Exception {
DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr01").join("rel1", "table1.attr01", "table2.attr03").build();
DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr02").join("rel1", "table1.attr01", "table2.attr03").build();
List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
assertEquals(2, tokens.size());
DbEntity entity = existing.getDbEntity("table1");
assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
entity = db.getDbEntity("table1");
assertEquals(factory().createAddRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
use of org.apache.cayenne.map.DataMap in project cayenne by apache.
the class DataMapMergerTest method testAddColumn.
@Test
public void testAddColumn() throws Exception {
DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt())).build();
DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt())).build();
List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
assertEquals(1, tokens.size());
DbEntity entity = existing.getDbEntity("table1");
assertEquals(factory().createAddColumnToDb(entity, entity.getAttribute("attr02")).getTokenValue(), tokens.get(0).getTokenValue());
}
use of org.apache.cayenne.map.DataMap in project cayenne by apache.
the class DataMapMergerTest method testAddPrimaryKey.
@Test
public void testAddPrimaryKey() throws Exception {
DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt().primaryKey())).build();
DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt())).build();
List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
assertEquals(1, tokens.size());
}
Aggregations