Skip to content
TapSchema.java 19.8 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
/* 
 * _____________________________________________________________________________
 * 
 * INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
 * Trieste INAF - IA2 Italian Center for Astronomical Archives
 * _____________________________________________________________________________
 * 
 * Copyright (C) 2016 Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License Version 3 as published by the
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package it.inaf.ia2.tsm;

import it.inaf.ia2.tsm.datalayer.DBBroker;
import it.inaf.ia2.tsm.datalayer.DBBrokerFactory;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import it.inaf.ia2.tsm.xmlmodel.TableModel;
import it.inaf.ia2.tsm.xmlmodel.TapSchemaModel;
import it.inaf.ia2.tsm.xmlmodel.TapSchemaModels;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The main implementation of {@link TapSchema}.
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class TapSchema implements EntitiesContainer<Schema>, Serializable {

    // Mandatory tables constants
    public static final String TABLES_TABLE = "tables";
    public static final String SCHEMAS_TABLE = "schemas";
    public static final String COLUMNS_TABLE = "columns";
    public static final String KEYS_TABLE = "keys";
    public static final String KEY_COLUMNS_TABLE = "key_columns";

    private static final long serialVersionUID = 1678083091602571256L;

    private static final Logger LOG = LoggerFactory.getLogger(TapSchema.class);

    private final Map<String, Schema> schemas;
    private final List<KeyMetadata> keysMetadata;
    private final Set<Key> allKeys;
    private final ConsistencyChecks consistencyChecks;

    private boolean loading;
    private String version;
    private DBWrapper dbWrapper;
    private String tapSchemaName;
    private boolean exists;

    private transient DBBroker sourceDBBroker;
    private transient DBBroker tapSchemaDBBroker;

    public final DBBroker getSourceDBBroker() {
        if (sourceDBBroker == null) {
            sourceDBBroker = DBBrokerFactory.getDBBroker(dbWrapper.getSourceDataSourceWrapper());
        }
        return sourceDBBroker;
    }

    public final DBBroker getTapSchemaDBBroker() {
        if (tapSchemaDBBroker == null) {
            tapSchemaDBBroker = DBBrokerFactory.getDBBroker(dbWrapper.getTapSchemaDataSourceWrapper());
        }
        return tapSchemaDBBroker;
    }

    private TapSchema() {
        // for serialization
        schemas = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        allKeys = new HashSet<>();
        keysMetadata = new ArrayList<>();
        consistencyChecks = new ConsistencyChecks();
    }

    public TapSchema(String version, DBWrapper dbWrapper, String tapSchemaName, boolean exists) throws SQLException {
        this();

        loading = true;
        this.version = version;
        this.dbWrapper = dbWrapper;
        this.tapSchemaName = tapSchemaName;
        this.exists = exists;

        // Initializing schemas map
        for (String schemaName : getSourceDBBroker().getAllSchemaNames()) {
            schemas.put(schemaName, null);
        }
        schemas.put(tapSchemaName, null); // the TAP_SCHEMA contains itself

//        if (exists) {
//            DaoSchema.fillSavedSchemas(dbWrapper, (this));
//            DaoTable.fillSavedTables(dbWrapper, (this));
//            DaoColumn.fillSavedColumns(dbWrapper, (this));
//            DaoKey.fillSavedKeys(dbWrapper, (this));
//        }
        loading = false;
        checkKeys();
    }

    public DBBroker getDBBroker(String schemaName) {
        if (schemaName.equals(tapSchemaName)) {
            return getTapSchemaDBBroker();
        } else {
            return getSourceDBBroker();
        }
    }

    /**
     * The name of the TAP_SCHEMA schema.
     */
    public String getName() {
        return tapSchemaName;
    }

    /**
     * The version selected for this TAP_SCHEMA.
     */
    public String getVersion() {
        return version;
    }

    private void loadSchemaKeysMetadata(String schemaName) throws SQLException {
        keysMetadata.addAll(getDBBroker(schemaName).getKeysMetadata(schemaName));
    }

    protected Set<Key> getAllKeys() {
        return allKeys;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Schema addChild(String schemaName) throws SQLException {
        LOG.debug("Adding schema {}", schemaName);

        Schema schema;

        if (!schemas.containsKey(schemaName)) {

            consistencyChecks.addUnexistingSchema(schemaName);
            schema = null;
        } else {

            schema = schemas.get(schemaName);

            if (schema == null) {
                schema = new Schema(this, schemaName);
                schema.setStatus(Status.ADDED_NOT_PERSISTED);
                schemas.put(schemaName, schema);
                loadSchemaKeysMetadata(schemaName);
            } else {
                switch (schema.getStatus()) {
                    case TO_REMOVE:
                        schema.setStatus(Status.ADDED_PERSISTED);
                        break;
                    case REMOVED_NOT_PERSISTED:
                        schema.setStatus(Status.ADDED_NOT_PERSISTED);
                        break;
                    default:
                        throw new IllegalArgumentException("Cannot add the schema " + schemaName + ". Invalid status. Schema status is " + schema.getStatus());
                }
            }
        }

        checkKeys();

        return schema;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeChild(String schemaName) {
        LOG.debug("Removing schema {}", schemaName);

        if (!schemas.containsKey(schemaName)) {
            throw new IllegalArgumentException("The database doesn't contains a schema named " + schemaName);
        }

        Schema schema = schemas.get(schemaName);
        if (schema == null || schema.getStatus() == Status.LOADED) {
            throw new IllegalArgumentException("Cannot remove the schema " + schemaName + ". It has never been added.");
        }

        if (schema.getStatus() == Status.ADDED_NOT_PERSISTED) {
            schema.setStatus(Status.REMOVED_NOT_PERSISTED);
        } else if (schema.getStatus() == Status.ADDED_PERSISTED) {
            schema.setStatus(Status.TO_REMOVE);
        }

        checkKeys();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Schema getChild(String childName, Status... statuses) {
        return TSMUtil.getChild(schemas, childName, statuses);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Schema> getChildren(Status... statuses) {
        return TSMUtil.getChildrenByStatus(schemas.values(), statuses);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> getAddableChildrenNames() {
        return TSMUtil.getAddableChildrenNames(schemas);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Schema> getAddedChildren() {
        return getChildren(Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Schema> getAddedOrRemovedChildren() {
        return getChildren(Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED, Status.TO_REMOVE, Status.REMOVED_NOT_PERSISTED);
    }

    /**
     * This method has to be used after TAP_SCHEMA modifications are committed
     * to the database, in order to remove from the memory the schemas with
     * status {@code Status.TO_REMOVE} or {@code Status.REMOVED_NOT_PERSISTED}.
     */
    public void cleanSchema(String schemaName) {
        if (!schemas.containsKey(schemaName)) {
            throw new IllegalArgumentException("The TAP_SCHEMA doesn't contain the schema " + schemaName);
        }
        schemas.put(schemaName, null);
    }

    /**
     * Save or update the TAP_SCHEMA changes into the database.
     */
    public void save() throws SQLException {

        fillKeyIds();

        DBBroker broker = getTapSchemaDBBroker();

        broker.save(this);

        if (!exists) {
            // Adding TAP_SCHEMA into TAP_SCHEMA
            Schema tapSchemaSchema = addChild(tapSchemaName);
            for (String tableName : tapSchemaSchema.getAddableChildrenNames()) {
                Table table = tapSchemaSchema.addChild(tableName);
                for (String columnName : table.getAddableChildrenNames()) {
                    table.addChild(columnName);
                }
            }
            LOG.debug(this.toString());
            TSMUtil.putInfoIntoTapSchemaSchema(tapSchemaSchema);

            exists = true; // important!

            //broker.save(this); // save again
        }

        exists = true;

        consistencyChecks.getInconsistencies().clear();
    }

    /**
     * Retrieve the maximum key id from all the schemas that are added into the
     * TAP_SCHEMA.
     *
     * @return the maximum key, if it exists, zero otherwise.
     */
    private int getMaxKeyId() {
        int maxKeyId = 0;
        for (Key key : allKeys) {
            if (key.getId() != null && !KeyMetadata.NEW_KEY.equals(key.getId())) {
                int keyId = Integer.parseInt(key.getId());
                if (keyId > maxKeyId) {
                    maxKeyId = keyId;
                }
            }
        }
        return maxKeyId;
    }

    private void fillKeyIds() {

        List<Key> newKeys = new ArrayList<>();
        for (Key key : allKeys) {
            if (key.isVisible() && KeyMetadata.NEW_KEY.equals(key.getId())) {
                newKeys.add(key);
            }
        }

        int maxKeyId = getMaxKeyId();
        for (Key newKey : newKeys) {
            maxKeyId++;
            newKey.setId(maxKeyId + "");
        }
    }

//    /**
//     * Set keys visibility based on other entities visibility (a key is visible
//     * if all schemas, tables and columns involved have
//     * {@link Status} {@code ADDED_PERSISTED} or {@code ADDED_NOT_PERSISTED}).
//     */
//    protected void checkKeys() {
//
//        int currentKey = getMaxKeyId() + 1;
//
//        for (Key key : allKeys) {
//
//            ((Key) key).setVisible(false);
//
//            Schema fromSchema = getChild(key.getFromSchemaName(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//            Schema targetSchema = getChild(key.getTargetSchemaName(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//            if (fromSchema != null && targetSchema != null) {
//
//                Table fromTable = fromSchema.getChild(key.getFromTableSimpleName(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//                Table targetTable = targetSchema.getChild(key.getTargetTableSimpleName(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//
//                if (fromTable != null && targetTable != null) {
//
//                    boolean allColumnsVisible = true;
//
//                    for (KeyColumn keyColumn : key.getKeyColumns()) {
//
//                        Column fromColumn = fromTable.getChild(keyColumn.getFromColumn(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//                        Column targetColumn = targetTable.getChild(keyColumn.getTargetColumn(), Status.ADDED_PERSISTED, Status.ADDED_NOT_PERSISTED);
//
//                        if (fromColumn == null || targetColumn == null) {
//                            allColumnsVisible = false;
//                            break;
//                        }
//                    }
//
//                    if (allColumnsVisible) {
//                        ((Key) key).setVisible(true);
//                        if (key.getId() == null) {
//                            key.setId(currentKey + "");
//                            currentKey++;
//                        }
//                    }
//                }
//            }
//        }
//        for (Key key : allKeys) {
//            log.debug("{} [{}]", key, key.getStatus());
//        }
//    }
//
//    public void addFictitiousKey(Table fromTable, String[] fromColumns, Table targetTable, String[] targetColumns) {
//        Key key = new Key(dbWrapper, this, fromTable.getCompleteName(), targetTable.getCompleteName());
//        key.setId((getMaxKeyId() + 1) + "");
//
//        for (int i = 0; i < fromColumns.length; i++) {
//            key.addKeyColumn(fromColumns[i], targetColumns[i]);
//        }
//
//        fromTable.addFromKey(key);
//        targetTable.addTargetKey(key);
//
//        allKeys.add(key);
//        checkKeys();
//    }
    /**
     * Automatically add and remove keys that should be visible.
     */
    public final void checkKeys() {
        if (!loading) {

            for (KeyMetadata keyMetadata : keysMetadata) {

                // Check if key should be exposed in TAP_SCHEMA
                boolean keyVisible = true;

                for (String fromColumn : keyMetadata.getFromColumns()) {
                    if (!isColumnVisible(keyMetadata.getFromSchema(), keyMetadata.getFromTable(), fromColumn)) {
                        keyVisible = false;
                        break;
                    }
                }
                if (keyVisible) {
                    for (String targetColumn : keyMetadata.getTargetColumns()) {
                        if (!isColumnVisible(keyMetadata.getTargetSchema(), keyMetadata.getTargetTable(), targetColumn)) {
                            keyVisible = false;
                            break;
                        }
                    }
                }

                Key key = null;
                for (Key k : allKeys) {
                    if (k.getKeyMetadata().equals(keyMetadata)) {
                        key = k;
                        break;
                    }
                }

                // TODO: use status instead of set visibile [?]
                if (keyVisible) {
                    if (key == null) {
                        key = new Key(this, keyMetadata);
                        allKeys.add(key);
                    }
                    key.setVisible(true);
                } else if (key != null) {
                    key.setVisible(false);
                }
            }
        }
    }

    /**
     * Print all TAP_SCHEMA tree (useful for debugging).
     */
    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("\n");

        sb.append(String.format(">> TAP_SCHEMA %s <<\n", tapSchemaName));

//        for (Schema schema : getChildren()) {
//            sb.append("--");
//            sb.append(schema.getName());
//            sb.append(String.format(" [%s]", schema.getStatus()));
//            sb.append("\n");
//            List<Table> tables = schema.getChildren();
//            for (int i = 0; i < tables.size(); i++) {
//                Table table = tables.get(i);
//                sb.append("   |--");
//                sb.append(table.getName());
//                sb.append(String.format(" [%s]", table.getStatus()));
//                sb.append("\n");
//
//                String padder = i < tables.size() - 1 ? "   |   " : "       ";
//
//                for (Column column : table.getChildren()) {
//                    sb.append(padder);
//                    sb.append("|--");
//                    sb.append(column.getName());
//                    sb.append(String.format(" [%s]", column.getStatus()));
//                    sb.append("\n");
//                }
//
//                if (table.getAllFromKeys().size() > 0) {
//                    sb.append(padder);
//                    sb.append("** From keys **\n");
//                    for (Key fromKey : table.getAllFromKeys()) {
//                        sb.append(padder);
//                        sb.append("* ");
//                        sb.append(fromKey.toString());
//                        sb.append(String.format(" [visible=%s]", fromKey.isVisible()));
//                        sb.append("\n");
//                    }
//                }
//                if (table.getAllTargetKeys().size() > 0) {
//                    sb.append(padder);
//                    sb.append("** Target keys **\n");
//                    for (Key targetKey : table.getAllTargetKeys()) {
//                        sb.append(padder);
//                        sb.append("* ");
//                        sb.append(targetKey.toString());
//                        sb.append(String.format(" [visible=%s]", targetKey.isVisible()));
//                        sb.append("\n");
//                    }
//                }
//
//                sb.append("\n");
//            }
//        }
        return sb.toString();
    }

    public boolean isSchemaVisible(String schemaName) {
        Schema schema = schemas.get(schemaName);
        if (schema == null) {
            return false;
        }
        return schema.getStatus() == Status.ADDED_PERSISTED
                || schema.getStatus() == Status.ADDED_NOT_PERSISTED;
    }

    public boolean isTableVisible(String schemaName, String tableName) {
        if (!isSchemaVisible(schemaName)) {
            return false;
        }

        Table table = schemas.get(schemaName).getChild(tableName);
        if (table == null) {
            return false;
        }

        if (table.getStatus() == Status.ADDED_PERSISTED
                || table.getStatus() == Status.ADDED_NOT_PERSISTED) {
            return isSchemaVisible(schemaName);
        }

        return false;
    }

    public boolean isColumnVisible(String schemaName, String tableName, String columnName) {
        if (!isTableVisible(schemaName, tableName)) {
            return false;
        }

        Column column = schemas.get(schemaName).getChild(tableName).getChild(columnName);
        if (column == null) {
            return false;
        }

        if (column.getStatus() == Status.ADDED_PERSISTED
                || column.getStatus() == Status.ADDED_NOT_PERSISTED) {
            return isTableVisible(schemaName, tableName);
        }

        return false;
    }

    /**
     * Define if the TAP_SCHEMA schema was already written into the database.
     */
    public boolean exists() {
        return exists;
    }

    public ConsistencyChecks getConsistencyChecks() {
        return consistencyChecks;
    }

    public TapSchemaModel getTapSchemaModel() {
        return TapSchemaModels.getTapSchemaModel(version);
    }

    public TableModel getTableModel(String tableName) {
        return getTapSchemaModel().getTables().get(tableName);
    }

    public KeyMetadata searchKeyMetadata(String fromSchemaName, String fromTableName, String fromColumnName) {
        for (KeyMetadata km : keysMetadata) {
            if (km.getFromSchema().equals(fromSchemaName) && km.getFromTable().equals(fromTableName)
                    && km.getFromColumns().contains(fromColumnName)) {
                return km;
            }
        }
        return null;
    }

    public Map<String, Object> getSchemaMetadata(String schemaName) {
        Map<String, Object> metadata = new HashMap<>();
        metadata.put(Schema.SCHEMA_NAME_KEY, schemaName);
        return metadata;
    }

    public List<Key> getVisibileKeys() {
        List<Key> visibleKeys = new ArrayList<>();
        for (Key key : allKeys) {
            if (key.isVisible()) {
                visibleKeys.add(key);
            }
        }
        return visibleKeys;
    }
}