package com.rthoni.intellij.codefromds.dbo.template;

import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * Created by robin on 11/18/16.
 */
public class TableDataSourceDbo {

    private String _name;

    private List<ColumnDataSourceDbo> _columns = new Vector<>();

    private List<ForeignKeyDbo> _sourceForeignKeys = new Vector<>();

    private List<ForeignKeyDbo> _targetForeignKeys = new Vector<>();

    public String getName() {
        return _name;
    }

    public void setName(String name) {
        _name = name;
    }

    public List<ColumnDataSourceDbo> getColumns() {
        return _columns;
    }

    public void addColumn(ColumnDataSourceDbo column) {
        _columns.add(column);
    }

    public ColumnDataSourceDbo findColumn(String name)
    {
        for (ColumnDataSourceDbo column : _columns) {
            if (column.getName().equals(name)) {
                return column;
            }
        }
        return null;
    }

    public List<ColumnDataSourceDbo> getPrimaryKeys() {
        return _columns.stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList());
    }

    public boolean hasAny() {
        for (ColumnDataSourceDbo column : _columns) {
            if (column.isSelected()) {
                return true;
            }
        }
        return false;
    }

    public boolean hasAll()
    {
        for (ColumnDataSourceDbo column : _columns) {
            if (!column.isSelected()) {
                return false;
            }
        }
        return true;
    }

    public List<ForeignKeyDbo> getSourceForeignKeys() {
        return _sourceForeignKeys;
    }

    public void addSourceForeignKey(ForeignKeyDbo sourceForeignKeys) {
        _sourceForeignKeys.add(sourceForeignKeys);
    }

    public List<ForeignKeyDbo> getTargetForeignKeys() {
        return _targetForeignKeys;
    }

    public void addTargetForeignKey(ForeignKeyDbo targetForeignKeys) {
        _targetForeignKeys.add(targetForeignKeys);
    }
}
