/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.data.impl.sqlconfig;

import java.util.Hashtable;

import org.xml.sax.SAXException;

import fr.gouv.finances.cp.utils.xml.marshal.InvalidXmlDefinition;
import fr.gouv.finances.cp.utils.xml.marshal.XmlAttributes;
import fr.gouv.finances.cp.utils.xml.marshal.XmlMarshallable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;

public class TTable implements XmlMarshallable, Comparable {
	public static final String TAG = "table";
	
	private String baseName, basedOn, id, type;
	private boolean mainTable;
	private int version;
	private Hashtable<Integer,TPatch> patches;
	private StringBuffer sb;

	public String getBasedOn() {
		return basedOn;
	}

	public void setBasedOn(String basedOn) {
		this.basedOn = basedOn;
	}

	public String getBaseName() {
		return baseName;
	}

	public void setBaseName(String baseName) {
		this.baseName = baseName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public boolean isMainTable() {
		return mainTable;
	}

	public void setMainTable(boolean mainTable) {
		this.mainTable = mainTable;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public TTable(String tagName) {
		super();
		patches = new Hashtable<Integer,TPatch>();
		sb = new StringBuffer();
	}

	public void addCharacterData(String cData) throws SAXException {
		sb.append(cData);
	}

	public void addChild(XmlMarshallable child, String tagName) throws SAXException {
		if(TPatch.TAG.equals(tagName)) {
			TPatch patch = (TPatch)child;
                        patch.setTable(this);
			patches.put(new Integer(patch.getFromVersion()),patch);
		}
	}

	public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
		id=attributes.getValue("id");
		baseName = attributes.getValue("base-name");
		basedOn = attributes.getValue("based-on");
		mainTable = attributes.getBooleanValue("main");
		version=attributes.getIntValue("version");
		type=attributes.getValue("type");
		return this;
	}

	public void marshall(XmlOutputter output) { }

	public void validate() throws InvalidXmlDefinition { }
	@Override
	public TTable clone() {
		TTable other = new TTable("");
		other.id = this.id;
		other.baseName = this.baseName;
		other.basedOn = this.basedOn;
		other.mainTable = this.mainTable;
		other.version = this.version;
		other.type = this.type;
		other.sb.append(this.sb.toString());
		for (Integer s:patches.keySet()) {
			other.patches.put(s, this.patches.get(s).clone(other));
		}
		
		return other;
	}
	public String getScript() { return sb.toString().replaceAll("\\$\\{base-name\\}",getBaseName()); }
	public TPatch getPatchFromVersion(int version) { return patches.get(new Integer(version)); }
	public String toString() {
//		StringBuffer buff = new StringBuffer();
//		buff.append("id=").append(id).append("\nbase-name=").append(baseName);
//		return buff.toString();
		return getBaseName();
	}
        public boolean equals(Object o) {
            if(o instanceof TTable) {
                TTable other = (TTable)o;
                return other.baseName.equals(baseName);
            }
            return false;
        }

    public int compareTo(Object o) {
        if(o instanceof TTable) {
                TTable other = (TTable)o;
                return other.baseName.compareTo(baseName);
        }
        return -1;
    }
}
