/*===========================================================================
  Copyright (C) 2010 by the Okapi Framework contributors
-----------------------------------------------------------------------------
  This library is free software; you can redistribute it and/or modify it 
  under the terms of the GNU Lesser General Public License as published by 
  the Free Software Foundation; either version 2.1 of the License, or (at 
  your option) any later version.

  This library 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 Lesser 
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the Free Software Foundation, 
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

  See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
===========================================================================*/

package net.sf.okapi.lib.terminology.simpletb;

import java.io.File;
import java.util.List;

import net.sf.okapi.common.IParameters;
import net.sf.okapi.common.LocaleId;
import net.sf.okapi.common.Util;
import net.sf.okapi.common.resource.TextFragment;
import net.sf.okapi.lib.terminology.ITermAccess;
import net.sf.okapi.lib.terminology.TermHit;

public class SimpleTBConnector implements ITermAccess {

	private Parameters params;
	private SimpleTB tb;
	
	public SimpleTBConnector () {
		params = new Parameters();
	}

	@Override
	public IParameters getParameters () {
		return params;
	}

	@Override
	public void setParameters (IParameters params) {
		this.params = (Parameters)params;
	}

	@Override
	public void open () {
		tb = new SimpleTB(params.getSourceLocale(), params.getTargetLocale());
		// Import from file, if a path is defined
		if ( !Util.isEmpty(params.getGlossaryPath()) ) {
			tb.importTBX(new File(params.getGlossaryPath()));
		}
	}

	@Override
	public void close() {
		tb.removeAll();
	}

	@Override
	public List<TermHit> getExistingTerms (TextFragment fragment,
		LocaleId fragmentLocId,
		LocaleId otherLocId)
	{
		return tb.getExistingTerms(fragment, fragmentLocId, otherLocId);
	}
	
	@Override
	public List<TermHit> getExistingStrings (TextFragment fragment,
		LocaleId fragmentLocId,
		LocaleId otherLocId)
	{
		return tb.getExistingStrings(fragment, fragmentLocId, otherLocId);
	}
	
	public void initializeSearch(boolean stringSearch,
		boolean betweenCodes)
	{
		tb.initialize(stringSearch, betweenCodes);
	}
	
	public Entry addEntry (String srcTerm,
		String trgTerm)
	{
		return tb.addEntry(srcTerm, trgTerm);
	}

}
