/*
 * 
 * Created on Jun 5, 2005
 * 
 * György Süveges
 * Etixpert Kft.
 * Copyright(c) 2005
 * 
 */
package com.etixpert.evolution.gui.servlets.elements;

import javax.servlet.http.HttpSession;
import com.etixpert.evolution.*;
import java.util.*;
import java.sql.*;

/**
 * @author Suveges Gyorgy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LookupSelect extends Select {
    protected String table;
    protected String valueField;
    protected String keyField;
    protected Iterator it;
    protected Connection conn;
    /**
     * @param session
     * @param jspSite
     * @param key
     */
    public LookupSelect(HttpSession session, String jspSite, String key, String table,String keyField, String valueField) {
        super(session, jspSite, key);
        this.table =table;
        this.valueField = valueField;
        this.keyField = keyField;
    }

    /**
     * @param session
     * @param jspSite
     * @param key
     * @param defaultValue
     */
    public LookupSelect(HttpSession session, String jspSite, String key, 
            String defaultValue, String table,String keyField, String valueField) {
        super(session, jspSite, key, defaultValue);
        this.table = table;
        this.valueField = valueField;
        this.keyField = keyField;
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.elements.Select#reset()
     */
    protected void reset() throws Exception {
        ObjectSearch os = new ObjectSearch(table, keyField);
        List l;
        if (conn == null) 
            l= os.search();
        else 
            l = os.search(conn);
        
        it = l.iterator();
        
        

    }

    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.elements.Select#hasNext()
     */
    protected boolean hasNext() throws Exception {
        // TODO Auto-generated method stub
        return it.hasNext();
    }

    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.elements.Select#next()
     */
    protected String[] next() throws Exception {
        // TODO Auto-generated method stub
        PersistableObject po = (PersistableObject) it.next();
        String[] pair = new String[2];
        pair[0] = po.getString(keyField, "");
        pair[1] = po.getString(valueField, "");
        return pair;
        
    }

    /**
     * @return Returns the table.
     */
    public String getTable() {
        return table;
    }
    /**
     * @param table The table to set.
     */
    public void setTable(String table) {
        this.table = table;
    }
    /**
     * @param conn The conn to set.
     */
    public void setConnection(Connection conn) {
        this.conn = conn;
    }
}
