package pl.bnsit.peaa.temporalobject;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import pl.bnsit.emtemplate.EnityManagerTemplate;



public class Runner {

	public static void main(String[] args) throws InterruptedException {
		
		Order order = new Order();
		order.createNewVersion( "11", "Pilka do kosza", 30D );
		order.getVersioningHistory().getOrderHourMilestones().add( order.getCurrentVersion() );
		logOrder( order );
		
		Thread.sleep( 1500  );
		
		order.createNewVersion( "12", "Bardzo fajna pilka do kosza", 55D );
		logOrder( order );
		
		Thread.sleep( 1500  );
		
		order.createNewVersion( "13", "Pilka do kosza w promocji", 43.99D );
		logOrder( order );
		
		Thread.sleep( 1500  );
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory( "HibernatePM" );
		EntityManager em = emf.createEntityManager();
		EnityManagerTemplate template = new EnityManagerTemplate( em );
		
		template.persist( order );
		
		List orders = template.findByNamedQuery( "Order.findAll" );
		System.out.println( orders );
		
		em.close();
		emf.close();
	}

	private static void logOrder( Order order ) {
		System.out.println( "Logowanie zamówienia" );
		System.out.println( "Identyfikator: " + order.getCustomId() );
		System.out.println( "Nazwa produktu: " + order.getName() );
		System.out.println( "Cena: " + order.getPrice() );
		System.out.println( "-------------------------------------" );
		System.out.println();
	}
	
}
