/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.DaoMesto;
import domen.Mesto;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author student
 */
public class DaoMestoImpl implements DaoMesto {
    EntityManagerFactory emf;

    public DaoMestoImpl() {
        emf = Persistence.createEntityManagerFactory("JPA1PU");
    }
    
    
    @Override
    public void sacuvaj(Mesto mesto) {
      
        EntityManager em = emf.createEntityManager();
        
        em.persist(mesto);
        em.getTransaction().begin();
        em.getTransaction().commit();
        System.out.println("Mesto je sacuvano.");
        em.close();
    }

    @Override
    public void sacuvajIliAzuriraj(Mesto mesto) {
        EntityManager em = emf.createEntityManager();
        
        Mesto dbMesto = em.find(Mesto.class, mesto.getPtt());
        em.getTransaction().begin();
        if (dbMesto == null) em.persist(mesto);
        else {
            dbMesto.setNaziv(mesto.getNaziv());
        }
        em.getTransaction().commit();
        em.close();
    }
    
    
    
}
