mongodb_teste.Jmongo.java Source code

Java tutorial

Introduction

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

import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import static java.lang.String.format;
import static java.lang.String.format;
import org.bson.Document;

import java.text.DateFormat;
import static java.text.MessageFormat.format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import static java.util.Arrays.asList;
import org.bson.Document;
import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;

import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Sorts.ascending;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import org.bson.conversions.Bson;

/**
 *
 * @author miguelfreitas
 */
public class Jmongo extends javax.swing.JFrame {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);

    /**
     * Creates new form Jmongo
     */
    public Jmongo() {
        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() {

        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        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();
        jInNomeProf = new javax.swing.JTextField();
        jInemailProf = new javax.swing.JTextField();
        jInMateriaProf = new javax.swing.JTextField();
        jInNotaProf = new javax.swing.JTextField();
        jIntelefoneProf = new javax.swing.JTextField();
        jBInserir = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jSearchName = new javax.swing.JTextField();
        jBProfSearch = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jshowProfSearch = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jshowMateriaSearch = new javax.swing.JTextArea();
        jMatSearch = new javax.swing.JTextField();
        jBMatSearch = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jMatInNomeProf = new javax.swing.JTextField();
        jMatInNome = new javax.swing.JTextField();
        jMatInNota = new javax.swing.JTextField();
        jBinsertMat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nome");

        jLabel2.setText("email");

        jLabel3.setText("Nome da Materia");

        jLabel4.setText("nota");

        jLabel5.setText("Telefone");

        jBInserir.setText("Pronto!");
        jBInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBInserirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3)
                                .addComponent(jLabel4).addComponent(jLabel5))
                        .addGap(19, 19, 19)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jInNomeProf).addComponent(jInemailProf).addComponent(jInMateriaProf)
                                .addComponent(jInNotaProf).addComponent(jIntelefoneProf))
                        .addContainerGap())
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(141, 141, 141).addComponent(jBInserir)
                        .addContainerGap(178, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(27, 27, 27).addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(jInNomeProf, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(jInemailProf,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(jInMateriaProf,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4).addComponent(jInNotaProf,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5).addComponent(jIntelefoneProf,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18).addComponent(jBInserir).addContainerGap(36, Short.MAX_VALUE)));

        jTabbedPane2.addTab("Inserir Prof", jPanel1);

        jLabel6.setText("Nome");

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

        jshowProfSearch.setColumns(20);
        jshowProfSearch.setRows(5);
        jScrollPane3.setViewportView(jshowProfSearch);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jBProfSearch))
                        .addGroup(jPanel2Layout.createSequentialGroup().addGap(72, 72, 72)
                                .addComponent(jSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, 234,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 76, Short.MAX_VALUE))
                        .addComponent(jScrollPane3)).addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(26, 26, 26).addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6)
                        .addComponent(jSearchName, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jBProfSearch))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                        .addContainerGap()));

        jTabbedPane2.addTab("Pesq Prof", jPanel2);

        jLabel7.setText("Nome");

        jshowMateriaSearch.setColumns(20);
        jshowMateriaSearch.setRows(5);
        jScrollPane2.setViewportView(jshowMateriaSearch);

        jBMatSearch.setText("ok");
        jBMatSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBMatSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2)
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jMatSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 248,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31,
                                                Short.MAX_VALUE)
                                        .addComponent(jBMatSearch)))
                        .addContainerGap()));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel7)
                        .addComponent(jMatSearch, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jBMatSearch)).addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                        .addContainerGap()));

        jTabbedPane2.addTab("Pesq Materia", jPanel3);

        jLabel8.setText("Nome do Professor");

        jLabel9.setText("Nome de Materia");

        jLabel10.setText("Nota");

        jBinsertMat.setText("Inserir");
        jBinsertMat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBinsertMatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(jPanel4Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8).addComponent(jLabel9).addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jMatInNomeProf).addComponent(jMatInNome).addComponent(jMatInNota))
                        .addContainerGap())
                .addGroup(jPanel4Layout.createSequentialGroup().addGap(148, 148, 148).addComponent(jBinsertMat)
                        .addContainerGap(178, Short.MAX_VALUE)));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addGap(56, 56, 56).addGroup(jPanel4Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel8)
                        .addComponent(jMatInNomeProf, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9).addComponent(jMatInNome,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel10).addComponent(jMatInNota,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45).addComponent(jBinsertMat).addContainerGap(54, Short.MAX_VALUE)));

        jTabbedPane2.addTab("Inserir Mat", jPanel4);

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

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

    private void jBInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBInserirActionPerformed
        // TODO add your handling code here:
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase("BDprject");
        db.getCollection("Professores")
                .insertOne(new Document("nome", jInNomeProf.getText()).append("email", jInemailProf.getText())
                        .append("materias", asList(new Document().append("nome", jInMateriaProf.getText())
                                .append("nota", jInNotaProf.getText())))
                        .append("telefone", jIntelefoneProf.getText()));
        JOptionPane.showMessageDialog(null, "Inserido com sucesso");
        mongoClient.close();
    }//GEN-LAST:event_jBInserirActionPerformed

    private void jBProfSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBProfSearchActionPerformed
        // TODO add your handling code here:
        jshowProfSearch.setText("");
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase("BDprject");
        Iterable<Document> iterable = db.getCollection("Professores")
                .aggregate(Arrays.asList((Aggregates.match(new Document("nome", jSearchName.getText()))),
                        Aggregates.unwind("$materias"),
                        Aggregates.group("$materias.nota", Accumulators.sum("total", 1)),
                        Aggregates.sort(new Document("total", -1))));
        Iterator<Document> it = iterable.iterator();
        while (it.hasNext()) {
            jshowProfSearch.append(it.next().toString() + "\n");
        }
        mongoClient.close();
    }//GEN-LAST:event_jBProfSearchActionPerformed

    private void jBMatSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBMatSearchActionPerformed
        // TODO add your handling code here:
        jshowMateriaSearch.setText("");
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase("BDprject");
        Iterable<Document> iterable = db.getCollection("Professores")
                .find(new Document("materias.nome", jMatSearch.getText()));
        Iterator<Document> it = iterable.iterator();
        while (it.hasNext()) {
            jshowMateriaSearch.append(it.next().toString() + "\n");
        }
        mongoClient.close();
    }//GEN-LAST:event_jBMatSearchActionPerformed

    private void jBinsertMatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBinsertMatActionPerformed
        // TODO add your handling code here:
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase("BDprject");
        db.getCollection("Professores").updateOne(new Document("nome", jMatInNomeProf.getText()),
                new Document("$push", new Document("materias",
                        new Document("nome", jMatInNome.getText()).append("nota", jMatInNota.getText()))));
        JOptionPane.showMessageDialog(null, "Inserido com sucesso");
        mongoClient.close();
    }//GEN-LAST:event_jBinsertMatActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBInserir;
    private javax.swing.JButton jBMatSearch;
    private javax.swing.JButton jBProfSearch;
    private javax.swing.JButton jBinsertMat;
    private javax.swing.JTextField jInMateriaProf;
    private javax.swing.JTextField jInNomeProf;
    private javax.swing.JTextField jInNotaProf;
    private javax.swing.JTextField jInemailProf;
    private javax.swing.JTextField jIntelefoneProf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    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.JTextField jMatInNome;
    private javax.swing.JTextField jMatInNomeProf;
    private javax.swing.JTextField jMatInNota;
    private javax.swing.JTextField jMatSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jSearchName;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextArea jshowMateriaSearch;
    private javax.swing.JTextArea jshowProfSearch;
    // End of variables declaration//GEN-END:variables
}