nosqltools.UpdatePanel.java Source code

Java tutorial

Introduction

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

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

/**
 *
 * @author Ronald
 */
public class UpdatePanel extends javax.swing.JPanel {

    private QueryCollectionDialog parent;
    protected String QueryString = "";
    protected int NoOfDocumentsUpdated = 0;

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

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfSearchField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfSearchValue = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfNewField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfNewValue = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(565, 165));
        setMinimumSize(new java.awt.Dimension(565, 165));
        setPreferredSize(new java.awt.Dimension(565, 165));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Set the criteria to select the documents that will be updated:");

        jLabel2.setText("Where Field:");

        jLabel3.setText("Has Value:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Enter the field and value that will be updated into the document specified above:");

        jLabel5.setText("Where Field:");

        jLabel6.setText("Has Value:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(jLabel4).addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(jLabel2).addGap(18, 18, 18)
                                        .addComponent(tfSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 159,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3).addGap(18, 18, 18).addComponent(tfSearchValue,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 159,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addGroup(layout.createSequentialGroup().addComponent(jLabel5)
                                                .addGap(18, 18, 18)
                                                .addComponent(tfNewField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        159, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        79, Short.MAX_VALUE)
                                                .addComponent(jLabel6)))
                                        .addGap(18, 18, 18).addComponent(tfNewValue,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 159,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap()))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(tfSearchField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3).addComponent(tfSearchValue,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27,
                                        Short.MAX_VALUE)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4).addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5).addComponent(tfNewField,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(8, 8, 8))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(tfNewValue, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel6))
                                .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    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.JSeparator jSeparator1;
    private javax.swing.JTextField tfNewField;
    private javax.swing.JTextField tfNewValue;
    private javax.swing.JTextField tfSearchField;
    private javax.swing.JTextField tfSearchValue;
    // End of variables declaration//GEN-END:variables

    protected void executeUpdateQuery(DBCollection collection) {
        BasicDBObject query = null;
        //this.parent = parent;
        this.QueryString = "";

        QueryString = Initializations.SET_SYNTAX + tfNewField.getText() + " = " + tfNewValue.getText() + "\n"
                + Initializations.WHERE_SYNTAX + tfSearchField.getText() + " = " + tfSearchValue.getText();
        query = getSearchQuery(tfSearchField.getText(), tfSearchValue.getText());
        NoOfDocumentsUpdated = collection.find(query).count();

        try {
            collection.updateMulti(this.getSearchQuery(tfSearchField.getText(), tfSearchValue.getText()),
                    this.getNewDocument(tfNewField.getText(), tfNewValue.getText()));
        } catch (Exception exp) {
            System.out.println("You are here");
            exp.getMessage();
            exp.printStackTrace();
        }

    }

    private BasicDBObject getSearchQuery(String searchField, String searchValue) {
        BasicDBObject searchQuery = new BasicDBObject().append(searchField, searchValue);
        return searchQuery;
    }

    private BasicDBObject getNewDocument(String newField, String newValue) {
        BasicDBObject newDocument = new BasicDBObject();
        newDocument.append("$set", new BasicDBObject().append(newField, newValue));

        return newDocument;
    }
}