/*
 * Créé le 27 juin 07
 *
 * Pour changer le modèle de ce fichier généré, allez à :
 * Fenêtre&gt;Préférences&gt;Java&gt;Génération de code&gt;Code et commentaires
 */

package fr.gouv.finances.cp.xemelios.export;

import java.util.Vector;

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 ExportElementModel implements XmlMarshallable {
	
	public static final transient String TAG = "element";
        public static final transient QName QN = new QName(TAG);
	private String id, libelle;
	private Vector<ExportChampModel> champs;
	private Vector<ExportElementModel> enfants;
	private ExportElementModel parent;
	
	public ExportElementModel (QName tagName) {
		id = libelle = "";
		champs = new Vector<ExportChampModel>();
		enfants = new Vector<ExportElementModel>();
	}

	public void addCharacterData(String cData) throws SAXException {}

	public void addChild(XmlMarshallable child, QName tagName) throws SAXException {
		if (ExportChampModel.QN.equals(tagName)) {
			champs.add((ExportChampModel)child);
		} else if (ExportElementModel.QN.equals(tagName)) {
			ExportElementModel e = (ExportElementModel)child;
			e.setParent(this);
			enfants.add(e);
		}
	}

	public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
		id = attributes.getValue("id");
		libelle = attributes.getValue("libelle");
		return this;
	}

	public void marshall(XmlOutputter output) {
		output.startTag("element");
		output.addAttribute("id", id);
		output.addAttribute("libelle", libelle);
		for (ExportChampModel ecm:champs) { ecm.marshall(output); }
		for (ExportElementModel eem:enfants) { eem.marshall(output); }
		output.endTag("element");
	}
	
	public ExportElementModel clone () {
		ExportElementModel em = new ExportElementModel(QN);
		em.setId(id);
		em.setLibelle(libelle);
		em.setParent(parent.clone());
		for (ExportChampModel e:champs) {
			try { em.addChild(e, ExportChampModel.QN); } catch (Exception ex) {}
		}
		for (ExportElementModel e:enfants) {
			try { em.addChild(e, ExportElementModel.QN); } catch (Exception ex) {}
		}
		return em;
	}

	public void validate() throws InvalidXmlDefinition {}

	public Vector<ExportElementModel> getEnfants() {
		return enfants;
	}

	public void setEnfants(Vector<ExportElementModel> enfants) {
		this.enfants = enfants;
	}

	public String getId() {
		return id;
	}

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

	public Vector<ExportChampModel> getChamps() {
		return champs;
	}

	public void setChamps(Vector<ExportChampModel> champs) {
		this.champs = champs;
	}

	public void addEnfant (ExportElementModel enf) {
		if (this.enfants==null) this.enfants = new Vector<ExportElementModel>();
		this.enfants.add(enf);
	}
	public void addChamp (ExportChampModel enf) {
		if (this.champs==null) this.champs = new Vector<ExportChampModel>();
		this.champs.add(enf);
	}

	public String getLibelle() {
		return libelle;
	}

	public void setLibelle(String libelle) {
		this.libelle = libelle;
	}
	
	public String toString () {
		return getLibelle();
	}
	
	public boolean hasChampSelected () {
		for (ExportChampModel ecm:champs) {
			if (ecm.isSelectionne()) return true;
		}
		return false;
	}
	
	public void selectChampsKey () {
		for (ExportChampModel ecm:champs) {
			if (ecm.isIdentifiant()) {
				ecm.setSelectionne(true);
			}
		}
	}
	public void selectParentChampsKey () {
		ExportElementModel p = parent;
		while (p!=null) {
			p.selectChampsKey();
			p = p.getParent();
		}
	}

	public ExportElementModel getParent() {
		return parent;
	}

	public void setParent(ExportElementModel parent) {
		this.parent = parent;
	}

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        return null;
    }

    public QName getQName() {
        return QN;
    }
	
}
