Search in sources :

Example 36 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class CompatibilityUpgradeServiceIT method testUpgradeStandAloneDataMapDom.

@Test
public void testUpgradeStandAloneDataMapDom() throws Exception {
    Injector injector = getInjector();
    CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) injector.getInstance(UpgradeService.class);
    DocumentProvider documentProvider = injector.getInstance(DocumentProvider.class);
    URL dataMapUrl = getClass().getResource("test-map-v6.map.xml");
    Document dataMapDocument = documentProvider.getDocument(dataMapUrl);
    assertNull(dataMapDocument);
    Resource resource = new URLResource(dataMapUrl);
    upgradeService.upgradeDataMap(resource);
    dataMapDocument = documentProvider.getDocument(dataMapUrl);
    assertNotNull(dataMapDocument);
    assertEquals("10", dataMapDocument.getDocumentElement().getAttribute("project-version"));
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) UpgradeService(org.apache.cayenne.project.upgrade.UpgradeService) Injector(org.apache.cayenne.di.Injector) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) Document(org.w3c.dom.Document) URL(java.net.URL) Test(org.junit.Test)

Example 37 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DefaultDbImportAction method newTargetDataMap.

protected DataMap newTargetDataMap(DbImportConfiguration config) throws IOException {
    DataMap dataMap = new DataMap();
    dataMap.setName(config.getDataMapName());
    dataMap.setConfigurationSource(new URLResource(config.getTargetDataMap().toURI().toURL()));
    dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
    // update map defaults
    // do not override default package of existing DataMap unless it is
    // explicitly requested by the plugin caller
    String defaultPackage = config.getDefaultPackage();
    if (defaultPackage != null && defaultPackage.length() > 0) {
        dataMap.setDefaultPackage(defaultPackage);
    }
    CatalogFilter[] catalogs = config.getDbLoaderConfig().getFiltersConfig().getCatalogs();
    if (catalogs.length > 0) {
        // do not override default catalog of existing DataMap unless it is
        // explicitly requested by the plugin caller, and the provided catalog is
        // not a pattern
        String catalog = catalogs[0].name;
        if (catalog != null && catalog.length() > 0 && catalog.indexOf('%') < 0) {
            dataMap.setDefaultCatalog(catalog);
        }
        // do not override default schema of existing DataMap unless it is
        // explicitly requested by the plugin caller, and the provided schema is
        // not a pattern
        String schema = catalogs[0].schemas[0].name;
        if (schema != null && schema.length() > 0 && schema.indexOf('%') < 0) {
            dataMap.setDefaultSchema(schema);
        }
    }
    return dataMap;
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) CatalogFilter(org.apache.cayenne.dbsync.reverse.filters.CatalogFilter) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap)

Example 38 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DBCP2DataSourceFactoryTest method testGetDataSource_InvalidLocation.

@Test
public void testGetDataSource_InvalidLocation() throws Exception {
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/");
    assertNotNull(url);
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setConfigurationSource(new URLResource(url));
    nodeDescriptor.setParameters("testDBCP2.properties.nosuchfile");
    DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
    try {
        factory.getDataSource(nodeDescriptor);
        fail("didn't throw on absent config file");
    } catch (IOException ex) {
    // expected
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 39 with URLResource

use of org.apache.cayenne.resource.URLResource 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 40 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class SaveAsAction method saveAll.

/**
 * Saves project and related files. Saving is done to temporary files, and
 * only on successful save, master files are replaced with new versions.
 */
protected boolean saveAll() throws Exception {
    Project p = getCurrentProject();
    String oldPath = null;
    if (p.getConfigurationResource() != null) {
        oldPath = p.getConfigurationResource().getURL().getPath();
    }
    File projectDir = fileChooser.newProjectDir(Application.getFrame(), p);
    if (projectDir == null) {
        return false;
    }
    if (projectDir.exists() && !projectDir.canWrite()) {
        JOptionPane.showMessageDialog(Application.getFrame(), "Can't save project - unable to write to file \"" + projectDir.getPath() + "\"", "Can't Save Project", JOptionPane.OK_OPTION);
        return false;
    }
    getProjectController().getFileChangeTracker().pauseWatching();
    URLResource res = new URLResource(projectDir.toURI().toURL());
    ProjectSaver saver = getApplication().getInjector().getInstance(ProjectSaver.class);
    boolean isNewProject = p.getConfigurationResource() == null;
    Preferences tempOldPref = null;
    if (isNewProject) {
        tempOldPref = getApplication().getMainPreferenceForProject();
    }
    saver.saveAs(p, res);
    if (oldPath != null && oldPath.length() != 0 && !oldPath.equals(p.getConfigurationResource().getURL().getPath())) {
        String newName = p.getConfigurationResource().getURL().getPath().replace(".xml", "");
        String oldName = oldPath.replace(".xml", "");
        Preferences oldPref = getProjectController().getPreferenceForProject();
        String projPath = oldPref.absolutePath().replace(oldName, "");
        Preferences newPref = getProjectController().getPreferenceForProject().node(projPath + newName);
        RenamedPreferences.copyPreferences(newPref, getProjectController().getPreferenceForProject(), false);
    } else if (isNewProject) {
        if (tempOldPref != null) {
            String newProjectName = getApplication().getNewProjectTemporaryName();
            if (tempOldPref.absolutePath().contains(newProjectName)) {
                String projPath = tempOldPref.absolutePath().replace("/" + newProjectName, "");
                String newName = p.getConfigurationResource().getURL().getPath().replace(".xml", "");
                Preferences newPref = getApplication().getMainPreferenceForProject().node(projPath + newName);
                RenamedPreferences.copyPreferences(newPref, tempOldPref, false);
                tempOldPref.removeNode();
            }
        }
    }
    RenamedPreferences.removeNewPreferences();
    File file = new File(p.getConfigurationResource().getURL().toURI());
    getApplication().getFrameController().addToLastProjListAction(file);
    Application.getFrame().fireRecentFileListChanged();
    // Reset the watcher now
    getProjectController().getFileChangeTracker().reconfigure();
    return true;
}
Also used : Project(org.apache.cayenne.project.Project) URLResource(org.apache.cayenne.resource.URLResource) ProjectSaver(org.apache.cayenne.project.ProjectSaver) Preferences(java.util.prefs.Preferences) RenamedPreferences(org.apache.cayenne.pref.RenamedPreferences) File(java.io.File)

Aggregations

URLResource (org.apache.cayenne.resource.URLResource)40 URL (java.net.URL)31 Test (org.junit.Test)29 DataMap (org.apache.cayenne.map.DataMap)19 File (java.io.File)12 Injector (org.apache.cayenne.di.Injector)12 Resource (org.apache.cayenne.resource.Resource)11 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)10 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)7 XMLDataChannelDescriptorLoader (org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader)5 XMLDataMapLoader (org.apache.cayenne.configuration.xml.XMLDataMapLoader)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 ConfigurationNameMapper (org.apache.cayenne.configuration.ConfigurationNameMapper)4 DefaultConfigurationNameMapper (org.apache.cayenne.configuration.DefaultConfigurationNameMapper)4 DataChannelMetaData (org.apache.cayenne.configuration.xml.DataChannelMetaData)4 DefaultHandlerFactory (org.apache.cayenne.configuration.xml.DefaultHandlerFactory)4 HandlerFactory (org.apache.cayenne.configuration.xml.HandlerFactory)4 NoopDataChannelMetaData (org.apache.cayenne.configuration.xml.NoopDataChannelMetaData)4 XMLReaderProvider (org.apache.cayenne.configuration.xml.XMLReaderProvider)4 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)4