/*
 *    GS3MGSearch.java
 *    Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
 *
 *    This program is free software; you can redistribute it and/or modify
 *   the Free Software Foundation; either version 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.greenstone.gsdl3.service;

// Greenstone classes
import org.greenstone.mg.*;
import org.greenstone.gsdl3.util.GSXML;
import org.greenstone.gsdl3.util.GS3OID;
import org.greenstone.gsdl3.util.SQLQuery;

// XML classes
import org.w3c.dom.Document;
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList;

// java classes
import java.io.File;


/**
 *
 * @author <a href="mailto:kjdon@cs.waikato.ac.nz">Katherine Don</a>
 */

public class GS3MGSearch 
    extends AbstractMGSearch 
{
    protected SQLQuery database = null;
    /** the base index prefix */
    protected String base_index_prefix = null;


    /** constructor */
    public GS3MGSearch()
    {
	this.database = new SQLQuery();
    }

    public void cleanUp() {
	super.cleanUp();
	this.database.closeConnection();
    }

    /** configure this service */
    public boolean configure(Element info, Element extra_info)
    {
	if (!super.configure(info, extra_info)){
	    return false;
	}

	// get the base prefix
	Element def = (Element) GSXML.getChildByTagName(info, "baseIndexPrefix");
	if (def != null) {
	    this.base_index_prefix = def.getAttribute(GSXML.NAME_ATT);
	}
	if (this.base_index_prefix == null || this.base_index_prefix.equals("")) {
	    System.err.println("Error: base index prefix not specified!");
	    return false;
	}
	String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
	if (site_name.equals("")) {
	    System.err.println("GS3Search Error: Cannot extract the site name from site home: "+this.site_home);
	    return false;
	}
	if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
	    System.err.println("GS3Search Error: Could not open SQL database!");
	    return false;
	}

	return true;
    }
    /** returns the document type of the doc that the specified node 
	belongs to. should be one of 
	GSXML.DOC_TYPE_SIMPLE, 
	GSXML.DOC_TYPE_PAGED, 
	GSXML.DOC_TYPE_HIERARCHY
    */
    protected String getDocType(String node_id){
	boolean hierarchical = false;
	if (!GS3OID.isDocTop(node_id) || database.isHierarchicalDocument(node_id) ) {
	    hierarchical = true;
	} 
	// what about paged???
	if (!hierarchical) {
	    // a simple document
	    return GSXML.DOC_TYPE_SIMPLE;
	} else {
	    // a hierarchical doc
	    return GSXML.DOC_TYPE_HIERARCHY;
	}
	// don't have paged yet.

    }
    
    /** returns true if the node has child nodes */
    protected boolean hasChildren(String node_id){
	return database.documentHasChildren(node_id);
    }
    
    /** returns true if the node has a parent */
    protected boolean hasParent(String node_id){
	String parent = GS3OID.getParent(node_id);
	if (parent.equals(node_id)) {
	    return false;
	}
	return true;
    }
    
   /** convert MG internal id to Greenstone oid */
    protected String MGNum2OID(long docnum)
    {
	String mg_id = this.base_index_prefix+"."+docnum;
	String doc_id = database.MGNum2OID(mg_id);
	// hack !!!
	if (doc_id.endsWith("-All")) {
	    // get rid of the All bit
	    doc_id = doc_id.substring(0,doc_id.length()-4);
	    //doc_id=doc_id.replaceAll("All","1");
	}
	return doc_id;
	
    }

}


