Skip to content
KeyMetadata.java 5.25 KiB
Newer Older
/*
 * _____________________________________________________________________________
 * 
 * INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
 * Trieste INAF - IA2 Italian Center for Astronomical Archives
 * _____________________________________________________________________________
 * 
 * Copyright (C) 2017 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 java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class KeyMetadata implements Serializable {

    public static final String NEW_KEY = "#NEW#";
    private static final long serialVersionUID = -30688540834789724L;

    private final String fromSchema;
    private final String fromTable;
    private final List<String> fromColumns;
    private final String targetSchema;
    private final String targetTable;
    private final List<String> targetColumns;

    public KeyMetadata(String fromSchema, String fromTable, String targetSchema, String targetTable) {
        this.fromSchema = fromSchema;
        this.fromTable = fromTable;
        this.fromColumns = new ArrayList<>();
        this.targetSchema = targetSchema;
        this.targetTable = targetTable;
        this.targetColumns = new ArrayList<>();
    }

    public String getFromSchema() {
        return fromSchema;
    }

    public String getFromTable() {
        return fromTable;
    }

    public List<String> getFromColumns() {
        return fromColumns;
    }

    public String getTargetSchema() {
        return targetSchema;
    }

    public String getTargetTable() {
        return targetTable;
    }

    public List<String> getTargetColumns() {
        return targetColumns;
    }

    public void addKeyColumn(String fromColumn, String targetColumn) {
        fromColumns.add(fromColumn);
        targetColumns.add(targetColumn);
    }

    public Map<String, Object> getKeyMetadata() {
        Map<String, Object> metadata = new HashMap<>();
        metadata.put(Key.ID_KEY, NEW_KEY);
        metadata.put(Key.FROM_TABLE_KEY, fromSchema + "." + fromTable);
        metadata.put(Key.TARGET_TABLE_KEY, targetSchema + "." + targetTable);
        return metadata;
    }

    public Map<String, Object> getKeyColumnMetadata(String fromColumn, String targetColumn) {
        Map<String, Object> metadata = new HashMap<>();
        metadata.put(KeyColumn.KEY_ID_KEY, NEW_KEY);
        metadata.put(KeyColumn.FROM_COLUMN_KEY, fromColumn);
        metadata.put(KeyColumn.TARGET_COLUMN_KEY, targetColumn);
        return metadata;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + Objects.hashCode(this.fromSchema);
        hash = 17 * hash + Objects.hashCode(this.fromTable);
        for (String fromColumn : fromColumns) {
            hash = 17 * hash + Objects.hashCode(fromColumn);
        }
        hash = 17 * hash + Objects.hashCode(this.targetSchema);
        hash = 17 * hash + Objects.hashCode(this.targetTable);
        for (String targetColumn : targetColumns) {
            hash = 17 * hash + Objects.hashCode(targetColumn);
        }
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final KeyMetadata other = (KeyMetadata) obj;
        if (!Objects.equals(this.fromSchema, other.fromSchema)) {
            return false;
        }
        if (!Objects.equals(this.fromTable, other.fromTable)) {
            return false;
        }
        if (!Objects.equals(this.targetSchema, other.targetSchema)) {
            return false;
        }
        if (!Objects.equals(this.targetTable, other.targetTable)) {
            return false;
        }
        if (this.fromColumns.size() != other.fromColumns.size()) {
            return false;
        }
        for (int i = 0; i < fromColumns.size(); i++) {
            String fromColumn = fromColumns.get(i);
            if (!Objects.equals(fromColumn, other.fromColumns.get(i))) {
                return false;
            }
        }
        if (this.targetColumns.size() != other.targetColumns.size()) {
            return false;
        }
        for (int i = 0; i < targetColumns.size(); i++) {
            String targetColumn = targetColumns.get(i);
            if (!Objects.equals(targetColumn, other.targetColumns.get(i))) {
                return false;
            }
        }
        return true;
    }
}