Search in sources :

Example 1 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class CompatibilityDataChannelDescriptorLoader method load.

@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
    if (configurationResource == null) {
        throw new NullPointerException("Null configurationResource");
    }
    if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
        throw new ConfigurationException("CompatibilityUpgradeService expected");
    }
    CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
    UpgradeMetaData metaData = upgradeService.getUpgradeType(configurationResource);
    if (metaData.getUpgradeType() == UpgradeType.UPGRADE_NOT_NEEDED) {
        return super.load(configurationResource);
    }
    if (metaData.getUpgradeType() == UpgradeType.DOWNGRADE_NEEDED) {
        throw new ConfigurationException("Unable to load configuration from %s: " + "It was created using a newer version of the Modeler", configurationResource.getURL());
    }
    if (metaData.getUpgradeType() == UpgradeType.INTERMEDIATE_UPGRADE_NEEDED) {
        throw new ConfigurationException("Unable to load configuration from %s: " + "Open the project in the older Modeler to do an intermediate upgrade.", configurationResource.getURL());
    }
    URL configurationURL = configurationResource.getURL();
    upgradeService.upgradeProject(configurationResource);
    Document projectDocument = documentProvider.getDocument(configurationURL);
    if (projectDocument == null) {
        throw new ConfigurationException("Unable to upgrade " + configurationURL);
    }
    logger.info("Loading XML configuration resource from " + configurationURL);
    final DataChannelDescriptor descriptor = new DataChannelDescriptor();
    descriptor.setConfigurationSource(configurationResource);
    descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource));
    try {
        DOMSource source = new DOMSource(projectDocument);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory transFactory = TransformerFactory.newInstance();
        transFactory.newTransformer().transform(source, new StreamResult(baos));
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        XMLReader parser = Util.createXmlReader();
        LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
        loaderContext.addDataMapListener(new DataMapLoaderListener() {

            @Override
            public void onDataMapLoaded(DataMap dataMap) {
                descriptor.getDataMaps().add(dataMap);
            }
        });
        DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
        parser.setContentHandler(rootHandler);
        parser.setErrorHandler(rootHandler);
        parser.parse(isource);
    } catch (Exception e) {
        throw new ConfigurationException("Error loading configuration from %s", e, configurationURL);
    }
    // Finally upgrade model, if needed
    upgradeService.upgradeModel(configurationResource, descriptor);
    return new ConfigurationTree<>(descriptor, null);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) UpgradeMetaData(org.apache.cayenne.project.upgrade.UpgradeMetaData) URL(java.net.URL) ConfigurationException(org.apache.cayenne.ConfigurationException) DataMap(org.apache.cayenne.map.DataMap) ConfigurationException(org.apache.cayenne.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompatibilityUpgradeService(org.apache.cayenne.project.compatibility.CompatibilityUpgradeService) XMLReader(org.xml.sax.XMLReader)

Example 2 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class CompatibilityDataMapLoader method load.

@Override
public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
    Document document = documentProvider.getDocument(configurationResource.getURL());
    // no document yet in provider, maybe DataMap is directly loaded
    if (document == null) {
        if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
            throw new ConfigurationException("CompatibilityUpgradeService expected");
        }
        // try to upgrade datamap directly
        CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
        upgradeService.upgradeDataMap(configurationResource);
        document = documentProvider.getDocument(configurationResource.getURL());
        // still no document, try to load it without upgrade, though it likely will fail
        if (document == null) {
            return super.load(configurationResource);
        }
    }
    final DataMap[] maps = new DataMap[1];
    try {
        DOMSource source = new DOMSource(document);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory transFactory = TransformerFactory.newInstance();
        transFactory.newTransformer().transform(source, new StreamResult(baos));
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        XMLReader parser = Util.createXmlReader();
        LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
        loaderContext.addDataMapListener(dataMap -> maps[0] = dataMap);
        RootDataMapHandler rootHandler = new RootDataMapHandler(loaderContext);
        parser.setContentHandler(rootHandler);
        parser.setErrorHandler(rootHandler);
        parser.parse(isource);
    } catch (Exception e) {
        throw new CayenneRuntimeException("Error loading configuration from %s", e, configurationResource.getURL());
    }
    if (maps[0] == null) {
        throw new CayenneRuntimeException("Unable to load data map from %s", configurationResource.getURL());
    }
    DataMap map = maps[0];
    if (map.getName() == null) {
        // set name based on location if no name provided by map itself
        map.setName(mapNameFromLocation(configurationResource.getURL().getFile()));
    }
    return map;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ConfigurationException(org.apache.cayenne.ConfigurationException) DataMap(org.apache.cayenne.map.DataMap) ConfigurationException(org.apache.cayenne.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompatibilityUpgradeService(org.apache.cayenne.project.compatibility.CompatibilityUpgradeService) XMLReader(org.xml.sax.XMLReader)

Example 3 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class CompatibilityDataChannelDescriptorLoaderIT method testLoad.

@Test
public void testLoad() throws Exception {
    Injector injector = getInjector();
    DataChannelDescriptorLoader loader = injector.getInstance(DataChannelDescriptorLoader.class);
    assertTrue(loader instanceof CompatibilityDataChannelDescriptorLoader);
    URL resourceUrl = getClass().getResource("../../project/compatibility/cayenne-project-v6.xml");
    Resource resource = new URLResource(resourceUrl);
    ConfigurationTree<DataChannelDescriptor> configurationTree = loader.load(resource);
    assertNotNull(configurationTree.getRootNode());
    assertTrue(configurationTree.getLoadFailures().isEmpty());
    assertEquals(1, configurationTree.getRootNode().getDataMaps().size());
    DataMap dataMap = configurationTree.getRootNode().getDataMaps().iterator().next();
    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) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Injector(org.apache.cayenne.di.Injector) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) 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 DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DataDomain method removeDataMap.

