TeacherOne.java Source code

Java tutorial

Introduction

Here is the source code for TeacherOne.java

Source

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/*
 * 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.
 */

/**
 *
 * @author mbp
 */
public class TeacherOne extends javax.swing.JPanel {

    /**
     * Creates new form TeacherSection
     */
    public static MongoClient conn;
    public static DB db;
    public static String firstname;

    public static Login login;
    public static Teacher teacher;
    public static String stuFirst;
    public static String stuMid;
    public static String stuLast;
    public static int grade;
    public static int section;

    public TeacherOne(MongoClient connection, DB db1, String firstname, Teacher t2, Login t) {
        conn = connection;
        db = db1;
        this.firstname = firstname;
        teacher = t2;
        login = t;
        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() {

        FirstnameField = new javax.swing.JTextField();
        MidnameField = new javax.swing.JTextField();
        LastnameField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        SectionField = new javax.swing.JTextField();
        GradeField = new javax.swing.JTextField();
        GetdataBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        FinalField = new javax.swing.JTextField();
        MidField = new javax.swing.JTextField();
        UpdateBtn = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        BackBtn = new javax.swing.JButton();
        LogoutBtn = new javax.swing.JButton();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }

            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

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

        jLabel1.setText("First name");

        jLabel2.setText("Mid name");

        jLabel3.setText("Last name");

        jLabel4.setText("Grade");

        jLabel5.setText("Section");

        GetdataBtn.setText("Get Data");
        GetdataBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GetdataBtnActionPerformed(evt);
            }
        });

        jLabel6.setText("Mid");

        jLabel7.setText("Final");

        UpdateBtn.setText("Update");
        UpdateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateBtnActionPerformed(evt);
            }
        });

        jLabel8.setText("Note: -1 mean mark not ready");

        BackBtn.setText("Back");
        BackBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackBtnActionPerformed(evt);
            }
        });

        LogoutBtn.setText("Log Out");
        LogoutBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(43, 43, 43).addComponent(jLabel1).addGap(55, 55, 55)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3).addGap(48, 48, 48))
                .addGroup(layout.createSequentialGroup().addGap(69, 69, 69).addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(UpdateBtn, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(GetdataBtn, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel7).addGap(62, 62, 62))
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(98, 98, 98).addComponent(jLabel4)
                                .addGap(98, 98, 98).addComponent(jLabel5))
                        .addGroup(layout.createSequentialGroup().addGap(34, 34, 34).addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(MidField, javax.swing.GroupLayout.PREFERRED_SIZE, 78,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(FinalField, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(12, 12, 12))
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(FirstnameField, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(27, 27, 27)
                                        .addComponent(MidnameField, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(30, 30, 30).addComponent(LastnameField,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(33, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(GradeField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(63, 63, 63)
                                                .addComponent(SectionField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(109, 109, 109))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(LogoutBtn)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(BackBtn).addContainerGap())))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 229,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(84, 84, 84)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(17, 17, 17)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(FirstnameField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(MidnameField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(LastnameField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4).addComponent(jLabel5))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(GradeField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(SectionField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(GetdataBtn))
                                        .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        76, Short.MAX_VALUE)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel6).addComponent(jLabel7))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(FinalField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(MidField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup().addGap(0, 19, Short.MAX_VALUE)
                                                .addComponent(UpdateBtn)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 16,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(BackBtn).addComponent(LogoutBtn))
                                .addGap(21, 21, 21)));
    }// </editor-fold>//GEN-END:initComponents

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

    }//GEN-LAST:event_MidnameFieldActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown

        // TODO add your handling code here:

    }//GEN-LAST:event_formComponentShown

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        UpdateBtn.setVisible(false);
        MidField.setVisible(false);
        FinalField.setVisible(false);
        jLabel6.setVisible(false);
        jLabel7.setVisible(false);
        jLabel8.setVisible(false);
    }//GEN-LAST:event_formComponentResized

    private void GetdataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetdataBtnActionPerformed
        // TODO add your handling code here:
        stuFirst = FirstnameField.getText();
        stuMid = MidnameField.getText();
        stuLast = LastnameField.getText();
        grade = Integer.parseInt(GradeField.getText());
        section = Integer.parseInt(SectionField.getText());
        DBCollection coll = db.getCollection("student");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("firstname", stuFirst));
        obj.add(new BasicDBObject("mid_name", stuMid));
        obj.add(new BasicDBObject("lastname", stuLast));
        obj.add(new BasicDBObject("grade", grade));
        obj.add(new BasicDBObject("section", section));

        andQuery.put("$and", obj);
        DBCursor cursor = coll.find(andQuery);

        if (cursor.count() != 0) {
            UpdateBtn.setVisible(true);
            MidField.setVisible(true);
            FinalField.setVisible(true);
            jLabel6.setVisible(true);
            jLabel7.setVisible(true);
            jLabel8.setVisible(true);

            BasicDBList marks = (BasicDBList) cursor.next().get("marks");
            for (int j = 0; j < marks.size(); ++j) {
                BasicDBObject mark = (BasicDBObject) marks.get(j);

                if (mark.getString("teacher").equals(firstname)) {

                    if (mark.get("mid") == null) {

                        MidField.setText("-1");
                        FinalField.setText("-1");

                    } else if (mark.get("mid") != null && mark.get("final") == null) {

                        MidField.setText(mark.getString("mid"));
                        FinalField.setText("-1");
                    } else {

                        MidField.setText(mark.getString("mid"));
                        FinalField.setText(mark.getString("final"));

                    }

                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "wrong data,try again!");
            FirstnameField.setText("");
            MidnameField.setText("");
            LastnameField.setText("");
            GradeField.setText("");
            SectionField.setText("");
        }
    }//GEN-LAST:event_GetdataBtnActionPerformed

    private void UpdateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateBtnActionPerformed
        // TODO add your handling code here:
        DBCollection coll = db.getCollection("student");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("firstname", stuFirst));
        obj.add(new BasicDBObject("mid_name", stuMid));
        obj.add(new BasicDBObject("lastname", stuLast));
        obj.add(new BasicDBObject("grade", grade));
        obj.add(new BasicDBObject("section", section));
        obj.add(new BasicDBObject("marks.teacher", firstname));
        andQuery.put("$and", obj);
        BasicDBObject data = new BasicDBObject();
        if (MidField.getText().equals("-1") && FinalField.getText().equals("-1")) {
            JOptionPane.showMessageDialog(null, "No data was updated!");
        } else if (!MidField.getText().equals("-1") && FinalField.getText().equals("-1")) {
            data.put("marks.$.mid", MidField.getText());
            JOptionPane.showMessageDialog(null, "data was updated .");
        } else {
            data.put("marks.$.mid", MidField.getText());
            data.put("marks.$.final", FinalField.getText());
            JOptionPane.showMessageDialog(null, "data was updated .");

        }
        BasicDBObject command = new BasicDBObject();
        command.put("$set", data);
        coll.update(andQuery, command);

    }//GEN-LAST:event_UpdateBtnActionPerformed

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BackBtn;
    private javax.swing.JTextField FinalField;
    private javax.swing.JTextField FirstnameField;
    private javax.swing.JButton GetdataBtn;
    private javax.swing.JTextField GradeField;
    private javax.swing.JTextField LastnameField;
    private javax.swing.JButton LogoutBtn;
    private javax.swing.JTextField MidField;
    private javax.swing.JTextField MidnameField;
    private javax.swing.JTextField SectionField;
    private javax.swing.JButton UpdateBtn;
    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.JLabel jLabel8;
    // End of variables declaration//GEN-END:variables
}