panel.AnalysisPanel.java Source code

Java tutorial

Introduction

Here is the source code for panel.AnalysisPanel.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 panel;

import com.google.gson.Gson;
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.knowm.xchart.Chart;
import org.knowm.xchart.QuickChart;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import static panel.GatheringPanel.mapUser;
import static panel.GatheringPanel.login;
import static panel.GatheringPanel.pass;

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
import org.apache.poi.ss.usermodel.charts.ChartLegend;
import org.apache.poi.ss.usermodel.charts.DataSources;
import org.apache.poi.ss.usermodel.charts.LegendPosition;
import org.apache.poi.ss.usermodel.charts.LineChartData;
import org.apache.poi.ss.usermodel.charts.ValueAxis;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
import static panel.GatheringPanel.numberPort;
import static panel.GatheringPanel.nameServer;

/**
 *
 * @author 
 */
public class AnalysisPanel extends javax.swing.JPanel {

    /**
     * Creates new form Two
     */
    public AnalysisPanel() {
        initComponents();
        jList1.setModel(listModel);
        for (Entry<String, Integer> entry : mapUser.entrySet()) {
            listModel.addElement(entry.getKey() + " - " + entry.getValue());
        }

    }

    public static DefaultListModel listModel = new DefaultListModel();

    public static boolean AnalysisPanel_get = false;
    static String citySelect = "";
    static String countrySelect = "";
    static boolean isMan = false;
    static boolean isFemale = false;
    static int ofAge = -5;
    static int untilAge = -5;
    static int circleToAnalysisPanel = -5;

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

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList<>();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jCheckBox5 = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        jList1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jButton1.setIcon(new javax.swing.ImageIcon(
                getClass().getResource("/panel/image/graph_highPerformanceComputingChart_5170_32xMD.png"))); // NOI18N
        jButton1.setText("?");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jCheckBox1.setText("?  ? ?");

        jCheckBox2.setText("?  ? ?");

        jCheckBox3.setText("? ");
        jCheckBox3.setActionCommand(
                "?   \n  ??  ?");

        jCheckBox4.setText("  ");
        jCheckBox4.setToolTipText("");

        jLabel1.setText("  ??  ?");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/panel/image/refresh_16xLG.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jCheckBox5.setText("? ");

        jLabel2.setText("  ??  ?");

