/*
 * Created on 27-09-2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package net.sf.hibern8ide;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.hibernate.Criteria;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.impl.SessionImpl;
import bsh.EvalError;
import bsh.Interpreter;

/**
 * @author MAX
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaPage extends AbstractQueryPage {

    
    private String criteriaCode;    

    Criteria criteria = null;

    private Interpreter ip;
    
    /**
     * @param session2
     */
    public JavaPage(Session session, String criteriaCode) {
        super(session);
        this.criteriaCode =  criteriaCode;
        try {        
            ip = setupInterpreter(session);
            Object o =  ip.eval(criteriaCode);
            // ugly! TODO: make un-ugly!
            if(o instanceof Criteria) {
                criteria = (Criteria) o;
            } if (o instanceof List) {
                list = (List) o;
            } else {
                list = new ArrayList();
                list.add(o);   
            }                                                   
        } catch (EvalError e) {
            exceptions.add(e);
            release();
        } catch (HibernateException e) {
            exceptions.add(e);
            release();
        }                    
    }

    private Interpreter setupInterpreter(Session session) throws EvalError, HibernateException {
        Interpreter ip = new Interpreter();
        ip.set("session", session);
        
        SessionImplementor si = (SessionImplementor)session;
        Map map = si.getFactory().getAllClassMetadata();
        
        Iterator iterator = map.keySet().iterator();
        
        String imports = new String();
        while (iterator.hasNext()) {
            Class element = (Class) iterator.next();
            imports += "import " + element.getName() + ";\n";
        }
        
        ip.eval(imports);
        
        return ip;
    }

    /* (non-Javadoc)
     * @see net.sf.hibern8ide.IQueryPage#getList()
     */
    public List getList() {
        if(list!=null) return list;
        try {
            if(criteria!=null) {
                list = criteria.list();
            } else {
                return Collections.EMPTY_LIST;
            }
        } catch (HibernateException e) {
            exceptions.add(e);
            release();
        } 
        return list;
    }

    /* (non-Javadoc)
     * @see net.sf.hibern8ide.IQueryPage#getPathNames()
     */
    public List getPathNames() {
        List l = new ArrayList();
        l.add("<no info>");       
        return l;
    }

    /* (non-Javadoc)
     * @see net.sf.hibern8ide.IQueryPage#getQueryString()
     */
    public String getQueryString() {
        return criteriaCode;
    }

    /* (non-Javadoc)
     * @see net.sf.hibern8ide.AbstractQueryPage#release()
     */
    public void release() {
        
        super.release();
    }
}
