Search in sources :

Example 1 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor 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 DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor 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 3 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class DataMapValidator method validateName.

private void validateName(DataMap map, ValidationResult validationResult) {
    String name = map.getName();
    if (Util.isEmptyString(name)) {
        addFailure(validationResult, map, "Unnamed DataMap");
        return;
    }
    DataChannelDescriptor domain = map.getDataChannelDescriptor();
    if (domain == null) {
        return;
    }
    // check for duplicate names in the parent context
    for (DataMap otherMap : domain.getDataMaps()) {
        if (otherMap == map) {
            continue;
        }
        if (name.equals(otherMap.getName())) {
            addFailure(validationResult, map, "Duplicate DataMap name: %s", name);
            return;
        }
    }
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class DataChannelProjectLoaderTest method testLoad.

@Test
public void testLoad() {
    DataChannelProjectLoader loader = new DataChannelProjectLoader();
    Module testModule = binder -> {
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
        binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
        binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
        binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
        binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    injector.injectMembers(loader);
    String testConfigName = "PROJECT1";
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
    Resource rootSource = new URLResource(url);
    Project project = loader.loadProject(rootSource);
    assertNotNull(project);
    DataChannelDescriptor rootNode = (DataChannelDescriptor) project.getRootNode();
    assertNotNull(rootNode);
    assertSame(rootSource, rootNode.getConfigurationSource());
    assertNotNull(project.getConfigurationResource());
    assertEquals(project.getConfigurationResource(), rootSource);
}
Also used : Injector(org.apache.cayenne.di.Injector) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Resource(org.apache.cayenne.resource.Resource) Assert(org.junit.Assert) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 5 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class NameBuilderTest method testName_DataChannelDescriptorContext.

@Test
public void testName_DataChannelDescriptorContext() throws Exception {
    DataChannelDescriptor descriptor = new DataChannelDescriptor();
    DataMap m0 = new DataMap();
    m0.setName(NameBuilder.builder(m0).in(descriptor).name());
    assertEquals("datamap", m0.getName());
    descriptor.getDataMaps().add(m0);
    DataMap m1 = new DataMap();
    m1.setName(NameBuilder.builder(m1).in(descriptor).name());
    assertEquals("datamap1", m1.getName());
    descriptor.getDataMaps().add(m1);
    DataNodeDescriptor nd0 = new DataNodeDescriptor();
    nd0.setName(NameBuilder.builder(nd0).in(descriptor).name());
    assertEquals("datanode", nd0.getName());
    descriptor.getNodeDescriptors().add(nd0);
    DataNodeDescriptor nd1 = new DataNodeDescriptor();
    nd1.setName(NameBuilder.builder(nd1).in(descriptor).name());
    assertEquals("datanode1", nd1.getName());
    descriptor.getNodeDescriptors().add(nd1);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)100 DataMap (org.apache.cayenne.map.DataMap)50 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)28 Test (org.junit.Test)20 ObjEntity (org.apache.cayenne.map.ObjEntity)18 DbEntity (org.apache.cayenne.map.DbEntity)12 ProjectController (org.apache.cayenne.modeler.ProjectController)12 URL (java.net.URL)11 Embeddable (org.apache.cayenne.map.Embeddable)11 ArrayList (java.util.ArrayList)10 URLResource (org.apache.cayenne.resource.URLResource)10 ObjAttribute (org.apache.cayenne.map.ObjAttribute)8 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)8 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 DataNodeEvent (org.apache.cayenne.configuration.event.DataNodeEvent)6 Injector (org.apache.cayenne.di.Injector)6 Procedure (org.apache.cayenne.map.Procedure)6 ProcedureDisplayEvent (org.apache.cayenne.modeler.event.ProcedureDisplayEvent)6 Resource (org.apache.cayenne.resource.Resource)6