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