package com.ebao.aureum.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class ObjectModel implements Serializable {

	private static final long serialVersionUID = 1L;

	private String rootTable;

	private String rootColumn;

	private String rootValue;

	private Map<String, TableModel> nameOftable = new LinkedHashMap<String, TableModel>();

	ObjectModel(String rootTable, String rootColumn, String rootValue) {
		this.rootTable = rootTable;
		this.rootColumn = rootColumn;
		this.rootValue = rootValue;
	}

	public String getRootTable() {
		return rootTable;
	}

	public String getRootColumn() {
		return rootColumn;
	}

	public String getRootValue() {
		return rootValue;
	}

	boolean containsName(String tableName) {
		return nameOftable.containsKey(tableName);
	}

	public Collection<TableModel> getTables() {
		return nameOftable.values();
	}

	public Collection<String> getTableNames() {
		return nameOftable.keySet();
	}

	public TableModel getTable(String tableName) {
		return nameOftable.get(tableName);
	}

	boolean addTable(TableModel table) {
		TableModel oldTable = nameOftable.get(table.getTableName());
		if (oldTable == null) {
			nameOftable.put(table.getTableName(), table);
			return true;
		} else {
			return oldTable.addRows(table.getRows());
		}
	}

	boolean addTables(Collection<TableModel> tables) {
		boolean ret = false;
		for (TableModel table : tables) {
			ret |= addTable(table);
		}
		return ret;
	}

	public ObjectModel clone() {
		ObjectModel clone = new ObjectModel(rootTable, rootColumn, rootValue);
		for(Map.Entry<String, TableModel> entry : this.nameOftable.entrySet()){
			clone.nameOftable.put(entry.getKey(), entry.getValue().clone()); 
		}
		return clone;
	}
	

}
