package com.opentransport.rdfmapper.nmbs;

import com.opentransport.rdfmapper.nmbs.containers.Station;
import com.opentransport.rdfmapper.nmbs.containers.StationInfo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Nicola De Clercq
 */
public class StationDatabase {
    
    private Map<String,StationInfo> database;
    private Map<String,String> mapping;
    private static volatile StationDatabase stationDB;
    private HashMap Stations = new HashMap();
    
    private StationDatabase() {
        database = new HashMap<>();
        mapping = new HashMap<>();
        JSONParser parser = new JSONParser();
        try {
            FileReader fr = new FileReader("NMBS_stations.json");
            JSONObject json = (JSONObject) parser.parse(fr);
            JSONArray stations = (JSONArray) json.get("stations");
            for (int i = 0; i < stations.size(); i++) {
                JSONObject station = (JSONObject) stations.get(i);
                String name = (String) station.get("name");
                String id = (String) station.get("id");
                String longitude = (String) station.get("longitude");
                String latitude = (String) station.get("latitude");
                database.put(id,new StationInfo(name,id,longitude,latitude));
                mapping.put(name,id);
            }
            fr.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StationDatabase.class.getName()).log(Level.SEVERE,null,ex);
        } catch (IOException | ParseException ex) {
            Logger.getLogger(StationDatabase.class.getName()).log(Level.SEVERE,null,ex);
        }
    }
    
    public static StationDatabase getInstance() {
        if (stationDB == null) {
            stationDB = new StationDatabase();
        }
        return stationDB;
    }
    
    public StationInfo getStationInfo(String stationId) {
        return database.get(stationId);
    }
    
    public String getStationId(String stationName) {
        return mapping.get(stationName);
    }
    
    public List<String> getAllStationIds() {
        return new ArrayList<>(database.keySet());
    }
    
    public List<String> getAllStationIdsFromGTFSFeed(){
        try {
            readTxtGetAllStations();
            return new ArrayList<>(Stations.keySet());
        } catch (IOException ex) {
            Logger.getLogger(StationDatabase.class.getName()).log(Level.SEVERE, null, ex);
            return new ArrayList<>(database.keySet());
        }       
    }
    
    private void readTxtGetAllStations()throws FileNotFoundException, IOException{
        
    FileCleaner fc = new FileCleaner() ;   
    fc.cleanUpFile("stops",".txt");
    BufferedReader in = new BufferedReader(new FileReader("stops.txt"));
    String line;
    int lineCounter=0;
    while((line = in.readLine()) != null)
    {
        if (lineCounter > 0) {
            
            String[] parts = line.split(",");
            String[] stationID = parts[0].split(":");            
            String statId = stationID[1];
            Station station = new Station(stationID[1], parts[1],parts[2],parts[3]);        
            Stations.put(station.getId(), station);
        }
        
        lineCounter++;
    }
    in.close(); 
    }
    
    
}
@@ -676,20 +676,16 @@
 abase %7B%0A
-    
 %0A    pri
@@ -864,20 +864,16 @@
 hMap();%0A
-    
 %0A    pri
@@ -2040,28 +2040,24 @@
     %7D%0A    %7D%0A
-    
 %0A    public 
@@ -2209,36 +2209,32 @@
 tationDB;%0A    %7D%0A
-    
 %0A    public Stat
@@ -2314,36 +2314,32 @@
 ationId);%0A    %7D%0A
-    
 %0A    public Stri
@@ -2415,36 +2415,32 @@
 ionName);%0A    %7D%0A
-    
 %0A    public List
@@ -2522,28 +2522,24 @@
 t());%0A    %7D%0A
-    
 %0A    public 
@@ -2883,26 +2883,15 @@
    %7D
-       
 %0A    %7D%0A
-    
 %0A   
@@ -2970,24 +2970,16 @@
 eption%7B%0A
-        
 %0A    Fil
@@ -3011,19 +3011,16 @@
 aner() ;
-   
 %0A    fc.
@@ -3148,24 +3148,30 @@
   in
-t lineCounter=0;
+.readLine(); // header
 %0A   
@@ -3231,39 +3231,27 @@
 if (
+!
 line
-Counter %3E 0) %7B%0A            
+.equals(%22%22)) %7B
 %0A   
@@ -3346,16 +3346,17 @@
 it(%22:%22);
+%0A
         
@@ -3351,33 +3351,100 @@
 %22);%0A            
-%0A
+// Don't add platforms%0A            if (stationID.length == 1) %7B%0A    
             Stri
@@ -3465,17 +3465,17 @@
 ationID%5B
-1
+0
 %5D;%0A     
@@ -3477,24 +3477,28 @@
             
+  
+  
 Station stat
@@ -3525,17 +3525,17 @@
 ationID%5B
-1
+0
 %5D, parts
@@ -3557,25 +3557,21 @@
 rts%5B3%5D);
-    
+%0A
     
-%0A
         
@@ -3621,27 +3621,22 @@
 %0A       
- %7D%0A
      
-   
+%7D
 %0A       
@@ -3640,22 +3640,9 @@
     
-lineCounter++;
+%7D
 %0A   
@@ -3663,23 +3663,15 @@
 e();
- 
 %0A    %7D%0A
-    %0A    
+%0A
 %0A%7D
+%0A

Fix reading stops.txt