package org.pdxfinder.services;

import org.pdxfinder.graph.dao.Marker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OmicTransformationService {

    private static final Logger log = LoggerFactory.getLogger(OmicTransformationService.class);
    private DataImportService dataImportService;

    @Autowired
    OmicTransformationService(DataImportService dataImportService){
        this.dataImportService = dataImportService;
    }

    private Map<String, String> geneIdCache = new HashMap<>();

    public void convertListOfNcbiToHgnc(List<String> geneList){
        String fileOut = "ncbiToHugoAccesions";
        BufferedWriter out = null;
        FileWriter fstream = null;
        try {
            fstream = new FileWriter(fileOut, true);
            out = new BufferedWriter(fstream);
            BufferedWriter finalOut = out;
            geneList.forEach(g ->
                    {
                        try {
                            String conversionRow = String.format("%s\t%s\n", g, ncbiGeneIdToHgncSymbol(g));
                            finalOut.write(conversionRow);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
            );
            out.flush();
            out.close();
        } catch(IOException e){
            log.error("Failure opening output file %n {}", e.toString());
        }
        finally{
            try {
                if (fstream != null) fstream.close();
                if (out != null) out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public String ncbiGeneIdToHgncSymbol(String ncbiGene) {
        String hgncSymbol = geneIdCache.get(ncbiGene);
        if (hgncSymbol == null) {
            Marker marker = dataImportService.getMarkerbyNcbiGeneId(ncbiGene);
            if (marker.hasHgncSymbol()) {
                hgncSymbol = marker.getHgncSymbol();
                geneIdCache.put(ncbiGene, hgncSymbol);
            } else { log.warn("No marker found for NCBI gene Id {} Cannot generate Hgnc symbol", ncbiGene); }
        }
        return hgncSymbol;
    }

}

@@ -914,42 +914,8 @@
 s%22;%0A
-        BufferedWriter out = null;
 %0A   
@@ -1009,24 +1009,25 @@
 Out, true);%0A
+%0A
             
@@ -1026,16 +1026,35 @@
         
+try(BufferedWriter 
 out = ne
@@ -1078,18 +1078,24 @@
 fstream)
-;%0A
+) %7B%0A    
         
@@ -1137,24 +1137,28 @@
             
+    
 geneList.for
@@ -1167,16 +1167,20 @@
 ch(g -%3E%0A
+    
         
@@ -1209,32 +1209,36 @@
                 
+    
 try %7B%0A          
@@ -1251,24 +1251,28 @@
             
+  
+  
 String conve
@@ -1363,24 +1363,28 @@
             
+    
 finalOut.wri
@@ -1418,32 +1418,36 @@
                 
+ 
+   
 %7D catch (IOExcep
@@ -1488,29 +1488,59 @@
     
-e.printStackTrace();%0A
+    log.error(%22Exception in conversion %7B%7D%22,e);%0A    
     
@@ -1573,34 +1573,40 @@
                 
-%7D%0A
+    %7D%0A  
             );%0A 
@@ -1601,19 +1601,25 @@
         
+  
 );%0A
+    
         
@@ -1647,28 +1647,124 @@
         
-out.close();
+%7D%0A            catch(Exception e)%7B%0A                log.error(%22Bufferedwriter exception %7B%7D%22, e);%0A            %7D
 %0A       
@@ -1965,54 +1965,8 @@
 ();%0A
-                if (out != null) out.close();%0A
     
@@ -2020,27 +2020,36 @@
     
-ex.printStackTrace(
+log.error(%22Exception %7B%7D%22, ex
 );%0A 

Fix sonarqube flagged bugs