finale.year.stage.main.Inscription.java Source code

Java tutorial

Introduction

Here is the source code for finale.year.stage.main.Inscription.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 finale.year.stage.main;

import finale.year.stage.agent.AgentScolarite;
import finale.year.stage.responsable.ResponsableInterface;
import finale.year.stage.utility.Util;
import java.awt.Color;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author philipchicco
 */
public class Inscription extends javax.swing.JPanel {

    /**
     * Creates new form Inscription
     */
    public Inscription() {
        initComponents();
    }

    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        sexGroup = new javax.swing.ButtonGroup();
        inscriptionPanel = new javax.swing.JPanel();
        idField = new javax.swing.JTextField();
        emailField = new javax.swing.JTextField();
        idLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        passField = new javax.swing.JPasswordField();
        confirmPassField = new javax.swing.JPasswordField();
        passLabel = new javax.swing.JLabel();
        confirmPassLabel = new javax.swing.JLabel();
        submitBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        errorStatusBar = new javax.swing.JLabel();

        setBackground(new java.awt.Color(85, 172, 238));
        setLayout(new java.awt.GridBagLayout());

        inscriptionPanel.setBackground(new java.awt.Color(254, 254, 254));
        inscriptionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Inscription Form",
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Abyssinica SIL", 0, 18),
                new java.awt.Color(90, 72, 216))); // NOI18N

        idField.setColumns(35);
        idField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idFieldActionPerformed(evt);
            }
        });

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

        idLabel.setText("ID :");

        emailLabel.setText("Email :");

        passField.setColumns(35);

        confirmPassField.setColumns(35);
        confirmPassField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmPassFieldActionPerformed(evt);
            }
        });

        passLabel.setText("Password :");

        confirmPassLabel.setText("Confirm Password :");

        submitBtn.setText("Submit");
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });

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

        javax.swing.GroupLayout inscriptionPanelLayout = new javax.swing.GroupLayout(inscriptionPanel);
        inscriptionPanel.setLayout(inscriptionPanelLayout);
        inscriptionPanelLayout.setHorizontalGroup(inscriptionPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(inscriptionPanelLayout.createSequentialGroup().addGap(42, 42, 42)
                        .addGroup(inscriptionPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(emailLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 90,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(passLabel).addComponent(confirmPassLabel).addComponent(idLabel,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addGroup(inscriptionPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(errorStatusBar)
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(emailField, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(passField, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(confirmPassField, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(idField).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                inscriptionPanelLayout.createSequentialGroup()
                                                        .addComponent(cancelBtn).addGap(18, 18, 18)
                                                        .addComponent(submitBtn))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        inscriptionPanelLayout.setVerticalGroup(
                inscriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(inscriptionPanelLayout.createSequentialGroup().addContainerGap()
                                .addComponent(errorStatusBar).addGap(18, 18, 18)
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(idLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(inscriptionPanelLayout.createSequentialGroup().addGap(32, 32, 32)
                                                .addComponent(emailLabel))
                                        .addGroup(inscriptionPanelLayout.createSequentialGroup().addGap(22, 22, 22)
                                                .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(passLabel))
                                .addGap(18, 18, 18)
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(confirmPassField, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(confirmPassLabel))
                                .addGap(61, 61, 61)
                                .addGroup(inscriptionPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(cancelBtn).addComponent(submitBtn))
                                .addContainerGap(210, Short.MAX_VALUE)));

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 6;
        gridBagConstraints.ipady = 99;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(37, 56, 12, 50);
        add(inscriptionPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

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

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

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

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

        String email = getEmailField().getText();
        char[] password = getPasswordField().getPassword();
        char[] confirmedPassword = getconfirmPasswordField().getPassword();
        String id = getidField().getText();

        //Transfer to SignUp Thread
        new SignUpThread(email, password, confirmedPassword, id).start();

    }//GEN-LAST:event_submitBtnActionPerformed

    public static void updateStatus(final String message) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                try {
                    errorStatusBar.setText("");
                    errorStatusBar.setForeground(Color.red);
                    errorStatusBar.setText(message);

                } catch (NullPointerException e) {
                    System.out.println(e.getMessage());
                    return;
                }
            }

        });

    }

    //SignUp Management Thread
    private class SignUpThread extends Thread {

        private char[] passwordEncrypted;
        private char[] passwordConfirmed;
        private String email;
        private String password;
        private String id;

        public SignUpThread(String email, char[] passE, char[] passC, String id) {

            this.email = email;
            this.id = id;
            passwordEncrypted = passE;
            passwordConfirmed = passC;

        }

        @Override
        public void run() {

            //Check Email Syntax
            if (Util.validateEmail(email)) {
                //Check ID Field
                if (id.equals("") || id == null) {
                    Inscription.updateStatus("ID Empty !");
                }
                //Check ID Length
                if (id.length() != 6) {
                    Inscription.updateStatus("ID MIN 6 Characters ! ");
                    return;
                }
                //Check password Syntax
                if (passwordConfirmed.equals("") || passwordConfirmed == null && passwordEncrypted.equals("")
                        || passwordEncrypted == null) {
                    Inscription.updateStatus("Password(s) Empty ! ");
                    return;
                }
                //Check Password Length
                if (passwordConfirmed.length != 8) {
                    Inscription.updateStatus("Password(s) TOO Short ! Max/Min 8 Character(s)! ");
                    return;
                }
                if (Arrays.equals(passwordEncrypted, passwordConfirmed)) {

                    //Decrypt Passwords
                    StringBuilder bd = new StringBuilder();
                    for (char ch : passwordConfirmed) {
                        bd.append(String.format("%s", ch));
                    }
                    password = bd.toString();
                    //All fields are correct , Open main_window
                    Inscription.handleResponse(Util.signUp(email, password, id));
                    //Send to Handle Response
                    //handleResponse(Inscription.serverResponse);
                } else {
                    Inscription.updateStatus("Password(s) MisMatch");
                }

            } else {
                Inscription.updateStatus("Invalid Email");
            }
        }
    }

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        // TODO add your handling code here:
        clear();//Clear alls fields and set To Empty
        mainF.mainFrame.dispose();
        Authentification auth = new Authentification();
        mainFrame = new mainF();
        mainFrame.launchFrame(auth, mainFrame);
    }//GEN-LAST:event_cancelBtnActionPerformed

    private JButton getcancelBtn() {
        return cancelBtn;
    }

    private JButton getsubmitBtn() {
        return submitBtn;
    }

    private JPasswordField getconfirmPasswordField() {
        return confirmPassField;
    }

    private JPasswordField getPasswordField() {
        return passField;
    }

    private JTextField getEmailField() {
        return emailField;
    }

    private JTextField getidField() {
        return idField;
    }

    private static JLabel getErrorStatusBar() {
        return errorStatusBar;
    }

    private void clear() {
        getconfirmPasswordField().setText(EMPTY);
        getPasswordField().setText(EMPTY);
        getEmailField().setText(EMPTY);
        getidField().setText(EMPTY);
    }

    public static ResponsableInterface getResponsableWindow() {
        return new ResponsableInterface();
    }

    public static ResponsableInterface getResponsableWindow(JSONObject user) {
        return new ResponsableInterface(user);
    }

    private static void handleResponse(JSONObject serverRes) {
        //Handle Server Response here : Open particular user
        JSONObject user = null;
        int code = -1;
        String responseMessage = null;
        int type = -1;

        if (serverRes == null) {
            updateStatus(" Server Unresponsive! Null Exception ");
        } else {
            try {

                code = serverRes.getInt("response code");
                responseMessage = serverRes.getString("response message");
            } catch (JSONException ex) {
                Inscription.updateStatus("Response Handle Error " + ex.getMessage().trim());
            }

        }

        if (code == 1) {
            try {
                user = serverRes.getJSONObject("user");
                type = user.getInt("type");
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            switch (type) {
            case TYPE_4: //Responsable

                mainF.mainFrame.dispose();
                respInterface = getResponsableWindow(user);
                mainFrame = new mainF();
                mainFrame.launchFrame(respInterface, mainFrame);
                break;

            case TYPE_3: //Agnet de la Scolarite
                mainF.mainFrame.dispose();
                //String[] args = new String[2];
                AgentScolarite.main(user);
                break;
            default:
                Inscription.updateStatus(" Unauthrized User /n Contact Administrator ");
                break;

            }
            //
        } else {
            //If Server Status is not Ok : get ServerResoponse Message 
            Inscription.updateStatus(responseMessage);
        }

    }

    //private JSONObject userObject = null;

    //Status Code
    private final static String STATUS_OK = "1";

    //These are user types (Responsable,Etudiant, Tutuer, Agent, Enseignant )
    private final static int TYPE_0 = 0; //Student
    private final static int TYPE_1 = 1; //Tutor
    private final static int TYPE_2 = 2; //Enseignant
    private final static int TYPE_3 = 3; //Agent
    private final static int TYPE_4 = 4; //Responsable

    private static ResponsableInterface respInterface = null;
    private static mainF mainFrame = null;
    private final String EMPTY = "";
    private static String serverResponse = null;
    private boolean access = false;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JPasswordField confirmPassField;
    private javax.swing.JLabel confirmPassLabel;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private static javax.swing.JLabel errorStatusBar;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel idLabel;
    private javax.swing.JPanel inscriptionPanel;
    private javax.swing.JPasswordField passField;
    private javax.swing.JLabel passLabel;
    private javax.swing.ButtonGroup sexGroup;
    private javax.swing.JButton submitBtn;
    // End of variables declaration//GEN-END:variables
}