/*
 * 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 TDocument implements XmlMarshallable {
	public static final String TAG = "document";
	
	private String id, repositoryImportXsltFile;
	private TTable repositoryTable, listBcTable, specialKeyTable;
	private Hashtable<String,TEtat> etats;
	private String baseDirectory;

	public TDocument(String tagName) {
		super();
		etats = new Hashtable<String,TEtat>();
	}
	public void setBaseDirectory(String baseDir) {
		baseDirectory=baseDir;
		for(TEtat etat:etats.values()) etat.setBaseDirectory(baseDir);
	}

	public void addCharacterData(String cData) throws SAXException { }

	public void addChild(XmlMarshallable child, String tagName) throws SAXException {
		if(TTable.TAG.equals(tagName)) {
			TTable table  = (TTable)child;
			if("repository".equals(table.getType())) repositoryTable = table;
			else if("special-key".equals(table.getType())) specialKeyTable = table;
			else if("list-bc".equals(table.getType())) listBcTable = table;
		} else if(TEtat.TAG.equals(tagName)) {
			TEtat etat = (TEtat)child;
			etat.setBaseDirectory(baseDirectory);
			etats.put(etat.getId(),etat);
		}
	}

	public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
		id = attributes.getValue("id");
		repositoryImportXsltFile = attributes.getValue("repository-import-xslt-file");
		return this;
	}

	public void marshall(XmlOutputter output) { }

	public void validate() throws InvalidXmlDefinition { }
	@Override
	public TDocument clone() {
		TDocument other = new TDocument ("");
		other.id = this.id;
		other.repositoryImportXsltFile = this.repositoryImportXsltFile;
		other.repositoryTable = this.repositoryTable.clone();
		other.listBcTable = this.listBcTable.clone();
		other.specialKeyTable = this.specialKeyTable.clone();
		for (String s:etats.keySet()) {
			other.etats.put(s, this.etats.get(s).clone());
		}
		other.baseDirectory=baseDirectory;
		return other;
	}

	public String getId() {
		return id;
	}

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

	public String getRepositoryImportXsltFile() {
		return repositoryImportXsltFile;
	}

	public void setRepositoryImportXsltFile(String repositoryImportXsltFile) {
		this.repositoryImportXsltFile = repositoryImportXsltFile;
	}
	public TEtat getEtat(String etatId) { return etats.get(etatId); }

	public TTable getListBcTable() {
		return listBcTable;
	}

	public void setListBcTable(TTable listBcTable) {
		this.listBcTable = listBcTable;
	}

	public TTable getRepositoryTable() {
		return repositoryTable;
	}

	public void setRepositoryTable(TTable repositoryTable) {
		this.repositoryTable = repositoryTable;
	}

	public TTable getSpecialKeyTable() {
		return specialKeyTable;
	}

	public void setSpecialKeyTable(TTable specialKeyTable) {
		this.specialKeyTable = specialKeyTable;
	}
	
	public Iterable<TEtat> getEtats() {
		return etats.values();
	}
}
