/*
 * Created on 27-Jun-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.roe.wfau;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FileDrivenWSASchema {
    static long lastModTime=0;
    //static HashMap sourceMap=new HashMap();
    static Map sourceMap=Collections.synchronizedMap(new HashMap());
    //static HashMap detectionMap=new HashMap();
    static Map detectionMap=Collections.synchronizedMap(new HashMap());
    
    static String fileName="u:/temp.csv";
    public static void main(String[] args) {
        //loadHashMaps();
        //loadHashMaps();
        for (int i=0; i<1; i++){
            try {
            Thread.sleep(1000);
            System.out.println(getSourceName("101"));
            }
            catch (Exception e){
                
            }
        }
        System.out.println(getSourceName("101"));
        System.out.println(getProgID("dxs"));
        System.out.println(getProgID("dXS"));
    }
    static {
        loadHashMaps();
    }
    public static String getSourceName(String progID){
        loadHashMaps();
        return (String)sourceMap.get(progID);
    }
    public static String getDetectionName(String progID){
        return (String)detectionMap.get(progID);
    }
    public static String getProgID(String str){
        List keys= new ArrayList(sourceMap.keySet());
        Iterator iter = keys.iterator();
        String name= null;
        while (iter.hasNext()){
            Object key=iter.next();
            if (((String)sourceMap.get(key)).matches("(?i).*"+str+".*")){
                return (String)key;
            }
        }
        return null;
        
    }
    
    public synchronized static void loadHashMaps(){
    
    File dataFile=new File(fileName);
    // on reload hashmaps is file has been updated
    if (dataFile.lastModified() > lastModTime){ 
        StringTokenizer st; 
        lastModTime=dataFile.lastModified();
        try {
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        String line;
        String [] strArray;
        while ((line = in.readLine()) != null) {
            int i=0;
            st = new StringTokenizer(line, ","); 
            strArray = new String [st.countTokens()];
            while (st.hasMoreTokens()){
                //do something with each token
                strArray[i]=st.nextToken().trim();
                
                i++; 
            }
            detectionMap.put(strArray[0],strArray[2]);
            sourceMap.put(strArray[0],strArray[3]);
          
        }
        in.close();
        System.out.println(dataFile.lastModified());
        }
        catch (IOException ioEx){
            // do nothing
        }
    }
    }
}
