/*
 *    GS2SolrRetrieve.java
 *    Copyright (C) 2026 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;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.greenstone.gsdl3.core.GSException;
import org.greenstone.gsdl3.util.GSXML;
import org.greenstone.gsdl3.util.SolrCoreManager;
import org.greenstone.gsdl3.util.SolrTextWrapper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.NodeList;


public class GS2SolrRetrieve
    extends GS2LuceneRetrieve
{
    
  static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2SolrRetrieve.class.getName());

  protected SolrTextWrapper solr_src = null;
  protected String corename = null;

  // we'll use the core manager even though we'll only ever load in one core
  protected SolrCoreManager core_manager = null;
  
  public GS2SolrRetrieve() {
    super();
    this.core_manager = new SolrCoreManager();
    this.solr_src = new SolrTextWrapper();
  }
  
  public boolean configure(Element info, Element extra_info) {
    if (!super.configure(info, extra_info))
    {
      return false;
    }
    logger.info("Configuring "+getClass().getSimpleName()+"...");

    // don't really want to do this each configure, but we can't do it in the constructor
    core_manager.initialise(this.router.getSiteName(), this.cluster_name);
    NodeList core_list = info.getElementsByTagName("solrcore");
    if (core_list.getLength()==1) {
      Element core = (Element)core_list.item(0);
      corename = core.getAttribute(GSXML.NAME_ATT);
    } else {
      logger.error("buildConfig.xml should have one solrcore listed for GS2SolrRetrieve for "+this.cluster_name+"!");
      return false;
    }

    // clear the map of any previous solr cores in case its a reconfigure
    core_manager.cleanUp();

    ArrayList<String> cores = new ArrayList<String>();
    cores.add(corename);
    core_manager.setCorenames(cores);
    if (!core_manager.checkSolrCores()) {
      return false;
    }
    this.solr_src.setSolrCore(core_manager.getCore(corename));
    this.solr_src.initialise();
    
    return true;
    
  }
  
  public void cleanUp()
  {
    
    super.cleanUp();
    //this.solr_src.cleanUp();
    this.core_manager.cleanUp();
  }

  @Override
    protected Element getNodeContent(Document doc, String doc_id, String lang) throws GSException
  {
    
    String[] args = new String[1];
    args[0] = doc_id;
    String doc_content = getTextString("TextRetrievalError", lang, args);
    try {

      String text = this.solr_src.getText(doc_id); 
      if (text != null) {
        doc_content = resolveTextMacros(text, doc_id, lang);
      }

    }
    catch (Exception e)
    {
      
      logger.error("Error trying to get document text for " + doc_id + " in collection " + this.cluster_name + ": " + e);
      throw new GSException("Error trying to get document text for " + doc_id + " in collection " + this.cluster_name + ": " + e);
    }

    Element content_node = doc.createElement(GSXML.NODE_CONTENT_ELEM);
    Text t = doc.createTextNode(doc_content);
    content_node.appendChild(t);
    return content_node;
  }
    
}

