/*
 * 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU 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.Attributes;
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;
import javax.xml.namespace.QName;

public class TDocument implements XmlMarshallable {

    public static final transient String TAG = "document";
    public static final transient QName QN = new QName(TAG);
    private String id,  repositoryImportXsltFile;
    private TTable repositoryTable,  listBcTable,  specialKeyTable;
    private Hashtable<String, TEtat> etats;
    private String baseDirectory, extendedDoc;

    public TDocument(QName 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, QName tagName) throws SAXException {
        if (TTable.QN.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.QN.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");
//        extendedDoc = attributes.getValue("extends");
        return this;
    }

    public void marshall(XmlOutputter output) {
    }

    public void validate() throws InvalidXmlDefinition {
    }

    @Override
    public TDocument clone() {
        TDocument other = new TDocument(QN);
        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();
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        QName name = new QName(uri,localName);
        if(TTable.QN.equals(name)) {
            String type = atts.getValue("type");
            if ("repository".equals(type)) {
                return repositoryTable;
            } else if ("special-key".equals(type)) {
                return specialKeyTable;
            } else if ("list-bc".equals(type)) {
                return listBcTable;
            }
        } else if (TEtat.QN.equals(name)) {
            return etats.get(atts.getValue("id"));
        }
        return null;
    }

    public QName getQName() {
        return QN;
    }

//    public String getExtendedDoc() {
//        return extendedDoc;
//    }
}
