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());
}
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());
}
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();
}
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;
}
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;
}
Aggregations