Java tutorial
package info.sarihh.unimodeling.gui; import com.itextpdf.awt.DefaultFontMapper; import com.itextpdf.awt.FontMapper; import com.itextpdf.text.Rectangle; import java.awt.geom.Rectangle2D; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; import info.sarihh.unimodeling.utility.DatabaseConnectionFactory; import info.sarihh.unimodeling.utility.ExtensionFileFilter; import info.sarihh.unimodeling.utility.TimeDiff; import java.awt.Color; import java.awt.Graphics2D; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.StringTokenizer; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JSpinner; import javax.swing.JSpinner.DefaultEditor; import javax.swing.SpinnerDateModel; import javax.swing.SpinnerModel; import javax.swing.SwingUtilities; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * This class creates the internal frame for the RFID server. * Author: Sari Haj Hussein */ public class DynamicBPEstimateFrame extends javax.swing.JInternalFrame { /** * Creates new form RFIDServerFrame */ public DynamicBPEstimateFrame(UniModelingGUI uniModelingGUI) { this.unModeingGUI = uniModelingGUI; pdfFileFilter = new ExtensionFileFilter(new String[] { "PDF" }, "PDF files"); initComponents(); driversComboBox.setSelectedIndex(1); connectDatabaseDialog.pack(); plottingDialog.pack(); } /** * 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() { java.awt.GridBagConstraints gridBagConstraints; connectDatabaseDialog = new javax.swing.JDialog(); driversLabel = new javax.swing.JLabel(); driversComboBox = new javax.swing.JComboBox(); urlLabel = new javax.swing.JLabel(); urlTextField = new javax.swing.JTextField(); userNameLabel = new javax.swing.JLabel(); userNameTextField = new javax.swing.JTextField(); passwordLabel = new javax.swing.JLabel(); passwordTextField = new javax.swing.JPasswordField(); buttonPanel2 = new javax.swing.JPanel(); testButton = new javax.swing.JButton(); buttonFiller5 = new javax.swing.Box.Filler(new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 32767)); okButton1 = new javax.swing.JButton(); plottingDialog = new javax.swing.JDialog(); containerPanel1 = new javax.swing.JPanel(); buttonPanel1 = new javax.swing.JPanel(); saveButton = new javax.swing.JButton(); okButton = new javax.swing.JButton(); fileChooser = new javax.swing.JFileChooser(); containerScrollPane = new javax.swing.JScrollPane(); containerPanel = new javax.swing.JPanel(); startDateLabel = new javax.swing.JLabel(); SpinnerModel startDateModel = new SpinnerDateModel(); startDateSpinner = new javax.swing.JSpinner(startDateModel); startTimeLabel = new javax.swing.JLabel(); SpinnerDateModel startTimeModel = new SpinnerDateModel(); startTimeModel.setCalendarField(Calendar.MINUTE); startTimeSpinner = new javax.swing.JSpinner(startTimeModel); monitoringPeriodLabel = new javax.swing.JLabel(); monitoringPeriodTextField = new javax.swing.JFormattedTextField(); monitoringPeriodUnitLabel = new javax.swing.JLabel(); monitoringPeriodUnitComboBox = new javax.swing.JComboBox(); probabilityTweakingLabel = new javax.swing.JLabel(); probabilityTweakingTextField = new javax.swing.JFormattedTextField(); activityLogScrollPane = new javax.swing.JScrollPane(); activityLogTextArea = new javax.swing.JTextArea(); buttonPanel = new javax.swing.JPanel(); connectDatabaseButton = new javax.swing.JButton(); estimateButton = new javax.swing.JButton(); clearLogButton = new javax.swing.JButton(); connectDatabaseDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); java.util.ResourceBundle bundle = java.util.ResourceBundle .getBundle("info/sarihh/unimodeling/gui/DynamicBPEstimate"); // NOI18N connectDatabaseDialog.setTitle(bundle.getString("DynamicBPEstimateFrame.connectDatabaseDialog.title_1_1")); // NOI18N connectDatabaseDialog.setModal(true); connectDatabaseDialog.setResizable(false); connectDatabaseDialog.getContentPane().setLayout(new java.awt.GridBagLayout()); driversLabel.setText(bundle.getString("DynamicBPEstimateFrame.driversLabel.text_1_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(driversLabel, gridBagConstraints); driversComboBox.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "MySQL Driver", "Oracle Thin Driver", "PostgreSQL Driver" })); driversComboBox.setPreferredSize(new java.awt.Dimension(360, 22)); driversComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { driversComboBoxActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(driversComboBox, gridBagConstraints); urlLabel.setText(bundle.getString("DynamicBPEstimateFrame.urlLabel.text_1_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(urlLabel, gridBagConstraints); urlTextField.setText("jdbc:mysql://<hostname>[,<failoverhost>][<:3306>]/<dbname>"); // NOI18N urlTextField.setPreferredSize(new java.awt.Dimension(360, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(urlTextField, gridBagConstraints); userNameLabel.setText(bundle.getString("DynamicBPEstimateFrame.userNameLabel.text_1_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(userNameLabel, gridBagConstraints); userNameTextField.setPreferredSize(new java.awt.Dimension(360, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(userNameTextField, gridBagConstraints); passwordLabel.setText(bundle.getString("DynamicBPEstimateFrame.passwordLabel.text_1_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(passwordLabel, gridBagConstraints); passwordTextField.setPreferredSize(new java.awt.Dimension(360, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(passwordTextField, gridBagConstraints); testButton.setText(bundle.getString("DynamicBPEstimateFrame.testButton.text_1_1")); // NOI18N testButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { testButtonActionPerformed(evt); } }); buttonPanel2.add(testButton); buttonPanel2.add(buttonFiller5); okButton1.setText(bundle.getString("DynamicBPEstimateFrame.okButton1.text_1_1")); // NOI18N okButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButton1ActionPerformed(evt); } }); buttonPanel2.add(okButton1); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); connectDatabaseDialog.getContentPane().add(buttonPanel2, gridBagConstraints); plottingDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); plottingDialog.setTitle(bundle.getString("DynamicBPEstimateFrame.plottingDialog.title")); // NOI18N plottingDialog.setMinimumSize(new java.awt.Dimension(450, 350)); plottingDialog.setModal(true); plottingDialog.setResizable(false); containerPanel1.setLayout(new java.awt.BorderLayout()); plottingDialog.getContentPane().add(containerPanel1, java.awt.BorderLayout.CENTER); saveButton.setText(bundle.getString("DynamicBPEstimateFrame.saveButton.text")); // NOI18N saveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveButtonActionPerformed(evt); } }); buttonPanel1.add(saveButton); okButton.setText(bundle.getString("DynamicBPEstimateFrame.okButton.text")); // NOI18N okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); buttonPanel1.add(okButton); plottingDialog.getContentPane().add(buttonPanel1, java.awt.BorderLayout.SOUTH); fileChooser.setCurrentDirectory(new java.io.File(".")); fileChooser.setDialogTitle(bundle.getString("DynamicBPEstimateFrame.fileChooser.dialogTitle")); // NOI18N fileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG); fileChooser.setFileFilter(pdfFileFilter); setClosable(true); setIconifiable(true); setMaximizable(true); setResizable(true); setTitle(bundle.getString("DynamicBPEstimateFrame.title_1_1")); // NOI18N containerPanel.setLayout(new java.awt.GridBagLayout()); startDateLabel.setText(bundle.getString("DynamicBPEstimateFrame.startDateLabel.text_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(startDateLabel, gridBagConstraints); DefaultEditor startDateEditor = new JSpinner.DateEditor(startDateSpinner, "yyyy-MM-dd"); //startDateEditor.getTextField().setEditable(false); startDateSpinner.setEditor(startDateEditor); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(startDateSpinner, gridBagConstraints); startTimeLabel.setText(bundle.getString("DynamicBPEstimateFrame.startTimeLabel.text")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(startTimeLabel, gridBagConstraints); DefaultEditor startTimeEditor = new JSpinner.DateEditor(startTimeSpinner, "HH:mm:ss"); //startTimeEditor.getTextField().setEditable(false); startTimeSpinner.setEditor(startTimeEditor); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(startTimeSpinner, gridBagConstraints); monitoringPeriodLabel.setText(bundle.getString("DynamicBPEstimateFrame.monitoringPeriodLabel.text")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(monitoringPeriodLabel, gridBagConstraints); monitoringPeriodTextField.setText("30"); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(monitoringPeriodTextField, gridBagConstraints); monitoringPeriodUnitLabel .setText(bundle.getString("DynamicBPEstimateFrame.monitoringPeriodUnitLabel.text_1")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(monitoringPeriodUnitLabel, gridBagConstraints); monitoringPeriodUnitComboBox.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "Day", "Hour", "Minute", "Second", "Millisecond" })); monitoringPeriodUnitComboBox.setSelectedIndex(2); monitoringPeriodUnitComboBox.setPreferredSize(new java.awt.Dimension(360, 22)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(monitoringPeriodUnitComboBox, gridBagConstraints); probabilityTweakingLabel.setText(bundle.getString("DynamicBPEstimateFrame.probabilityTweakingLabel.text")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(probabilityTweakingLabel, gridBagConstraints); probabilityTweakingTextField.setText("0.1"); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(probabilityTweakingTextField, gridBagConstraints); activityLogScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder( bundle.getString("DynamicBPEstimateFrame.activityLogScrollPane.border.title_1_1"))); // NOI18N activityLogScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); activityLogTextArea.setColumns(40); activityLogTextArea.setEditable(false); activityLogTextArea.setLineWrap(true); activityLogTextArea.setRows(12); activityLogTextArea.setWrapStyleWord(true); activityLogScrollPane.setViewportView(activityLogTextArea); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 4; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; containerPanel.add(activityLogScrollPane, gridBagConstraints); connectDatabaseButton.setText(bundle.getString("DynamicBPEstimateFrame.connectDatabaseButton.text_1_1")); // NOI18N connectDatabaseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connectDatabaseButtonActionPerformed(evt); } }); buttonPanel.add(connectDatabaseButton); estimateButton.setText(bundle.getString("DynamicBPEstimateFrame.estimateButton.text_1_1")); // NOI18N estimateButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { estimateButtonActionPerformed(evt); } }); buttonPanel.add(estimateButton); clearLogButton.setText(bundle.getString("DynamicBPEstimateFrame.clearLogButton.text_1_1")); // NOI18N clearLogButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearLogButtonActionPerformed(evt); } }); buttonPanel.add(clearLogButton); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = 4; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); containerPanel.add(buttonPanel, gridBagConstraints); containerScrollPane.setViewportView(containerPanel); getContentPane().add(containerScrollPane, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold>//GEN-END:initComponents private void clearLogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearLogButtonActionPerformed activityLogTextArea.setText(""); }//GEN-LAST:event_clearLogButtonActionPerformed private void connectDatabaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectDatabaseButtonActionPerformed SwingUtilities.invokeLater(new Runnable() { public void run() { connectDatabaseDialog.setLocationRelativeTo(DynamicBPEstimateFrame.this); connectDatabaseDialog.setVisible(true); } }); }//GEN-LAST:event_connectDatabaseButtonActionPerformed private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed conn = DatabaseConnectionFactory.getDatabaseConnection(driversComboBox.getSelectedItem().toString(), urlTextField.getText(), userNameTextField.getText(), new String(passwordTextField.getPassword())); if (conn != null) { JOptionPane.showMessageDialog(DynamicBPEstimateFrame.this, "Connection succeeded!", "UniModeling", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(DynamicBPEstimateFrame.this, "Connection failed!", "UniModeling", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_testButtonActionPerformed private void okButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButton1ActionPerformed conn = DatabaseConnectionFactory.getDatabaseConnection(driversComboBox.getSelectedItem().toString(), urlTextField.getText(), userNameTextField.getText(), new String(passwordTextField.getPassword())); if (conn != null) { appendLogText("A connection made with the database: " + urlTextField.getText() + " using driver: " + driversComboBox.getSelectedItem() + "."); } else { appendLogText("A connection failed with the database: " + urlTextField.getText() + " using driver: " + driversComboBox.getSelectedItem() + "."); } connectDatabaseDialog.setVisible(false); }//GEN-LAST:event_okButton1ActionPerformed private void estimateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_estimateButtonActionPerformed String startDate = new SimpleDateFormat("yyyy-MM-dd").format(startDateSpinner.getValue()); String startTime = new SimpleDateFormat("HH:mm:ss").format(startTimeSpinner.getValue()); String startDateTime = startDate + " " + startTime; String monitoringPerdiod = monitoringPeriodTextField.getText() + " " + monitoringPeriodUnitComboBox.getSelectedItem().toString(); double eta = Double.parseDouble(probabilityTweakingTextField.getText()); HashMap<String, Double> bpMap = dynamicBPEstimate(startDateTime, monitoringPerdiod, eta); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(bpMap.get("CD"), "Dynamic BP Estimate", "CD"); dataset.setValue(bpMap.get("CC"), "Dynamic BP Estimate", "CC"); dataset.setValue(bpMap.get("MC"), "Dynamic BP Estimate", "MC"); dataset.setValue(bpMap.get("SMC"), "Dynamic BP Estimate", "SMC"); dataset.setValue(bpMap.get("TTS"), "Dynamic BP Estimate", "TTS"); dataset.setValue(bpMap.get("CH"), "Dynamic BP Estimate", "CH"); dataset.setValue(bpMap.get("RB"), "Dynamic BP Estimate", "RB"); dataset.setValue(bpMap.get("OB"), "Dynamic BP Estimate", "OB"); dataset.setValue(bpMap.get("OC"), "Dynamic BP Estimate", "OC"); dataset.setValue(bpMap.get("CGS"), "Dynamic BP Estimate", "CGS"); String title = monitoringPerdiod + " starting " + startDateTime; chart = ChartFactory.createBarChart(title, "Semantic Location", "Dynamic BP Estimate", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot plot = chart.getCategoryPlot(); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.black); ChartPanel panel = new ChartPanel(chart, false); plottingDialog.setLocationRelativeTo(DynamicBPEstimateFrame.this); containerPanel1.removeAll(); containerPanel1.add(panel); containerPanel1.repaint(); plottingDialog.setVisible(true); }//GEN-LAST:event_estimateButtonActionPerformed private void driversComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_driversComboBoxActionPerformed switch (driversComboBox.getSelectedIndex()) { case 0: urlTextField.setText("jdbc:mysql://<hostname>[,<failoverhost>][<:3306>]/<dbname>"); userNameTextField.setText(""); passwordTextField.setText(""); break; case 1: urlTextField.setText("jdbc:oracle:thin:@<server>[:<1521>]:<database_name>"); break; case 2: urlTextField.setText("jdbc:postgresql:[<//host>[:<5432>/]]<database>"); userNameTextField.setText(""); passwordTextField.setText(""); break; default: break; } }//GEN-LAST:event_driversComboBoxActionPerformed private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed plottingDialog.setVisible(false); }//GEN-LAST:event_okButtonActionPerformed private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed int returnValue = fileChooser.showSaveDialog(this); if (returnValue == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { saveChartAsPDF(file, chart, 450, 350, new DefaultFontMapper()); } catch (IOException ioe) { ioe.printStackTrace(); } } }//GEN-LAST:event_saveButtonActionPerformed public static void saveChartAsPDF(File file, JFreeChart chart, int width, int height, FontMapper mapper) throws IOException { OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); writeChartAsPDF(out, chart, width, height, mapper); out.close(); } public static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper) throws IOException { Rectangle pagesize = new Rectangle(width, height); Document document = new Document(pagesize, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("JFreeChart"); document.addSubject("Demonstration"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, mapper); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D, null); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { System.err.println(de.getMessage()); } document.close(); } public void appendLogText(final String text) { SwingUtilities.invokeLater(new Runnable() { public void run() { if (!text.equals("")) { activityLogTextArea.append(dateFormat.format(new Date()) + " -> " + text + "\n"); activityLogTextArea.setCaretPosition(activityLogTextArea.getText().length()); } } }); } public Connection getConnection() { return conn; } private HashMap<String, Double> normalize(HashMap<String, Double> bpMap) { double sum = 0.0; HashMap<String, Double> bpMap2 = new HashMap<>(); for (String location : bpMap.keySet()) { sum += bpMap.get(location).doubleValue(); } for (String location : bpMap.keySet()) { bpMap2.put(location, new Double(bpMap.get(location).doubleValue() / sum)); } return bpMap2; } public static double getProbability(String semanticLocation, String probSemanticLocations) { StringTokenizer st = new StringTokenizer(probSemanticLocations, ","); while (st.hasMoreTokens()) { String token = st.nextToken(); if (token.contains(semanticLocation)) { return Double.valueOf(token.substring(token.indexOf(":") + 1)); } } return 0.0; } private int rowCount(ResultSet resultSet, String loc) { try { int counter = 0; while (resultSet.next()) { if (resultSet.getString("INFER_LOC").contains(loc)) { counter++; } } resultSet.beforeFirst(); return counter; } catch (SQLException e) { for (Throwable t : e) { t.printStackTrace(); } return 0; } } private HashMap<String, Double> dynamicBPEstimate(String startDateTime, String monitoringPerior, double eta) { // Set the start time of monitoring in the calendar Timestamp dayTS = Timestamp.valueOf(startDateTime); Calendar calendar_s = Calendar.getInstance(); calendar_s.setTimeInMillis(dayTS.getTime()); // Set the end time of monitoring in the calendar Calendar calendar_e = (Calendar) calendar_s.clone(); StringTokenizer st = new StringTokenizer(monitoringPerior); if (monitoringPerior.contains("Day")) { calendar_e.set(Calendar.DAY_OF_MONTH, calendar_e.get(Calendar.DAY_OF_MONTH) + Integer.valueOf(st.nextToken())); } else if (monitoringPerior.contains("Hour")) { calendar_e.set(Calendar.HOUR_OF_DAY, calendar_e.get(Calendar.HOUR_OF_DAY) + Integer.valueOf(st.nextToken())); } else if (monitoringPerior.contains("Minute")) { calendar_e.set(Calendar.MINUTE, calendar_e.get(Calendar.MINUTE) + Integer.valueOf(st.nextToken())); } else if (monitoringPerior.contains("Second")) { calendar_e.set(Calendar.SECOND, calendar_e.get(Calendar.SECOND) + Integer.valueOf(st.nextToken())); } else if (monitoringPerior.contains("Millisecond")) { calendar_e.set(Calendar.MILLISECOND, calendar_e.get(Calendar.MILLISECOND) + Integer.valueOf(st.nextToken())); } // Determine the increase and decrease using the probability tweaking parameter double increase = 1.0 + (eta / 100.0); double decrease = 1.0 - (eta / 100.0); String[] W_l = { "CD", "CC", "MC", "SMC", "TTS", "CH", "RB", "OB", "OC", "CGS" }; // Reduce the set to the scope of the readings HashMap<String, Double> bpMap = new HashMap<>(); try { // Retrieve I-REC([s_time,e_time]) from the INFER_TABLE ResultSet resultSet = null; String query = "SELECT * FROM INFER_TABLE WHERE S_TIME >= ? AND E_TIME <= ? ORDER BY LICENSE_PLATE, S_TIME, E_TIME"; PreparedStatement statement = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String s_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar_s.getTime()); String e_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar_e.getTime()); statement.setTimestamp(1, Timestamp.valueOf(s_time)); statement.setTimestamp(2, Timestamp.valueOf(e_time)); resultSet = statement.executeQuery(); for (String loc : W_l) { double bpE = rowCount(resultSet, loc); while (resultSet.next()) { // Determine the detection time with tau granularity Timestamp sTime = resultSet.getTimestamp("S_TIME"); Timestamp eTime = resultSet.getTimestamp("E_TIME"); long[] diffArray = TimeDiff.getTimeDifference(eTime, sTime); long diffInMilliseconds = 86400000 * diffArray[0] + 3600000 * diffArray[1] + 60000 * diffArray[2] + 1000 * diffArray[3] + diffArray[4]; double diffInSeconds = diffInMilliseconds * 0.001; double diffInMinutes = diffInSeconds / 60.0; double prob = getProbability(loc, resultSet.getString("INFER_LOC")); // Tweak bpE accordingly if (prob > 0) { bpE *= prob; for (long i = 0; i < diffInMinutes; i++) { bpE *= increase; } } else { for (long i = 0; i < diffInMinutes; i++) { bpE *= decrease; } } } // Rewind the result set for the next interation resultSet.beforeFirst(); bpMap.put(loc, new Double(bpE)); } HashMap<String, Double> bpMap2 = normalize(bpMap); resultSet.beforeFirst(); resultSet.last(); int recordCount = resultSet.getRow(); appendLogText(recordCount + " inferred records were processed."); resultSet.close(); statement.close(); return bpMap2; } catch (SQLException e) { for (Throwable t : e) { t.printStackTrace(); } return null; } } private UniModelingGUI unModeingGUI = null; private static Connection conn = null; private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); private JFreeChart chart = null; private ExtensionFileFilter pdfFileFilter; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JScrollPane activityLogScrollPane; private javax.swing.JTextArea activityLogTextArea; private javax.swing.JComboBox algorithmComboBox; private javax.swing.Box.Filler buttonFiller5; private javax.swing.JPanel buttonPanel; private javax.swing.JPanel buttonPanel1; private javax.swing.JPanel buttonPanel2; private javax.swing.JButton clearLogButton; private javax.swing.JButton connectDatabaseButton; private javax.swing.JDialog connectDatabaseDialog; private javax.swing.JPanel containerPanel; private javax.swing.JPanel containerPanel1; private javax.swing.JScrollPane containerScrollPane; private javax.swing.JComboBox driversComboBox; private javax.swing.JLabel driversLabel; private javax.swing.JButton estimateButton; private javax.swing.JFileChooser fileChooser; private javax.swing.JLabel monitoringPeriodLabel; private javax.swing.JFormattedTextField monitoringPeriodTextField; private javax.swing.JComboBox monitoringPeriodUnitComboBox; private javax.swing.JLabel monitoringPeriodUnitLabel; private javax.swing.JButton okButton; private javax.swing.JButton okButton1; private javax.swing.JLabel passwordLabel; private javax.swing.JPasswordField passwordTextField; private javax.swing.JDialog plottingDialog; private javax.swing.JLabel probabilityTweakingLabel; private javax.swing.JFormattedTextField probabilityTweakingTextField; private javax.swing.JButton saveButton; private javax.swing.JLabel startDateLabel; private javax.swing.JSpinner startDateSpinner; private javax.swing.JLabel startTimeLabel; private javax.swing.JSpinner startTimeSpinner; private javax.swing.JButton testButton; private javax.swing.JLabel urlLabel; private javax.swing.JTextField urlTextField; private javax.swing.JLabel userNameLabel; private javax.swing.JTextField userNameTextField; // End of variables declaration//GEN-END:variables }