Search in sources :

Example 1 with CollectRdbException

use of org.openforis.collect.relational.CollectRdbException in project collect by openforis.

the class RelationalSchemaGenerator method createCodeListTable.

protected CodeTable createCodeListTable(RelationalSchema rs, CodeList codeList, CodeTable parent, Integer hierarchyIdx) throws CollectRdbException {
    String tableName = CodeListTables.getTableName(config, codeList, hierarchyIdx);
    CodeTable table = new CodeTable(config.getCodeListTablePrefix(), tableName, codeList, parent, config.getDefaultCode(), config.getDefaultCodeLabels());
    if (rs.containsTable(tableName)) {
        throw new CollectRdbException("Duplicate table '" + tableName + "' for CodeList " + codeList.getName());
    }
    // Create PK column
    Column<?> pkColumn = new CodePrimaryKeyColumn(CodeListTables.getIdColumnName(config, table.getName()));
    table.addColumn(pkColumn);
    // Create PK constraint
    addPKConstraint(table, pkColumn);
    // add code column
    CodeListCodeColumn codeColumn = new CodeListCodeColumn(CodeListTables.getCodeColumnName(codeList, hierarchyIdx));
    table.addColumn(codeColumn);
    if (parent != null) {
        // Create Parent FK column
        Column<?> parentIdColumn = new CodeParentKeyColumn(CodeListTables.getIdColumnName(config, parent.getName()));
        addColumn(table, parentIdColumn);
        // Create FK constraint
        String fkConstraintName = config.getFkConstraintPrefix() + table.getBaseName() + "_" + parent.getBaseName();
        PrimaryKeyConstraint parentPkConstraint = parent.getPrimaryKeyConstraint();
        ReferentialConstraint fkConstraint = new ReferentialConstraint(fkConstraintName, table, parentPkConstraint, parentIdColumn);
        table.addConstraint(fkConstraint);
    }
    Survey survey = codeList.getSurvey();
    // add default language label column
    {
        String columnName = CodeListTables.getLabelColumnName(config, codeList, hierarchyIdx);
        CodeLabelColumn col = new CodeLabelColumn(survey.getDefaultLanguage(), columnName);
        addColumn(table, col);
    }
    // add label columns
    for (String langCode : survey.getLanguages()) {
        String colName = CodeListTables.getLabelColumnName(config, codeList, hierarchyIdx, langCode);
        CodeLabelColumn col = new CodeLabelColumn(langCode, colName);
        addColumn(table, col);
    }
    // add default language description column
    {
        String colName = CodeListTables.getDescriptionColumnName(config, codeList, hierarchyIdx);
        CodeListDescriptionColumn col = new CodeListDescriptionColumn(survey.getDefaultLanguage(), colName);
        addColumn(table, col);
    }
    // add description columns
    for (String langCode : survey.getLanguages()) {
        String colName = CodeListTables.getDescriptionColumnName(config, codeList, hierarchyIdx, langCode);
        CodeListDescriptionColumn col = new CodeListDescriptionColumn(langCode, colName);
        table.addColumn(col);
    }
    return table;
}
Also used : Survey(org.openforis.idm.metamodel.Survey) CollectSurvey(org.openforis.collect.model.CollectSurvey) CollectRdbException(org.openforis.collect.relational.CollectRdbException)

Example 2 with CollectRdbException

use of org.openforis.collect.relational.CollectRdbException in project collect by openforis.

the class LiquibaseRelationalSchemaCreator method createRelationalSchema.

@Override
public void createRelationalSchema(RelationalSchema schema, Connection targetConn) throws CollectRdbException {
    PrintStream ps = null;
    try {
        LiquidbaseDatabaseSnapshotBuilder snapshotGen = new LiquidbaseDatabaseSnapshotBuilder();
        Database rdb = getDatabaseImplementation(targetConn);
        boolean dbSupportsFKs = rdb instanceof SQLiteDatabase ? false : true;
        DatabaseSnapshot generatedSnapshot = snapshotGen.createSnapshot(schema, dbSupportsFKs);
        String targetSchema = schema.getName();
        rdb.setDefaultSchemaName(targetSchema);
        DatabaseSnapshot emptyDbSnapshot = new DatabaseSnapshot(rdb, targetSchema);
        // Generate change set
        Diff diff = new Diff(generatedSnapshot, emptyDbSnapshot);
        DiffResult diffResult = diff.compare();
        File tmpFile = File.createTempFile("collect-schemagen", ".xml");
        ps = new PrintStream(new FileOutputStream(tmpFile));
        diffResult.setChangeSetAuthor("collect3");
        diffResult.setChangeSetContext("schemagen");
        System.out.println("Writing change log to " + tmpFile.getAbsolutePath());
        diffResult.printChangeLog(ps, rdb);
        ps.flush();
        // Execute change set
        Liquibase liq = new Liquibase(tmpFile.getName(), new FileSystemResourceAccessor(tmpFile.getParent()), rdb);
        liq.update("schemagen");
    } catch (LiquibaseException e) {
        throw new CollectRdbException("Failed to update schema", e);
    } catch (IOException e) {
        throw new CollectRdbException("Failed to create temp db changelog file", e);
    } catch (ParserConfigurationException e) {
        throw new CollectRdbException("Failed to write temp db changelog file", e);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Diff(liquibase.diff.Diff) IOException(java.io.IOException) Liquibase(liquibase.Liquibase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) CollectRdbException(org.openforis.collect.relational.CollectRdbException) FileOutputStream(java.io.FileOutputStream) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) DiffResult(liquibase.diff.DiffResult) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) LiquibaseException(liquibase.exception.LiquibaseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) File(java.io.File)

Aggregations

CollectRdbException (org.openforis.collect.relational.CollectRdbException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Liquibase (liquibase.Liquibase)1 Database (liquibase.database.Database)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 Diff (liquibase.diff.Diff)1 DiffResult (liquibase.diff.DiffResult)1 LiquibaseException (liquibase.exception.LiquibaseException)1 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)1 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 Survey (org.openforis.idm.metamodel.Survey)1