Skip to content
DLUtil.java 5.54 KiB
Newer Older
package it.inaf.oats.ia2.tapschemamanager.datalayer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public class DLUtil {

    public static <T extends TapSchemaEntity> T getEntityByName(List<T> entities, String name) {
        for (T entity : entities) {
            if (entity.getName().equals(name)) {
                return entity;
            }
        }
        return null;
    }

    public static <T extends TapSchemaEntity> List<String> getEntitiesNames(List<T> entities) {
        List<String> entitiesNames = new ArrayList<String>();
        for (T entity : entities) {
            entitiesNames.add(entity.getName());
        }
        return entitiesNames;
    }

    /**
     * Lists are already ordered because the JPA relations are created specifying @OrderBy,
     * so when you need to add an entity you should insert it in order.
     * 
     * @param <T>
     * @param entities
     * @param newEntity 
     */
    protected static <T extends TapSchemaEntity> void insertEntityInOrder(List<T> entities, T newEntity) {
        int i = 0;
        for (T entity : entities) {
            if (entity.getName().compareTo(newEntity.getName()) > 0) {
                break;
            }
            i++;
        }
        entities.add(i, newEntity);
    }

    protected static <T extends TapSchemaEntity> void removeEntity(List<T> entities, String entityName) {
        Iterator<T> iterator = entities.iterator();
        while (iterator.hasNext()) {
            T entity = iterator.next();
            if (entity.getName().equals(entityName)) {
                iterator.remove();
                return;
            }
        }
    }

    private static void setColumnDescription(TableEntity tableEntity, String columnName, String description) {
        ColumnEntity columnEntity = getEntityByName(tableEntity.getColumns(), columnName);
        columnEntity.setDescription(description);
        columnEntity.setStd(1);
    }

    protected static void putInfoIntoTapSchemaSchema(SchemaEntity schema) {

        schema.setDescription("a special schema to describe a TAP tableset");

        // SCHEMAS
        TableEntity schemasTable = schema.getTableByShortName("schemas");
        schemasTable.setDescription("description of schemas in this tableset");

        setColumnDescription(schemasTable, "schema_name", "schema name for reference to TAP_SCHEMA.schemas");
        setColumnDescription(schemasTable, "utype", "lists the utypes of schemas in the tableset");
        setColumnDescription(schemasTable, "description", "describes schemas in the tableset");

        // TABLES
        TableEntity tablesTable = schema.getTableByShortName("tables");
        tablesTable.setDescription("description of tables in this tableset");

        setColumnDescription(tablesTable, "schema_name", "the schema this table belongs to");
        setColumnDescription(tablesTable, "table_name", "the fully qualified table name");
        setColumnDescription(tablesTable, "table_type", "one of: table view");
        setColumnDescription(tablesTable, "utype", "lists the utype of tables in the tableset");
        setColumnDescription(tablesTable, "description", "describes tables in the tableset");

        // COLUMNS
        TableEntity columnsTable = schema.getTableByShortName("columns");
        columnsTable.setDescription("description of columns in this tableset");

        setColumnDescription(columnsTable, "table_name", "the table this column belongs to");
        setColumnDescription(columnsTable, "column_name", "the column name");
        setColumnDescription(columnsTable, "utype", "lists the utypes of columns in the tableset");
        setColumnDescription(columnsTable, "ucd", "lists the UCDs of columns in the tableset");
        setColumnDescription(columnsTable, "unit", "lists the unit used for column values in the tableset");
        setColumnDescription(columnsTable, "description", "describes the columns in the tableset");
        setColumnDescription(columnsTable, "datatype", "lists the ADQL datatype of columns in the tableset");
        setColumnDescription(columnsTable, "size", "lists the size of variable-length columns in the tableset");
        setColumnDescription(columnsTable, "principal", "a principal column; 1 means 1, 0 means 0");
        setColumnDescription(columnsTable, "indexed", "an indexed column; 1 means 1, 0 means 0");
        setColumnDescription(columnsTable, "std", "a standard column; 1 means 1, 0 means 0");

        // KEYS
        TableEntity keysTable = schema.getTableByShortName("keys");
        keysTable.setDescription("description of foreign keys in this tableset");

        setColumnDescription(keysTable, "key_id", "unique key to join to TAP_SCHEMA.key_columns");
        setColumnDescription(keysTable, "from_table", "the table with the foreign key");
        setColumnDescription(keysTable, "target_table", "the table with the primary key");
        setColumnDescription(keysTable, "utype", "lists the utype of keys in the tableset");
        setColumnDescription(keysTable, "description", "describes keys in the tableset");

        // KEY COLUMNS
        TableEntity keyColumnsTable = schema.getTableByShortName("key_columns");
        keyColumnsTable.setDescription("description of foreign key columns in this tableset");

        setColumnDescription(keyColumnsTable, "key_id", "key to join to TAP_SCHEMA.keys");
        setColumnDescription(keyColumnsTable, "from_column", "column in the from_table");
        setColumnDescription(keyColumnsTable, "target_column", "column in the target_table");
    }