package net.methodyne.bellvue.core;
import net.methodyne.bellvue.system.Bulk;

import java.util.Vector;
import java.util.HashMap;
import java.io.Serializable;

/**
 * The Finder is the interface to query the datastore.
 * <br>It needs the name of the bclass to search in.
 * <br>Querys can be added for each field of the queried bclass with the put method.
 * <br>This interface is used by the Bellvue GUI as well so the syntax of the query fields is the same as described in the user
 * <br>guide in the section about searching/querying.

 * @author Yuri
 * @version 1.0
 * <br>Date: 19.02.2003
 * <br>Time: 16:10:01
 * <br> copyright Methodyne GmbH, Zug - Switzerland.
 *
 */

public class Finder implements Serializable{
    public long id = -1l;

    public String title;
    /**
     * name of the Bclass with package
     */
    public String classname;
    /**
     * the Vector holding the matching objects
     */
    public Vector olist; //
    /**
     * used by GUI
     */
    public int index; //
    /**
     * used by GUI
     */
    public boolean t_o; //
    /**
     * used by GUI
     */
    public boolean f_l; //
    /**
     * here the "fieldname : query" get collected
     */
    public HashMap querys; //

    /**
     *
     * @param classname of the Bclass to query
     */
    public Finder(String classname){
        this.title = cName(classname);
        this.classname = classname;
        querys = new HashMap();
        olist = new Vector();
        index = 0;
        t_o = false; // show only object names in list
        f_l = true; // show finder
    }

/**
 * Perform a query against the datastore
 * @param sd the session data
 * @return A Vector holding the matching objects or null
 */
    public Vector run( Sdata sd){
        if(sd == null){
            System.out.println("Finder:run:Error: No session data!");
            return null;
        }
        if(classname == null) {
            System.out.println("Finder:run:Error: No bclass name!");
            return null;
        }
        if(sd.dl == null) {
            System.out.println("Finder:run:Error: No dataLayer!");
            return null;
        }
        sd.dl.fetch(this, sd);
        return olist;
    }

/**
 * Adds querys
  * @param name The name of a field from the searched Bclass
 * @param value The query for example ">2000", "100 to 200", "true", "!Smith.*"
 *  <br>To logically join querys with "and", "or", "xor" do this:
 * <pre>Finder finder = new Finder("com.demo.User");
 * finder.put("username", "John");
 * finder.put("qry_username", "and"); // lowercase for "and", "or", "xor"
 * finder.put("age", ">21");
 * finder.run(sd); // the session dataset
 * </pre>
 * This matches all objects where username is John AND the age is over 21.
 */
    public void put(String name, String value){
        querys.put(name, value);
    }

    /**
     * Shows a reasonable title to the user in the GUI
     * @return a String with the name of the Bclass and the number of matching objects
     */
    public String getTitle(){
        String ret = title;
//        if(olist.size() > 0) ret += " " + olist.size();
        return ret ;
    }

    /**
     * GUI Function to export the search result to a file
     * @param sd
     */
    public void actionExportReslut(Sdata sd) {
        if(olist.size() > 0){
            String exdir = new Bulk().doClass(olist, classname, sd) + "/" + classname;
            net.methodyne.bellvue.core.ResultObject r = new net.methodyne.bellvue.core.ResultObject();
            r.title = "Export from search result";
            r.result = "Download exported file from here: <br><a href=\"files/" + exdir + "\">file/" + exdir + "</A>";
            sd.view.add(r);
        }
    }

    private String cName(String full){
        int len = full.length();
        for(int i = len -1 ; i >=0 ; i-- ){
            if( full.charAt(i) == '.' ){
                if(i == len){
                   return full;
                }
                else{
                   return full.substring(i+1);
                }
            }
        }
        return full;
    }

}