Search in sources :

Example 1 with Resource

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

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

the class CompatibilityUpgradeServiceIT method testUpgradeFullProjectDom.

@Test
public void testUpgradeFullProjectDom() throws Exception {
    Injector injector = getInjector();
    CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) injector.getInstance(UpgradeService.class);
    DocumentProvider documentProvider = injector.getInstance(DocumentProvider.class);
    URL resourceUrl = getClass().getResource("cayenne-project-v6.xml");
    Resource resource = new URLResource(resourceUrl);
    upgradeService.upgradeProject(resource);
    Document domainDocument = documentProvider.getDocument(resourceUrl);
    assertNotNull(domainDocument);
    assertEquals("10", domainDocument.getDocumentElement().getAttribute("project-version"));
    URL dataMapUrl = getClass().getResource("test-map-v6.map.xml");
    Document dataMapDocument = documentProvider.getDocument(dataMapUrl);
    assertNotNull(dataMapDocument);
    assertEquals("10", dataMapDocument.getDocumentElement().getAttribute("project-version"));
    assertEquals(1, dataMapDocument.getElementsByTagName("obj-entity").getLength());
    assertEquals(1, dataMapDocument.getElementsByTagName("db-entity").getLength());
    assertEquals(2, dataMapDocument.getElementsByTagName("db-attribute").getLength());
}
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 3 with Resource

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

the class FileProjectSaver method save.

void save(Project project, Resource baseResource, boolean deleteOldResources) {
    Collection<ConfigurationNode> nodes = project.getRootNode().acceptVisitor(saveableNodesGetter);
    Collection<SaveUnit> units = new ArrayList<>(nodes.size());
    for (ConfigurationNode node : nodes) {
        String targetLocation = nameMapper.configurationLocation(node);
        Resource targetResource = baseResource.getRelativeResource(targetLocation);
        units.add(createSaveUnit(node, targetResource, null));
        for (ProjectExtension extension : extensions) {
            ConfigurationNodeVisitor<String> namingDelegate = extension.createNamingDelegate();
            SaverDelegate unitSaverDelegate = extension.createSaverDelegate();
            String fileName = node.acceptVisitor(namingDelegate);
            if (fileName != null) {
                // not null means that this should go to a separate file
                targetResource = baseResource.getRelativeResource(fileName);
                units.add(createSaveUnit(node, targetResource, unitSaverDelegate));
            }
        }
    }
    checkAccess(units);
    try {
        saveToTempFiles(units);
        saveCommit(units);
    } finally {
        clearTempFiles(units);
    }
    try {
        if (deleteOldResources) {
            clearRenamedFiles(units);
            Collection<URL> unusedResources = project.getUnusedResources();
            for (SaveUnit unit : units) {
                unusedResources.remove(unit.sourceConfiguration.getURL());
            }
            deleteUnusedFiles(unusedResources);
        }
    } catch (IOException ex) {
        throw new CayenneRuntimeException(ex);
    }
    // I guess we should reset projects state regardless of the value of
    // 'deleteOldResources'
    project.getUnusedResources().clear();
}
Also used : SaverDelegate(org.apache.cayenne.project.extension.SaverDelegate) ArrayList(java.util.ArrayList) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) IOException(java.io.IOException) URL(java.net.URL) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension)

Example 4 with Resource

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

the class DefaultUpgradeService method getAdditionalDatamapResources.

List<Resource> getAdditionalDatamapResources(UpgradeUnit upgradeUnit) {
    List<Resource> resources = new ArrayList<>();
    try {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate("/domain/map/@name", upgradeUnit.getDocument(), XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node mapNode = nodes.item(i);
            // in version 3.0.0.1 and earlier map tag had attribute location,
            // but it was always equal to data map name + ".map.xml"
            Resource mapResource = upgradeUnit.getResource().getRelativeResource(mapNode.getNodeValue() + ".map.xml");
            resources.add(mapResource);
        }
    } catch (Exception ex) {
        logger.warn("Can't get additional dataMap resources: ", ex);
    }
    return resources;
}
Also used : XPath(javax.xml.xpath.XPath) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Resource(org.apache.cayenne.resource.Resource) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) ConfigurationException(org.apache.cayenne.ConfigurationException)

Example 5 with Resource

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

the class DefaultUpgradeService method upgradeDOM.

protected List<UpgradeUnit> upgradeDOM(Resource resource, List<UpgradeHandler> handlerList) {
    List<UpgradeUnit> allUnits = new ArrayList<>();
    // Load DOM for all resources
    Document projectDocument = Util.readDocument(resource.getURL());
    UpgradeUnit projectUnit = new UpgradeUnit(resource, projectDocument);
    allUnits.add(projectUnit);
    List<Resource> dataMapResources = getAdditionalDatamapResources(projectUnit);
    List<UpgradeUnit> dataMapUnits = new ArrayList<>(dataMapResources.size());
    for (Resource dataMapResource : dataMapResources) {
        dataMapUnits.add(new UpgradeUnit(dataMapResource, Util.readDocument(dataMapResource.getURL())));
    }
    allUnits.addAll(dataMapUnits);
    // Update DOM
    for (UpgradeHandler handler : handlerList) {
        handler.processProjectDom(projectUnit);
        for (UpgradeUnit dataMapUnit : dataMapUnits) {
            handler.processDataMapDom(dataMapUnit);
        }
    }
    return allUnits;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.cayenne.resource.Resource) Document(org.w3c.dom.Document) UpgradeHandler(org.apache.cayenne.project.upgrade.handlers.UpgradeHandler)

Aggregations

Resource (org.apache.cayenne.resource.Resource)21 URLResource (org.apache.cayenne.resource.URLResource)13 URL (java.net.URL)11 Test (org.junit.Test)11 Injector (org.apache.cayenne.di.Injector)8 DataMap (org.apache.cayenne.map.DataMap)7 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)6 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)6 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)5 DataChannelDescriptorLoader (org.apache.cayenne.configuration.DataChannelDescriptorLoader)5 File (java.io.File)4 ConfigurationNameMapper (org.apache.cayenne.configuration.ConfigurationNameMapper)4 DefaultConfigurationNameMapper (org.apache.cayenne.configuration.DefaultConfigurationNameMapper)4 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)4 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)4 DIBootstrap (org.apache.cayenne.di.DIBootstrap)4 Module (org.apache.cayenne.di.Module)4 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)4 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)4 Assert (org.junit.Assert)4