/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.k_int.e20cl;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.CSVReader;

/**
 * Title:		ControlledVocabs
 * @author:		Rob Tice (rob.tice@k-int.com)
 * @version:	$Id: ControlledVocabs.java,v 1.5 2006/09/15 14:44:11 rob Exp $
 * Copyright:	Copyright 1999-2006 Knowledge Integration Ltd
 * Company: 	Knowledge Integration Ltd
 * Description:
 * 
 * 
 *
 * Created:		Jun 6, 2006
 * 
 * 
 * History
 *				$Log: ControlledVocabs.java,v $
 *				Revision 1.5  2006/09/15 14:44:11  rob
 *				release
 *				
 *				Revision 1.4  2006/06/27 23:20:20  rob
 *				filenames changed
 *				
 *				Revision 1.3  2006/06/22 13:03:18  rob
 *				changed indexing to UPPER
 *				
 *				Revision 1.2  2006/06/12 18:01:05  rob
 *				Allow for case insensitive match (a bit copy and paste but needs must :))
 *				
 *				Revision 1.1  2006/06/08 12:43:16  rob
 *				extensions for e20cl
 *				
 * 		
 */

public class ControlledVocabs
{
    public static HashMap subjects;
    public static HashMap people;
    public static HashMap events;
    public static HashMap places;
    public static HashMap themes;
    public static HashMap  orgs;
   
    static
    {
        try
        {
            CSVReader reader = new CSVReader(new FileReader("vocabs/subject.csv"));
            List subjects_array = reader.readAll();
            subjects=new HashMap();
            Iterator i = subjects_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
                subjects.put(list[0].toUpperCase().trim(),list[0].trim());
            }
            
            reader = new CSVReader(new FileReader("vocabs/person.csv"));
            List people_array = reader.readAll();
            people=new HashMap();
            i = people_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
                people.put(list[0].toUpperCase().trim(),list[0].trim());
            }
            
            reader = new CSVReader(new FileReader("vocabs/event.csv"));
            List events_array = reader.readAll();
            events=new HashMap();
            i = events_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
                events.put(list[0].toUpperCase().trim(),list[0].trim());
            }
            
            reader = new CSVReader(new FileReader("vocabs/place.csv"));
            List places_array = reader.readAll();
            places=new HashMap();
            i = places_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
                places.put(list[0].toUpperCase().trim(),list[0].trim());
            }
            
            reader = new CSVReader(new FileReader("vocabs/theme.csv"));
            List themes_array = reader.readAll();
            themes=new HashMap();
            i = themes_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
                themes.put(list[0].toUpperCase().trim(),list[0].trim());
            }
            
            reader = new CSVReader(new FileReader("vocabs/organisation.csv"));
            List orgs_array = reader.readAll();
            orgs=new HashMap();
            i = orgs_array.iterator();
            
            while(i.hasNext())
            {
                String[] list = (String[]) i.next();
               // System.out.println("Static initilaiser adding "+list[0].toUpperCase().trim());
                orgs.put(list[0].toUpperCase().trim(),list[0].trim());
            }
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    
    public static VocabBundle getPlace(String place)
    {
        return testMatch(place, places);
       /** String vocab_string = (String) places.get(place.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
            bundle.setStatus(VocabBundle.NO_MATCH);
        else if(place.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
    public static VocabBundle getEvent(String event)
    {
        return testMatch(event, events);
        /**String vocab_string = (String) events.get(event.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
            bundle.setStatus(VocabBundle.NO_MATCH);
        else if(event.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
   
    public static VocabBundle getPerson(String person)
    {
        return testMatch(person, people);
       /** String vocab_string = (String) people.get(person.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
            bundle.setStatus(VocabBundle.NO_MATCH);
        else if(person.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
    public static VocabBundle getSubject(String subject)
    {
        return testMatch(subject, subjects);
        /**String vocab_string = (String) subjects.get(subject.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
            bundle.setStatus(VocabBundle.NO_MATCH);
        else if(subject.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
    public static VocabBundle getTheme(String theme)
    {
        return testMatch(theme, themes);
        /**String vocab_string = (String) themes.get(theme.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
        {
            bundle.setStatus(VocabBundle.NO_MATCH);
            String trim = theme.trim();
        }
        else if(theme.equals(vocab_string))
        {
            bundle.setControlledTerm(vocab_string);
            bundle.setStatus(VocabBundle.MATCH);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
    public static VocabBundle getOrg(String org)
    {
        return testMatch(org, orgs);
        //System.out.println("Looking for "+org.toUpperCase());
        /**String vocab_string = (String) orgs.get(org.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
        {
           // System.out.println("Falied to match in controlled vocabs retriever");
            bundle.setStatus(VocabBundle.NO_MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else if(org.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_CASE_INSENSITIVE);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;**/
    }
    
    
    private static VocabBundle testMatch(String value, Map values)
    {
        String vocab_string = (String) values.get(value.toUpperCase());
        VocabBundle bundle=new VocabBundle();
      
        if(vocab_string==null)
        {
           // System.out.println("Falied to match in controlled vocabs retriever");
            bundle.setStatus(VocabBundle.NO_MATCH);
            bundle.setControlledTerm(vocab_string);           
            String trim = value.trim();
            if(value.equals(trim)==false)
            {
                value=trim;
                bundle = testMatch(value, values);
                if(bundle.getStatus()==VocabBundle.EXACT_MATCH)
                    bundle.setStatus(VocabBundle.MATCH_WITH_EDIT); // force replacement               
            }            
            if(bundle.getStatus()==VocabBundle.NO_MATCH)
            {
                if(value.indexOf("&")!=-1)
                {
                    value = value.replace("&", "and");
                    bundle = testMatch(value, values);
                }
                
                if(bundle.getStatus()==VocabBundle.EXACT_MATCH)
                    bundle.setStatus(VocabBundle.MATCH_WITH_EDIT);                
            }                            
        }
        else if(value.equals(vocab_string))
        {
            bundle.setStatus(VocabBundle.EXACT_MATCH);
            bundle.setControlledTerm(vocab_string);
        }
        else
        {
            bundle.setStatus(VocabBundle.MATCH_WITH_EDIT);
            bundle.setControlledTerm(vocab_string);
        }
        return bundle;
    }
    
 
}
