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 yelpmongodb; import com.mongodb.AggregationOutput; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import java.awt.Component; import java.awt.Dimension; import java.awt.GridLayout; import static java.lang.Math.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Scanner; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableModel; /** * * @author Tim */ public class hw4 extends javax.swing.JFrame { /** * Creates new form hw4 */ public hw4() { 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() { mainPanel = new javax.swing.JPanel(); mainCategoriesScroll = new javax.swing.JScrollPane(); mainCategoriesPanel = new javax.swing.JPanel(); activeCheckBox = new javax.swing.JCheckBox(); artsCheckBox = new javax.swing.JCheckBox(); autoCheckBox = new javax.swing.JCheckBox(); carCheckBox = new javax.swing.JCheckBox(); cafesCheckBox = new javax.swing.JCheckBox(); beautyCheckBox = new javax.swing.JCheckBox(); convenienceCheckBox = new javax.swing.JCheckBox(); dentistsCheckBox = new javax.swing.JCheckBox(); doctorsCheckBox = new javax.swing.JCheckBox(); drugstoresCheckBox = new javax.swing.JCheckBox(); departmentCheckBox = new javax.swing.JCheckBox(); eduCheckBox = new javax.swing.JCheckBox(); eventCheckBox = new javax.swing.JCheckBox(); flowersCheckBox = new javax.swing.JCheckBox(); foodCheckBox = new javax.swing.JCheckBox(); healthCheckBox = new javax.swing.JCheckBox(); homeCheckBox = new javax.swing.JCheckBox(); gardenCheckBox = new javax.swing.JCheckBox(); hospitalsCheckBox = new javax.swing.JCheckBox(); hotelsCheckBox = new javax.swing.JCheckBox(); hardwareCheckBox = new javax.swing.JCheckBox(); groceryCheckBox = new javax.swing.JCheckBox(); medicalCheckBox = new javax.swing.JCheckBox(); nurseriesCheckBox = new javax.swing.JCheckBox(); nightlifeCheckBox = new javax.swing.JCheckBox(); restaurantsCheckBox = new javax.swing.JCheckBox(); shoppingCheckBox = new javax.swing.JCheckBox(); transportCheckBox = new javax.swing.JCheckBox(); attributesScrollBar = new javax.swing.JScrollPane(); attributesPanel = new javax.swing.JPanel(); businessesScrollBar = new javax.swing.JScrollPane(); businessesTable = new javax.swing.JTable(); poiText = new javax.swing.JLabel(); poiSelection = new javax.swing.JComboBox(); proximityText = new javax.swing.JLabel(); proximitySelection = new javax.swing.JComboBox(); searchText = new javax.swing.JLabel(); searchSelection = new javax.swing.JComboBox(); searchButton = new javax.swing.JButton(); closeButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new java.awt.Dimension(1100, 700)); setPreferredSize(new java.awt.Dimension(1100, 700)); getContentPane().setLayout(null); mainPanel.setMinimumSize(new java.awt.Dimension(1000, 675)); mainCategoriesScroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); activeCheckBox.setText("Active Life"); activeCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { activeCheckBoxActionPerformed(evt); } }); artsCheckBox.setText("Arts & Entertainment"); artsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { artsCheckBoxActionPerformed(evt); } }); autoCheckBox.setText("Automotive"); autoCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { autoCheckBoxActionPerformed(evt); } }); carCheckBox.setText("Car Rental"); carCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { carCheckBoxActionPerformed(evt); } }); cafesCheckBox.setText("Cafes"); cafesCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cafesCheckBoxActionPerformed(evt); } }); beautyCheckBox.setText("Beauty & Spas"); beautyCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { beautyCheckBoxActionPerformed(evt); } }); convenienceCheckBox.setText("Convenience Stores"); convenienceCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { convenienceCheckBoxActionPerformed(evt); } }); dentistsCheckBox.setText("Dentists"); dentistsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dentistsCheckBoxActionPerformed(evt); } }); doctorsCheckBox.setText("Doctors"); doctorsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { doctorsCheckBoxActionPerformed(evt); } }); drugstoresCheckBox.setText("Drugstores"); drugstoresCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { drugstoresCheckBoxActionPerformed(evt); } }); departmentCheckBox.setText("Department Stores"); departmentCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { departmentCheckBoxActionPerformed(evt); } }); eduCheckBox.setText("Education"); eduCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { eduCheckBoxActionPerformed(evt); } }); eventCheckBox.setText("Event Planning & Services"); eventCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { eventCheckBoxActionPerformed(evt); } }); flowersCheckBox.setText("Flowers & Gifts"); flowersCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { flowersCheckBoxActionPerformed(evt); } }); foodCheckBox.setText("Food"); foodCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { foodCheckBoxActionPerformed(evt); } }); healthCheckBox.setText("Health & Medical"); healthCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { healthCheckBoxActionPerformed(evt); } }); homeCheckBox.setText("Home Services"); homeCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { homeCheckBoxActionPerformed(evt); } }); gardenCheckBox.setText("Home & Garden"); gardenCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { gardenCheckBoxActionPerformed(evt); } }); hospitalsCheckBox.setText("Hospitals"); hospitalsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { hospitalsCheckBoxActionPerformed(evt); } }); hotelsCheckBox.setText("Hotels & Travel"); hotelsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { hotelsCheckBoxActionPerformed(evt); } }); hardwareCheckBox.setText("Hardware Stores"); hardwareCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { hardwareCheckBoxActionPerformed(evt); } }); groceryCheckBox.setText("Grocery"); groceryCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { groceryCheckBoxActionPerformed(evt); } }); medicalCheckBox.setText("Medical Centers"); medicalCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { medicalCheckBoxActionPerformed(evt); } }); nurseriesCheckBox.setText("Nurseries & Gardening"); nurseriesCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nurseriesCheckBoxActionPerformed(evt); } }); nightlifeCheckBox.setText("Nightlife"); nightlifeCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nightlifeCheckBoxActionPerformed(evt); } }); restaurantsCheckBox.setText("Restaurants"); restaurantsCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { restaurantsCheckBoxActionPerformed(evt); } }); shoppingCheckBox.setText("Shopping"); shoppingCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shoppingCheckBoxActionPerformed(evt); } }); transportCheckBox.setText("Transportation"); transportCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { transportCheckBoxActionPerformed(evt); } }); javax.swing.GroupLayout mainCategoriesPanelLayout = new javax.swing.GroupLayout(mainCategoriesPanel); mainCategoriesPanel.setLayout(mainCategoriesPanelLayout); mainCategoriesPanelLayout.setHorizontalGroup(mainCategoriesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainCategoriesPanelLayout.createSequentialGroup().addContainerGap() .addGroup(mainCategoriesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainCategoriesPanelLayout.createSequentialGroup() .addGroup(mainCategoriesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(homeCheckBox).addComponent(gardenCheckBox) .addComponent(hospitalsCheckBox).addComponent(hotelsCheckBox) .addComponent(groceryCheckBox).addComponent(medicalCheckBox)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(mainCategoriesPanelLayout.createSequentialGroup() .addGroup(mainCategoriesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(foodCheckBox).addComponent(departmentCheckBox) .addComponent(eduCheckBox).addComponent(eventCheckBox) .addComponent(carCheckBox).addComponent(activeCheckBox) .addComponent(artsCheckBox).addComponent(autoCheckBox) .addComponent(cafesCheckBox).addComponent(beautyCheckBox) .addComponent(convenienceCheckBox).addComponent(dentistsCheckBox) .addComponent(doctorsCheckBox).addComponent(drugstoresCheckBox) .addComponent(flowersCheckBox).addComponent(healthCheckBox) .addComponent(nurseriesCheckBox).addComponent(hardwareCheckBox) .addComponent(nightlifeCheckBox).addComponent(restaurantsCheckBox) .addComponent(shoppingCheckBox).addComponent(transportCheckBox)) .addGap(0, 416, Short.MAX_VALUE))))); mainCategoriesPanelLayout.setVerticalGroup( mainCategoriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainCategoriesPanelLayout.createSequentialGroup().addContainerGap() .addComponent(activeCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(artsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(autoCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(carCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cafesCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(beautyCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(convenienceCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dentistsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(doctorsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(drugstoresCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(departmentCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(eduCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(eventCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(flowersCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(foodCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(healthCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(homeCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(gardenCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(hospitalsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(hotelsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(hardwareCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(groceryCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(medicalCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(nurseriesCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(nightlifeCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(restaurantsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(shoppingCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(transportCheckBox).addContainerGap(75, Short.MAX_VALUE))); mainCategoriesScroll.setViewportView(mainCategoriesPanel); javax.swing.GroupLayout attributesPanelLayout = new javax.swing.GroupLayout(attributesPanel); attributesPanel.setLayout(attributesPanelLayout); attributesPanelLayout.setHorizontalGroup(attributesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 248, Short.MAX_VALUE)); attributesPanelLayout.setVerticalGroup(attributesPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); attributesScrollBar.setViewportView(attributesPanel); businessesTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Business", "City", "State", "Stars" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class }; boolean[] canEdit = new boolean[] { false, false, false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); businessesTable.getTableHeader().setReorderingAllowed(false); businessesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { int row = businessesTable.getSelectedRow(); if (row >= 0 && !e.getValueIsAdjusting() && !businessesTable.getSelectionModel().isSelectionEmpty()) { String name = businessesTable.getValueAt(row, 0).toString(); String city = businessesTable.getValueAt(row, 1).toString(); String state = businessesTable.getValueAt(row, 2).toString(); String rating = businessesTable.getValueAt(row, 3).toString(); try { createReviewTable(name, city, state, rating); } catch (Exception ex) { ex.printStackTrace(); } } } }); businessesScrollBar.setViewportView(businessesTable); poiText.setText("Point of Interest:"); poiSelection.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "5415 Monona Dr\\nMonona, WI 53716", "2819 N Central Ave\\nPhoenix, AZ 85004", "16770 North Perimeter Drive\\nScottsdale, AZ 85260", "4450 N. Lamb Blvd.\\nLas Vegas, NV 89115", "546 N Grand Ave\\nSun Prairie, WI 53590" })); proximityText.setText("Proximity:"); proximitySelection.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "5 miles", "10 miles", "20 miles", "30 miles", "50 miles" })); searchText.setText("Search For:"); searchSelection.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "All Attributes", "Any Attributes" })); searchButton.setText("Search"); searchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchButtonActionPerformed(evt); } }); closeButton.setText("Close"); closeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonActionPerformed(evt); } }); javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout .setHorizontalGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(mainPanelLayout .createSequentialGroup().addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup().addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(mainCategoriesScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addGroup(mainPanelLayout.createSequentialGroup().addContainerGap() .addComponent(poiSelection, 0, 238, Short.MAX_VALUE))) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED)) .addGroup(mainPanelLayout.createSequentialGroup().addContainerGap() .addComponent(poiText).addGap(123, 123, 123))) .addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(attributesScrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(proximityText).addComponent(proximitySelection, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(businessesScrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE) .addGroup(mainPanelLayout.createSequentialGroup().addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(searchText).addComponent(searchSelection, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27))))); mainPanelLayout.setVerticalGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup().addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(mainCategoriesScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addComponent(attributesScrollBar).addComponent(businessesScrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(mainPanelLayout.createSequentialGroup().addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(poiText).addComponent(proximityText).addComponent(searchText)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(mainPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(poiSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(proximitySelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(closeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(0, 29, Short.MAX_VALUE))); getContentPane().add(mainPanel); mainPanel.setBounds(0, 0, 1000, 675); pack(); }// </editor-fold>//GEN-END:initComponents private void activeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_activeCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_activeCheckBoxActionPerformed private void artsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_artsCheckBoxActionPerformed private void autoCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_autoCheckBoxActionPerformed private void carCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_carCheckBoxActionPerformed private void cafesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cafesCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_cafesCheckBoxActionPerformed private void beautyCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_beautyCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_beautyCheckBoxActionPerformed private void convenienceCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convenienceCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_convenienceCheckBoxActionPerformed private void dentistsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dentistsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_dentistsCheckBoxActionPerformed private void doctorsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doctorsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_doctorsCheckBoxActionPerformed private void drugstoresCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drugstoresCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_drugstoresCheckBoxActionPerformed private void departmentCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_departmentCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_departmentCheckBoxActionPerformed private void eduCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eduCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_eduCheckBoxActionPerformed private void eventCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_eventCheckBoxActionPerformed private void flowersCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_flowersCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_flowersCheckBoxActionPerformed private void foodCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_foodCheckBoxActionPerformed private void healthCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_healthCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_healthCheckBoxActionPerformed private void homeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_homeCheckBoxActionPerformed private void gardenCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gardenCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_gardenCheckBoxActionPerformed private void hospitalsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hospitalsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_hospitalsCheckBoxActionPerformed private void hotelsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hotelsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_hotelsCheckBoxActionPerformed private void hardwareCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hardwareCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_hardwareCheckBoxActionPerformed private void groceryCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_groceryCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_groceryCheckBoxActionPerformed private void medicalCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_medicalCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_medicalCheckBoxActionPerformed private void nurseriesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nurseriesCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_nurseriesCheckBoxActionPerformed private void nightlifeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nightlifeCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_nightlifeCheckBoxActionPerformed private void restaurantsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurantsCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_restaurantsCheckBoxActionPerformed private void shoppingCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shoppingCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_shoppingCheckBoxActionPerformed private void transportCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transportCheckBoxActionPerformed checkMainCategories(); }//GEN-LAST:event_transportCheckBoxActionPerformed private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed DefaultTableModel table = (DefaultTableModel) businessesTable.getModel(); table.setRowCount(0); double[] poi = getPointOfInterest(); int proximity = getProximity(); ArrayList<String> maincat = getCategories(mainCategoriesPanel); ArrayList<String> att = getCategories(attributesPanel); if (maincat.size() > 0 && att.size() > 0) { queryBusinesses(poi, proximity, maincat, att); } }//GEN-LAST:event_searchButtonActionPerformed private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed System.exit(0); }//GEN-LAST:event_closeButtonActionPerformed /** * @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(hw4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(hw4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(hw4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(hw4.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 hw4().setVisible(true); } }); } private void checkMainCategories() { ArrayList<String> list = getCategories(mainCategoriesPanel); ArrayList<String> attList = new ArrayList<>(); removeComponents(attributesPanel, attributesScrollBar); MongoClient client = new MongoClient(); DB db = client.getDB("db"); DBCollection coll = db.getCollection("business"); String attribute = ""; if (list.size() > 0) { DBObject mainQuery = mainCategoriesQuery(list, 0); List curs = coll.distinct("attributes", mainQuery); for (int i = 0; i < curs.size(); i++) { BasicDBObject dbo = (BasicDBObject) curs.get(i); for (Entry<String, Object> entry : dbo.entrySet()) { if (entry.getValue() instanceof BasicDBObject) { BasicDBObject nestedobj = (BasicDBObject) entry.getValue(); for (Entry<String, Object> nestedentry : nestedobj.entrySet()) { attribute = entry.getKey() + ":" + nestedentry.getKey() + ":" + nestedentry.getValue(); } } else { attribute = entry.getKey() + ":" + entry.getValue(); } if (!(attList.contains(attribute))) { attList.add(attribute); } } } createAttributeCheckBox(attList); client.close(); } } private ArrayList<String> getCategories(JPanel panel) { ArrayList<String> list = new ArrayList<>(); for (Component comp : panel.getComponents()) { if (comp instanceof javax.swing.JCheckBox) { JCheckBox cb = (JCheckBox) comp; if (cb.isSelected()) { list.add(cb.getText()); } } } return list; } private void removeComponents(JPanel panel, JScrollPane spanel) { if (panel.getComponents().length > 0) { for (Component comp : panel.getComponents()) { if (comp instanceof JCheckBox) { panel.remove(comp); } } spanel.updateUI(); } } private void createReviewTable(String name, String city, String state, String rating) { String columnNames[] = new String[] { "Date", "Rating", "Review Text", "Username", "Useful Votes" }; JFrame frame = new JFrame("Reviews"); frame.setMinimumSize(new Dimension(1000, 600)); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 0)); JTable table = new JTable(); DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setColumnIdentifiers(columnNames); JScrollPane scrollPane = new JScrollPane(table); String bid = getBusinessID(name, city, state, Double.parseDouble(rating)); MongoClient client = new MongoClient(); DB db = client.getDB("db"); DBCollection coll = db.getCollection("review"); String username = ""; DBObject clause = new BasicDBObject("business_id", bid); DBCursor cursor = coll.find(clause); while (cursor.hasNext()) { DBObject dbo = cursor.next(); String userid = dbo.get("user_id").toString(); coll = db.getCollection("user"); clause = new BasicDBObject("user_id", userid); DBCursor userCursor = coll.find(clause); username = userCursor.next().get("name").toString(); DBObject voteList = (DBObject) dbo.get("votes"); model.addRow(new Object[] { dbo.get("date"), dbo.get("stars"), dbo.get("text"), username, voteList.get("useful") }); } client.close(); panel.add(scrollPane); frame.add(panel); frame.pack(); frame.setVisible(true); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBox activeCheckBox; private javax.swing.JCheckBox artsCheckBox; private javax.swing.JPanel attributesPanel; private javax.swing.JScrollPane attributesScrollBar; private javax.swing.JCheckBox autoCheckBox; private javax.swing.JCheckBox beautyCheckBox; private javax.swing.JScrollPane businessesScrollBar; private javax.swing.JTable businessesTable; private javax.swing.JCheckBox cafesCheckBox; private javax.swing.JCheckBox carCheckBox; private javax.swing.JButton closeButton; private javax.swing.JCheckBox convenienceCheckBox; private javax.swing.JCheckBox dentistsCheckBox; private javax.swing.JCheckBox departmentCheckBox; private javax.swing.JCheckBox doctorsCheckBox; private javax.swing.JCheckBox drugstoresCheckBox; private javax.swing.JCheckBox eduCheckBox; private javax.swing.JCheckBox eventCheckBox; private javax.swing.JCheckBox flowersCheckBox; private javax.swing.JCheckBox foodCheckBox; private javax.swing.JCheckBox gardenCheckBox; private javax.swing.JCheckBox groceryCheckBox; private javax.swing.JCheckBox hardwareCheckBox; private javax.swing.JCheckBox healthCheckBox; private javax.swing.JCheckBox homeCheckBox; private javax.swing.JCheckBox hospitalsCheckBox; private javax.swing.JCheckBox hotelsCheckBox; private javax.swing.JPanel mainCategoriesPanel; private javax.swing.JScrollPane mainCategoriesScroll; private javax.swing.JPanel mainPanel; private javax.swing.JCheckBox medicalCheckBox; private javax.swing.JCheckBox nightlifeCheckBox; private javax.swing.JCheckBox nurseriesCheckBox; private javax.swing.JComboBox poiSelection; private javax.swing.JLabel poiText; private javax.swing.JComboBox proximitySelection; private javax.swing.JLabel proximityText; private javax.swing.JCheckBox restaurantsCheckBox; private javax.swing.JButton searchButton; private javax.swing.JComboBox searchSelection; private javax.swing.JLabel searchText; private javax.swing.JCheckBox shoppingCheckBox; private javax.swing.JCheckBox transportCheckBox; // End of variables declaration//GEN-END:variables private void createAttributeCheckBox(ArrayList<String> attList) { attributesPanel.setLayout(new GridLayout(attList.size(), 1)); for (String entry : attList) { JCheckBox jcb = new JCheckBox(entry); attributesPanel.add(jcb); attributesScrollBar.updateUI(); } } private void queryBusinesses(double[] poi, int proximity, ArrayList<String> maincat, ArrayList<String> att) { MongoClient client = new MongoClient(); DB db = client.getDB("db"); DBCollection coll = db.getCollection("business"); String[] arr = { "$longitude", "$latitude" }; List<DBObject> aggregate = new ArrayList<>(); DBObject project = BasicDBObjectBuilder.start().push("$project").add("loc", arr) .add("business_id", "$business_id").add("name", "$name").add("city", "$city").add("state", "$state") .add("stars", "$stars").add("attributes", "$attributes").add("categories", "$categories").get(); DBObject geoMatch = getGeoQuery(poi, proximity, 3963.2); DBObject mainMatch = mainCategoriesQuery(maincat, 1); DBObject attMatch = attributesQuery(att); aggregate.add(project); aggregate.add(geoMatch); aggregate.add(mainMatch); aggregate.add(attMatch); AggregationOutput aggrcurs = coll.aggregate(aggregate); Iterable<DBObject> res = aggrcurs.results(); Iterator iter = res.iterator(); DefaultTableModel table = (DefaultTableModel) businessesTable.getModel(); while (iter.hasNext()) { DBObject dbo = (DBObject) iter.next(); table.addRow( new Object[] { dbo.get("name"), dbo.get("city"), dbo.get("state"), (double) dbo.get("stars") }); } client.close(); } private DBObject mainCategoriesQuery(ArrayList<String> mainList, int queryFlag) { BasicDBList mainQueryList = new BasicDBList(); DBObject mainQuery = null; for (String entry : mainList) { DBObject clause = new BasicDBObject("categories", entry); mainQueryList.add(clause); } if (queryFlag == 0) { mainQuery = new BasicDBObject("$and", mainQueryList); } else { mainQuery = new BasicDBObject("$match", new BasicDBObject("$and", mainQueryList)); } return mainQuery; } private DBObject attributesQuery(ArrayList<String> att) { BasicDBList attList = new BasicDBList(); DBObject attQuery; for (String entry : att) { String[] attribute = entry.split(":"); DBObject clause = attributeTypeFormat(attribute); attList.add(clause); } if (searchSelection.getSelectedIndex() == 0) { attQuery = new BasicDBObject("$and", attList); } else { attQuery = new BasicDBObject("$or", attList); } return new BasicDBObject("$match", attQuery); } private DBObject attributeTypeFormat(String[] attribute) { DBObject clause = null; if (attribute.length == 3) { if (attribute[2].compareTo("false") == 0) { clause = new BasicDBObject("attributes." + attribute[0] + "." + attribute[1], false); } else if (attribute[2].compareTo("true") == 0) { clause = new BasicDBObject("attributes." + attribute[0] + "." + attribute[1], true); } else if (isInteger(attribute[2])) { clause = new BasicDBObject("attributes." + attribute[0] + "." + attribute[1], Integer.parseInt(attribute[2])); } else { clause = new BasicDBObject("attributes." + attribute[0] + "." + attribute[1], attribute[2]); } } else { if (attribute[1].compareTo("false") == 0) { clause = new BasicDBObject("attributes." + attribute[0], false); } else if (attribute[1].compareTo("true") == 0) { clause = new BasicDBObject("attributes." + attribute[0], true); } else if (isInteger(attribute[1])) { clause = new BasicDBObject("attributes." + attribute[0], Integer.parseInt(attribute[1])); } else { clause = new BasicDBObject("attributes." + attribute[0], attribute[1]); } } return clause; } private boolean isInteger(String s) { Scanner sc = new Scanner(s); if (!sc.hasNextInt()) { return false; } sc.nextInt(); return !sc.hasNext(); } private int getProximity() { String distance = (String) proximitySelection.getSelectedItem(); String[] distArr = distance.split(" "); return Integer.parseInt(distArr[0]); } private double[] getPointOfInterest() { int index = poiSelection.getSelectedIndex(); double[] lnglat = new double[2]; if (index == 0) { lnglat[0] = -89.326097500000003; lnglat[1] = 43.0605902; } else if (index == 1) { lnglat[0] = -112.0734183; lnglat[1] = 33.479541599999997; } else if (index == 2) { lnglat[0] = -111.896652; lnglat[1] = 33.637824000000002; } else if (index == 3) { lnglat[0] = -115.0794033; lnglat[1] = 36.2414968; } else { lnglat[0] = -89.219459000000001; lnglat[1] = 43.192062999999997; } return lnglat; } private DBObject getGeoQuery(double[] lnglat, int proximity, double radius) { BasicDBList coords = new BasicDBList(); coords.add(lnglat[0]); coords.add(lnglat[1]); BasicDBList params = new BasicDBList(); params.add(coords); params.add(proximity / radius); DBObject match = BasicDBObjectBuilder.start().push("$match") .add("loc", new BasicDBObject("$geoWithin", new BasicDBObject("$center", params))).get(); return match; } private String getBusinessID(String name, String city, String state, double rating) { MongoClient client = new MongoClient(); DB db = client.getDB("db"); DBCollection coll = db.getCollection("business"); BasicDBList businessID = new BasicDBList(); DBObject bclause = new BasicDBObject("name", name); DBObject bclause2 = new BasicDBObject("city", city); DBObject bclause3 = new BasicDBObject("state", state); DBObject bclause4 = new BasicDBObject("stars", rating); businessID.add(bclause); businessID.add(bclause2); businessID.add(bclause3); businessID.add(bclause4); DBObject businessQuery = new BasicDBObject("$and", businessID); DBCursor cursor = coll.find(businessQuery); String bid = cursor.next().get("business_id").toString(); client.close(); return bid; } }