From 127c2fdd55d06b0330ad75d29ecae21490734f58 Mon Sep 17 00:00:00 2001 From: Gabriel Barros Date: Mon, 21 Oct 2024 17:43:43 +0100 Subject: [PATCH] Cleaning up the code --- .../common/adapters/SIARDDK1007Adapter.java | 136 ++++++++ .../common/adapters/SIARDDK128Adapter.java | 134 ++++++++ .../siard/common/adapters/SIARDDKAdapter.java | 33 ++ .../SIARDDK1007TableIndexFileStrategy.java | 291 +----------------- .../SIARDDK128TableIndexFileStrategy.java | 284 +---------------- .../SIARDDKTableIndexFileStrategy.java | 262 ++++++++++++++++ 6 files changed, 577 insertions(+), 563 deletions(-) create mode 100644 dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK1007Adapter.java create mode 100644 dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK128Adapter.java create mode 100644 dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDKAdapter.java create mode 100644 dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDKTableIndexFileStrategy.java diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK1007Adapter.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK1007Adapter.java new file mode 100644 index 00000000..bf3227b5 --- /dev/null +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK1007Adapter.java @@ -0,0 +1,136 @@ +package com.databasepreservation.modules.siard.common.adapters; + +import java.math.BigInteger; +import java.util.List; +import java.util.Map; + +import com.databasepreservation.modules.siard.constants.SIARDDKConstants; + +import dk.sa.xmlns.diark._1_0.tableindex.ColumnType; +import dk.sa.xmlns.diark._1_0.tableindex.ColumnsType; +import dk.sa.xmlns.diark._1_0.tableindex.ForeignKeyType; +import dk.sa.xmlns.diark._1_0.tableindex.ForeignKeysType; +import dk.sa.xmlns.diark._1_0.tableindex.FunctionalDescriptionType; +import dk.sa.xmlns.diark._1_0.tableindex.PrimaryKeyType; +import dk.sa.xmlns.diark._1_0.tableindex.ReferenceType; +import dk.sa.xmlns.diark._1_0.tableindex.SiardDiark; +import dk.sa.xmlns.diark._1_0.tableindex.TableType; +import dk.sa.xmlns.diark._1_0.tableindex.TablesType; +import dk.sa.xmlns.diark._1_0.tableindex.ViewType; +import dk.sa.xmlns.diark._1_0.tableindex.ViewsType; + +/** + * @author Gabriel Barros + */ +public class SIARDDK1007Adapter implements SIARDDKAdapter { + private final SiardDiark siardDiark; + private final TablesType tablesType; + private final ViewsType viewsType; + + public SIARDDK1007Adapter() { + siardDiark = new SiardDiark(); + tablesType = new TablesType(); + viewsType = new ViewsType(); + + siardDiark.setTables(tablesType); + siardDiark.setViews(viewsType); + } + + @Override + public void setVersion(String value) { + siardDiark.setVersion(value); + } + + @Override + public void setDbName(String dbOriginalName) { + siardDiark.setDbName(dbOriginalName); + } + + @Override + public void setDatabaseProduct(String productName) { + siardDiark.setDatabaseProduct(productName); + } + + @Override + public void addTable(String tableName, String tableFolder, String tableDescription) { + TableType tableType = new TableType(); + tableType.setName(tableName); + tableType.setFolder(tableFolder); + tableType.setDescription(tableDescription); + tableType.setColumns(new ColumnsType()); + tablesType.getTable().add(tableType); + } + + @Override + public void addColumnForTable(String tableName, String columnName, String columnID, String columType, + String columnOriginalType, String columnDefaultValue, Boolean columnStructureNillable, String columnDescription, + String lobType) { + ColumnType columnType = new ColumnType(); + columnType.setName(columnName); + columnType.setColumnID(columnID); + columnType.setType(columType); + columnType.setTypeOriginal(columnOriginalType); + columnType.setDefaultValue(columnDefaultValue); + columnType.setNullable(columnStructureNillable); + columnType.setDescription(columnDescription); + + if (lobType != null) { + if (lobType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { + FunctionalDescriptionType functionalDescriptionType = FunctionalDescriptionType.DOKUMENTIDENTIFIKATION; + columnType.getFunctionalDescription().add(functionalDescriptionType); + } + } + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get().getColumns() + .getColumn().add(columnType); + } + + @Override + public Object getSIARDDK() { + return siardDiark; + } + + @Override + public void addPrimaryKeyForTable(String tableName, String primaryKeyName, List escapedColumnNames) { + PrimaryKeyType primaryKeyType = new PrimaryKeyType(); + primaryKeyType.setName(primaryKeyName); + primaryKeyType.getColumn().addAll(escapedColumnNames); + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get() + .setPrimaryKey(primaryKeyType); + } + + @Override + public void addForeignKeyForTable(String tableName, String foreignKeyName, String foreignKeyReferencedTable, + Map escapedReferencedColumns) { + TableType tableType = tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst() + .get(); + if (tableType.getForeignKeys() == null) { + tableType.setForeignKeys(new ForeignKeysType()); + } + ForeignKeyType foreignKeyType = new ForeignKeyType(); + foreignKeyType.setName(foreignKeyName); + foreignKeyType.setReferencedTable(foreignKeyReferencedTable); + escapedReferencedColumns.forEach((column, ref) -> { + ReferenceType referenceType = new ReferenceType(); + referenceType.setColumn(column); + referenceType.setReferenced(ref); + foreignKeyType.getReference().add(referenceType); + }); + tableType.getForeignKeys().getForeignKey().add(foreignKeyType); + } + + @Override + public void setRowsForTable(String tableName, BigInteger bigInteger) { + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get() + .setRows(bigInteger); + } + + @Override + public void addView(String viewName, String viewQueryOriginal, String viewDescription) { + ViewType viewType = new ViewType(); + viewType.setName(viewName); + viewType.setQueryOriginal(viewQueryOriginal); + viewType.setDescription(viewDescription); + viewsType.getView().add(viewType); + } + +} diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK128Adapter.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK128Adapter.java new file mode 100644 index 00000000..c45d832b --- /dev/null +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDK128Adapter.java @@ -0,0 +1,134 @@ +package com.databasepreservation.modules.siard.common.adapters; + +import java.math.BigInteger; +import java.util.List; +import java.util.Map; + +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ColumnType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ColumnsType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ForeignKeyType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ForeignKeysType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.FunctionalDescriptionType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.PrimaryKeyType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ReferenceType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.SiardDiark; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.TableType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.TablesType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ViewType; +import com.databasepreservation.modules.siard.bindings.siard_dk_128.ViewsType; +import com.databasepreservation.modules.siard.constants.SIARDDKConstants; + +/** + * @author Gabriel Barros + */ +public class SIARDDK128Adapter implements SIARDDKAdapter { + private final SiardDiark siardDiark; + private final TablesType tablesType; + private final ViewsType viewsType; + + public SIARDDK128Adapter() { + siardDiark = new SiardDiark(); + tablesType = new TablesType(); + viewsType = new ViewsType(); + + siardDiark.setTables(tablesType); + siardDiark.setViews(viewsType); + } + + @Override + public void setVersion(String value) { + siardDiark.setVersion(value); + } + + @Override + public void setDbName(String dbOriginalName) { + siardDiark.setDbName(dbOriginalName); + } + + @Override + public void setDatabaseProduct(String productName) { + siardDiark.setDatabaseProduct(productName); + } + + @Override + public void addTable(String tableName, String tableFolder, String tableDescription) { + TableType tableType = new TableType(); + tableType.setName(tableName); + tableType.setFolder(tableFolder); + tableType.setDescription(tableDescription); + tableType.setColumns(new ColumnsType()); + tablesType.getTable().add(tableType); + } + + @Override + public void addColumnForTable(String tableName, String columnName, String columnID, String columType, + String columnOriginalType, String columnDefaultValue, Boolean columnStructureNillable, String columnDescription, + String lobType) { + ColumnType columnType = new ColumnType(); + columnType.setName(columnName); + columnType.setColumnID(columnID); + columnType.setType(columType); + columnType.setTypeOriginal(columnOriginalType); + columnType.setDefaultValue(columnDefaultValue); + columnType.setNullable(columnStructureNillable); + columnType.setDescription(columnDescription); + + if (lobType != null) { + if (lobType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { + FunctionalDescriptionType functionalDescriptionType = FunctionalDescriptionType.DOKUMENTIDENTIFIKATION; + columnType.getFunctionalDescription().add(functionalDescriptionType); + } + } + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get().getColumns() + .getColumn().add(columnType); + } + + @Override + public Object getSIARDDK() { + return siardDiark; + } + + @Override + public void addPrimaryKeyForTable(String tableName, String primaryKeyName, List escapedColumnNames) { + PrimaryKeyType primaryKeyType = new PrimaryKeyType(); + primaryKeyType.setName(primaryKeyName); + primaryKeyType.getColumn().addAll(escapedColumnNames); + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get() + .setPrimaryKey(primaryKeyType); + } + + @Override + public void addForeignKeyForTable(String tableName, String foreignKeyName, String foreignKeyReferencedTable, + Map escapedReferencedColumns) { + TableType tableType = tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst() + .get(); + if (tableType.getForeignKeys() == null) { + tableType.setForeignKeys(new ForeignKeysType()); + } + ForeignKeyType foreignKeyType = new ForeignKeyType(); + foreignKeyType.setName(foreignKeyName); + foreignKeyType.setReferencedTable(foreignKeyReferencedTable); + escapedReferencedColumns.forEach((column, ref) -> { + ReferenceType referenceType = new ReferenceType(); + referenceType.setColumn(column); + referenceType.setReferenced(ref); + foreignKeyType.getReference().add(referenceType); + }); + tableType.getForeignKeys().getForeignKey().add(foreignKeyType); + } + + @Override + public void setRowsForTable(String tableName, BigInteger bigInteger) { + tablesType.getTable().stream().filter(table -> table.getName().equals(tableName)).findFirst().get() + .setRows(bigInteger); + } + + @Override + public void addView(String viewName, String viewQueryOriginal, String viewDescription) { + ViewType viewType = new ViewType(); + viewType.setName(viewName); + viewType.setQueryOriginal(viewQueryOriginal); + viewType.setDescription(viewDescription); + viewsType.getView().add(viewType); + } +} diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDKAdapter.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDKAdapter.java new file mode 100644 index 00000000..0bcddca9 --- /dev/null +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/common/adapters/SIARDDKAdapter.java @@ -0,0 +1,33 @@ +package com.databasepreservation.modules.siard.common.adapters; + +import java.math.BigInteger; +import java.util.List; +import java.util.Map; + +/** + * @author Gabriel Barros + */ +public interface SIARDDKAdapter { + void setVersion(String value); + + void setDbName(String dbOriginalName); + + void setDatabaseProduct(String productName); + + void addTable(String tableName, String tableFolder, String tableDescription); + + void addColumnForTable(String tableName, String columnName, String columnID, String columType, + String columnOriginalType, String columnDefaultValue, Boolean columnStructureNillable, String columnDescription, + String lobType); + + Object getSIARDDK(); + + void addPrimaryKeyForTable(String tableName, String primaryKeyName, List escapedColumnNames); + + void addForeignKeyForTable(String tableName, String foreignKeyName, String foreignKeyReferencedTable, + Map escapedReferencedColumns); + + void setRowsForTable(String tableName, BigInteger bigInteger); + + void addView(String viewName, String viewQueryOriginal, String viewDescription); +} diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK1007TableIndexFileStrategy.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK1007TableIndexFileStrategy.java index 7a5aef83..b8c6eb62 100644 --- a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK1007TableIndexFileStrategy.java +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK1007TableIndexFileStrategy.java @@ -7,300 +7,21 @@ */ package com.databasepreservation.modules.siard.out.metadata; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.databasepreservation.model.exception.ModuleException; -import com.databasepreservation.model.structure.ColumnStructure; -import com.databasepreservation.model.structure.DatabaseStructure; -import com.databasepreservation.model.structure.ForeignKey; -import com.databasepreservation.model.structure.PrimaryKey; -import com.databasepreservation.model.structure.Reference; -import com.databasepreservation.model.structure.SchemaStructure; -import com.databasepreservation.model.structure.TableStructure; -import com.databasepreservation.model.structure.ViewStructure; -import com.databasepreservation.model.structure.type.Type; -import com.databasepreservation.modules.siard.constants.SIARDDKConstants; +import com.databasepreservation.modules.siard.common.adapters.SIARDDK1007Adapter; +import com.databasepreservation.modules.siard.common.adapters.SIARDDKAdapter; import com.databasepreservation.modules.siard.out.content.LOBsTracker; -import dk.sa.xmlns.diark._1_0.tableindex.ColumnType; -import dk.sa.xmlns.diark._1_0.tableindex.ColumnsType; -import dk.sa.xmlns.diark._1_0.tableindex.ForeignKeyType; -import dk.sa.xmlns.diark._1_0.tableindex.ForeignKeysType; -import dk.sa.xmlns.diark._1_0.tableindex.FunctionalDescriptionType; -import dk.sa.xmlns.diark._1_0.tableindex.PrimaryKeyType; -import dk.sa.xmlns.diark._1_0.tableindex.ReferenceType; -import dk.sa.xmlns.diark._1_0.tableindex.SiardDiark; -import dk.sa.xmlns.diark._1_0.tableindex.TableType; -import dk.sa.xmlns.diark._1_0.tableindex.TablesType; -import dk.sa.xmlns.diark._1_0.tableindex.ViewType; -import dk.sa.xmlns.diark._1_0.tableindex.ViewsType; - /** * @author Andreas Kring * */ -public class SIARDDK1007TableIndexFileStrategy implements IndexFileStrategy { - - // LOBsTracker used to get the locations of functionalDescriptions - private LOBsTracker lobsTracker; - private String regex; - +public class SIARDDK1007TableIndexFileStrategy extends SIARDDKTableIndexFileStrategy { public SIARDDK1007TableIndexFileStrategy(LOBsTracker lobsTracker) { - this.lobsTracker = lobsTracker; - regex = "(\\p{L}(_|\\w)*)|(\".*\")"; + super(lobsTracker); } - private static final Logger logger = LoggerFactory.getLogger(SIARDDK1007TableIndexFileStrategy.class); - @Override - public Object generateXML(DatabaseStructure dbStructure) throws ModuleException { - - // Set version - mandatory - SiardDiark siardDiark = new SiardDiark(); - siardDiark.setVersion("1.0"); - - // Set dbName - mandatory - if (dbStructure.getDbOriginalName() != null){ - siardDiark.setDbName(dbStructure.getDbOriginalName()); - } else { - siardDiark.setDbName(dbStructure.getName()); - } - - - // Set databaseProduct - if (StringUtils.isNotBlank(dbStructure.getProductName())) { - siardDiark.setDatabaseProduct(dbStructure.getProductName()); - } - - // Set tables - mandatory - int tableCounter = 1; - TablesType tablesType = new TablesType(); - - List schemas = dbStructure.getSchemas(); - // System.out.println(schemas.get(0)); - - // Check that all tables have primary keys - List tablesWithNoPrimaryKeys = new ArrayList(); - StringBuilder tableListBuilder = new StringBuilder(); - for (SchemaStructure schemaStructure : schemas) { - if (!schemaStructure.getTables().isEmpty()) { - for (TableStructure tableStructure : schemaStructure.getTables()) { - PrimaryKey primaryKey = tableStructure.getPrimaryKey(); - if (primaryKey == null) { - tablesWithNoPrimaryKeys.add(tableStructure.getName()); - tableListBuilder.append(schemaStructure.getName()).append(".").append(tableStructure.getName()) - .append(", "); - } - } - } - } - if (!tablesWithNoPrimaryKeys.isEmpty()) { - logger.warn( - "No primary keys in the following table(s): " + tableListBuilder.substring(0, tableListBuilder.length() - 2)); - } - - for (SchemaStructure schemaStructure : schemas) { - if (schemaStructure.getTables().isEmpty()) { - logger.info("No tables found in this schema: " + schemaStructure.getName()); - continue; - } else { - for (TableStructure tableStructure : schemaStructure.getTables()) { - - // Set table - mandatory - - TableType tableType = new TableType(); - - // Set name - mandatory - tableType.setName(escapeString(tableStructure.getName())); - - // Set folder - mandatory - tableType.setFolder("table" + Integer.toString(tableCounter)); - - // Set description - if (tableStructure.getDescription() != null && !tableStructure.getDescription().trim().isEmpty()) { - tableType.setDescription(tableStructure.getDescription().trim()); - } else { - tableType.setDescription("Description should be entered manually"); - } - - // Set columns - mandatory - int columnCounter = 1; - ColumnsType columns = new ColumnsType(); - for (ColumnStructure columnStructure : tableStructure.getColumns()) { - - // Set column - mandatory - - ColumnType column = new ColumnType(); - Type type = columnStructure.getType(); - - // Set column name - mandatory - column.setName(escapeString(columnStructure.getName())); - - // Set columnID - mandatory - column.setColumnID("c" + Integer.toString(columnCounter)); - - // Set type - mandatory - String sql99DataType = type.getSql99TypeName(); - if (sql99DataType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { - column.setType("INTEGER"); - } else if (sql99DataType.equals(SIARDDKConstants.CHARACTER_LARGE_OBJECT)) { - - if (lobsTracker.getMaxClobLength(tableCounter, columnCounter) > 0) { - column.setType(SIARDDKConstants.DEFAULT_CLOB_TYPE + "(" - + lobsTracker.getMaxClobLength(tableCounter, columnCounter) + ")"); - } else { - column.setType(SIARDDKConstants.DEFAULT_CLOB_TYPE + "(1)"); - } - } else if (sql99DataType.startsWith("BIT VARYING") || sql99DataType.startsWith("BINARY VARYING")) { - - // Convert BIT VARYING/BINARY VARYING TO CHARACTER VARYING - - String length = sql99DataType.split("\\(")[1].trim(); - length = length.substring(0, length.length() - 1); - column.setType("CHARACTER VARYING(" + length + ")"); - - } else { - column.setType(type.getSql99TypeName()); - } - - // Set typeOriginal - if (StringUtils.isNotBlank(type.getOriginalTypeName())) { - column.setTypeOriginal(type.getOriginalTypeName()); - } - - // Set defaultValue - if (StringUtils.isNotBlank(columnStructure.getDefaultValue())) { - column.setDefaultValue(columnStructure.getDefaultValue()); - } - - // Set nullable - column.setNullable(columnStructure.isNillable()); - - // Set description - if (columnStructure.getDescription() != null && !columnStructure.getDescription().trim().isEmpty()) { - column.setDescription(columnStructure.getDescription().trim()); - } else { - column.setDescription("Description should be set"); - } - - // Set functionalDescription - String lobType = lobsTracker.getLOBsType(tableCounter, columnCounter); - if (lobType != null) { - if (lobType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { - FunctionalDescriptionType functionalDescriptionType = FunctionalDescriptionType.DOKUMENTIDENTIFIKATION; - column.getFunctionalDescription().add(functionalDescriptionType); - } - } - - columns.getColumn().add(column); - columnCounter += 1; - - } - tableType.setColumns(columns); - - // Set primary key - mandatory - PrimaryKeyType primaryKeyType = new PrimaryKeyType(); // JAXB - PrimaryKey primaryKey = tableStructure.getPrimaryKey(); - if (primaryKey == null) { - primaryKeyType.setName("MISSING"); - primaryKeyType.getColumn().add("MISSING"); - } else { - primaryKeyType.setName(escapeString(primaryKey.getName())); - List columnNames = primaryKey.getColumnNames(); - for (String columnName : columnNames) { - // Set column names for primary key - - primaryKeyType.getColumn().add(escapeString(columnName)); - } - } - tableType.setPrimaryKey(primaryKeyType); - - // Set foreignKeys - ForeignKeysType foreignKeysType = new ForeignKeysType(); - List foreignKeys = tableStructure.getForeignKeys(); - if (foreignKeys != null && foreignKeys.size() > 0) { - for (ForeignKey key : foreignKeys) { - ForeignKeyType foreignKeyType = new ForeignKeyType(); - - // Set key name - mandatory - foreignKeyType.setName(escapeString(key.getName())); - - // Set referenced table - mandatory - foreignKeyType.setReferencedTable(escapeString(key.getReferencedTable())); - - // Set reference - mandatory - for (Reference ref : key.getReferences()) { - ReferenceType referenceType = new ReferenceType(); - referenceType.setColumn(escapeString(ref.getColumn())); - referenceType.setReferenced(escapeString(ref.getReferenced())); - foreignKeyType.getReference().add(referenceType); - } - foreignKeysType.getForeignKey().add(foreignKeyType); - } - tableType.setForeignKeys(foreignKeysType); - } - - // Set rows - if (tableStructure.getRows() >= 0) { - tableType.setRows(BigInteger.valueOf(tableStructure.getRows())); - } else { - throw new ModuleException() - .withMessage("Error while exporting table structure: number of table rows not set"); - } - - tablesType.getTable().add(tableType); - - tableCounter += 1; - } - - // Set views - List viewStructures = schemaStructure.getViews(); - - if (viewStructures != null && viewStructures.size() > 0) { - ViewsType viewsType = new ViewsType(); - for (ViewStructure viewStructure : viewStructures) { - - // Set view - mandatory - ViewType viewType = new ViewType(); - - // Set view name - mandatory - viewType.setName(escapeString(viewStructure.getName())); - - // Set queryOriginal - mandatory - if (StringUtils.isNotBlank(viewStructure.getQueryOriginal())) { - viewType.setQueryOriginal(viewStructure.getQueryOriginal()); - } else { - viewType.setQueryOriginal("unknown"); - } - - // Set description - if (StringUtils.isNotBlank(viewStructure.getDescription())) { - viewType.setDescription(viewStructure.getDescription()); - } - - viewsType.getView().add(viewType); - } - siardDiark.setViews(viewsType); - } - } - } - siardDiark.setTables(tablesType); - - return siardDiark; - } - - String escapeString(String s) { - if (s.matches(regex)) { - return s; - } else { - s = new StringBuilder().append("\"").append(s).append("\"").toString(); - } - return s; + protected SIARDDKAdapter getSIARDDKBinding() { + return new SIARDDK1007Adapter(); } } diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK128TableIndexFileStrategy.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK128TableIndexFileStrategy.java index 393f965c..4e02a154 100644 --- a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK128TableIndexFileStrategy.java +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDK128TableIndexFileStrategy.java @@ -7,293 +7,21 @@ */ package com.databasepreservation.modules.siard.out.metadata; -import com.databasepreservation.model.exception.ModuleException; -import com.databasepreservation.model.structure.ColumnStructure; -import com.databasepreservation.model.structure.DatabaseStructure; -import com.databasepreservation.model.structure.ForeignKey; -import com.databasepreservation.model.structure.PrimaryKey; -import com.databasepreservation.model.structure.Reference; -import com.databasepreservation.model.structure.SchemaStructure; -import com.databasepreservation.model.structure.TableStructure; -import com.databasepreservation.model.structure.ViewStructure; -import com.databasepreservation.model.structure.type.Type; -import com.databasepreservation.modules.siard.constants.SIARDDKConstants; +import com.databasepreservation.modules.siard.common.adapters.SIARDDK128Adapter; +import com.databasepreservation.modules.siard.common.adapters.SIARDDKAdapter; import com.databasepreservation.modules.siard.out.content.LOBsTracker; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ColumnType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ColumnsType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ForeignKeyType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ForeignKeysType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.FunctionalDescriptionType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.PrimaryKeyType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ReferenceType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.SiardDiark; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.TableType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.TablesType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ViewType; -import com.databasepreservation.modules.siard.bindings.siard_dk_128.ViewsType; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.List; /** * @author Andreas Kring * */ -public class SIARDDK128TableIndexFileStrategy implements IndexFileStrategy { - - // LOBsTracker used to get the locations of functionalDescriptions - private LOBsTracker lobsTracker; - private String regex; - +public class SIARDDK128TableIndexFileStrategy extends SIARDDKTableIndexFileStrategy { public SIARDDK128TableIndexFileStrategy(LOBsTracker lobsTracker) { - this.lobsTracker = lobsTracker; - regex = "(\\p{L}(_|\\w)*)|(\".*\")"; + super(lobsTracker); } - private static final Logger logger = LoggerFactory.getLogger(SIARDDK128TableIndexFileStrategy.class); - @Override - public Object generateXML(DatabaseStructure dbStructure) throws ModuleException { - - // Set version - mandatory - SiardDiark siardDiark = new SiardDiark(); - siardDiark.setVersion("1.0"); - - // Set dbName - mandatory - siardDiark.setDbName(dbStructure.getDbOriginalName()); - - // Set databaseProduct - if (StringUtils.isNotBlank(dbStructure.getProductName())) { - siardDiark.setDatabaseProduct(dbStructure.getProductName()); - } - - // Set tables - mandatory - int tableCounter = 1; - TablesType tablesType = new TablesType(); - - List schemas = dbStructure.getSchemas(); - // System.out.println(schemas.get(0)); - - // Check that all tables have primary keys - List tablesWithNoPrimaryKeys = new ArrayList(); - StringBuilder tableListBuilder = new StringBuilder(); - for (SchemaStructure schemaStructure : schemas) { - if (!schemaStructure.getTables().isEmpty()) { - for (TableStructure tableStructure : schemaStructure.getTables()) { - PrimaryKey primaryKey = tableStructure.getPrimaryKey(); - if (primaryKey == null) { - tablesWithNoPrimaryKeys.add(tableStructure.getName()); - tableListBuilder.append(schemaStructure.getName()).append(".").append(tableStructure.getName()) - .append(", "); - } - } - } - } - if (!tablesWithNoPrimaryKeys.isEmpty()) { - logger.warn( - "No primary keys in the following table(s): " + tableListBuilder.substring(0, tableListBuilder.length() - 2)); - } - - for (SchemaStructure schemaStructure : schemas) { - if (schemaStructure.getTables().isEmpty()) { - logger.info("No tables found in this schema: " + schemaStructure.getName()); - continue; - } else { - for (TableStructure tableStructure : schemaStructure.getTables()) { - - // Set table - mandatory - - TableType tableType = new TableType(); - - // Set name - mandatory - tableType.setName(escapeString(tableStructure.getName())); - - // Set folder - mandatory - tableType.setFolder("table" + Integer.toString(tableCounter)); - - // Set description - if (tableStructure.getDescription() != null && !tableStructure.getDescription().trim().isEmpty()) { - tableType.setDescription(tableStructure.getDescription().trim()); - } else { - tableType.setDescription("Description should be entered manually"); - } - - // Set columns - mandatory - int columnCounter = 1; - ColumnsType columns = new ColumnsType(); - for (ColumnStructure columnStructure : tableStructure.getColumns()) { - - // Set column - mandatory - - ColumnType column = new ColumnType(); - Type type = columnStructure.getType(); - - // Set column name - mandatory - column.setName(escapeString(columnStructure.getName())); - - // Set columnID - mandatory - column.setColumnID("c" + Integer.toString(columnCounter)); - - // Set type - mandatory - String sql99DataType = type.getSql99TypeName(); - if (sql99DataType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { - column.setType("INTEGER"); - } else if (sql99DataType.equals(SIARDDKConstants.CHARACTER_LARGE_OBJECT)) { - - if (lobsTracker.getMaxClobLength(tableCounter, columnCounter) > 0) { - column.setType(SIARDDKConstants.DEFAULT_CLOB_TYPE + "(" - + lobsTracker.getMaxClobLength(tableCounter, columnCounter) + ")"); - } else { - column.setType(SIARDDKConstants.DEFAULT_CLOB_TYPE + "(1)"); - } - } else if (sql99DataType.startsWith("BIT VARYING") || sql99DataType.startsWith("BINARY VARYING")) { - - // Convert BIT VARYING/BINARY VARYING TO CHARACTER VARYING - - String length = sql99DataType.split("\\(")[1].trim(); - length = length.substring(0, length.length() - 1); - column.setType("CHARACTER VARYING(" + length + ")"); - - } else { - column.setType(type.getSql99TypeName()); - } - - // Set typeOriginal - if (StringUtils.isNotBlank(type.getOriginalTypeName())) { - column.setTypeOriginal(type.getOriginalTypeName()); - } - - // Set defaultValue - if (StringUtils.isNotBlank(columnStructure.getDefaultValue())) { - column.setDefaultValue(columnStructure.getDefaultValue()); - } - - // Set nullable - column.setNullable(columnStructure.isNillable()); - - // Set description - if (columnStructure.getDescription() != null && !columnStructure.getDescription().trim().isEmpty()) { - column.setDescription(columnStructure.getDescription().trim()); - } else { - column.setDescription("Description should be set"); - } - - // Set functionalDescription - String lobType = lobsTracker.getLOBsType(tableCounter, columnCounter); - if (lobType != null) { - if (lobType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { - FunctionalDescriptionType functionalDescriptionType = FunctionalDescriptionType.DOKUMENTIDENTIFIKATION; - column.getFunctionalDescription().add(functionalDescriptionType); - } - } - - columns.getColumn().add(column); - columnCounter += 1; - - } - tableType.setColumns(columns); - - // Set primary key - mandatory - PrimaryKeyType primaryKeyType = new PrimaryKeyType(); // JAXB - PrimaryKey primaryKey = tableStructure.getPrimaryKey(); - if (primaryKey == null) { - primaryKeyType.setName("MISSING"); - primaryKeyType.getColumn().add("MISSING"); - } else { - primaryKeyType.setName(escapeString(primaryKey.getName())); - List columnNames = primaryKey.getColumnNames(); - for (String columnName : columnNames) { - // Set column names for primary key - - primaryKeyType.getColumn().add(escapeString(columnName)); - } - } - tableType.setPrimaryKey(primaryKeyType); - - // Set foreignKeys - ForeignKeysType foreignKeysType = new ForeignKeysType(); - List foreignKeys = tableStructure.getForeignKeys(); - if (foreignKeys != null && foreignKeys.size() > 0) { - for (ForeignKey key : foreignKeys) { - ForeignKeyType foreignKeyType = new ForeignKeyType(); - - // Set key name - mandatory - foreignKeyType.setName(escapeString(key.getName())); - - // Set referenced table - mandatory - foreignKeyType.setReferencedTable(escapeString(key.getReferencedTable())); - - // Set reference - mandatory - for (Reference ref : key.getReferences()) { - ReferenceType referenceType = new ReferenceType(); - referenceType.setColumn(escapeString(ref.getColumn())); - referenceType.setReferenced(escapeString(ref.getReferenced())); - foreignKeyType.getReference().add(referenceType); - } - foreignKeysType.getForeignKey().add(foreignKeyType); - } - tableType.setForeignKeys(foreignKeysType); - } - - // Set rows - if (tableStructure.getRows() >= 0) { - tableType.setRows(BigInteger.valueOf(tableStructure.getRows())); - } else { - throw new ModuleException() - .withMessage("Error while exporting table structure: number of table rows not set"); - } - - tablesType.getTable().add(tableType); - - tableCounter += 1; - } - - // Set views - List viewStructures = schemaStructure.getViews(); - - if (viewStructures != null && viewStructures.size() > 0) { - ViewsType viewsType = new ViewsType(); - for (ViewStructure viewStructure : viewStructures) { - - // Set view - mandatory - ViewType viewType = new ViewType(); - - // Set view name - mandatory - viewType.setName(escapeString(viewStructure.getName())); - - // Set queryOriginal - mandatory - if (StringUtils.isNotBlank(viewStructure.getQueryOriginal())) { - viewType.setQueryOriginal(viewStructure.getQueryOriginal()); - } else { - viewType.setQueryOriginal("unknown"); - } - - // Set description - if (StringUtils.isNotBlank(viewStructure.getDescription())) { - viewType.setDescription(viewStructure.getDescription()); - } - - viewsType.getView().add(viewType); - } - siardDiark.setViews(viewsType); - } - } - } - siardDiark.setTables(tablesType); - - return siardDiark; - } - - String escapeString(String s) { - if (s.matches(regex)) { - return s; - } else { - s = new StringBuilder().append("\"").append(s).append("\"").toString(); - } - return s; + protected SIARDDKAdapter getSIARDDKBinding() { + return new SIARDDK128Adapter(); } } diff --git a/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDKTableIndexFileStrategy.java b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDKTableIndexFileStrategy.java new file mode 100644 index 00000000..4b85e67b --- /dev/null +++ b/dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/out/metadata/SIARDDKTableIndexFileStrategy.java @@ -0,0 +1,262 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE file at the root of the source + * tree and available online at + * + * https://github.com/keeps/db-preservation-toolkit + */ +package com.databasepreservation.modules.siard.out.metadata; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.databasepreservation.model.exception.ModuleException; +import com.databasepreservation.model.structure.ColumnStructure; +import com.databasepreservation.model.structure.DatabaseStructure; +import com.databasepreservation.model.structure.ForeignKey; +import com.databasepreservation.model.structure.PrimaryKey; +import com.databasepreservation.model.structure.Reference; +import com.databasepreservation.model.structure.SchemaStructure; +import com.databasepreservation.model.structure.TableStructure; +import com.databasepreservation.model.structure.ViewStructure; +import com.databasepreservation.model.structure.type.Type; +import com.databasepreservation.modules.siard.common.adapters.SIARDDKAdapter; +import com.databasepreservation.modules.siard.constants.SIARDDKConstants; +import com.databasepreservation.modules.siard.out.content.LOBsTracker; + +/** + * @author Andreas Kring + * + */ +public abstract class SIARDDKTableIndexFileStrategy implements IndexFileStrategy { + + // LOBsTracker used to get the locations of functionalDescriptions + private LOBsTracker lobsTracker; + private String regex; + + public SIARDDKTableIndexFileStrategy(LOBsTracker lobsTracker) { + this.lobsTracker = lobsTracker; + regex = "(\\p{L}(_|\\w)*)|(\".*\")"; + } + + private static final Logger logger = LoggerFactory.getLogger(SIARDDKTableIndexFileStrategy.class); + + protected abstract SIARDDKAdapter getSIARDDKBinding(); + + @Override + public Object generateXML(DatabaseStructure dbStructure) throws ModuleException { + + // Set version - mandatory + SIARDDKAdapter siarddkBinding = getSIARDDKBinding(); + siarddkBinding.setVersion("1.0"); + + // Set dbName - mandatory + if (dbStructure.getDbOriginalName() != null) { + siarddkBinding.setDbName(dbStructure.getDbOriginalName()); + } else { + siarddkBinding.setDbName(dbStructure.getName()); + } + + // Set databaseProduct + if (StringUtils.isNotBlank(dbStructure.getProductName())) { + siarddkBinding.setDatabaseProduct(dbStructure.getProductName()); + } + + // Set tables - mandatory + int tableCounter = 1; + List schemas = dbStructure.getSchemas(); + // System.out.println(schemas.get(0)); + + // Check that all tables have primary keys + List tablesWithNoPrimaryKeys = new ArrayList(); + StringBuilder tableListBuilder = new StringBuilder(); + for (SchemaStructure schemaStructure : schemas) { + if (!schemaStructure.getTables().isEmpty()) { + for (TableStructure tableStructure : schemaStructure.getTables()) { + PrimaryKey primaryKey = tableStructure.getPrimaryKey(); + if (primaryKey == null) { + tablesWithNoPrimaryKeys.add(tableStructure.getName()); + tableListBuilder.append(schemaStructure.getName()).append(".").append(tableStructure.getName()) + .append(", "); + } + } + } + } + if (!tablesWithNoPrimaryKeys.isEmpty()) { + logger.warn( + "No primary keys in the following table(s): " + tableListBuilder.substring(0, tableListBuilder.length() - 2)); + } + + for (SchemaStructure schemaStructure : schemas) { + if (schemaStructure.getTables().isEmpty()) { + logger.info("No tables found in this schema: " + schemaStructure.getName()); + continue; + } else { + for (TableStructure tableStructure : schemaStructure.getTables()) { + // Set name - mandatory + String tableName = escapeString(tableStructure.getName()); + + // Set folder - mandatory + String tableFolder = "table" + Integer.toString(tableCounter); + + // Set description + String tableDescription = "Description should be entered manually"; + if (tableStructure.getDescription() != null && !tableStructure.getDescription().trim().isEmpty()) { + tableDescription = tableStructure.getDescription().trim(); + } + siarddkBinding.addTable(tableName, tableFolder, tableDescription); + + // Set columns - mandatory + int columnCounter = 1; + for (ColumnStructure columnStructure : tableStructure.getColumns()) { + + // Set column - mandatory + Type type = columnStructure.getType(); + + // Set column name - mandatory + String columnName = escapeString(columnStructure.getName()); + + // Set columnID - mandatory + String columnID = "c" + Integer.toString(columnCounter); + + // Set type - mandatory + String sql99DataType = type.getSql99TypeName(); + String columType = type.getSql99TypeName(); + if (sql99DataType.equals(SIARDDKConstants.BINARY_LARGE_OBJECT)) { + columType = "INTEGER"; + } else if (sql99DataType.equals(SIARDDKConstants.CHARACTER_LARGE_OBJECT)) { + + if (lobsTracker.getMaxClobLength(tableCounter, columnCounter) > 0) { + columType = SIARDDKConstants.DEFAULT_CLOB_TYPE + "(" + + lobsTracker.getMaxClobLength(tableCounter, columnCounter) + ")"; + } else { + columType = SIARDDKConstants.DEFAULT_CLOB_TYPE + "(1)"; + } + } else if (sql99DataType.startsWith("BIT VARYING") || sql99DataType.startsWith("BINARY VARYING")) { + + // Convert BIT VARYING/BINARY VARYING TO CHARACTER VARYING + + String length = sql99DataType.split("\\(")[1].trim(); + length = length.substring(0, length.length() - 1); + columType = "CHARACTER VARYING(" + length + ")"; + + } + + // Set typeOriginal + String columnOriginalType = StringUtils.isNotBlank(type.getOriginalTypeName()) ? type.getOriginalTypeName() + : null; + + // Set defaultValue + String columnDefaultValue = StringUtils.isNotBlank(columnStructure.getDefaultValue()) + ? columnStructure.getDefaultValue() + : null; + + // Set nullable + Boolean columnStructureNillable = columnStructure.isNillable(); + + // Set description + String columnDescription = "Description should be set"; + if (columnStructure.getDescription() != null && !columnStructure.getDescription().trim().isEmpty()) { + columnDescription = columnStructure.getDescription().trim(); + } + + // Set functionalDescription + String lobType = lobsTracker.getLOBsType(tableCounter, columnCounter); + + siarddkBinding.addColumnForTable(tableName, columnName, columnID, columType, columnOriginalType, + columnDefaultValue, columnStructureNillable, columnDescription, lobType); + columnCounter += 1; + + } + + // Set primary key - mandatory + PrimaryKey primaryKey = tableStructure.getPrimaryKey(); + if (primaryKey == null) { + siarddkBinding.addPrimaryKeyForTable(tableName, "MISSING", List.of("MISSING")); + } else { + String primaryKeyName = escapeString(primaryKey.getName()); + List columnNames = primaryKey.getColumnNames(); + List escapedColumnNames = new ArrayList<>(); + for (String columnName : columnNames) { + escapedColumnNames.add(escapeString(columnName)); + } + + siarddkBinding.addPrimaryKeyForTable(tableName, primaryKeyName, escapedColumnNames); + } + + // Set foreignKeys + List foreignKeys = tableStructure.getForeignKeys(); + if (foreignKeys != null && foreignKeys.size() > 0) { + for (ForeignKey key : foreignKeys) { + // Set key name - mandatory + String foreignKeyName = escapeString(key.getName()); + + // Set referenced table - mandatory + String foreignKeyReferencedTable = escapeString(key.getReferencedTable()); + + // Set reference - mandatory + Map escapedReferencedColumns = new LinkedHashMap<>(); + for (Reference ref : key.getReferences()) { + escapedReferencedColumns.put(escapeString(ref.getColumn()), escapeString(ref.getReferenced())); + } + + siarddkBinding.addForeignKeyForTable(tableName, foreignKeyName, foreignKeyReferencedTable, + escapedReferencedColumns); + } + } + + // Set rows + if (tableStructure.getRows() >= 0) { + siarddkBinding.setRowsForTable(tableName, BigInteger.valueOf(tableStructure.getRows())); + } else { + throw new ModuleException() + .withMessage("Error while exporting table structure: number of table rows not set"); + } + + tableCounter += 1; + } + + // Set views + List viewStructures = schemaStructure.getViews(); + + if (viewStructures != null && viewStructures.size() > 0) { + for (ViewStructure viewStructure : viewStructures) { + // Set view name - mandatory + String viewName = escapeString(viewStructure.getName()); + + // Set queryOriginal - mandatory + String viewQueryOriginal = "unknown"; + if (StringUtils.isNotBlank(viewStructure.getQueryOriginal())) { + viewQueryOriginal = viewStructure.getQueryOriginal(); + } + + // Set description + String viewDescription = StringUtils.isNotBlank(viewStructure.getDescription()) + ? viewStructure.getDescription() + : null; + + siarddkBinding.addView(viewName, viewQueryOriginal, viewDescription); + } + } + } + } + + return siarddkBinding.getSIARDDK(); + } + + String escapeString(String s) { + if (s.matches(regex)) { + return s; + } else { + s = new StringBuilder().append("\"").append(s).append("\"").toString(); + } + return s; + } +}