/*
 * 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 bp2_lab10;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author samet
 */
public class Soru1 extends javax.swing.JFrame {

    /**
     * Creates new form Soru1
     */
    DefaultTableModel dtm;
    List<Doktor> doktorlar;

    public Soru1() {
        initComponents();

        ///SORGULAMA
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
        EntityManager em = emf.createEntityManager();

        Query q = em.createQuery("SELECT d FROM Doktor d");
        doktorlar = q.getResultList();
        for (Doktor dr : doktorlar) {
            cmb_doktorlar.addItem(dr.getAdi() + " " + dr.getSoyadi());
        }

        dtm = new DefaultTableModel();
        dtm.setColumnIdentifiers(new Object[]{"ID", "ADI", "SOYADI", "DOkTOR ID"});
        q = em.createQuery("SELECT h FROM Hasta h");
        List<Hasta> hastalar = q.getResultList();
        for (Hasta hs : hastalar) {
            dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
        }
        tbl_hastalar.setModel(dtm);
        /////
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btn_hastaekle = new javax.swing.JButton();
        btn_sorgula = new javax.swing.JButton();
        SİL = new javax.swing.JButton();
        btn_degistir = new javax.swing.JButton();
        txt_hastaid = new javax.swing.JTextField();
        txt_hastaadi = new javax.swing.JTextField();
        txt_hastasoyadi = new javax.swing.JTextField();
        cmb_doktorlar = new javax.swing.JComboBox<>();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_hastalar = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btn_hastaekle.setText("HASTA EKLE");
        btn_hastaekle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_hastaekleActionPerformed(evt);
            }
        });
        getContentPane().add(btn_hastaekle, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 30, -1, -1));

        btn_sorgula.setText("SOGULA");
        btn_sorgula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_sorgulaActionPerformed(evt);
            }
        });
        getContentPane().add(btn_sorgula, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 130, 110, -1));

        SİL.setText("SİL");
        SİL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SİLActionPerformed(evt);
            }
        });
        getContentPane().add(SİL, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 170, 110, -1));

        btn_degistir.setText("DEĞİSTİR");
        btn_degistir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_degistirActionPerformed(evt);
            }
        });
        getContentPane().add(btn_degistir, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 170, 110, -1));
        getContentPane().add(txt_hastaid, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 30, 150, -1));
        getContentPane().add(txt_hastaadi, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, 150, -1));
        getContentPane().add(txt_hastasoyadi, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, 150, -1));

        getContentPane().add(cmb_doktorlar, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 130, 150, -1));

        tbl_hastalar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbl_hastalar);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 220, 580, 297));

        jLabel1.setText("DOKTORLAR");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, -1, -1));

        jLabel2.setText("ID");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, -1, -1));

        jLabel3.setText("ADI");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 60, -1, -1));

        jLabel4.setText("SOYADI");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 100, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_hastaekleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_hastaekleActionPerformed
        // TODO add your handling code here:
        Hasta newhasta = new Hasta();
        newhasta.setId(Integer.parseInt(txt_hastaid.getText()));
        newhasta.setAdi(txt_hastaadi.getText());
        newhasta.setSoyadi(txt_hastaadi.getText());
        newhasta.setIdDoktor(doktorlar.get(cmb_doktorlar.getSelectedIndex()).getId());
        ///EKLEME
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(newhasta);
        em.getTransaction().commit();

        dtm.setRowCount(0);
        Query q = em.createQuery("SELECT h FROM Hasta h");
        List<Hasta> hastalar = q.getResultList();
        for (Hasta hs : hastalar) {
            dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
        }
        tbl_hastalar.setModel(dtm);

        /////

    }//GEN-LAST:event_btn_hastaekleActionPerformed

    private void btn_sorgulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_sorgulaActionPerformed
        // TODO add your handling code here:
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
        EntityManager em = emf.createEntityManager();

        dtm.setRowCount(0);
        Query q = em.createQuery("SELECT h FROM Hasta h WHERE h.idDoktor=:did");
        q.setParameter("did", doktorlar.get(cmb_doktorlar.getSelectedIndex()).getId());
        List<Hasta> hastalar = q.getResultList();
        for (Hasta hs : hastalar) {
            dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
        }
        tbl_hastalar.setModel(dtm);

    }//GEN-LAST:event_btn_sorgulaActionPerformed

    private void btn_degistirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_degistirActionPerformed
        // TODO add your handling code here:
           // UPDATE
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
        EntityManager em = emf.createEntityManager();

        Query q = em.createQuery("UPDATE Hasta h SET h.adi=:ad,h.soyadi=:soyad, h.idDoktor=:did  WHERE h.id=:id");
        q.setParameter("ad", txt_hastaadi.getText());
        q.setParameter("soyad", txt_hastasoyadi.getText());
        q.setParameter("did", doktorlar.get(cmb_doktorlar.getSelectedIndex()).getId());
        q.setParameter("id", tbl_hastalar.getValueAt(tbl_hastalar.getSelectedRow(), 0));
        em.getTransaction().begin();
        q.executeUpdate();
        em.getTransaction().commit();
        ///
        dtm.setRowCount(0);
        q = em.createQuery("SELECT h FROM Hasta h");

        List<Hasta> hastalar = q.getResultList();
        for (Hasta hs : hastalar) {
            dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
        }
        tbl_hastalar.setModel(dtm);


    }//GEN-LAST:event_btn_degistirActionPerformed

    private void SİLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SİLActionPerformed
        // TODO add your handling code here:
        
        //SİLME
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
        EntityManager em = emf.createEntityManager();

        Query q = em.createQuery("DELETE FROM Hasta h WHERE h.id=:id");

        q.setParameter("id", tbl_hastalar.getValueAt(tbl_hastalar.getSelectedRow(), 0));
        em.getTransaction().begin();
        q.executeUpdate();
        em.getTransaction().commit();
        //
        
        // SELECT
        dtm.setRowCount(0);
        q = em.createQuery("SELECT h FROM Hasta h");

        List<Hasta> hastalar = q.getResultList();
        for (Hasta hs : hastalar) {
            dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
        }
        tbl_hastalar.setModel(dtm);
        
        //7
    }//GEN-LAST:event_SİLActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Soru1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Soru1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Soru1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Soru1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Soru1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SİL;
    private javax.swing.JButton btn_degistir;
    private javax.swing.JButton btn_hastaekle;
    private javax.swing.JButton btn_sorgula;
    private javax.swing.JComboBox<String> cmb_doktorlar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbl_hastalar;
    private javax.swing.JTextField txt_hastaadi;
    private javax.swing.JTextField txt_hastaid;
    private javax.swing.JTextField txt_hastasoyadi;
    // End of variables declaration//GEN-END:variables
}