// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.error_analyzer.backend;

import com.google.error_analyzer.backend.LogDao;
import java.io.IOException;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

//This class contains the methods used for processing logs and storing them to the database.
//This includes removal of special characters, conversion into json string.
public class StoreLogs {

    private static final Logger logger = LogManager.getLogger(StoreLogs.class);
    public LogDao logDao = new LogDao();

    //convert the logText and logLineNumber to a json string
    public String convertToJsonString(String logText, int logLineNumber) {
        final String removeSpecialCharactersString = "[^a-zA-Z0-9:\"]";
        final String space = " ";
        logText = logText.replaceAll(removeSpecialCharactersString, space);
        //remove special characters
        final String doubleInvertedComma = "\"";
        final String singleInvertedComma = "\'";
        logText = logText.replaceAll(doubleInvertedComma, singleInvertedComma);
        logText = logText.replaceAll("\\s+", " ");
        //remove extra white space
        String jsonString = String.format("{\"logLineNumber\":%1$s," +
            "\"logText\":\"%2$s\"}", logLineNumber, logText);
        return jsonString;
    }

    //Calls the method StoreLog if an index with name fileName does not 
    //exist in db
    public String checkAndStoreLog(String fileName, String log)
    throws IOException {
        if (logDao.fileExists(fileName) == true) {
            logger.info("File already exists");
            final String response =
                "\t\t\t<h2> Sorry! the file already exists</h2>";
            return (response);
        } else {
            final String response = storeLog(fileName, log);
            logger.info("File Stored");
            return (response);

        }
    }

    //Stores the log in an index with name fileName
    public String storeLog(String fileName, String log) throws IOException {
        final String lineBreakString = "\\r?\\n";
        int logLineNumber = 1;
        String logLines[] = log.split(lineBreakString);
        for (String logLine: logLines) {
            if (!(logLine.equals(""))) {
                String logLineNum = Integer.toString(logLineNumber);
                String jsonString = convertToJsonString(logLine, logLineNumber);
                logDao.storeLogLine(fileName, jsonString, logLineNum);
                logLineNumber++;
            }
        }
        final String response = "\t\t\t<h2> File Stored</h2>";
        return response;
    }



}
@@ -2048,34 +2048,29 @@
 log)
-%0A    throws IOException %7B%0A
+ %7B%0A        try %7B%0A    
     
@@ -2124,35 +2124,40 @@
             
+    
 logger.
-info
+error
 (%22File alrea
@@ -2173,32 +2173,36 @@
 %22);%0A            
+    
 final String res
@@ -2225,16 +2225,20 @@
         
+  
+  
 %22%5Ct%5Ct%5Ct%3C
@@ -2283,32 +2283,36 @@
 %3E%22;%0A            
+    
 return (response
@@ -2322,16 +2322,20 @@
         
+   
+ 
 %7D else %7B
@@ -2335,16 +2335,20 @@
  else %7B%0A
+    
         
@@ -2396,24 +2396,28 @@
 Name, log);%0A
+    
             
@@ -2448,32 +2448,36 @@
 %22);%0A            
+    
 return (response
@@ -2479,16 +2479,228 @@
 ponse);%0A
+            %7D%0A        %7D catch (Exception e) %7B%0A            final String response = String.format(%22Could not store file %251$s%22, e);%0A            logger.error(%22Could not store file%22, e);%0A            return (response);
 %0A       

Add try-catch block