Java tutorial
/* * 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.AggregationOutput; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBObject; import java.util.Iterator; /** * * @author mgalea */ public class GroupByPanel extends javax.swing.JPanel { private QueryCollectionDialog parent; protected String QueryString = ""; private String SortOrder = Initializations.ASCENDING_SYNTAX; /** * Creates new form groupByPanel */ public GroupByPanel() { 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() { matchPanel = new javax.swing.JPanel(); ValueTextArea = new javax.swing.JTextField(); FieldTextArea = new javax.swing.JTextField(); whereValueFieldLabel = new javax.swing.JLabel(); groupByRB = new javax.swing.JRadioButton(); groupByTF = new javax.swing.JTextField(); whereFieldLabel = new javax.swing.JLabel(); countRB = new javax.swing.JRadioButton(); SumRB = new javax.swing.JRadioButton(); AvgRB = new javax.swing.JRadioButton(); sumTF = new javax.swing.JTextField(); avgTF = new javax.swing.JTextField(); matchPanel.setMaximumSize(new java.awt.Dimension(471, 135)); matchPanel.setMinimumSize(new java.awt.Dimension(471, 135)); matchPanel.setPreferredSize(new java.awt.Dimension(471, 135)); whereValueFieldLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N whereValueFieldLabel.setText("Has Value = "); whereValueFieldLabel.setFocusable(false); groupByRB.setText("Group By:"); groupByRB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { groupByRBActionPerformed(evt); } }); groupByTF.setEnabled(false); whereFieldLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N whereFieldLabel.setText("Where Field:"); countRB.setSelected(true); countRB.setText("Count"); countRB.setEnabled(false); countRB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { countRBRBActionPerformed(evt); } }); SumRB.setText("Sum"); SumRB.setEnabled(false); SumRB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SumRBRBActionPerformed(evt); } }); AvgRB.setText("Average"); AvgRB.setEnabled(false); AvgRB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AvgRBRBActionPerformed(evt); } }); sumTF.setEnabled(false); avgTF.setEnabled(false); javax.swing.GroupLayout matchPanelLayout = new javax.swing.GroupLayout(matchPanel); matchPanel.setLayout(matchPanelLayout); matchPanelLayout.setHorizontalGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(matchPanelLayout.createSequentialGroup().addContainerGap() .addGroup(matchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(groupByRB).addComponent(whereFieldLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(matchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(FieldTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(groupByTF, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE) .addGroup( matchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(whereValueFieldLabel) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(SumRB).addComponent(AvgRB).addComponent(countRB))) .addGap(18, 18, 18) .addGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(avgTF, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE) .addComponent(sumTF).addComponent(ValueTextArea)) .addContainerGap())); matchPanelLayout.setVerticalGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(matchPanelLayout.createSequentialGroup().addContainerGap().addGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ValueTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(FieldTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(whereValueFieldLabel).addComponent(whereFieldLabel)) .addGroup(matchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(matchPanelLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(groupByRB).addComponent(groupByTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, matchPanelLayout .createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE) .addComponent(countRB).addGap(18, 18, 18) .addGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(avgTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(AvgRB)) .addGap(18, 18, 18) .addGroup(matchPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(sumTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(SumRB)) .addContainerGap())))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(matchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(matchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void groupByRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_groupByRBActionPerformed if (this.groupByRB.isSelected()) { groupByTF.setEnabled(true); countRB.setEnabled(true); countRB.setSelected(true); SumRB.setEnabled(true); SumRB.setSelected(false); sumTF.setText(""); AvgRB.setEnabled(true); AvgRB.setSelected(false); avgTF.setText(""); } else { groupByTF.setEnabled(false); countRB.setEnabled(false); SumRB.setEnabled(false); sumTF.setEnabled(false); AvgRB.setEnabled(false); avgTF.setEnabled(false); } }//GEN-LAST:event_groupByRBActionPerformed private void countRBRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_countRBRBActionPerformed if (evt.getSource().equals(countRB)) { countRB.setSelected(true); SumRB.setSelected(false); sumTF.setEnabled(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else if (evt.getSource().equals(SumRB)) { SumRB.setSelected(true); sumTF.setEnabled(true); countRB.setSelected(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else { AvgRB.setSelected(true); avgTF.setEnabled(true); sumTF.setEnabled(false); SumRB.setSelected(false); countRB.setSelected(false); } }//GEN-LAST:event_countRBRBActionPerformed private void SumRBRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SumRBRBActionPerformed if (evt.getSource().equals(countRB)) { countRB.setSelected(true); SumRB.setSelected(false); sumTF.setEnabled(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else if (evt.getSource().equals(SumRB)) { SumRB.setSelected(true); sumTF.setEnabled(true); countRB.setSelected(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else { AvgRB.setSelected(true); avgTF.setEnabled(true); sumTF.setEnabled(false); SumRB.setSelected(false); countRB.setSelected(false); } }//GEN-LAST:event_SumRBRBActionPerformed private void AvgRBRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AvgRBRBActionPerformed if (evt.getSource().equals(countRB)) { countRB.setSelected(true); SumRB.setSelected(false); sumTF.setEnabled(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else if (evt.getSource().equals(SumRB)) { SumRB.setSelected(true); sumTF.setEnabled(true); countRB.setSelected(false); AvgRB.setSelected(false); avgTF.setEnabled(false); } else { AvgRB.setSelected(true); avgTF.setEnabled(true); sumTF.setEnabled(false); SumRB.setSelected(false); countRB.setSelected(false); } }//GEN-LAST:event_AvgRBRBActionPerformed protected String executeGroupByOperation(DBCollection collection, QueryCollectionDialog parent) { this.parent = parent; AggregationOutput aggOut = null; String idField = this.FieldTextArea.getText(); String valueField = this.ValueTextArea.getText(); BasicDBObject match = new BasicDBObject("$match", new BasicDBObject(idField, valueField)); BasicDBObject group = null; BasicDBObject sort = null; this.QueryString = ""; String groupByWith = ""; String orderON = ""; String orderDirection = ""; if (this.groupByRB.isSelected()) { if (this.countRB.isSelected()) { groupByWith = Initializations.COUNT_AGGREGATE_SYNTAX; group = new BasicDBObject("$group", new BasicDBObject("_id", "$" + this.groupByTF.getText()) .append("Count", new BasicDBObject("$sum", 1))); } else if (this.SumRB.isSelected()) { groupByWith = Initializations.SUM_AGGREGATE_SYNTAX; group = new BasicDBObject("$group", new BasicDBObject("_id", "$" + this.groupByTF.getText()) .append("Sum", new BasicDBObject("$sum", "$" + this.sumTF.getText()))); } else if (this.AvgRB.isSelected()) { groupByWith = Initializations.AVERAGE_AGGREGATE_SYNTAX; group = new BasicDBObject("$group", new BasicDBObject("_id", "$" + this.groupByTF.getText()) .append("Average", new BasicDBObject("$avg", "$" + this.avgTF.getText()))); } //To allow group by without any matching condition... if ((idField == null || idField.equals("")) && (valueField == null || valueField.equals(""))) { if (this.parent.orderByRB.isSelected()) { orderON = groupByTF.getText() + " "; sort = getOrderByObject(groupByTF.getText()); } if (sort == null) { QueryString += Initializations.GROUP_BY_SYNTAX + groupByTF.getText() + Initializations.WITH_SYNTAX + groupByWith + "\n"; aggOut = collection.aggregate(group); } else { QueryString += Initializations.GROUP_BY_SYNTAX + groupByTF.getText() + Initializations.WITH_SYNTAX + groupByWith + "\n"; QueryString += Initializations.ORDER_BY_SYNTAX + orderON + SortOrder + "\n"; aggOut = collection.aggregate(group, sort); } } else { //Check whether order by is selected and act accordingly... if (this.parent.orderByRB.isSelected()) { if (groupByTF.getText() != "" || groupByTF.getText() != null) { orderON = groupByTF.getText() + " "; sort = getOrderByObject(groupByTF.getText()); } else { orderON = idField + " "; sort = getOrderByObject(idField); } } if (sort == null) { QueryString += Initializations.SELECT_SYNTAX + FieldTextArea.getText() + " = " + ValueTextArea.getText() + "\n"; QueryString += Initializations.GROUP_BY_SYNTAX + groupByTF.getText() + Initializations.WITH_SYNTAX + groupByWith + "\n"; aggOut = collection.aggregate(match, group); } else { QueryString += Initializations.SELECT_SYNTAX + FieldTextArea.getText() + " = " + ValueTextArea.getText() + "\n"; QueryString += Initializations.GROUP_BY_SYNTAX + groupByTF.getText() + Initializations.WITH_SYNTAX + groupByWith + "\n"; QueryString += Initializations.ORDER_BY_SYNTAX + orderON + SortOrder + "\n"; aggOut = collection.aggregate(match, group, sort); } } } else { if (sort == null) { QueryString += Initializations.SELECT_SYNTAX + FieldTextArea.getText() + " = " + ValueTextArea.getText() + "\n"; aggOut = collection.aggregate(match); } else { QueryString += Initializations.SELECT_SYNTAX + FieldTextArea.getText() + " = " + ValueTextArea.getText() + "\n"; QueryString += Initializations.ORDER_BY_SYNTAX + orderON + SortOrder + "\n"; aggOut = collection.aggregate(match, sort); } } Iterator<DBObject> results = aggOut.results().iterator(); String res = ""; while (results.hasNext()) { res += results.next().toString() + "\n\n"; } return res; } private BasicDBObject getOrderByObject(String idField) { String orderByValue = this.parent.orderByTF.getText(); int sortOrder = 1; //Get the order by order if Asc or Desc... by default ASC i.e 1 if (this.parent.descendingRB.isSelected()) { SortOrder = Initializations.DESCENDING_SYNTAX; sortOrder = -1; } //Since the field name will be converted to _id by mongo aggregate... if (!idField.equals("") && orderByValue.equalsIgnoreCase(idField)) { return new BasicDBObject("$sort", new BasicDBObject("_id", sortOrder)); } else { return new BasicDBObject("$sort", new BasicDBObject(orderByValue, sortOrder)); } } // Variables declaration - do not modify//GEN-BEGIN:variables protected javax.swing.JRadioButton AvgRB; protected javax.swing.JTextField FieldTextArea; protected javax.swing.JRadioButton SumRB; protected javax.swing.JTextField ValueTextArea; protected javax.swing.JTextField avgTF; protected javax.swing.JRadioButton countRB; protected javax.swing.JRadioButton groupByRB; protected javax.swing.JTextField groupByTF; private javax.swing.JPanel matchPanel; protected javax.swing.JTextField sumTF; private javax.swing.JLabel whereFieldLabel; private javax.swing.JLabel whereValueFieldLabel; // End of variables declaration//GEN-END:variables }