/**
 * Removes named DataMap from this DataDomain and any underlying DataNodes
 * that include it.
 *
 * @since 3.1
 */
public void removeDataMap(String mapName) {
    DataMap map = getDataMap(mapName);
    if (map == null) {
        return;
    }
    // remove from data nodes
    for (DataNode node : nodes.values()) {
        node.removeDataMap(mapName);
    }
    nodesByDataMapName.remove(mapName);
    // remove from EntityResolver
    getEntityResolver().removeDataMap(map);
    refreshEntitySorter();
}
Also used : DataMap(org.apache.cayenne.map.DataMap)

Example 5 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DataDomain method lookupDataNode.

/**
 * Returns a DataNode that should handle queries for all entities in a
 * DataMap.
 *
 * @since 1.1
 */
public DataNode lookupDataNode(DataMap map) {
    DataNode node = nodesByDataMapName.get(map.getName());
    if (node == null) {
        // linked...
        for (DataNode n : getDataNodes()) {
            for (DataMap m : n.getDataMaps()) {
                if (m == map) {
                    nodesByDataMapName.put(map.getName(), n);
                    node = n;
                    break;
                }
            }
            if (node != null) {
                break;
            }
        }
        if (node == null) {
            if (defaultNode != null) {
                nodesByDataMapName.put(map.getName(), defaultNode);
                node = defaultNode;
            } else {
                throw new CayenneRuntimeException("No DataNode configured for DataMap '%s'" + " and no default DataNode set", map.getName());
            }
        }
    }
    return node;
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataMap (org.apache.cayenne.map.DataMap)233 Test (org.junit.Test)88 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)48 DbEntity (org.apache.cayenne.map.DbEntity)48 ObjEntity (org.apache.cayenne.map.ObjEntity)45 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)22 URL (java.net.URL)21 URLResource (org.apache.cayenne.resource.URLResource)20 ArrayList (java.util.ArrayList)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)19 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)17 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)16 DataMapEvent (org.apache.cayenne.configuration.event.DataMapEvent)15 File (java.io.File)14 Procedure (org.apache.cayenne.map.Procedure)14 Embeddable (org.apache.cayenne.map.Embeddable)13 Injector (org.apache.cayenne.di.Injector)12 DbAttribute (org.apache.cayenne.map.DbAttribute)11 EntityResolver (org.apache.cayenne.map.EntityResolver)11 Entity (org.apache.cayenne.map.Entity)10