package com.company;
/**
 * Log Nazi -- Created by Steven Richardson on 2/2/2016.
 */



import javax.swing.*;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Steven on 1/29/2016.
 */
public class LogReader
{
    // declare variables
    static String theFile;
    static int rows = 0;
    static int cols = 0;
    static String xPort;
    static String[][] myArray;

    //constructor call methods and initialize variables on creation
    public LogReader() throws IOException
    {
        chooseFile();
        getNumOfRowsCols();
        setUpMyCSVArray();
        xPort = getPort();
        searchRecord(setUpMyCSVArray());

    }


    static void chooseFile()
    {

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Select theFile to be checked:");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);// need to
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {

            theFile = chooser.getSelectedFile().toString();

        }
        else
        {
            System.out.println("No Selection ");

        }

    }

    static void getNumOfRowsCols()
    {
        Scanner scanIn;

        String InputLine;

        DateFormat xDateFormat = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
        Date myDate = new Date();
        String xfDate = xDateFormat.format(myDate);
        try
        {
            String xFileLocation;
            xFileLocation = theFile;

            scanIn = new Scanner(new BufferedReader(new FileReader(xFileLocation)));
            scanIn.useDelimiter(",");

            while (scanIn.hasNextLine())
            {
                InputLine = scanIn.nextLine();
                String[] inArray = InputLine.split(",");
                rows++;
                cols = inArray.length;

            }
        } catch (Exception e)
        {
            System.out.println(e);
        }

    }

    static String[][] setUpMyCSVArray() throws FileNotFoundException
    {
        System.out.println(rows + " " + cols);
        myArray = new String[rows][cols];
        //setup scanner to receive input
        Scanner scanIn = null;

        //declare and initialize variables
        int Rowc = 0;
//        int row = 0;
//        int Colc = 0;
//        int col = 0;
        int linNum = 0;
        String outputStr = "";
        String inputLine = "";
        double xnum = 0;

        //declare file location
        String xfileLocation;

        //initialize with the value from the file chooser in Main
        xfileLocation = theFile;
        JOptionPane.showMessageDialog(null, "****parsing csv...****");
        try
        {
            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));

            while (scanIn.hasNextLine())
            {
                inputLine = scanIn.nextLine();

                String[] inArray = inputLine.split(",");
                for (int x = 0; x < inArray.length; x++)
                {
                    myArray[Rowc][x] = inArray[x];
                }
                Rowc++;
            }

        } catch (ArrayIndexOutOfBoundsException e)
        {
            System.err.println("That's not it");

        }

        return myArray;
    }

    static String getPort()
    {
        String aPort;
        aPort = JOptionPane.showInputDialog(null, "Enter the search phrase:");
        return aPort;
    }

    static void searchRecord(String[][] record)
    {//  out[14] || out[17]
        System.out.println(rows + "\t" + cols);

        String outputStr = "";


        String myArray[][] = record;

        for (String[] out :
                myArray)
        {
            int iterator = 0;
            for (String o
                    : out
                    )
            {
                if (out[iterator].contains(xPort))
                {
                    System.out.print(o);

                }
            }
            iterator++;


        }


    }

    public static void main(String[] args) throws IOException
    {
        new LogReader();
    }

}

