package org.sistema.hibernate.events;

import java.util.Date;

import org.sistema.hibernate.events.dao.GenericDAO;
import org.sistema.hibernate.events.models.Sale;
import org.sistema.hibernate.events.models.Salesperson;

/**
 * Main class
 * 
 * @author Eugenia Pérez
 * @email eugenia_perez@cuatrovientos.org
 */
public class Main {

	static GenericDAO<Salesperson> salespersonDAO = new GenericDAO<Salesperson>();
	static GenericDAO<Sale> salesDAO = new GenericDAO<Sale>();

	/**
	 * Load the salespeople and save them in the database.
	 */
	private static void loadSalespeople() {
		Salesperson sp1 = new Salesperson("Eugenia Martínez", 30000f, 0.15f);
		Salesperson sp2 = new Salesperson("Alfredo Pérez", 33000f, 0.20f);

		salespersonDAO.insert(sp1);
		salespersonDAO.insert(sp2);

	}

	/**
	 * Save some sales and add to the current sales of the sales agent.
	 */
	private static void saveSales() {

		Sale sale1 = new Sale(1200.0, 0.1f, new Date());
		Sale sale2 = new Sale(650.0, 0.25f, new Date());
		Salesperson sp1 = salespersonDAO.selectById(1l, Salesperson.class);
		sp1.addSales(sale1);
		sp1.addSales(sale2);

		salesDAO.insert(sale1);
		salesDAO.insert(sale2);
	}

	/**
	 * Remove an specific sale to prove the right interceptors performance.
	 */
	private static void deleteSales() {
		salesDAO.delete(salesDAO.selectById(1l, Sale.class));
	}

	/**
	 * Load the application and manage the logical operations.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		Main.loadSalespeople();
		Main.saveSales();
		Main.deleteSales();

	}

}
