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

	public void addCharacterData(String cData) throws SAXException {}

	public void addChild(XmlMarshallable child, String tagName) throws SAXException {
		if (ExportChampModel.TAG.equals(tagName)) {
			champs.add((ExportChampModel)child);
		} else if (ExportElementModel.TAG.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(TAG);
		em.setId(id);
		em.setLibelle(libelle);
		em.setParent(parent.clone());
		for (ExportChampModel e:champs) {
			try { em.addChild(e, ExportChampModel.TAG); } catch (Exception ex) {}
		}
		for (ExportElementModel e:enfants) {
			try { em.addChild(e, ExportElementModel.TAG); } 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;
	}
	
}
