/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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.
 *
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.cp.xemelios.ui.search;

import java.util.Vector;

import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import org.jdesktop.swingworker.SwingWorker;

import fr.gouv.finances.cp.utils.Pair;
import fr.gouv.finances.cp.utils.ui.DisplayExceptionDlg;
import fr.gouv.finances.cp.xemelios.common.config.CritereModel;
import fr.gouv.finances.cp.xemelios.common.config.ElementModel;
import fr.gouv.finances.cp.xemelios.common.config.ListeResultatModel;
import fr.gouv.finances.cp.xemelios.data.DataLayerManager;
import fr.gouv.finances.cp.xemelios.data.DataResultSet;
import fr.gouv.finances.cp.xemelios.ui.SearchWindow;

/**
 * @author chm
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class XPathExecuter extends SwingWorker<DataResultSet, String> {
    private static Logger logger=Logger.getLogger(XPathExecuter.class);
    private ListeResultatModel lrm;

    private long start;
    private String xPathQuery;

    private ElementModel elementModel;
    private Pair collectivite,budget;
    private Vector<CritereModel> models = null;

    private SearchWindow sw;

    private Throwable errorWhileSearching;

    public XPathExecuter(ElementModel elementModel, String xPathQuery, Vector<CritereModel> models, Pair collectivite, Pair budget, ListeResultatModel lrm, long start, SearchWindow sw) {
        super();
        this.xPathQuery = xPathQuery;
        this.models=models;
        this.lrm = lrm;
        this.start = start;
        this.elementModel = elementModel;
        this.collectivite=collectivite;
        this.budget=budget;
        this.sw = sw;
    }
    @Override
    public DataResultSet doInBackground() {
        try {
            // TODO: return the search result
            logger.debug("XQuery="+xPathQuery);
            DataResultSet rs = DataLayerManager.getImplementation().search(elementModel,collectivite,budget,xPathQuery,lrm,models);
            return rs;
        }
        catch (Throwable t) {
            logger.error("une erreur est survenue.",t);
            try {
                errorWhileSearching = t;
                // on error, try to return an empty ResultSetClient
                return DataLayerManager.getImplementation().getEmptyDataResultSet();
            }
            catch (Throwable t2) {}
        }
        return null;
    }
    @Override
    public void done() {
        try {
            if (errorWhileSearching != null) {
                Runnable code = new Runnable() {
                    public void run() {
                        DisplayExceptionDlg.displayException(sw.parentFrame, errorWhileSearching);
                    }
                };
                if (SwingUtilities.isEventDispatchThread())
                    code.run();
                else SwingUtilities.invokeLater(code);
            } else {
                DataResultSet rs = get();
                Runnable code = new ResultDisplayerThread(rs, lrm, start, elementModel, sw);
                if (SwingUtilities.isEventDispatchThread())
                    code.run();
                else try {
                    SwingUtilities.invokeAndWait(code);
                }
                catch (Throwable t) {}
            }
            firePropertyChange("COMPLETED", Boolean.FALSE, Boolean.TRUE);
        }
        catch (Throwable t) {
            DisplayExceptionDlg.displayException(sw.parentFrame, t);
        }
        finally {
            sw.parentFrame.stopWaiter();
        }
    }
}
