Search in sources :

Example 6 with TableMetadata

use of org.molgenis.emx2.TableMetadata in project molgenis-emx2 by molgenis.

the class SqlTableMetadata method alterColumn.

@Override
public TableMetadata alterColumn(String columnName, Column column) {
    // ignore mg_ columns
    if (column.getName().startsWith("mg_"))
        return this;
    Column oldColumn = getColumn(columnName);
    if (oldColumn == null) {
        throw new MolgenisException("Alter column failed: Column  '" + getTableName() + "." + column.getName() + "' does not exist");
    }
    if (getInherit() != null && getInheritedTable().getColumn(columnName) != null) {
        throw new MolgenisException("Alter column " + getTableName() + "." + columnName + " failed: column is part of inherited table " + getInherit());
    }
    if (getInherit() != null && getInheritedTable().getColumn(column.getName()) != null) {
        throw new MolgenisException("Rename column from " + getTableName() + "." + columnName + " to " + getTableName() + "." + column.getName() + " failed: column '" + column.getName() + "' is part of inherited table " + getInherit());
    }
    getDatabase().tx(db -> sync(alterColumnTransaction(getSchemaName(), getTableName(), columnName, column, db)));
    // reload the state
    ((SqlSchemaMetadata) getSchema()).sync(getDatabase().getSchema(getSchemaName()).getMetadata());
    return this;
}
Also used : MetadataUtils.deleteColumn(org.molgenis.emx2.sql.MetadataUtils.deleteColumn)

Example 7 with TableMetadata

use of org.molgenis.emx2.TableMetadata in project molgenis-emx2 by molgenis.

the class SettingsDataModel method create.

public static void create(Schema schema) {
    TableMetadata tm = table("Settings");
    tm.add(column("schema").setKey(1));
    tm.add(column("key").setKey(1));
    tm.add(column("value"));
    schema.create(tm);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata)

Example 8 with TableMetadata

use of org.molgenis.emx2.TableMetadata in project molgenis-emx2 by molgenis.

the class TableSort method sortTableByDependency.

public static void sortTableByDependency(List<TableMetadata> tableList) {
    ArrayList<TableMetadata> result = new ArrayList<>();
    ArrayList<TableMetadata> todo = new ArrayList<>(tableList);
    // ensure deterministic order
    todo.sort(new Comparator<TableMetadata>() {

        @Override
        public int compare(TableMetadata o1, TableMetadata o2) {
            return o1.getTableName().compareTo(o2.getTableName());
        }
    });
    while (!todo.isEmpty()) {
        int size = todo.size();
        for (int i = 0; i < todo.size(); i++) {
            TableMetadata current = todo.get(i);
            boolean depends = false;
            for (int j = 0; j < todo.size(); j++) {
                if (current.getInherit() != null && current.getImportSchema() == null && todo.get(j).equals(current.getInheritedTable())) {
                    depends = true;
                    break;
                }
            }
            if (!depends)
                for (Column c : current.getColumns()) {
                    if (c.getRefTableName() != null && !c.isRefback()) {
                        for (int j = 0; j < todo.size(); j++) {
                            if (i != j && (todo.get(j).getTableName().equals(c.getRefTableName()))) {
                                depends = true;
                                break;
                            }
                        }
                    }
                }
            if (!depends) {
                result.add(todo.get(i));
                todo.remove(i);
            }
        }
        // check for circular relationship
        if (size == todo.size()) {
            throw new MolgenisException("circular dependency error: following tables have circular dependency: " + todo.stream().map(TableMetadata::getTableName).collect(Collectors.joining(",")));
        }
    }
    tableList.clear();
    tableList.addAll(result);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata) Column(org.molgenis.emx2.Column) ArrayList(java.util.ArrayList) MolgenisException(org.molgenis.emx2.MolgenisException)

Example 9 with TableMetadata

use of org.molgenis.emx2.TableMetadata in project molgenis-emx2 by molgenis.

the class TestGetSetAllTypesOnRows method addContents.

private void addContents(SchemaMetadata m, List<ColumnType> columnTypes) {
    TableMetadata t = m.create(table("TypeTest"));
    for (ColumnType columnType : columnTypes) {
        t.add(column("test" + columnType).setType(columnType).setRequired(true));
        t.add(column("test" + columnType + "_nullable").setType(columnType));
        t.add(column("test" + columnType + "_readonly").setType(columnType).setReadonly(true));
    }
}
Also used : ColumnType(org.molgenis.emx2.ColumnType)

Example 10 with TableMetadata

use of org.molgenis.emx2.TableMetadata in project molgenis-emx2 by molgenis.

the class OpenApiYamlGenerator method createOpenApi.

public static OpenAPI createOpenApi(SchemaMetadata schema) {
    Paths paths = new Paths();
    Components components = new Components();
    // basic metadata
    OpenAPI api = new OpenAPI();
    api.info(info(schema));
    api.security(securityRequirementList());
    // components
    components.addSecuritySchemes("ApiKeyAuth", securityScheme());
    components.addSchemas(PROBLEM, problemSchema());
    components.addResponses(PROBLEM, problemResponse());
    // api/zip/:schema
    String zipType = "Zip";
    String zipPrefix = "/" + schema.getName() + "/api/zip";
    PathItem zipPath = getSchemaOperations(zipType, ACCEPT_ZIP);
    paths.addPathItem(zipPrefix, zipPath);
    // api/excel/:schema
    String excelType = "Excel";
    String excelPrefix = "/" + schema.getName() + "/api/excel";
    PathItem excelPath = getSchemaOperations(excelType, ACCEPT_EXCEL);
    paths.addPathItem(excelPrefix, excelPath);
    // table paths
    for (TableMetadata table : schema.getTables()) {
        paths.addPathItem(excelPrefix + "/" + table.getTableName(), getExcelTableOperations(excelType));
        paths.addPathItem(zipPrefix + "/" + table.getTableName(), getExcelTableOperations(zipType));
    }
    // api/csv/:schema
    // assembly
    api.setPaths(paths);
    api.setComponents(components);
    return api;
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata)

Aggregations

TableMetadata (org.molgenis.emx2.TableMetadata)8 ColumnType (org.molgenis.emx2.ColumnType)6 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Column (org.molgenis.emx2.Column)2 SchemaMetadata (org.molgenis.emx2.SchemaMetadata)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 MolgenisException (org.molgenis.emx2.MolgenisException)1 MetadataUtils.deleteColumn (org.molgenis.emx2.sql.MetadataUtils.deleteColumn)1