package org.pdxfinder.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.neo4j.ogm.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.stream.Stream;

@Service
public class UtilityService {


    private String homeDir = System.getProperty("user.home");
    private final static Logger log = LoggerFactory.getLogger(UtilityService.class);

    public JsonNode readJsonURL(String apiLink) {

        JsonNode jsonNode = null;
        ObjectMapper mapper = new ObjectMapper();

        try {

            URL url = new URL(apiLink);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            jsonNode = mapper.readTree(br);
            conn.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonNode;

    }



    public JsonNode readJsonLocal(String jsonFileLink) {

        JsonNode jsonNode = null;
        ObjectMapper mapper = new ObjectMapper();

        try {

            BufferedReader br = new BufferedReader(new FileReader(jsonFileLink));
            jsonNode = mapper.readTree(br);

        }catch (Exception e) {}

        return jsonNode;
    }




    public void writeToFile(String data, String name){

        String fileName = homeDir+"/Documents/"+name;

        // Write to the file using BufferedReader and FileWriter
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
            writer.append(data);
            writer.close();

        } catch (Exception e) {}

    }


    public Boolean deleteFile(String name) {

        String fileURL = homeDir+"/Documents/"+name;

        Boolean report = false;
        try {

            Path path = Paths.get(fileURL);
            Files.deleteIfExists(path);

            report = true;
        } catch (Exception e) {
        }

        return report;
    }



    public String parseURL(String urlStr) {

        StringBuilder sb = new StringBuilder();

        try {
            URL url = new URL(urlStr);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                sb.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
            log.error("Unable to read from URL " + urlStr, e);
        }
        return sb.toString();
    }


    public String parseFile(String path) {

        StringBuilder sb = new StringBuilder();

        try {
            Stream<String> stream = Files.lines(Paths.get(path));

            Iterator itr = stream.iterator();
            while (itr.hasNext()) {
                sb.append(itr.next());
            }
        } catch (Exception e) {
            log.error("Failed to load file " + path, e);
        }
        return sb.toString();
    }


    public String splitText(String data, String delim, String seperator){

        String result = "";

        String[] splits = data.split(delim);

        for (String split : splits){

            result += split.trim()+seperator;
        }

        return result;
    }



}

@@ -486,69 +486,9 @@
 til.
-Collections;%0Aimport java.util.Date;%0Aimport java.util.Iterator
+*
 ;%0Aim
@@ -617,24 +617,24 @@
 ser.home%22);%0A
-
     private 
@@ -703,24 +703,2000 @@
 ce.class);%0A%0A
+%0A    public List%3CMap%3CString, String%3E%3E serializeCSVToMaps(String csvFile) %7B%0A%0A        /*************************************************************************************************************%0A         *     LOAD DATA FROM FILE          *%0A         ***********************************/%0A%0A        FileInputStream fileStream = null;%0A        try %7B%0A            fileStream = new FileInputStream(csvFile);%0A        %7D catch (Exception e) %7B %7D%0A        DataInputStream csvData = new DataInputStream(fileStream);%0A%0A%0A        /*************************************************************************************************************%0A         *     INITIALIZE PARAMETERS         *%0A         ************************************/%0A%0A        int row = 0;%0A        String thisLine;%0A        List%3CString%3E tableHead = new ArrayList%3C%3E();%0A        List%3CMap%3CString, String%3E%3E csvMap = new ArrayList%3C%3E();%0A%0A%0A        /*************************************************************************************************************%0A         *    LOAD CSV FIRST ROW AS TABLE-HEAD, OTHER ROWS AS DATA, & LOAD TO MAP        *%0A         *******************************************************************************/%0A        try %7B%0A%0A            while ((thisLine = csvData.readLine()) != null) %7B%0A                String rowDataArr%5B%5D = thisLine.split(%22,%22);%0A                int column = 0;%0A%0A                if (row == 0) %7B%0A%0A                    for (column = 0; column %3C rowDataArr.length; column++) %7B%0A%0A                        tableHead.add(rowDataArr%5Bcolumn%5D);%0A                    %7D%0A                %7D else %7B%0A%0A                    Map%3CString, String%3E rowMap = new HashMap();%0A                    for (String columnHead : tableHead) %7B%0A%0A                        rowMap.put(columnHead, rowDataArr%5Bcolumn%5D);%0A                        column++;%0A                    %7D%0A                    csvMap.add(rowMap);%0A                %7D%0A                row++;%0A            %7D%0A        %7D catch (Exception e) %7B %7D%0A%0A        return csvMap;%0A%0A    %7D%0A%0A%0A%0A
     public J

Implement Universal CSV to Java Map List Serializer UtilityService