javaresturentdesktopclient.EmployeeDetailsPage.java Source code

Java tutorial

Introduction

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

import Entity.EmployeeEntity;
import TableModel.RestaurantTableModel;
import Tools.Constant;
import Tools.HttpClientUtil;
import Tools.ServletRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author siamzz
 */
public class EmployeeDetailsPage extends javax.swing.JFrame {

    /**
     * Creates new form EmployeeDetailsPage
     */
    public Home_page add;

    final static Integer positionId[] = { 1, 2, 3, 4, 5 };
    Integer item;
    String position = Constant.manager;

    public EmployeeDetailsPage(Home_page var) {
        initComponents();
        add = var;
        SetEmployeeId();
        ShowTable(position);
    }

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldFirstNAme = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldLastName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jTextFieldAddress = new javax.swing.JTextField();
        jTextFieldcontact = new javax.swing.JTextField();
        jTextFieldNationalId = new javax.swing.JTextField();
        jTextFieldSalary = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBoxPosition = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableEmplyee = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldEmpId = new javax.swing.JTextField();
        jButtonUpdate = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(715, 560));
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("First Name:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 270, 130, 40);

        jTextFieldFirstNAme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldFirstNAmeActionPerformed(evt);
            }
        });
        getContentPane().add(jTextFieldFirstNAme);
        jTextFieldFirstNAme.setBounds(120, 280, 220, 20);

        jLabel3.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(153, 0, 0));
        jLabel3.setText("Last Name:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 290, 140, 60);

        jTextFieldLastName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldLastNameActionPerformed(evt);
            }
        });
        getContentPane().add(jTextFieldLastName);
        jTextFieldLastName.setBounds(120, 310, 220, 20);

        jLabel4.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(153, 0, 0));
        jLabel4.setText("Address:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(40, 330, 130, 40);

        jLabel5.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 0, 0));
        jLabel5.setText("Contact No:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 350, 160, 60);

        jLabel6.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(153, 0, 0));
        jLabel6.setText("National Id:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(0, 390, 170, 40);

        jLabel7.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(153, 0, 0));
        jLabel7.setText("Position:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(10, 210, 150, 40);

        jLabel8.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(153, 0, 0));
        jLabel8.setText("Salary:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(30, 420, 150, 40);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/ADD BILL/DONE.png"))); // NOI18N
        jButton2.setToolTipText("add employee");
        jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(190, 470, 60, 20);
        getContentPane().add(jTextFieldAddress);
        jTextFieldAddress.setBounds(120, 340, 220, 20);

        jTextFieldcontact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldcontactActionPerformed(evt);
            }
        });
        getContentPane().add(jTextFieldcontact);
        jTextFieldcontact.setBounds(120, 370, 220, 20);
        getContentPane().add(jTextFieldNationalId);
        jTextFieldNationalId.setBounds(120, 400, 220, 20);
        getContentPane().add(jTextFieldSalary);
        jTextFieldSalary.setBounds(120, 430, 220, 20);

        jLabel9.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(153, 0, 0));
        jLabel9.setText("Add Employee");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(30, 460, 140, 40);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/left.png"))); // NOI18N
        jButton1.setToolTipText("go to employee option page");
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(640, 220, 50, 50);

        jComboBoxPosition.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "MANAGER", "WAITER", "CHEF", "CASHIER", "CLEANER" }));
        jComboBoxPosition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxPositionActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBoxPosition);
        jComboBoxPosition.setBounds(120, 220, 220, 20);

        jTableEmplyee.setBackground(new java.awt.Color(153, 0, 0));
        jTableEmplyee.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Employee id", "First name", "Last name", "Address", "Contact no", "National id",
                "Position", "Salary" }) {
            boolean[] canEdit = new boolean[] { true, false, true, true, true, true, true, true };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableEmplyee);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 10, 680, 190);

        jLabel10.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(153, 0, 0));
        jLabel10.setText("Employee Id:");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(0, 250, 140, 20);

        jTextFieldEmpId.setToolTipText("please press enter aftet inserting Employee ID for update employee");
        jTextFieldEmpId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldEmpIdActionPerformed(evt);
            }
        });
        getContentPane().add(jTextFieldEmpId);
        jTextFieldEmpId.setBounds(120, 250, 220, 20);

        jButtonUpdate.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/employee update/UPDATE.png"))); // NOI18N
        jButtonUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonUpdate);
        jButtonUpdate.setBounds(560, 470, 100, 20);

        jLabel11.setFont(new java.awt.Font("Viner Hand ITC", 1, 18)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(153, 0, 0));
        jLabel11.setText("update employee");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(390, 440, 160, 70);

        jLabel2.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/BUTTON IMAGE/background-1024x808 - Copy.jpg"))); // NOI18N
        jLabel2.setToolTipText("Please select position first for adding or updating employee");
        jLabel2.setMaximumSize(new java.awt.Dimension(715, 560));
        jLabel2.setMinimumSize(new java.awt.Dimension(715, 560));
        jLabel2.setPreferredSize(new java.awt.Dimension(715, 560));
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, 0, 700, 530);

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

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        add.show();
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

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

        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);

        /*List<NameValuePair> paramList = new ArrayList<>();
            
         paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_FIND_ID));
            
             
         String response = HttpClientUtil.postRequest(paramList);
            
         double Id_get=Double.parseDouble(response)+1;
            
         String Id1 = Double.toString(Id_get);
            
         String Id;
         Id = jTextFieldEmpId.setText(Id1);*/
        String fName = jTextFieldFirstNAme.getText();
        String lName = jTextFieldLastName.getText();
        String address = jTextFieldAddress.getText();
        String contact = jTextFieldcontact.getText();
        String national_id = jTextFieldNationalId.getText();
        String salary = jTextFieldSalary.getText();
        String employeeId = jTextFieldEmpId.getText();

        //i think contact no, national id  integer
        List<NameValuePair> paramLis = new ArrayList<>();
        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);
        paramLis.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_ADD_EMPLOYEE));
        paramLis.add(new BasicNameValuePair(Constant.KEY_EMPLOYEE_ID, employeeId));
        paramLis.add(new BasicNameValuePair(Constant.KEY_FNAME, fName));
        paramLis.add(new BasicNameValuePair(Constant.KEY_LNAME, lName));
        paramLis.add(new BasicNameValuePair(Constant.KEY_ADDRESS, address));
        paramLis.add(new BasicNameValuePair(Constant.KEY_CONTACT, contact));
        paramLis.add(new BasicNameValuePair(Constant.KEY_NATIONAL_ID, national_id));
        paramLis.add(new BasicNameValuePair(Constant.KEY_POSITION, position));
        paramLis.add(new BasicNameValuePair(Constant.KEY_SALARY, salary));

        String response = HttpClientUtil.postRequest(paramLis);

        if (response.equalsIgnoreCase("success")) {
            JOptionPane.showMessageDialog(null, "SUCCESSFULLY ADDED", "Information",
                    JOptionPane.INFORMATION_MESSAGE);
            SetEmployeeId();
            jTextFieldFirstNAme.setText(null);
            jTextFieldLastName.setText(null);
            jTextFieldAddress.setText(null);
            jTextFieldcontact.setText(null);
            jTextFieldNationalId.setText(null);
            jTextFieldSalary.setText(null);
            ShowTable(position);

        } else {
            JOptionPane.showMessageDialog(null, "Failed!", "Error Message", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton2ActionPerformed

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

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

    private void jComboBoxPositionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxPositionActionPerformed
        // TODO add your handling code here:

        int index;

        index = ((JComboBox) evt.getSource()).getSelectedIndex();
        item = positionId[index];

        if (item == 1) {
            position = Constant.manager;
        } else if (item == 2) {
            position = Constant.waiter;
        } else if (item == 3) {
            position = Constant.chef;
        } else if (item == 4) {
            position = Constant.cashier;
        } else if (item == 5) {
            position = Constant.cleaner;
        } else {
            position = null;
        }
        ShowTable(position);

    }//GEN-LAST:event_jComboBoxPositionActionPerformed

    private void jTextFieldEmpIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldEmpIdActionPerformed
        // TODO add your handling code here:
        String empId = jTextFieldEmpId.getText();

        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);
        List<NameValuePair> paramList = new ArrayList<>();
        paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_EMP_SHOW));
        paramList.add(new BasicNameValuePair(Constant.KEY_EMPLOYEE_ID, empId));

        String response = HttpClientUtil.postRequest(paramList);

        Gson gson = new GsonBuilder().create();
        java.lang.reflect.Type collectionType = new TypeToken<EmployeeEntity>() {
        }.getType();
        EmployeeEntity LIST = gson.fromJson(response, collectionType);

        jTextFieldFirstNAme.setText(LIST.getfirstName());
        jTextFieldLastName.setText(LIST.getlastName());
        jTextFieldAddress.setText(LIST.getAddress());
        jTextFieldcontact.setText(LIST.getContactNo());
        jTextFieldNationalId.setText(LIST.getnationalId());
        jTextFieldSalary.setText(LIST.getsalary());

    }//GEN-LAST:event_jTextFieldEmpIdActionPerformed

    private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateActionPerformed
        // TODO add your handling code here:
        String fName = jTextFieldFirstNAme.getText();
        String lName = jTextFieldLastName.getText();
        String address = jTextFieldAddress.getText();
        String contact = jTextFieldcontact.getText();
        String national_id = jTextFieldNationalId.getText();
        String salary = jTextFieldSalary.getText();
        String employeeId = jTextFieldEmpId.getText();

        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);
        List<NameValuePair> paramLis = new ArrayList<>();
        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);
        paramLis.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_UPDATE_EMPLOYEE));
        paramLis.add(new BasicNameValuePair(Constant.KEY_EMPLOYEE_ID, employeeId));
        paramLis.add(new BasicNameValuePair(Constant.KEY_FNAME, fName));
        paramLis.add(new BasicNameValuePair(Constant.KEY_LNAME, lName));
        paramLis.add(new BasicNameValuePair(Constant.KEY_ADDRESS, address));
        paramLis.add(new BasicNameValuePair(Constant.KEY_CONTACT, contact));
        paramLis.add(new BasicNameValuePair(Constant.KEY_NATIONAL_ID, national_id));
        paramLis.add(new BasicNameValuePair(Constant.KEY_POSITION, position));
        paramLis.add(new BasicNameValuePair(Constant.KEY_SALARY, salary));

        String response = HttpClientUtil.postRequest(paramLis);
        if (response.equalsIgnoreCase("success")) {
            JOptionPane.showMessageDialog(null, "Successfully updated", "Information",
                    JOptionPane.INFORMATION_MESSAGE);
            jTextFieldFirstNAme.setText(null);
            jTextFieldLastName.setText(null);
            jTextFieldAddress.setText(null);
            jTextFieldcontact.setText(null);
            jTextFieldNationalId.setText(null);
            jTextFieldSalary.setText(null);
            ShowTable(position);
            SetEmployeeId();
        } else {
            JOptionPane.showMessageDialog(null, "Failed!" + "please enter data correctly, thank you.",
                    "Error Message", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButtonUpdateActionPerformed

    /**
     * @param args the command line arguments
     */
    private void ShowTable(String Position) {
        int tabbedPanelID = Constant.EMPLOYEE_PANELID;
        List<NameValuePair> paramList = new ArrayList<>();

        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);
        paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_SHOW_EMPLOYEE));
        paramList.add(new BasicNameValuePair(Constant.KEY_POSITION, position.trim()));
        String response = HttpClientUtil.postRequest(paramList);

        Gson gson = new GsonBuilder().create();
        Object[][] objectArray = null;
        java.lang.reflect.Type collectionType = new TypeToken<List<EmployeeEntity>>() {
        }.getType();
        List<EmployeeEntity> EmployeeList = gson.fromJson(response, collectionType);
        objectArray = new Object[EmployeeList.size()][];
        int count = 0;

        for (EmployeeEntity prob : EmployeeList) {
            Object[] obj = new Object[8];
            obj[0] = prob.getemployeeId();
            obj[1] = prob.getfirstName();
            obj[2] = prob.getlastName();
            obj[3] = prob.getAddress();
            obj[4] = prob.getContactNo();
            obj[5] = prob.getnationalId();
            obj[6] = prob.getPosition();
            obj[7] = prob.getsalary();

            objectArray[count] = obj;
            count++;
        }
        RestaurantTableModel restaurantModel = new RestaurantTableModel(tabbedPanelID, objectArray);
        jTableEmplyee = new JTable(restaurantModel);
        jTableEmplyee.setPreferredScrollableViewportSize(new Dimension(800, 800));
        jTableEmplyee.setFillsViewportHeight(true);
        jScrollPane1.setViewportView(jTableEmplyee);
    }

    private void SetEmployeeId() {
        ServletRequest.setServletRequest(Constant.EMPLOYEE_SERVLET);

        List<NameValuePair> paramList = new ArrayList<>();

        paramList.add(new BasicNameValuePair(Constant.KEY_COMMAND, Constant.KEY_FIND_ID));

        String response = HttpClientUtil.postRequest(paramList);

        Integer Id_get = Integer.parseInt(response) + 1;

        String Id1 = Integer.toString(Id_get);

        String Id;
        jTextFieldEmpId.setText(Id1);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonUpdate;
    private javax.swing.JComboBox jComboBoxPosition;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    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.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableEmplyee;
    private javax.swing.JTextField jTextFieldAddress;
    private javax.swing.JTextField jTextFieldEmpId;
    private javax.swing.JTextField jTextFieldFirstNAme;
    private javax.swing.JTextField jTextFieldLastName;
    private javax.swing.JTextField jTextFieldNationalId;
    private javax.swing.JTextField jTextFieldSalary;
    private javax.swing.JTextField jTextFieldcontact;
    // End of variables declaration//GEN-END:variables
}