        jButton3.setText("?   Excel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(" ");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.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().addContainerGap().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jLabel1))
                                .addComponent(jCheckBox1)
                                .addComponent(jCheckBox2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCheckBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 193,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton4)))
                        .addGroup(layout.createSequentialGroup().addGap(69, 69, 69).addComponent(jButton2,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGap(21, 21, 21)
                                                .addComponent(jLabel2))
                                        .addComponent(jCheckBox5)))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 161,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jButton3))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jCheckBox4)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jTabbedPane1))
                                .addGroup(layout.createSequentialGroup().addGap(91, 136, Short.MAX_VALUE)
                                        .addComponent(jButton1).addGap(18, 18, 18)
                                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 297,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4).addGap(170, 170, 170)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(18, Short.MAX_VALUE).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout
                                .createSequentialGroup().addGap(9, 9, 9)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(26, 26, 26).addComponent(jTabbedPane1))
                        .addGroup(layout.createSequentialGroup().addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2).addGap(10, 10, 10).addComponent(jCheckBox2)
                                .addGap(17, 17, 17).addComponent(jCheckBox1).addGap(18, 18, 18)
                                .addComponent(jCheckBox3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1).addGap(18, 18, 18).addComponent(jCheckBox5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2).addGap(18, 18, 18).addComponent(jCheckBox4)
                                .addGap(29, 29, 29).addComponent(jButton4).addGap(0, 32, Short.MAX_VALUE)))
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    public static boolean is_connect_AnalysisPanel = false;
    Connection connectDB;
    Connection citCountryConn;
    Statement statConn = null;
    Statement statCitCountryConn = null;
    static String namebase = "";
    public int num = 1;
    public static String nm = "";

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

        nm = "";
        num = 1;
        if (GatheringPanel.getdata) {
            JOptionPane.showMessageDialog(null, " ?.?", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        namebase = "";
        SettingForm setting = new SettingForm();
        setting.installSettings();
        String user = login;
        String password = pass;
        String dbUrl = "jdbc:sqlserver://" + nameServer + ":" + numberPort + ";databaseName=master";
        if (!is_connect_AnalysisPanel) {
            try {
                connectDB = DriverManager.getConnection(dbUrl, user, password);
                citCountryConn = DriverManager.getConnection(dbUrl, user, password);
            } catch (SQLException ex) {
                Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, " ?  ", "",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
        is_connect_AnalysisPanel = true;
        try {
            statConn = connectDB.createStatement();
            statCitCountryConn = citCountryConn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, " ?  ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        jTabbedPane1.removeAll();

        Integer ind = jList1.getSelectedIndex();
        if (ind == -1) {
            JOptionPane.showMessageDialog(null, " ?", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        namebase = (String) listModel.getElementAt(ind);

        String n[] = namebase.split("-");
        namebase = n[0].replace(" ", "") + "_VK";
        nm = n[1].replace(" ", "");
        if (!jCheckBox1.isSelected() && !jCheckBox2.isSelected() && !jCheckBox3.isSelected()
                && !jCheckBox4.isSelected() && !jCheckBox5.isSelected()) {
            JOptionPane.showMessageDialog(null, "?   ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        ResultSet rselbd = null;
        try {
            rselbd = statConn.executeQuery("SELECT name, database_id, create_date FROM sys.databases ;");
        } catch (SQLException ex) {
            Logger.getLogger(GatheringPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, " ?  ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        boolean isdb = false;
        try {
            while (rselbd.next()) {

                String namedb = rselbd.getString("name");
                if (namedb != null && namedb.equals(namebase)) {
                    isdb = true;
                }
            }
            //
        } catch (SQLException ex) {
            Logger.getLogger(GatheringPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (!isdb) {
            JOptionPane.showMessageDialog(null, "   ?", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        ExecutorService exec2 = Executors.newFixedThreadPool(1);
        MainForm.jTabbedPane1.setEnabled(false);
        jButton1.setEnabled(false);
        jButton3.setEnabled(false);
        jCheckBox1.setEnabled(false);
        jCheckBox2.setEnabled(false);
        jCheckBox3.setEnabled(false);
        jCheckBox4.setEnabled(false);
        jCheckBox5.setEnabled(false);
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);
        AnalysisPanel_get = true;
        exec2.submit(new Runnable() {
            @Override
            public void run() {

                try {
                    mainAnalysisPanel();
                } catch (Exception e) {
                    Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, e);
                } finally {

                    MainForm.jTabbedPane1.setEnabled(true);
                    jButton1.setEnabled(true);
                    jButton3.setEnabled(true);
                    jCheckBox1.setEnabled(true);
                    jCheckBox2.setEnabled(true);
                    jCheckBox3.setEnabled(true);
                    jCheckBox4.setEnabled(true);
                    jCheckBox5.setEnabled(true);
                    jLabel1.setEnabled(true);
                    jLabel2.setEnabled(true);
                    AnalysisPanel_get = false;
                }

            }

        });
        exec2.shutdown();

    }//GEN-LAST:event_jButton1ActionPerformed

    public static Map<Integer, int[]> stat = new HashMap<Integer, int[]>();
    public static ConcurrentMap<Integer, Integer> id_vzr = new ConcurrentHashMap<Integer, Integer>();
    public static Map<Integer, int[]> stat_pred = new HashMap<Integer, int[]>();
    static Map<Integer, int[]> usersAge = new HashMap<Integer, int[]>();
    static Map<Integer, Integer> usersAgeOfUser = new HashMap<Integer, Integer>();
    static Map<Integer, Integer> friendOfAge = new HashMap<Integer, Integer>();
    public static double[] xData1;
    public static double[] yData1;
    public static double[] xData2;
    public static double[] yData2;
    public static double[] xData3;
    public static double[] yData3;
    public static double[] xData4;
    public static double[] yData4;
    public static double[] xData5;
    public static double[] yData5;
    public static Chart chart2;
    public static Chart chart1;
    public static Chart chart3;
    public static Chart chart4;
    public static Chart chart5;
    public static String cit = "cit=";
    public static String countr = "countr=";

    public void mainAnalysisPanel() throws SQLException, IOException // ?  ? ?
    {
        xData1 = null;
        yData1 = null;
        xData2 = null;
        yData2 = null;
        xData3 = null;
        yData3 = null;
        xData4 = null;
        yData4 = null;
        xData5 = null;
        yData5 = null;
        chart2 = null;
        chart1 = null;
        chart3 = null;
        chart4 = null;
        chart5 = null;
        cit = "cit=";
        countr = "countr=";

        stat = new HashMap<Integer, int[]>();
        stat_pred = new HashMap<Integer, int[]>();
        usersAge = new HashMap<Integer, int[]>();
        usersAgeOfUser = new HashMap<Integer, Integer>();
        friendOfAge = new HashMap<Integer, Integer>();
        id_vzr = new ConcurrentHashMap<Integer, Integer>();
        int cnt_mass = 0;
        String id_undo = "";
        ResultSet rsel_top;
        rsel_top = statConn.executeQuery("SELECT top(1) * FROM " + namebase + ".dbo.BASE_TABLE");
        while (rsel_top.next()) {
            id_undo = rsel_top.getString("id");
        }

        //
        ResultSet ct;
        int id_country = -1;
        int id_city = -1;
        try {
            if (!countrySelect.equals("")) {
                ct = statCitCountryConn.executeQuery(
                        "SELECT * FROM " + "C.dbo.countries WHERE name like " + "'%" + countrySelect + "%'");
                while (ct.next()) {
                    if (ct.getString("name").equals(countrySelect)) {
                        id_country = ct.getInt("country_id");
                    }

                }
            }
            if (!citySelect.equals("")) {
                ct = statCitCountryConn.executeQuery(
                        "SELECT * FROM " + "C.dbo.cities WHERE city like " + "'%" + citySelect + "%'");
                while (ct.next()) {
                    if (ct.getString("city").equals(citySelect)) {
                        id_city = ct.getInt("id");
                    }

                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "? ?? ??? ? ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        //
        Calendar SaveData = Calendar.getInstance();
        ResultSet sql_all_user;
        String wr1 = "";
        String res = "";
        if (id_country != -1) {
            wr1 = " country like " + "'%" + id_country + "%'" + "and DATALENGTH(country)="
                    + ((int) Math.log10(id_country) + 1 + 2);
            res += wr1;
        }
        String wr2 = "";
        if (id_city != -1) {
            wr2 = " city like " + "'%" + id_city + "%'" + "and DATALENGTH(city)="
                    + ((int) Math.log10(id_city) + 1 + 2);
            res += (res.equals("") ? "" : " and ") + wr2;
        }
        String wr3 = "";
        if (isMan) {
            wr3 = " sex like " + "'%" + 2 + "%'" + "and DATALENGTH(sex)=" + 3;
        }
        String wr4 = "";
        if (isFemale) {
            wr4 = " sex like " + "'%" + 1 + "%'" + "and DATALENGTH(sex)=" + 3;
        }

        if (isMan && isFemale) {

        } else {
            if (isMan) {
                res += (res.equals("") ? "" : " and ") + wr3;

            }
            if (isFemale) {
                res += (res.equals("") ? "" : " and ") + wr4;
            }
        }

        if (ofAge != -5 || untilAge != -5) {
            if (ofAge > untilAge) {
                if (ofAge != -5) {
                    res += (res.equals("") ? "" : " and ")
                            + " CONVERT(INT, SUBSTRING(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000),1,DATALENGTH(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000))-1))<"
                            + ofAge;
                }
                if (untilAge != -5) {
                    res += (res.equals("") ? "" : " and ")
                            + " CONVERT(INT, SUBSTRING(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000),1,DATALENGTH(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000))-1))>"
                            + untilAge;
                }
            } else {
                if (ofAge != -5) {
                    res += (res.equals("") ? "" : " and ")
                            + " CONVERT(INT, SUBSTRING(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000),1,DATALENGTH(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000))-1))>"
                            + ofAge;
                }
                if (untilAge != -5) {
                    res += (res.equals("") ? "" : " and ")
                            + " CONVERT(INT, SUBSTRING(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000),1,DATALENGTH(SUBSTRING (CONVERT(VARCHAR(MAX),bdate),2,8000))-1))<"
                            + untilAge;
                }
            }
        }

        if (circleToAnalysisPanel != -5) {
            res += (res.equals("") ? "" : " and ") + " count_circle=" + (circleToAnalysisPanel - 1);
        }

        ResultSet rsel_top2 = null;
        try {
            rsel_top2 = statConn.executeQuery("SELECT COUNT(*) as [Count] FROM " + namebase + ".dbo.BASE_TABLE"
                    + (res.equals("") ? "" : " where" + res)); //TODOTODO
        } catch (SQLException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        String countvse = "";
        try {
            while (rsel_top2.next()) {
                countvse = rsel_top2.getString("Count");
            }
        } catch (SQLException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        jProgressBar1.setStringPainted(true);
        jProgressBar1.setMinimum(0);
        jProgressBar1.setMaximum(countvse.equals("") ? 0 : Integer.valueOf(countvse));

        String zpr = "SELECT * FROM " + namebase + ".dbo.BASE_TABLE " + (res.equals("") ? "" : "where" + res);
        sql_all_user = statConn.executeQuery(zpr); //TODOTODOTODOTODO
        statConn.setQueryTimeout(0);

        int cnt = 0;
        int summvzr = 0;
        int count_man = 0;
        int count_female = 0;
        Boolean one = false;
        int count_user = 0;
        int vzr = 0;
        int err = 0;
        SaveData = Calendar.getInstance();
        int pereb = 0;
        String curr_city;
        String curr_country;
        String id_usr_par;
        String id;
        String sex;
        String bdate;
        int sredvzr;
        int sm;
        try {

            while (sql_all_user.next()) {
                err++;
                jProgressBar1.setValue(err);

                ////////////////////////////////////////////////////////////////
                // <editor-fold defaultstate="collapsed" desc="  ?">   
                curr_city = null;
                curr_country = null;
                curr_city = sql_all_user.getString("city");
                curr_country = sql_all_user.getString("country");
                id_usr_par = sql_all_user.getString("parent");
                if (curr_city != null && curr_city != "") {

                    if (!city.containsKey(curr_city) && !idAddCity.contains(id_usr_par)) {
                        city.put(curr_city, 1);
                        idAddCity.add(id_usr_par);
                    }
                    if (city.containsKey(curr_city) && !idAddCity.contains(id_usr_par)) {
                        int val = 0;
                        val = city.get(curr_city);
                        city.remove(curr_city);
                        city.put(curr_city, ++val);
                    }

                }
                if (curr_country != null && curr_country != "") {
                    if (!country.containsKey(curr_country) && !idAddCountry.contains(id_usr_par)) {
                        country.put(curr_country, 1);
                        idAddCountry.add(id_usr_par);
                    }
                    if (country.containsKey(curr_country) && !idAddCountry.contains(id_usr_par)) {
                        int val_country = 0;
                        val_country = country.get(curr_country);
                        country.remove(curr_country);
                        country.put(curr_country, ++val_country);
                    }
                }
                //</editor-fold>
                ////////////////////////////////////////////////////////////////

                id = sql_all_user.getString("id");
                sex = sql_all_user.getString("sex");
                bdate = sql_all_user.getString("bdate");
                if (id != null) {
                    id = id.replace("'", "");
                }
                if (sex != null) {
                    sex = sex.replace("'", "");
                }
                if (bdate != null) {
                    bdate = bdate.replace("'", "");
                }

                if (!id.equals(id_undo)) {
                    sredvzr = 0;

                    if (cnt != 0) {
                        sredvzr = cnt == 0 ? 0 : summvzr / cnt;
                    }

                    int[] data = { Integer.valueOf(id_undo), sredvzr, count_user, count_man, count_female };
                    // stat.put(iii++, data);
                    stat_pred.put(cnt_mass++, data);
                    id_undo = id;
                    cnt = 0;
                    summvzr = 0;
                    count_man = 0;
                    count_female = 0;
                    count_user = 0;
                    one = false;
                }
                if (id.equals(id_undo)) {

                    count_user++;
                    if (bdate != null) {
                        sm = Integer.parseInt(bdate);
                        summvzr += sm;
                        cnt++;
                    }
                    if (sex != null) {
                        if (sex.equals("1")) {
                            count_female++;
                        }
                        if (sex.equals("2")) {
                            count_man++;
                        }
                    }
                    // }
                }
            }
        } catch (Exception e) {
            err = err;
            System.out.println("  err= " + err + "\n" + e);
        }
        vr(SaveData, "  ");

        SaveData = Calendar.getInstance();

        ExecutorService exec2 = Executors.newFixedThreadPool(11);

        try {
            for (final int el : stat_pred.keySet()) {
                exec2.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            id_vzr.put(el, years(el));
                        } catch (IOException ex) {
                            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }

                });

            }
        } finally {

            exec2.shutdown();
            try {
                exec2.awaitTermination(1, TimeUnit.DAYS);
            } catch (InterruptedException ex) {
                System.out.println("370");
            }
        }

        vr(SaveData, " ? ? ? net ");

        SaveData = Calendar.getInstance();
        int vzr2;
        int sredvzr2;
        int count_user2;
        int count_man2;
        int count_female2;
        for (int pl : stat_pred.keySet()) {

            int[] user = stat_pred.get(pl);

            vzr2 = id_vzr.get(pl);

            if (vzr2 > 100 || vzr2 < 0) {
                vzr2 = 0;
            }

            sredvzr2 = user[1];
            count_user2 = user[2];
            count_man2 = user[3];
            count_female2 = user[4];

            int[] data = { vzr2, sredvzr2, count_user2, count_man2, count_female2 };
            stat.put(pl, data);

        }
        vr(SaveData, "   stat ");

        SaveData = Calendar.getInstance();
        int vozrr;
        int countvzr;
        int contthispol;
        int countvzrdr;
        int sredman;
        int sredfemale;
        int sredcount;

        for (int pl : stat.keySet()) {
            vozrr = 0;
            countvzr = 0;
            count_man = 0;
            count_female = 0;
            contthispol = 0;
            countvzrdr = 0;
            int[] user = stat.get(pl);
            if (!usersAgeOfUser.containsKey(user[0])) {
                for (int plz : stat.keySet()) { // ?  ?   ? 
                    int[] user_plz = stat.get(plz);
                    if (user[0] == user_plz[0]) {
                        vozrr += user_plz[1];
                        count_man += user_plz[3];
                        count_female += user_plz[4];
                        countvzr++;
                    }

                }

                if (!friendOfAge.containsKey(user[0])) {
                    for (int plz : stat.keySet()) {
                        int[] user_plz = stat.get(plz);
                        if (user[0] == user_plz[0]) {
                            contthispol += user_plz[2]; //? 
                            countvzrdr++;
                        }

                    }

                    sredvzr = countvzr == 0 ? 0 : vozrr / countvzr; //? ? ??
                    sredman = countvzr == 0 ? 0 : count_man / countvzr;
                    sredfemale = countvzr == 0 ? 0 : count_female / countvzr;
                    usersAgeOfUser.put(user[0], sredvzr);
                    int[] mass = { sredman, sredfemale };
                    usersAge.put(user[0], mass);

                    sredcount = countvzrdr == 0 ? 0 : contthispol / countvzrdr;
                    //? ?  ?  ?

                    friendOfAge.put(user[0], sredcount);

                }
            }
        }
        vr(SaveData, "   stat  vozrastnew  drug ");

        int i = 0;

        JPanel pnlChart;
        Font font;
        SaveData = Calendar.getInstance();
        if (jCheckBox1.isSelected()) {

            i = 0;

            int ii, j;

            int data[] = new int[usersAgeOfUser.size()];
            int k = 0;
            for (int obj : usersAgeOfUser.keySet()) {
                if (obj != 0) {
                    data[k++] = obj;
                }
            }

            int size = k;
            xData1 = new double[k];
            yData1 = new double[k];

            for (ii = 0; ii < size; ++ii) {
                for (j = size - 1; j > ii; --j) {
                    if (data[j] < data[j - 1]) {
                        int t = data[j - 1];
                        data[j - 1] = data[j];
                        data[j] = t;
                    }
                }
            }

            for (int obj : data) {
                if (obj <= 0) {
                    continue;
                }
                int vzrdrug = usersAgeOfUser.get(obj);

                xData1[i] = obj; // ? ?
                yData1[i++] = vzrdrug;
            }

            if (xData1.length > 0 && yData1.length > 0) {
                chart1 = QuickChart.getChart("?  ? ?",
                        "? ?", "?  ", "y",
                        xData1, yData1);
                pnlChart = new XChartPanel(chart1);

                font = new Font("Verdana", Font.PLAIN, 14);
                jTabbedPane1.setFont(font);
                jTabbedPane1.addTab(Integer.toString(num++), pnlChart);
            }

        }
        //
        if (jCheckBox3.isSelected()) {
            System.out.println("?  2");
            i = 0;
            int ii, j;

            int data[] = new int[usersAge.size()];
            int k = 0;
            for (int obj : usersAge.keySet()) {

                if (obj != 0) {
                    data[k++] = obj;
                }
            }

            int size = k;
            xData3 = new double[k];
            yData3 = new double[k];

            for (ii = 0; ii < size; ++ii) {
                for (j = size - 1; j > ii; --j) {
                    if (data[j] < data[j - 1]) {
                        int t = data[j - 1];
                        data[j - 1] = data[j];
                        data[j] = t;
                    }
                }
            }

            for (int obj : data) {
                if (obj <= 0) {
                    continue;
                }
                int[] vzrpeople = usersAge.get(obj);

                xData3[i] = obj;
                yData3[i++] = vzrpeople[0];
            }

            if (xData3.length > 0 && yData3.length > 0) {
                chart3 = QuickChart.getChart(
                        "?   ??  ?",
                        "? ?", "? ", "y", xData3,
                        yData3);
                pnlChart = new XChartPanel(chart3);

                font = new Font("Verdana", Font.PLAIN, 14);
                jTabbedPane1.setFont(font);
                jTabbedPane1.addTab(Integer.toString(num++), pnlChart);
            }
        }
        //
        if (jCheckBox5.isSelected()) {
            System.out.println("?  3");
            i = 0;
            int ii, j;

            int data[] = new int[usersAge.size()];
            int k = 0;
            for (int obj : usersAge.keySet()) {

                if (obj != 0) {
                    data[k++] = obj;
                }
            }

            int size = k;
            xData5 = new double[k];
            yData5 = new double[k];

            for (ii = 0; ii < size; ++ii) {
                for (j = size - 1; j > ii; --j) {
                    if (data[j] < data[j - 1]) {
                        int t = data[j - 1];
                        data[j - 1] = data[j];
                        data[j] = t;
                    }
                }
            }

            for (int obj : data) {
                if (obj <= 0) {
                    continue;
                }
                int[] vzrpeople = usersAge.get(obj);

                xData5[i] = obj;
                yData5[i++] = vzrpeople[1];
            }

            if (xData5.length > 0 && yData5.length > 0) {
                chart5 = QuickChart.getChart(
                        "?   ??  ?",
                        "? ?", "? ", "y", xData5,
                        yData5);
                pnlChart = new XChartPanel(chart5);

                font = new Font("Verdana", Font.PLAIN, 14);
                jTabbedPane1.setFont(font);
                jTabbedPane1.addTab(Integer.toString(num++), pnlChart);
            }
        }
        //
        if (jCheckBox2.isSelected()) {
            System.out.println("?  4");

            i = 0;

            int ii, j;

            int data[] = new int[friendOfAge.size()];
            int k = 0;
            for (int obj : friendOfAge.keySet()) {
                if (obj != 0) {
                    data[k++] = obj;
                }
            }

            int size = k;
            xData2 = new double[k];
            yData2 = new double[k];

            for (ii = 0; ii < size; ++ii) {
                for (j = size - 1; j > ii; --j) {
                    if (data[j] < data[j - 1]) {
                        int t = data[j - 1];
                        data[j - 1] = data[j];
                        data[j] = t;
                    }
                }
            }

            for (int obj : data) {
                if (obj <= 0) {
                    continue;
                }
                int vzrdrug = friendOfAge.get(obj);

                xData2[i] = obj;
                yData2[i++] = vzrdrug;
            }

            if (xData2.length > 0 && yData2.length > 0) {
                chart2 = QuickChart.getChart(
                        "C ?  ?  ?",
                        "? ?", "? ", "y", xData2,
                        yData2);
                pnlChart = new XChartPanel(chart2);

                font = new Font("Verdana", Font.PLAIN, 14);
                jTabbedPane1.setFont(font);
                jTabbedPane1.addTab(Integer.toString(num++), pnlChart);
            }
        }
        if (jCheckBox4.isSelected()) {
            cit = "cit=";
            countr = "countr=";
            InhabitationAnalysisForm sc = new InhabitationAnalysisForm(city, country);
            jTabbedPane1.addTab(Integer.toString(num++), sc);

        }

        // saveGraph();
        vr(SaveData, "   ");
    }

    void saveGraph() {
        try {
            //
            String sv = "";
            if (chart1 != null) {
                sv = "png=stat/Chart_" + MainForm.statmax + ";";
                BitmapEncoder.saveBitmap(chart1, "stat/Chart_" + MainForm.statmax, BitmapFormat.PNG);
            }
            if (chart2 != null) {
                sv += "png=stat/Chart2_" + MainForm.statmax + ";";
                BitmapEncoder.saveBitmap(chart2, "stat/Chart2_" + MainForm.statmax, BitmapFormat.PNG);
            }
            if (chart3 != null) {
                sv += "png=stat/Chart3_" + MainForm.statmax + ";";
                BitmapEncoder.saveBitmap(chart3, "stat/Chart3_" + MainForm.statmax, BitmapFormat.PNG);
            }
            if (chart4 != null) {
                sv += "png=stat/Chart4_" + MainForm.statmax + ";";
                BitmapEncoder.saveBitmap(chart4, "stat/Chart4_" + MainForm.statmax, BitmapFormat.PNG);
            }
            if (chart5 != null) {
                sv += "png=stat/Chart5_" + MainForm.statmax + ";";
                BitmapEncoder.saveBitmap(chart5, "stat/Chart5_" + MainForm.statmax, BitmapFormat.PNG);
            }
            if (!cit.equals("cit=")) {
                sv += cit + ";";
            }
            if (!countr.equals("countr=")) {
                sv += countr + ";";
            }
            MainForm.stat.put((MainForm.statmax++) + ":" + namebase, "id=" + nm + ";" + sv);
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

        //
    }

    public static Map<String, Integer> city = new HashMap<String, Integer>() {
    };
    ArrayList<String> idAddCity = new ArrayList<String>();
    public static Map<String, Integer> country = new HashMap<String, Integer>() {
    };
    ArrayList<String> idAddCountry = new ArrayList<String>();

    int years(int ID) throws MalformedURLException, IOException {

        try {
            String API_URL = "https://api.vk.com/method/";
            String method = "users.get";
            String url = "";
            int[] user = stat_pred.get(ID);
            int IDNEW = user[0];
            url = API_URL + method + "?&uid=" + IDNEW
                    + "&fields=bdate&name_case=nom&count&offset=0&lid&order=hints";
            String json;

            URL url2 = new URL(url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream()));
            json = reader.readLine();
            reader.close();

            String idgl = json.replace("'", "");
            idgl = idgl.replace("{\"response\":[", "").replace("]}", "");
            String res = "[" + idgl + "]";
            ArrayList<User> la = new ArrayList<User>();
            ArrayList<Map> list = new Gson().fromJson(res, la.getClass());
            String bdate = "";
            for (Map t : list) {

                for (Object entry : t.keySet()) {

                    Object val = t.get(entry);

                    if (entry.equals("bdate")) {

                        String str = "";
                        String st = (String) val;
                        int p = st.indexOf('.');
                        if (p >= 0) {
                            String left = st.substring(p + 1);

                            p = left.indexOf('.');

                            if (p >= 0) {
                                str = left.substring(p + 1);
                            }

                        }
                        if (!"".equals(str)) {
                            bdate = String.valueOf(2015 - Integer.parseInt(str));
                        }

                    }

                }

            }

            if (bdate.equals("")) {
                return 0;
            }

            return Integer.parseInt(bdate);
        } catch (Exception e) {
            return 0;
        }
    }

    public void vr(Calendar SaveData, String outp) {

        try {
            FileWriter writer5 = new FileWriter(GatheringPanel.pathScript + "log.txt", true);
            writer5.write(outp);
            System.out.print(outp);

            Calendar CurrentData = Calendar.getInstance();

            Calendar calculate = Calendar.getInstance();
            calculate.setTime(new Date(CurrentData.getTime().getTime() - SaveData.getTime().getTime()));

            System.out.println(calculate.get(Calendar.MINUTE) + ":" + calculate.get(Calendar.SECOND));
            writer5.write(calculate.get(Calendar.MINUTE) + ":" + calculate.get(Calendar.SECOND));
            writer5.write("\n");
            writer5.close();
        } catch (Exception e) {
            System.out.println("391");
        }

    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        listModel.removeAllElements();
        for (Entry<String, Integer> entry : mapUser.entrySet()) {
            listModel.addElement(entry.getKey() + " - " + entry.getValue());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

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

        if (GatheringPanel.getdata) {
            JOptionPane.showMessageDialog(null, " ?.?", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        Workbook wb = new XSSFWorkbook();
        if (xData2 != null) {
            excel(wb, " -  ? ?", xData2, yData2);
        }
        if (xData1 != null) {
            excel(wb, "?  ? ?", xData1, yData1);
        }
        if (xData3 != null) {
            excel(wb, "?   ??  ?", xData3,
                    yData3);
        }
        if (xData5 != null) {
            excel(wb, "?   ??  ?", xData5,
                    yData5);
        }

        if (xData1 == null && xData2 == null && xData3 == null && xData5 == null) {
            JOptionPane.showMessageDialog(null, "? ? ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        // Write the output to a file
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(GatheringPanel.pathExportExcel + "\\excel_vk.xlsx");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null,
                    "? ?   ? ??", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        try {
            wb.write(fileOut);
        } catch (IOException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "? ? ?", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        try {
            fileOut.close();
        } catch (IOException ex) {
            Logger.getLogger(AnalysisPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "? ?  excel ", "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE);

    }

    void excel(Workbook wb, String name, double xData[], double yData[]) {
        // Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet(name); //? ?
        final int NUM_OF_ROWS = 2;
        final int NUM_OF_COLUMNS = yData.length;

        // Create a row and put some cells in it. Rows are 0 based.
        Row row;
        Cell cell;
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
            row = sheet.createRow((short) rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                cell = row.createCell((short) colIndex);
                if (rowIndex == 0) {
                    cell.setCellValue(xData[colIndex]);
                }
                if (rowIndex == 1) {
                    cell.setCellValue(yData[colIndex]);
                }
            }
        }

        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 25, 35);

        org.apache.poi.ss.usermodel.Chart chart = drawing.createChart(anchor);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.TOP_RIGHT);

        LineChartData data = chart.getChartDataFactory().createLineChartData();

        // Use a category axis for the bottom axis.
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet,
                new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));

        data.addSeries(xs, ys1);

        chart.plot(data, bottomAxis, leftAxis);

    }//GEN-LAST:event_jButton3ActionPerformed

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

        FiltersForm pr = new FiltersForm();
        pr.show();

    }//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList<String> jList1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}