Search in sources :

Example 1 with TrakEM2MLParser

use of ini.trakem2.tree.TrakEM2MLParser in project TrakEM2 by trakem2.

the class DBLoader method upgradeProjectsTable.

/**
 * Used to upgrade old databases.
 */
private boolean upgradeProjectsTable() throws Exception {
    // Upgrade database if necessary: set a version field, create the TemplateThing entries in the database for each project from its XML template file, and delete the xml_template column
    // Check columns: see if trakem2_version is there
    ResultSet r = connection.prepareStatement("SELECT column_name FROM information_schema.columns WHERE table_name='ab_projects' AND column_name='xml_template'").executeQuery();
    if (r.next()) {
        YesNoCancelDialog yn = new YesNoCancelDialog(IJ.getInstance(), "Upgrade", "Need to upgrade table projects.\nNo data will be lost, but reorganized.\nProceed?");
        if (!yn.yesPressed()) {
            return false;
        }
        // retrieve and parse XML template from each project
        ResultSet r1 = connection.prepareStatement("SELECT * FROM ab_projects").executeQuery();
        while (r1.next()) {
            long project_id = r1.getLong("id");
            // parse the XML file stored in the db and save the TemplateThing into the ab_things table
            InputStream xml_stream = null;
            try {
                String query = "SELECT xml_template FROM ab_projects WHERE id=" + project_id;
                ResultSet result = connection.prepareStatement(query).executeQuery();
                if (result.next()) {
                    xml_stream = result.getBinaryStream("xml_template");
                }
                result.close();
            } catch (Exception e) {
                IJError.print(e);
                return false;
            }
            if (null == xml_stream) {
                Utils.showMessage("Failed to upgrade the database schema: XML template stream is null.");
                return false;
            }
            TemplateThing template_root = new TrakEM2MLParser(xml_stream).getTemplateRoot();
            if (null == template_root) {
                Utils.showMessage("Failed to upgrade the database schema: root TemplateThing is null.");
                return false;
            }
            Project project = new Project(project_id, r1.getString("title"));
            project.setTempLoader(this);
            template_root.addToDatabase(project);
        }
        r1.close();
        // remove the XML column
        connection.prepareStatement("ALTER TABLE ab_projects DROP xml_template").execute();
        // org.postgresql.util.PSQLException: ERROR: adding columns with defaults is not implemented in 7.4.* (only in 8.1.4+)
        // connection.prepareStatement("ALTER TABLE ab_projects ADD version text default '" + Utils.version + "'").execute();
        // so: workaround
        connection.prepareStatement("ALTER TABLE ab_projects ADD version TEXT").execute();
        connection.prepareStatement("ALTER TABLE ab_projects ALTER COLUMN version SET DEFAULT '" + Utils.version + "'").execute();
    }
    r.close();
    // success!
    return true;
}
Also used : Project(ini.trakem2.Project) TrakEM2MLParser(ini.trakem2.tree.TrakEM2MLParser) LoggingInputStream(ini.trakem2.io.LoggingInputStream) InputStream(java.io.InputStream) TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) YesNoCancelDialog(ij.gui.YesNoCancelDialog) SQLException(java.sql.SQLException)

Aggregations

YesNoCancelDialog (ij.gui.YesNoCancelDialog)1 Project (ini.trakem2.Project)1 LoggingInputStream (ini.trakem2.io.LoggingInputStream)1 TemplateThing (ini.trakem2.tree.TemplateThing)1 TrakEM2MLParser (ini.trakem2.tree.TrakEM2MLParser)1 InputStream (java.io.InputStream)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1