colectordedatos.Generador.java Source code

Java tutorial

Introduction

Here is the source code for colectordedatos.Generador.java

Source

/*
 * 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 colectordedatos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

/**
 *
 * @author jc
 */
public class Generador extends javax.swing.JFrame {

    private JSONArray arrJSON;
    JSONObject seriesArray;
    private JSONObject dashboard;

    /**
     * Creates new form Ventana
     */
    public Generador() {
        initComponents();
        setLocationRelativeTo(null);
        setResizable(false);
        setTitle("Regenerador de Dashboards");
    }

    /**
     * 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() {

        archivo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        paneles = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        series = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldEntityName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMetricPath = new javax.swing.JTextArea();
        Anadir = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        Nombre_nuevo = new javax.swing.JTextField();
        Enti_nueva = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        Serie_nueva = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        archivo.setText("C:\\\\Users\\\\Administrador\\\\Documents\\\\NetBeansProjects\\\\GADD\\\\test1.json");

        jButton1.setText("Cargar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        paneles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                panelesActionPerformed(evt);
            }
        });

        jLabel1.setText("Paneles :");

        jLabel2.setText("Series :");

        series.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seriesActionPerformed(evt);
            }
        });

        jLabel4.setText("MetricPath :");

        jLabel5.setText("EntityName :");

        jTextAreaMetricPath.setColumns(20);
        jTextAreaMetricPath.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMetricPath);

        Anadir.setText("Aadir");
        Anadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnadirActionPerformed(evt);
            }
        });

        jLabel6.setText("MetricPath :");

        jLabel7.setText("Entityname :");

        jLabel8.setText("Serie :");

        jButton2.setText("Atras");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel9.setText("Ruta del archivo :");

        jLabel10.setText("Aadir nueva metrica");

        jButton3.setText("Cerrar sesin");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel1).addComponent(jLabel2))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(paneles, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE)
                                                .addComponent(series, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE)))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jLabel5).addComponent(jLabel8)))
                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(Enti_nueva).addComponent(jTextFieldEntityName)
                                                .addComponent(jScrollPane1).addComponent(Serie_nueva)
                                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup().addGap(29, 29, 29)
                                                                .addComponent(Anadir).addGap(18, 18, 18)
                                                                .addComponent(jButton2,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18).addComponent(jButton3))
                                                        .addComponent(Nombre_nuevo,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 467,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGap(0, 0, Short.MAX_VALUE))))))
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(archivo, javax.swing.GroupLayout.PREFERRED_SIZE, 469,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1)
                        .addContainerGap())
                .addGroup(layout.createSequentialGroup().addGap(232, 232, 232).addComponent(jLabel10)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(22, 22, 22).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(archivo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9).addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(paneles, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 12,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(series, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(6, 6, 6).addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jLabel4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextFieldEntityName, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel5))
                        .addGap(18, 18, 18).addComponent(jLabel10).addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Serie_nueva, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Nombre_nuevo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Enti_nueva, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3).addComponent(jButton2).addComponent(Anadir))
                        .addContainerGap(21, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            // TODO add your handling code here:
            File snapshotsFile = new File(archivo.getText());
            FileInputStream fis = new FileInputStream(snapshotsFile);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader bf = new BufferedReader(isr);
            String linea = bf.readLine();
            StringBuffer sb = new StringBuffer();
            while (linea != null) {
                sb.append(linea.trim());
                linea = bf.readLine();
            }
            bf.close();
            isr.close();
            fis.close();

            dashboard = new JSONObject(sb.toString());
            arrJSON = dashboard.getJSONArray("widgetTemplates");
            paneles.removeAllItems();
            for (int i = 0; i < arrJSON.length(); i++) {
                JSONObject jsonObject = arrJSON.getJSONObject(i);
                paneles.addItem(jsonObject.getString("title"));
            }

        } catch (Exception ex) {
            Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void panelesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_panelesActionPerformed
        // TODO add your handling code here:
        try {
            series.removeAllItems();
            String panel = (String) paneles.getSelectedItem();
            JSONArray seriesArray = getSeries(panel, null);
            for (int j = 0; j < seriesArray.length(); j++) {
                JSONObject jsonObjectSerie = seriesArray.getJSONObject(j);
                series.addItem(jsonObjectSerie.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_panelesActionPerformed

    private void seriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seriesActionPerformed
        // TODO add your handling code here:
        try {
            String panel = (String) paneles.getSelectedItem();
            String serie = (String) series.getSelectedItem();

            JSONArray seriesArray = getSeries(panel, serie);
            JSONObject jsonSerie = seriesArray.getJSONObject(0);
            this.seriesArray = jsonSerie;
            JSONObject metricMatchCriteriaTemplate = jsonSerie.getJSONObject("metricMatchCriteriaTemplate");
            JSONObject metricExpressionTemplate = metricMatchCriteriaTemplate
                    .getJSONObject("metricExpressionTemplate");
            jTextAreaMetricPath.setText(metricExpressionTemplate.getString("metricPath"));
            JSONObject scopeEntity = metricExpressionTemplate.getJSONObject("scopeEntity");
            jTextFieldEntityName.setText(scopeEntity.getString("entityName"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_seriesActionPerformed

    private void AnadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnadirActionPerformed

        String serie_nue = Serie_nueva.getText();
        String nom_nuevo = Nombre_nuevo.getText();
        String entity_nuevo = Enti_nueva.getText();
        JSONObject seriesArray1 = new JSONObject();
        seriesArray1 = this.seriesArray;

        JSONObject metricMatchCriteriaTemplate;
        try {
            metricMatchCriteriaTemplate = seriesArray1.getJSONObject("metricMatchCriteriaTemplate");
            JSONObject metricExpressionTemplate = metricMatchCriteriaTemplate
                    .getJSONObject("metricExpressionTemplate");
            JSONObject scopeEntity = metricExpressionTemplate.getJSONObject("scopeEntity");
            try {
                seriesArray1.put("name", serie_nue);
                metricExpressionTemplate.put("metricPath", nom_nuevo);
                scopeEntity.put("entityName", entity_nuevo);

                this.seriesArray = seriesArray1;
            } catch (JSONException ex) {
                Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (JSONException ex) {
            Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            dashboard.put("widgetTemplates", arrJSON);
        } catch (JSONException ex) {
            Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
        }
        File f;
        f = new File("C:\\Users\\Administrador\\Documents\\NetBeansProjects\\GADD\\data.json");
        FileWriter w = null;
        try {
            w = new FileWriter(f);
        } catch (IOException ex) {
            Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
        }
        BufferedWriter bw = new BufferedWriter(w);
        PrintWriter wr = new PrintWriter(bw);
        wr.write(dashboard.toString());//escribimos en el archivo
        wr.close();
        try {
            bw.close();
        } catch (IOException ex) {
            Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex);
        }

        JOptionPane.showMessageDialog(this, "los datos se han transferido con exito", "mensaje",
                JOptionPane.INFORMATION_MESSAGE);

    }//GEN-LAST:event_AnadirActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        inicio ini = new inicio();
        ini.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @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(Generador.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Generador.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Generador.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Generador.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Generador().setVisible(true);
            }
        });
    }

    private JSONArray getSeries(String panel, String serie) {
        JSONArray arreglo = new JSONArray();

        try {
            for (int i = 0; i < arrJSON.length(); i++) {
                JSONObject jsonObject = arrJSON.getJSONObject(i);
                if (jsonObject.getString("title").equals(panel)) {
                    JSONArray seriesArray = jsonObject.getJSONArray("dataSeriesTemplates");
                    for (int j = 0; j < seriesArray.length(); j++) {
                        JSONObject jsonObjectSerie = seriesArray.getJSONObject(j);
                        if (serie != null) {
                            if (jsonObjectSerie.getString("name").equals(serie)) {
                                arreglo.put(jsonObjectSerie);
                            }
                        } else {
                            arreglo.put(jsonObjectSerie);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arreglo;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Anadir;
    private javax.swing.JTextField Enti_nueva;
    private javax.swing.JTextField Nombre_nuevo;
    private javax.swing.JTextField Serie_nueva;
    private javax.swing.JTextField archivo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaMetricPath;
    private javax.swing.JTextField jTextFieldEntityName;
    private javax.swing.JComboBox paneles;
    private javax.swing.JComboBox series;
    // End of variables declaration//GEN-END:variables
}