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 ueg.watchdog.view; import com.toedter.calendar.JTextFieldDateEditor; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.view.JasperViewer; import org.apache.commons.dbutils.DbUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ueg.watchdog.Constants; import ueg.watchdog.database.DbConnect; import ueg.watchdog.util.Validator; import javax.swing.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @author erandi Extends WatchDogBaseFrame class Class to set report properties * to generate detailed reports */ public class Report extends WatchDogBaseFrame { private static final Logger logger = LoggerFactory.getLogger(Report.class); public Report(WatchDogBaseFrame parentFrame) { super(parentFrame); initComponents(); super.setCloseOperation(); JTextFieldDateEditor editor1 = (JTextFieldDateEditor) jDateChooserFrom.getDateEditor(); editor1.setEditable(false); JTextFieldDateEditor editor2 = (JTextFieldDateEditor) jDateChooserTo.getDateEditor(); editor2.setEditable(false); } /** * 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() { 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(); jButtonPrint = new javax.swing.JButton(); birthdayValidatorLabel = new javax.swing.JLabel(); jDateChooserFrom = new com.toedter.calendar.JDateChooser(); jDateChooserTo = new com.toedter.calendar.JDateChooser(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); jLabel1.setFont(new java.awt.Font("DejaVu Serif", 1, 18)); // NOI18N jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("WatchDog - Report"); jLabel2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N jLabel2.setText("Report Generation Period"); jLabel3.setText("Date (From) : "); jLabel4.setText("Date (To) : "); jLabel5.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N jButtonPrint.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N jButtonPrint.setText("Print Report"); jButtonPrint.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonPrintActionPerformed(evt); } }); jDateChooserFrom.setDateFormatString("yyyy-MM-dd"); jDateChooserTo.setDateFormatString("yyyy-MM-dd"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(104, 104, 104).addComponent( jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(56, 56, 56).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addGroup(layout.createSequentialGroup().addGap(124, 124, 124) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE) .addComponent(jButtonPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addGap(52, 52, 52)) .addGroup(layout.createSequentialGroup().addGap(92, 92, 92) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jDateChooserFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jDateChooserTo, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(26, 26, 26) .addComponent(birthdayValidatorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(35, 35, 35))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jLabel1).addGap(18, 18, 18) .addComponent(jLabel2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jDateChooserFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jDateChooserTo, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32).addComponent(birthdayValidatorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonPrint, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE) .addGap(54, 54, 54))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPrintActionPerformed try { String dateFrom = ((JTextField) jDateChooserFrom.getDateEditor().getUiComponent()).getText(); String dateTo = ((JTextField) jDateChooserTo.getDateEditor().getUiComponent()).getText(); if (dateFrom.equals("") || dateTo.equals("")) { JOptionPane.showMessageDialog(null, "Please fill all the input fields!", "Failed!", 0); } else if (!Validator.validateInputs(dateFrom, 0)) { JOptionPane.showMessageDialog(null, "Date 'From' is not matching the required format!", "Failed!", 0); } else if (Validator.validateInputs(dateFrom, 1)) { JOptionPane.showMessageDialog(null, "Date 'From' can not be a future date!", "Failed!", 0); } else if (!Validator.validateInputs(dateTo, 0)) { JOptionPane.showMessageDialog(null, "Date 'To' is not matching the required format!", "Failed!", 0); } else if (Validator.validateInputs(dateTo, 1)) { JOptionPane.showMessageDialog(null, "Date 'To' can not be a future date!", "Failed!", 0); } else { Connection conn = DbConnect.getDBConnection(); String query = "SELECT video_stat.occurred_timestamp, video_stat.description, IFNULL(person_profile.first_name,'Unauthorized') AS first_name, " + "IFNULL(person_profile.last_name,'Unauthorized') AS last_name, video_stat.face FROM `video_stat` LEFT JOIN `person_profile` ON " + "video_stat.profile_id = person_profile.id LEFT JOIN video ON " + "video_stat.video_id = video.id WHERE video.start_time > ? AND video.end_time < ?"; try { PreparedStatement statement = conn.prepareStatement(query); statement.setString(1, dateFrom + " 00:00:00"); statement.setString(2, dateTo + " 23:59:59"); ResultSet resultSet = statement.executeQuery(); JasperReport jr = JasperCompileManager.compileReport( this.getClass().getClassLoader().getResourceAsStream(Constants.MAIN_REPORT_PATH)); JRDataSource dataSource = new JRResultSetDataSource(resultSet); JasperPrint jp = JasperFillManager.fillReport(jr, null, dataSource); JasperViewer.viewReport(jp, false); } catch (Exception ex) { logger.error("Error occurred when generating report", ex); } finally { DbUtils.closeQuietly(conn); } this.exitToParent(false); } } catch (Exception ex) { System.out.println(ex); } }//GEN-LAST:event_jButtonPrintActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel birthdayValidatorLabel; private javax.swing.JButton jButtonPrint; private com.toedter.calendar.JDateChooser jDateChooserFrom; private com.toedter.calendar.JDateChooser jDateChooserTo; 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; // End of variables declaration//GEN-END:variables }