Search in sources :

Example 1 with UpgradeMetaData

use of org.apache.cayenne.project.upgrade.UpgradeMetaData 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 UpgradeMetaData

use of org.apache.cayenne.project.upgrade.UpgradeMetaData in project cayenne by apache.

the class OpenProjectAction method openProject.

/**
 * Opens specified project file. File must already exist.
 */
public void openProject(File file) {
    try {
        if (!file.exists()) {
            JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - file \"" + file.getPath() + "\" does not exist", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
            return;
        }
        CayenneModelerController controller = Application.getInstance().getFrameController();
        controller.addToLastProjListAction(file);
        URL url = file.toURI().toURL();
        Resource rootSource = new URLResource(url);
        UpgradeService upgradeService = getApplication().getInjector().getInstance(UpgradeService.class);
        UpgradeMetaData metaData = upgradeService.getUpgradeType(rootSource);
        switch(metaData.getUpgradeType()) {
            case INTERMEDIATE_UPGRADE_NEEDED:
                String modelerVersion = PROJECT_TO_MODELER_VERSION.get(metaData.getProjectVersion());
                if (modelerVersion == null) {
                    modelerVersion = "";
                }
                JOptionPane.showMessageDialog(Application.getFrame(), "Open the project in the older Modeler " + modelerVersion + " to do an intermediate upgrade\nbefore you can upgrade to latest version.", "Can't Upgrade Project", JOptionPane.ERROR_MESSAGE);
                closeProject(false);
                return;
            case DOWNGRADE_NEEDED:
                JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - it was created using a newer version of the Modeler", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
                closeProject(false);
                return;
            case UPGRADE_NEEDED:
                if (processUpgrades()) {
                    rootSource = upgradeService.upgradeProject(rootSource);
                } else {
                    closeProject(false);
                    return;
                }
                break;
        }
        openProjectResourse(rootSource, controller);
    } catch (Exception ex) {
        logObj.warn("Error loading project file.", ex);
        ErrorDebugDialog.guiWarning(ex, "Error loading project");
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) UpgradeService(org.apache.cayenne.project.upgrade.UpgradeService) CayenneModelerController(org.apache.cayenne.modeler.CayenneModelerController) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) UpgradeMetaData(org.apache.cayenne.project.upgrade.UpgradeMetaData) URL(java.net.URL)

Aggregations

URL (java.net.URL)2 UpgradeMetaData (org.apache.cayenne.project.upgrade.UpgradeMetaData)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 ConfigurationException (org.apache.cayenne.ConfigurationException)1 ConfigurationTree (org.apache.cayenne.configuration.ConfigurationTree)1 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)1 DataMap (org.apache.cayenne.map.DataMap)1 CayenneModelerController (org.apache.cayenne.modeler.CayenneModelerController)1 CompatibilityUpgradeService (org.apache.cayenne.project.compatibility.CompatibilityUpgradeService)1 UpgradeService (org.apache.cayenne.project.upgrade.UpgradeService)1 Resource (org.apache.cayenne.resource.Resource)1 URLResource (org.apache.cayenne.resource.URLResource)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1