gui.AddPerson.java Source code

Java tutorial

Introduction

Here is the source code for gui.AddPerson.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 gui;

import static gui.CaregiverGUI.getCaregiverwsProxy;
import java.util.Arrays;
import javax.swing.JOptionPane;
import org.json.JSONObject;

/**
 *
 * @author hossam
 */
public class AddPerson extends javax.swing.JFrame {

    private String type;

    public AddPerson() {
        initComponents();
        getRootPane().setDefaultButton(jButton1);
        jTextField1.requestFocus();
    }

    public void setType(String type) {
        this.type = type;
    }

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

        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Username:");

        jLabel2.setText("First Name:");

        jLabel3.setText("Last Name:");

        jLabel4.setText("Mobile Number");

        jLabel5.setText("E-Mail Address");

        jLabel6.setText("Password:");

        jLabel7.setText("Confirm Password:");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(101, Short.MAX_VALUE).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 90,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)).addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton2)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTextField5).addComponent(jTextField4)
                                        .addComponent(jTextField3).addComponent(jTextField2)
                                        .addComponent(jTextField1).addComponent(jPasswordField1)
                                        .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(91, 91, 91)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1).addComponent(jButton2))
                        .addGap(22, 22, 22)));

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

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

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

        if (type.equals("ELDERLY"))
            elderlyType();
        if (type.equals("CAREGIVER"))
            caregiverType();
        if (type.equals("FAMILY_MEMBER"))
            familyMemeberType();
    }

    private void familyMemeberType() {
        if (!jTextField1.getText().isEmpty() && !jTextField2.getText().isEmpty() && !jTextField3.getText().isEmpty()
                && !jTextField4.getText().isEmpty() && !jTextField5.getText().isEmpty()
                && jPasswordField1.getPassword().length != 0
                && Arrays.equals(jPasswordField1.getPassword(), jPasswordField2.getPassword())) {
            JSONObject tmp = new JSONObject();
            try {
                tmp.put("username", jTextField1.getText());
                tmp.put("first_name", jTextField2.getText());
                tmp.put("last_name", jTextField3.getText());
                tmp.put("email_address", jTextField4.getText());
                tmp.put("mobile_number", jTextField5.getText());
                tmp.put("type", "FAMILY_MEMBER");
                tmp.put("password", Main.MD5Hash(jPasswordField1.getPassword()));
                getCaregiverwsProxy().addOrUpdatePerson(tmp.toString(), Main.caregiverGUI.getSelectedElderly());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
            Main.caregiverGUI.updateTable2();
            dispose();
        }

    }

    private void caregiverType() {
        if (!jTextField1.getText().isEmpty() && !jTextField2.getText().isEmpty() && !jTextField3.getText().isEmpty()
                && !jTextField4.getText().isEmpty() && !jTextField5.getText().isEmpty()
                && jPasswordField1.getPassword().length != 0
                && Arrays.equals(jPasswordField1.getPassword(), jPasswordField2.getPassword())) {
            JSONObject tmp = new JSONObject();
            try {
                tmp.put("username", jTextField1.getText());
                tmp.put("first_name", jTextField2.getText());
                tmp.put("last_name", jTextField3.getText());
                tmp.put("email_address", jTextField4.getText());
                tmp.put("mobile_number", jTextField5.getText());
                tmp.put("type", "CAREGIVER");
                tmp.put("password", Main.MD5Hash(jPasswordField1.getPassword()));
                getCaregiverwsProxy().addOrUpdatePerson(tmp.toString(), Main.caregiverGUI.getSelectedElderly());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
            Main.caregiverGUI.updateTable1();
            dispose();
        }

    }

    private void elderlyType() {
        if (!jTextField1.getText().isEmpty() && !jTextField2.getText().isEmpty() && !jTextField3.getText().isEmpty()
                && !jTextField4.getText().isEmpty() && !jTextField5.getText().isEmpty()
                && jPasswordField1.getPassword().length != 0
                && Arrays.equals(jPasswordField1.getPassword(), jPasswordField2.getPassword())) {
            JSONObject tmp = new JSONObject();
            try {
                tmp.put("username", jTextField1.getText());
                tmp.put("first_name", jTextField2.getText());
                tmp.put("last_name", jTextField3.getText());
                tmp.put("email_address", jTextField4.getText());
                tmp.put("mobile_number", jTextField5.getText());
                tmp.put("type", "ELDERLY");
                tmp.put("password", Main.MD5Hash(jPasswordField1.getPassword()));
                getCaregiverwsProxy().addOrUpdatePerson(tmp.toString(), "");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
            Main.caregiverGUI.updateTable8();
            dispose();
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    public void populateFields(String[] fields) {
        jTextField1.setText(fields[0]);
        jTextField2.setText(fields[1]);
        jTextField3.setText(fields[2]);
        jTextField4.setText(fields[3]);
        jTextField5.setText(fields[4]);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}