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 com.gkopevski.fuelcheck; import com.gkopevski.interfaces.IFuelEntryService; import com.gkopevski.model.FuelEntry; import com.gkopevski.printer.PrintFuelEntry; import com.gkopevski.utility.Constants; import com.gkopevski.utility.Utility; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author gkopevski */ public class FuelCheckForm extends javax.swing.JFrame { // @Autowired private IFuelEntryService fuelEntryService; /** * Creates new form FuelCheckForm */ WebDriver driver; public static FuelEntry latestFE; /** * @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(FuelCheckForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FuelCheckForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FuelCheckForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FuelCheckForm.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 FuelCheckForm().setVisible(true); } }); } public FuelCheckForm() { System.out.println("Today's password: " + Utility.generatePassword()); initComponents(); // System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe"); System.setProperty("phantomjs.binary.path", "drivers/phantomjs.exe"); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/spring-config.xml"); //Get the Service from the Bridge fuelEntryService = SpringContextBridge.services().getFuelEntryService(); List<FuelEntry> fuelEntries = fuelEntryService.getAllFuelEntries(); if (fuelEntries == null || fuelEntries.size() == 0) { System.out.println("Empty list"); } else { System.out.println("First element: " + fuelEntries.get(0).getDriver()); } latestFE = fuelEntryService.getLatestFuelEntry(); if (latestFE != null) { System.out.println("Last element: " + latestFE.getId()); } // FuelEntry fuelEntry = FuelEntryFactory.createFunnelEntry(); // fuelEntryService.saveFuelEntry(fuelEntry); } /** * 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() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); openMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem(); editMenu = new javax.swing.JMenu(); cutMenuItem = new javax.swing.JMenuItem(); copyMenuItem = new javax.swing.JMenuItem(); pasteMenuItem = new javax.swing.JMenuItem(); deleteMenuItem = new javax.swing.JMenuItem(); helpMenu = new javax.swing.JMenu(); contentsMenuItem = new javax.swing.JMenuItem(); aboutMenuItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Test"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("quit"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Print Latest"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Start Watching"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); fileMenu.setMnemonic('f'); fileMenu.setText("File"); openMenuItem.setMnemonic('o'); openMenuItem.setText("Open"); fileMenu.add(openMenuItem); saveMenuItem.setMnemonic('s'); saveMenuItem.setText("Save"); fileMenu.add(saveMenuItem); saveAsMenuItem.setMnemonic('a'); saveAsMenuItem.setText("Save As ..."); saveAsMenuItem.setDisplayedMnemonicIndex(5); fileMenu.add(saveAsMenuItem); exitMenuItem.setMnemonic('x'); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); editMenu.setMnemonic('e'); editMenu.setText("Edit"); cutMenuItem.setMnemonic('t'); cutMenuItem.setText("Cut"); editMenu.add(cutMenuItem); copyMenuItem.setMnemonic('y'); copyMenuItem.setText("Copy"); editMenu.add(copyMenuItem); pasteMenuItem.setMnemonic('p'); pasteMenuItem.setText("Paste"); editMenu.add(pasteMenuItem); deleteMenuItem.setMnemonic('d'); deleteMenuItem.setText("Delete"); editMenu.add(deleteMenuItem); menuBar.add(editMenu); helpMenu.setMnemonic('h'); helpMenu.setText("Help"); contentsMenuItem.setMnemonic('c'); contentsMenuItem.setText("Contents"); helpMenu.add(contentsMenuItem); aboutMenuItem.setMnemonic('a'); aboutMenuItem.setText("About"); helpMenu.add(aboutMenuItem); menuBar.add(helpMenu); setJMenuBar(menuBar); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(30, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton4).addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3).addComponent(jButton1)) .addContainerGap(19, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed try { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. driver = new PhantomJSDriver(); ; // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed driver.quit(); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed try { PrintFuelEntry pfe = new PrintFuelEntry(); pfe.printLatestEntry(); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jButton3ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed try { this.jButton4.setEnabled(false); ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleAtFixedRate(new Runnable() { @Override public void run() { crawlData(); } }, 0, Constants.TIMER_CRAWLING_SECONDS, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jButton4ActionPerformed /** * This method will crawl data from a web site. When data is fetched will be * compared with the latest get data from the database; If this data is * newer it will be stored in the variable latest data which will be from * type @FuelEntry. If the new data is newer will be passed for printing and * it will be printed; * */ private void crawlData() { try { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. driver = new PhantomJSDriver(); System.out.println("URL: " + Constants.BASE_URL + "/" + Constants.LOGIN_HTML); driver.get(Constants.BASE_URL + "/" + Constants.LOGIN_HTML); driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); WebElement element = driver.findElement(By.id("Field__UserLogin")); element.sendKeys(Constants.USERNAME); element = driver.findElement(By.id("Field__UserPassword")); element.sendKeys(Utility.generatePassword()); element = driver.findElement(By.xpath("(//div[@id='DivMenu']/table/tbody/tr/td)[1]")); element.click(); driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); // And now use this to visit Google // driver.get(Constants.BASE_URL + "/" + Constants.OVERVIEW_HTML); // Find the text input element by its name WebElement baseTable = driver.findElement( By.xpath("(//tr[@id='__DashBoardGroup21']/td/table[@id='__DashBoardGroup21']/tbody/tr)[3]")); List<WebElement> rawEntry = baseTable.findElements(By.tagName("td")); FuelEntry tempFuelEntry = new FuelEntry(); String[] quantityUnit = rawEntry.get(8).getText().split(" "); tempFuelEntry.setQuantity(new BigDecimal(quantityUnit[0])); tempFuelEntry.setMeasurementUnit(quantityUnit[1]); SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyyy hh:mm:ss"); Date date = dt.parse(rawEntry.get(6).getText()); if (latestFE == null || (date.after(latestFE.getStartDate()) && tempFuelEntry.getQuantity().compareTo(BigDecimal.ZERO) > 0)) { tempFuelEntry.setDriver(rawEntry.get(2).getText()); tempFuelEntry.setVehicle(rawEntry.get(3).getText()); tempFuelEntry.setProduct(rawEntry.get(4).getText()); tempFuelEntry.setDispenzer(Integer.valueOf(rawEntry.get(5).getText())); tempFuelEntry.setStartDate(date); fuelEntryService.saveFuelEntry(tempFuelEntry); latestFE = tempFuelEntry; PrintFuelEntry pfe = new PrintFuelEntry(); pfe.printLatestEntry(); } driver.quit(); } catch (Exception e) { e.printStackTrace(); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenuItem contentsMenuItem; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem cutMenuItem; private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenu editMenu; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JMenu helpMenu; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem openMenuItem; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; // End of variables declaration//GEN-END:variables }