package Modele;
import Config.JDOM;
import Config.PromoJDOM;
import java.util.LinkedList;
import java.util.List;
import org.jdom2.Element;
/**
* Classe GestionPromotion.
*
*
* @author Tiphaine TEYSSIER
* @author Tan Phong HUYNH
*
*/
public class GestionPromotion {
private LinkedList<Promotion> promotions;
private static GestionPromotion instance;
/**
*
*/
private GestionPromotion() {
promotions = new LinkedList<Promotion>();
loadFromXML();
}
/**
* recupere l'instance et charge les promos du XML(au premier appel)
* @return
*/
public static GestionPromotion getInstance() {
if (instance == null) {
instance = new GestionPromotion();
}
return instance;
}
public LinkedList<Promotion> getPromotions() {
return promotions;
}
/**
* Charge les donnée du XML
*/
private void loadFromXML() {
JDOM jdom = PromoJDOM.getInstance();
List<Element> promos = jdom.getRootNode().getChildren("promotion");
for (Element elem : promos) {
Promotion p = new Promotion(elem.getAttributeValue("lib"), Integer.valueOf(elem.getAttributeValue("id")));
promotions.addLast(p);
List<Element> eleves = elem.getChild("eleves").getChildren("eleve");
for (Element e : eleves) {
Eleve eleve = new Eleve(e.getChildText("nom"),
e.getChildText("prenom"), e.getChildText("email"),
e.getChildText("num"), p, Integer.valueOf(e.getAttributeValue("id")));
p.getEleves().addLast(eleve);
}
}
}
/**
* Créer une promotion et la sauvegarde dans le xml
* @param libelle
*/
public void createEmptyPromo(String libelle) {
Promotion p = new Promotion(libelle);
promotions.addLast(p);
p.save();
}
/**
* Supprime la promo de la memoire et du XML
* @param promo
*/
public void deletePromo(Promotion promo) {
promotions.remove(promo);
promo.delete();
}
}