use of org.apache.cayenne.project.Project 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]);
}
use of org.apache.cayenne.project.Project 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.project.Project in project cayenne by apache.
the class ProjectFileChangeTracker method reconfigure.
/**
* Reloads files to watch from the project. Useful when project's structure
* has changed
*/
public void reconfigure() {
pauseWatching();
removeAllFiles();
Project project = mediator.getProject();
// check if project exists and has been saved at least once.
if (project != null && project.getConfigurationResource() != null) {
String projectPath = project.getConfigurationResource().getURL().getPath() + File.separator;
addFile(projectPath);
Iterator<DataMap> it = ((DataChannelDescriptor) project.getRootNode()).getDataMaps().iterator();
while (it.hasNext()) {
DataMap dm = it.next();
if (dm.getConfigurationSource() != null) {
// if DataMap is in separate file, monitor it
addFile(dm.getConfigurationSource().getURL().getPath());
}
}
}
resumeWatching();
}
use of org.apache.cayenne.project.Project in project cayenne by apache.
the class GenerateCodeAction method performAction.
public void performAction(ActionEvent e) {
Collection<DataMap> dataMaps;
DataMap dataMap = getProjectController().getCurrentDataMap();
if (dataMap != null) {
dataMaps = new ArrayList<>();
dataMaps.add(dataMap);
new CodeGeneratorController(getApplication().getFrameController(), dataMaps).startup();
} else if (getProjectController().getCurrentDataNode() != null) {
Collection<String> nodeMaps = getProjectController().getCurrentDataNode().getDataMapNames();
Project project = getProjectController().getProject();
dataMaps = ((DataChannelDescriptor) project.getRootNode()).getDataMaps();
Collection<DataMap> resultMaps = new ArrayList<>();
for (DataMap map : dataMaps) {
if (nodeMaps.contains(map.getName())) {
resultMaps.add(map);
}
}
new CodeGeneratorController(getApplication().getFrameController(), resultMaps).startup();
} else {
Project project = getProjectController().getProject();
dataMaps = ((DataChannelDescriptor) project.getRootNode()).getDataMaps();
new CodeGeneratorController(getApplication().getFrameController(), dataMaps).startup();
}
}
use of org.apache.cayenne.project.Project in project cayenne by apache.
the class GenerateDBAction method performAction.
public void performAction(ActionEvent e) {
Collection<DataMap> dataMaps;
DataMap dataMap = getProjectController().getCurrentDataMap();
if (dataMap != null) {
dataMaps = new ArrayList<>();
dataMaps.add(dataMap);
} else {
Project project = getProjectController().getProject();
dataMaps = ((DataChannelDescriptor) project.getRootNode()).getDataMaps();
}
new DBGeneratorOptions(getProjectController(), "Generate DB Schema: Options", dataMaps).startupAction();
}
Aggregations