// The CSV File:
"session_id","time_stamp","end_time","bypassed","protocol","icmp_type","hostname","username","policy_id","c_client_addr","c_server_addr","c_server_port","c_client_port","s_client_addr","s_server_addr","s_server_port","s_client_port","client_intf","server_intf","c2p_bytes","p2c_bytes","s2p_bytes","p2s_bytes","filter_prefix","shield_blocked","firewall_blocked","firewall_flagged","firewall_rule_index","application_control_lite_protocol","application_control_lite_blocked","captive_portal_blocked","captive_portal_rule_index","application_control_application","application_control_protochain","application_control_blocked","application_control_flagged","application_control_confidence","application_control_ruleid","application_control_detail","bandwidth_control_priority","bandwidth_control_rule","ssl_inspector_ruleid","ssl_inspector_status","ssl_inspector_detail"
"95205376884384","2016-01-29 23:59:11.615000","2016-01-29 23:59:12.034000","False","6","None","172.16.1.2","None","1","172.16.1.2","54.86.2.80","443","57408","10.10.1.174","54.86.2.80","443","13719","2","1","1692","6214","6214","1692","None","None","False","False","0","None","None","None","None","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884383","2016-01-29 23:59:01.856000","2016-01-29 23:59:02.022000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49972","10.10.1.174","38.124.168.119","80","45162","6","1","213","388","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884382","2016-01-29 23:59:01.766000","2016-01-29 23:59:01.919000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49970","10.10.1.174","38.124.168.119","80","10131","6","1","217","392","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884381","2016-01-29 23:59:01.674000","2016-01-29 23:59:01.831000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49968","10.10.1.174","38.124.168.119","80","20596","6","1","217","392","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884362","2016-01-29 23:58:58.184000","2016-01-29 23:58:58.312000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.39","80","49966","10.10.1.174","4.28.136.39","80","31779","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884361","2016-01-29 23:58:58.105000","2016-01-29 23:58:58.234000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.39","80","49964","10.10.1.174","4.28.136.39","80","14783","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884360","2016-01-29 23:58:58.024000","2016-01-29 23:58:58.156000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.39","80","49962","10.10.1.174","4.28.136.39","80","22988","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884358","2016-01-29 23:58:55.267000","2016-01-29 23:58:55.396000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49960","10.10.1.174","4.28.136.36","80","16354","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884357","2016-01-29 23:58:55.188000","2016-01-29 23:58:55.318000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49958","10.10.1.174","4.28.136.36","80","39514","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884356","2016-01-29 23:58:55.108000","2016-01-29 23:58:55.240000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49956","10.10.1.174","4.28.136.36","80","38648","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884354","2016-01-29 23:58:52.365000","2016-01-29 23:58:52.498000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49954","10.10.1.174","38.117.98.202","80","27074","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884353","2016-01-29 23:58:52.271000","2016-01-29 23:58:52.416000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49952","10.10.1.174","38.117.98.202","80","34058","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884352","2016-01-29 23:58:52.190000","2016-01-29 23:58:52.323000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49950","10.10.1.174","38.117.98.202","80","42565","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884350","2016-01-29 23:58:49.359000","2016-01-29 23:58:49.495000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49948","10.10.1.174","38.117.98.202","80","44120","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884349","2016-01-29 23:58:49.278000","2016-01-29 23:58:49.411000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49946","10.10.1.174","38.117.98.202","80","21065","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884348","2016-01-29 23:58:49.195000","2016-01-29 23:58:49.333000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.202","80","49944","10.10.1.174","38.117.98.202","80","15553","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884346","2016-01-29 23:58:46.417000","2016-01-29 23:58:46.572000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49942","10.10.1.174","38.124.168.125","80","41932","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884345","2016-01-29 23:58:46.325000","2016-01-29 23:58:46.481000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49940","10.10.1.174","38.124.168.125","80","28290","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884344","2016-01-29 23:58:46.231000","2016-01-29 23:58:46.394000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49938","10.10.1.174","38.124.168.125","80","40103","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884342","2016-01-29 23:58:43.388000","2016-01-29 23:58:43.546000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49936","10.10.1.174","38.124.168.116","80","32971","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884341","2016-01-29 23:58:43.297000","2016-01-29 23:58:43.451000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49934","10.10.1.174","38.124.168.116","80","34628","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884340","2016-01-29 23:58:43.205000","2016-01-29 23:58:43.360000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49932","10.10.1.174","38.124.168.116","80","23783","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884337","2016-01-29 23:58:40.441000","2016-01-29 23:58:40.595000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49930","10.10.1.174","38.124.168.119","80","48760","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884336","2016-01-29 23:58:40.350000","2016-01-29 23:58:40.507000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49928","10.10.1.174","38.124.168.119","80","28579","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884335","2016-01-29 23:58:40.256000","2016-01-29 23:58:40.413000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49926","10.10.1.174","38.124.168.119","80","17897","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884333","2016-01-29 23:58:37.502000","2016-01-29 23:58:37.638000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.196","80","49924","10.10.1.174","38.117.98.196","80","24726","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884332","2016-01-29 23:58:37.421000","2016-01-29 23:58:37.560000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.196","80","49922","10.10.1.174","38.117.98.196","80","45814","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884331","2016-01-29 23:58:37.339000","2016-01-29 23:58:37.475000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.196","80","49920","10.10.1.174","38.117.98.196","80","10001","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884329","2016-01-29 23:58:34.558000","2016-01-29 23:58:34.714000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49918","10.10.1.174","38.124.168.116","80","24951","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884328","2016-01-29 23:58:34.468000","2016-01-29 23:58:34.620000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49916","10.10.1.174","38.124.168.116","80","33194","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884327","2016-01-29 23:58:34.375000","2016-01-29 23:58:34.533000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.116","80","49914","10.10.1.174","38.124.168.116","80","18080","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884325","2016-01-29 23:58:31.548000","2016-01-29 23:58:31.681000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.199","80","49912","10.10.1.174","38.117.98.199","80","37292","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884324","2016-01-29 23:58:31.467000","2016-01-29 23:58:31.601000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.199","80","49910","10.10.1.174","38.117.98.199","80","46680","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884323","2016-01-29 23:58:31.380000","2016-01-29 23:58:31.523000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.117.98.199","80","49908","10.10.1.174","38.117.98.199","80","49365","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884321","2016-01-29 23:58:28.536000","2016-01-29 23:58:28.692000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49906","10.10.1.174","38.124.168.119","80","39422","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884320","2016-01-29 23:58:28.445000","2016-01-29 23:58:28.601000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49904","10.10.1.174","38.124.168.119","80","41143","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884319","2016-01-29 23:58:28.353000","2016-01-29 23:58:28.508000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.119","80","49902","10.10.1.174","38.124.168.119","80","47938","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884317","2016-01-29 23:58:25.597000","2016-01-29 23:58:25.726000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49900","10.10.1.174","4.28.136.36","80","37843","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884316","2016-01-29 23:58:25.516000","2016-01-29 23:58:25.648000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49898","10.10.1.174","4.28.136.36","80","26823","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884315","2016-01-29 23:58:25.436000","2016-01-29 23:58:25.571000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.36","80","49896","10.10.1.174","4.28.136.36","80","11103","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884313","2016-01-29 23:58:22.658000","2016-01-29 23:58:22.814000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49894","10.10.1.174","38.124.168.125","80","26600","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884312","2016-01-29 23:58:22.566000","2016-01-29 23:58:22.722000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49892","10.10.1.174","38.124.168.125","80","38963","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884311","2016-01-29 23:58:22.472000","2016-01-29 23:58:22.632000","False","6","None","10.0.3.7","None","1","10.0.3.7","38.124.168.125","80","49890","10.10.1.174","38.124.168.125","80","13223","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884308","2016-01-29 23:58:19.716000","2016-01-29 23:58:19.845000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.42","80","49888","10.10.1.174","4.28.136.42","80","20250","6","1","208","383","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884307","2016-01-29 23:58:19.637000","2016-01-29 23:58:19.772000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.42","80","49886","10.10.1.174","4.28.136.42","80","18387","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"
"95205376884306","2016-01-29 23:58:19.555000","2016-01-29 23:58:19.692000","False","6","None","10.0.3.7","None","1","10.0.3.7","4.28.136.42","80","49884","10.10.1.174","4.28.136.42","80","40903","6","1","212","387","0","0","None","None","False","False","0","None","None","True","100001","None","None","None","None","None","None","None","None","None","None","None","None"

Sorry, I left out the question I had with my code. I am having a problem printing out a specific row that I determing in the if () on line 167. I am wanting to search the array and if I find a match to xPort vairable then print out that row. I cannot just search the entire row because the session id sometimes makes a match.

I figured it out. The problem was in my inner loop in the loop structure starting on line 168. I wrote a new loop this morning:

  String myArray[][] = record; // record contains a 2d array

        for (String[] out :
                myArray)
        {
            if (out[15].contains(xPort))
            {
                for (int i = 0; i < out.length - 1; i++)
                {
                    System.out.print(out[i]);

                }
                System.out.println();
            }


        }
This question has already been answered. Start a new discussion instead.