/*
 *    GS3MGPPRetrieve.java
 *    Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    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.mgpp.*;
import org.greenstone.gsdl3.core.GSException;
import org.greenstone.gsdl3.util.GSFile;
import org.greenstone.gsdl3.util.GSXML;
import org.greenstone.gsdl3.util.GS3OID;

// XML classes
import org.w3c.dom.Element;
import org.w3c.dom.Text;

// General Java classes
import java.io.File;

public class GS3MGPPRetrieve
    extends AbstractGS3DocumentRetrieve
{
    // Elements used in the config file that are specific to this class
    private static final String DEFAULT_LEVEL_ELEM = "defaultLevel";

    private MGPPWrapper mgpp_src = null;
    
    private String default_level = null;
    private String mgpp_textdir = null;
	
    public GS3MGPPRetrieve() {
	this.mgpp_src = new MGPPWrapper();
    }

    public void cleanUp() {
	super.cleanUp();
	this.mgpp_src.unloadIndexData();
    }

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

	// Do specific configuration
	System.out.println("Configuring GS3MGPPRetrieve...");

	// Get the default level out of <defaultLevel> (buildConfig.xml)
	Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
	if (def != null) {
	    this.default_level = def.getAttribute(GSXML.NAME_ATT);
	}
	if (this.default_level == null || this.default_level.equals("")) {
	    System.err.println("Error: default level not specified!");
	    return false;
	}
	// System.out.println("Default level: " + default_level_);
	
	// The location of the MGPP text files
	mgpp_textdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
	    File.separatorChar + GSFile.collectionTextPath(this.index_stem);

	return true;
    }
    
    /** returns the content of a node
     * should return a nodeContent element:
     * <nodeContent>text content or other elements</nodeContent>
     */
    protected Element getNodeContent(String doc_id) throws GSException {
	if (GS3OID.isDocTop(doc_id) && database.isHierarchicalDocument(doc_id)) {
	    // if we have a whole doc id, and the document is hierarchical,
	    // we want to change the id to be the top id of the section 
	    // hierarchy
	    doc_id = GS3OID.createOID(doc_id, "1");
	}

	String doc_num = this.database.OID2MGNum(doc_id);	    
	// doc nums have the index prefixed 
	doc_num = doc_num.substring(doc_num.indexOf(".")+1);
	int doc_int = Integer.parseInt(doc_num);

	Element content_node = this.doc.createElement(GSXML.NODE_CONTENT_ELEM);
	
	String doc_content = "";
	try {
	    doc_content = this.mgpp_src.getDocument(this.mgpp_textdir, 
						    this.default_level,
						    doc_int);
	    doc_content = resolveRelativeLinks(doc_content, doc_id);
	} catch (Exception e) {
	    System.out.println("exception happended with mgpp_src.getDocument()" + e);
	    doc_content = "this is the content for section hash id "+ doc_id+", mgpp doc num "+doc_int+"\n";
	}

	Text t = this.doc.createTextNode(doc_content);
	content_node.appendChild(t);
	return content_node;

    }


}
