patient_records_mng_sys.send.java Source code

Java tutorial

Introduction

Here is the source code for patient_records_mng_sys.send.java

Source

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

import patient_records_mng_sys.mail_touse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.swing.JOptionPane;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import static patient_records_mng_sys.History.rows;
import java.io.File;
import java.util.*;
import javax.activation.DataSource;
import javax.swing.table.DefaultTableModel;
import static patient_records_mng_sys.History.dTableModel;
import static patient_records_mng_sys.Patient_Records_Mng_Sys.username;

/**
 *
 * @author S.Jayasinghe
 */
public class send extends Patient_Records_Mng_Sys {
    Connection conn = null;
    static ResultSet rows;
    static Object[][] databaseResults;
    static Object[] columns = { "ID", "Patient ID", "Clinic", "Diagnosis", "Date of Entry", "Allergies", "Remarks",
            "Next Clinic" };
    static DefaultTableModel dTableModel = new DefaultTableModel(databaseResults, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;

            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }

    };

    public void fireTable(String value) {

        String keyvalue = value;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            //conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user="+user+"&password="+pass+"");
            Statement sqlState = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            String selectStuff = "Select `ID`,`patient_id`,`clinicName`,`diagnosis`,`dateOfEntry`,`allergiesIdentified`,`Remarks`,`nextClinicDate`,`ward` FROM `medical_records` WHERE `patient_id` LIKE "
                    + keyvalue + " ";
            rows = sqlState.executeQuery(selectStuff);
            Object[] tempRow;

            while (rows.next()) {
                tempRow = new Object[] { rows.getString(1), rows.getString(2), rows.getString(3), rows.getString(4),
                        rows.getDate(5), rows.getString(6), rows.getString(7), rows.getString(8) };
                dTableModel.addRow(tempRow);
            }

        } catch (ClassNotFoundException | SQLException e) {
            // e.printStackTrace();
        }

    }

    /**
     * Creates new form send
     */
    public static void test() {

        System.out.println("Works");
    }

    //Experiment

    //Experiment Over
    public void label() {
        jLabel9.setText("Hello " + username + " ");
        jLabel10.setText(level);
    };

    public send() {
        //this.label(); 

        conn = dbconnect.ConnectDB();

        setTitle("Send Medical Records");
        initComponents();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            //conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user=root&password=sparksndl");
            //conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user="+user+"&password="+pass+"");
            Statement sqlState = conn.createStatement();
            String select = "SELECT * FROM `patient` WHERE 1";
            ResultSet rows = sqlState.executeQuery(select);

            while (rows.next()) {
                search_key.addItem(rows.getString(1));

            }
            AutoCompleteDecorator.decorate(this.search_key);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        search_key = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        email = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(null);

        search_key.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_keyActionPerformed(evt);
            }
        });
        search_key.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                search_keyKeyPressed(evt);
            }

            public void keyTyped(java.awt.event.KeyEvent evt) {
                search_keyKeyTyped(evt);
            }
        });
        jPanel1.add(search_key);
        search_key.setBounds(230, 110, 190, 30);

        jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel10.setText("Level");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(840, 40, 60, 20);

        jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel9.setText("Name");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(820, 20, 100, 30);

        jLabel15.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel15.setForeground(new java.awt.Color(102, 102, 255));
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel15.setText("Profile");
        jLabel15.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jPanel1.add(jLabel15);
        jLabel15.setBounds(840, 10, 41, 17);

        jLabel17.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel17.setForeground(new java.awt.Color(204, 0, 51));
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel17.setText("Logout");
        jLabel17.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel17.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel17MouseClicked(evt);
            }
        });
        jPanel1.add(jLabel17);
        jLabel17.setBounds(840, 60, 43, 17);

        jLabel8.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/patient_records_mng_sys/images/admin1.png"))); // NOI18N
        jPanel1.add(jLabel8);
        jLabel8.setBounds(920, 10, 60, 80);

        jTable1.setModel(dTableModel);
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(0, 220, 1000, 280);

        jButton3.setText("Send");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(750, 110, 80, 30);

        email.setText("eg:doctor@hospital.com");
        email.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                emailMouseClicked(evt);
            }
        });
        email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailKeyPressed(evt);
            }
        });
        jPanel1.add(email);
        email.setBounds(580, 110, 150, 30);

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jLabel7.setText("Recipient Email Address:");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(430, 120, 180, 17);

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        jLabel5.setText("Find Patient Record by ID:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(70, 120, 150, 17);

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 24)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Send Patient Medical Records");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(340, 30, 330, 20);

        jButton1.setFont(new java.awt.Font("Calibri", 0, 24)); // NOI18N
        jButton1.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/patient_records_mng_sys/images/Back.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(10, 10, 80, 70);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 90, 1010, 10);

        jLabel1.setIcon(new javax.swing.ImageIcon(
                getClass().getResource("/patient_records_mng_sys/images/cool-hd-blurred.jpg"))); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(0, 0, 1000, 520);

        jMenu1.setText("File");

        jMenuItem1.setText("Exit System");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        setSize(new java.awt.Dimension(1012, 560));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        before_one one = new before_one();
        one.setVisible(true);
        this.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.setVisible(false); // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void emailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_emailMouseClicked
        email.setText(null); // TODO add your handling code here:
    }//GEN-LAST:event_emailMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        String email1 = email.getText();

        String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";

        Boolean b = email1.matches(EMAIL_REGEX);

        if (b == true) {

            System.out.println("Correct Email");
            // String bloody=(String)search_key.getSelectedItem();
            //this.test();
            //Create the PDF Document//

            try {
                String key = (String) search_key.getSelectedItem();
                System.out.println("Creating PDF File");
                String fileName = "patientReport.pdf"; // name of our file

                PDDocument doc = new PDDocument();
                PDPage page = new PDPage();

                doc.addPage(page);

                PDPageContentStream content = new PDPageContentStream(doc, page);

                content.beginText();
                content.setFont(PDType1Font.HELVETICA, 20);
                content.moveTextPositionByAmount(220, 750);
                content.drawString("Patient Report Form");
                content.endText();

                int x = 80;
                int y = 700;
                String spac = "        ";
                String documen = "PatientID" + spac + "PatientName" + spac + "Clinic" + spac + "Diagnose";

                content.beginText();
                content.setFont(PDType1Font.HELVETICA, 16);
                content.moveTextPositionByAmount(x, 710);
                content.drawString(documen);
                content.endText();

                //To add from database//
                // Connection conn=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    ///conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user=root&password=sparksndl");
                    //conn = DriverManager.getConnection("jdbc:mysql://localhost/itp?"+"user="+user+"&password="+pass+"");
                    Statement sqlState = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_UPDATABLE);
                    String selectStuff = "Select `ID`,`patient_id`,`clinicName`,`diagnosis`,`dateOfEntry`,`allergiesIdentified`,`Remarks`,`nextClinicDate`,`ward` FROM `medical_records` WHERE `patient_id` LIKE "
                            + key + " ";
                    rows = sqlState.executeQuery(selectStuff);
                    Object[] tempRow;
                    String[] records;
                    if (rows.next()) {
                        while (rows.next()) {
                            String id = rows.getString(1);
                            String name = "Classified";
                            String clinic = rows.getString(3);
                            String diagnose = rows.getString(4);
                            String aller = rows.getString(6);
                            String remark = rows.getString(7);
                            String space = "                 ";
                            String document = id + space + space + space + name + space + space + clinic + space
                                    + diagnose;
                            content.beginText();
                            content.setFont(PDType1Font.HELVETICA, 8);
                            content.moveTextPositionByAmount(x, y);
                            content.drawString(document);

                            content.endText();
                            y = y - 12;
                        }
                    } else {
                        JOptionPane.showMessageDialog(null,
                                "ID entry not available,Please register the patient or try again");
                    }

                } catch (ClassNotFoundException | SQLException e) {
                    System.out.println("Provide Correct Index");
                    //e.printStackTrace();
                }

                //Document Creation Over//

                content.close();
                doc.save(fileName);
                doc.close();

                System.out.println("your report has been created in : " + System.getProperty("user.dir"));

            } catch (IOException | COSVisitorException e) {

                System.out.println(e.getMessage());

            }
            // String test="jsandunil@gmail.com";
            emailer mailme = new emailer();
            mailme.sendFromGMail(email1);

            //mailme.
            //Document Over//

        } else

            JOptionPane.showMessageDialog(null, "Incorrect Email Provided.", "Email Address Format",
                    JOptionPane.ERROR_MESSAGE);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jLabel17MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel17MouseClicked
        this.setVisible(false);
        //redirect to homepage        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel17MouseClicked

    private void search_keyKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_search_keyKeyTyped

        // TODO add your handling code here:
    }//GEN-LAST:event_search_keyKeyTyped

    private void search_keyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_search_keyKeyPressed

    }//GEN-LAST:event_search_keyKeyPressed

    private void emailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyPressed
        //System.out.println("key pressed");        // TODO add your handling code here:
    }//GEN-LAST:event_emailKeyPressed

    private void search_keyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_keyActionPerformed

        String test = (String) search_key.getSelectedItem();
        System.out.println(test);
        String key = "1227";
        try {
            dTableModel.setRowCount(0);
            this.fireTable(test);
        } catch (Exception e) {

        }

    }//GEN-LAST:event_search_keyActionPerformed

    /**
     * @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(send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(send.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 send().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField email;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox search_key;
    // End of variables declaration//GEN-END:variables
}