/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aufgabe07;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author skleuker
 */
public class Main {

  public static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Aufgabe07sose10PU");
  public static EntityManager em = emf.createEntityManager();

  ;

  public static void show(String kommentar) {
    // emf = Persistence.createEntityManagerFactory("Aufgabe05PU");
    System.out.println(kommentar);
    for (Beispiel b : (List<Beispiel>) em.createQuery("SELECT b FROM Beispiel b").
            getResultList()) {
      System.out.println("  " + b);
    }
    System.out.println("-------------");
  }

  public static void start() {
    em.getTransaction().begin();
  }

  public static void ende() {
    em.getTransaction().commit();
    //emf.close();
  }

  public static void main(String[] args) {
    start();
    Beispiel b = new Beispiel("Hai", "wo");
    Beispiel b2 = new Beispiel("Hallo", "wer");
    em.persist(b);
    em.persist(b2);
    ende();
    show("Anfang");

    start();
    b.setA("Heino");
    em.persist(b);
    ende();
    show("Update a");

    start();
    b2.setA("Heino");
    em.persist(b2);
    ende();
    show("Not unique a");

    start();
    b2.setA(null);
    em.persist(b2);
    ende();
    show("Nullable a");

    try {
      start();
      b.setB("Heino");
      em.persist(b);
      ende();
    } catch (Exception e) {
      System.out.println("Exception: e" + e.getCause());
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
    } finally {
      show("Update b");
    }


    try {
      Beispiel b3 = new Beispiel("Hai", "wer");
      System.out.println("Versuch einfuegen: " + b3);
      start();
      em.persist(b3);
      ende();
    } catch (Exception e) {
      System.out.println("Exception: e " + e.getMessage());
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
    } finally {
      show("Not unique b");
    }

    try {
      Beispiel b4 = new Beispiel("Hai", null);
      System.out.println("Versuch einfuegen: " + b4);
      start();
      em.persist(b4);
      ende();
    } catch (Exception e) {
      System.out.println("Exception: e " + e.getMessage());
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
    } finally {
      show("Nullable b");
    }

    try {
      Beispiel b4 = new Beispiel("Hai", null);
      System.out.println("Versuch einfuegen: " + b4);
      start();
      em.persist(b4);
      ende();
    } catch (Exception e) {
      System.out.println("Exception: e " + e.getMessage());
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
    } finally {
      show("Nullable b");
      em.close();
      emf.close();
    }
  }
}
