Search in sources :

Example 1 with Project

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]);
}
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 2 with Project

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.");
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 3 with Project

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();
}
Also used : Project(org.apache.cayenne.project.Project) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with Project

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();
    }
}
Also used : Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) CodeGeneratorController(org.apache.cayenne.modeler.dialog.codegen.CodeGeneratorController) Collection(java.util.Collection) DataMap(org.apache.cayenne.map.DataMap)

Example 5 with Project

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();
}
Also used : Project(org.apache.cayenne.project.Project) DBGeneratorOptions(org.apache.cayenne.modeler.dialog.db.gen.DBGeneratorOptions) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

Project (org.apache.cayenne.project.Project)14 DataMap (org.apache.cayenne.map.DataMap)7 File (java.io.File)4 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)3 URLResource (org.apache.cayenne.resource.URLResource)3 DataMapBuilder (org.apache.cayenne.dbsync.merge.builders.DataMapBuilder)2 DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)2 DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)2 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)2 DefaultModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate)2 DbEntity (org.apache.cayenne.map.DbEntity)2 CayenneModelerController (org.apache.cayenne.modeler.CayenneModelerController)2 FileProjectSaver (org.apache.cayenne.project.FileProjectSaver)2 ProjectSaver (org.apache.cayenne.project.ProjectSaver)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 Preferences (java.util.prefs.Preferences)1 ConfigurationNode (org.apache.cayenne.configuration.ConfigurationNode)1 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)1