/*
 * 
 * Copyright
 *  2009 axYus - www.axyus.com
 *  2009 Christophe 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.controls;

import fr.gouv.finances.cp.xemelios.controls.models.DocumentControlModel;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;
import fr.gouv.finances.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.auth.UnauthorizedException;
import fr.gouv.finances.dgfip.xemelios.common.ToolException;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.data.DataAccessException;
import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Vector;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

/**
 *
 * @author Christophe Marchand <christophe.marchand@axyus.com>
 */
public class DefaultControlContentHandler extends AbstractControlContentHandler {

	private static final Logger logger = Logger.getLogger(DefaultControlContentHandler.class);

	public DefaultControlContentHandler(DocumentModel dm, DocumentControlModel docCtrlMod, Hashtable<String, Object> parameters, 
			  MainWindow frame, String file, ControlProgressListener cpl) {
		super(dm, docCtrlMod, parameters, frame, file, cpl);
	}

	public Pair getCollectivite(DocumentModel dm, String fileName) throws ToolException {
		Pair ret = null;
		if (ret == null) {
			QueryCollectivite qc = new QueryCollectivite(fileName);
			qc.setDefaultCollectivite(collectivite);
			if (SwingUtilities.isEventDispatchThread()) {
				qc.run();
			} else {
				try {
					SwingUtilities.invokeAndWait(qc);
				} catch (Throwable t) {
				}
			}
			ret = qc.getRet();
			if (ret == null) {
				throw new ToolException("Action interrompue", ToolException.ERROR_INTERRUPTED);

			}
		}
		return ret;
	}

	public Pair getBudget(DocumentModel dm, Pair collectivite, String fileName) throws ToolException, UnauthorizedException {
		Pair ret = null;
		if (ret == null) {
			TreeSet<Pair> existing = new TreeSet<Pair>();
			try {
				existing.addAll(DataLayerManager.getImplementation().getBudgets(dm, collectivite, owner.getConnectedUser()));
			} catch (DataConfigurationException e) {
				logger.error("in getBudget", e);
			} catch (DataAccessException e) {
				logger.error("in getBudget", e);
			}
			Vector<Pair> budgetsDm = dm.getDefaultBudgets();
			existing.addAll(budgetsDm);
//			if (!dm.getDefaultBudgets().isEmpty() && dm.getDefaultBudgets().firstElement() != null) {
//				existing.add(dm.getDefaultBudgets().firstElement());
//			}
			if (budget != null && (budget.libelle == null || budget.libelle.length() == 0)) {
				if (existing.contains(budget)) {
					for (Iterator<Pair> it = existing.iterator(); it.hasNext();) {
						Pair p = it.next();
						if (p.key.equals(budget.key)) {
							return p;
						}
					}
				}
			}
			QueryBudget qb = new QueryBudget(fileName, existing, collectivite.libelle);
			if (SwingUtilities.isEventDispatchThread()) {
				qb.run();
			} else {
				try {
					SwingUtilities.invokeAndWait(qb);
				} catch (Throwable t) {
				}
			}
			ret = qb.getRet();
			if (ret == null) {
				throw new ToolException("Action interrompue", ToolException.ERROR_INTERRUPTED);
			}
		}
		return ret;
	}
}
