/**
 * MappingXML.java
 * Copyright (C) 2005 Frederic Laurent
 * http://www.opikanoba.org
 *
 * This file is part of the Mapping XML with Hibernate
 *
 * Lantern 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.
 *
 * This exemple 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.
 *
 **/

 /**
  * Mapping XML avec Hibernate
  * Ce programme est volontairement très simple. Il montre comment 
  * ajouter dans une base de donnees relationnelles les donnees d'un
  * document XML en passant par le framework Hibernate et son mapping XML.
  *
  * @author Fréderic Laurent
  **/

import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import bean.TodoTask;
	
public class MappingXML {
	Logger log = Logger.getLogger("MappingXML");
	
	private static SessionFactory sessionFactory;
	
	public MappingXML(){
		sessionFactory = new Configuration().addResource("Todo.hbm.xml")
		.buildSessionFactory();
	}

	public void modificationPOJO(){
		
		try {
		    Session session = sessionFactory.openSession();
		    Transaction tx = session.beginTransaction();
			
			TodoTask tt= new TodoTask();
			tt.setTodoid(2);
			tt.setSummary("Faire une pause");
			tt.setDescription("Allez a la cafet, prendre un cafe avec Bob");
			tt.setPriority(2);
			
			session.saveOrUpdate(tt);

			tx.commit();
			session.close();
		} catch (Exception e){
			log.log(Level.SEVERE, e.getMessage());
		}

	}
	
	
	public void printXMLResult() {
		Element root=DocumentFactory.getInstance().createElement("todolist");
		Document doc=DocumentFactory.getInstance().createDocument(root) ; 
		
		
		Session session = sessionFactory.openSession();
		Session xmlSession = session.getSession(EntityMode.DOM4J);
		
		List res=(List) xmlSession.createQuery("from TodoTask").list();
		for (int i=0; i<res.size(); i++){
			Element elt = (Element) res.get(i);
			root.add(elt);
		}

		
		XMLWriter xw;
		try {
			xw = new XMLWriter(System.out, new OutputFormat("  ",true));
			xw.write(doc);
		} catch (Exception e) {
			log.log(Level.SEVERE, e.getMessage());
		}
		session.close();
	}

	
	public void insertXMLBuffer() {

		SAXReader sr=new SAXReader(false);
		Document doc=null;
		try {
			doc = sr.read(ClassLoader.getSystemClassLoader().getResourceAsStream("todo-ex.xml"));
		} catch (DocumentException e1) {
			log.log(Level.SEVERE, e1.getMessage());
			e1.printStackTrace();
		} 

		List todo=doc.selectNodes("//todo");
		
		Session session = sessionFactory.openSession();
		Session xmlSession = session.getSession(EntityMode.DOM4J);
		Transaction tx=session.beginTransaction();
		
		for (Iterator it=todo.iterator(); it.hasNext(); ) {
			xmlSession.saveOrUpdate("bean.TodoTask",it.next());
		}
		
		tx.commit();
		session.close();
	}

	
	
	public static void main(String[] args) {
		MappingXML mapxml = new MappingXML();
		mapxml.modificationPOJO();
		mapxml.insertXMLBuffer();
		mapxml.printXMLResult();
	}

}
