/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2009 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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.dgfip.xemelios.web.imports;

import fr.gouv.finances.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.services.XemeliosHelper;
import fr.gouv.finances.dgfip.xemelios.web.AttributeHelper;
import fr.gouv.finances.dgfip.xemelios.web.utils.StdDispatchAction;
import java.util.TreeSet;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author chm
 */
public class GetBudgetsAction extends StdDispatchAction {
    @Override
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        XemeliosUser user = AttributeHelper.getUser(request.getSession());
        String docId = request.getParameter("docId");
        String collectiviteId = request.getParameter("collectivite");
        DocumentsModel dms = XemeliosHelper.getDocumentsModel();
        DocumentModel dm = dms.getDocumentById(docId);
        Vector<Pair> existingBudgets = DataLayerManager.getImplementation().getBudgets(dm, new Pair(collectiviteId,""), user);
        TreeSet<Pair> budgets = new TreeSet<Pair>();
        budgets.addAll(dm.getDefaultBudgets());
        for(Pair p:existingBudgets) budgets.add(p);
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
        Document dom = domBuilder.newDocument();
        Element root = dom.createElement("budgets");
        for(Pair p:budgets) {
            Element budget = dom.createElement("budget");
            budget.setAttribute("value", p.key);
            budget.appendChild(dom.createTextNode(p.libelle));
            root.appendChild(budget);
        }
        dom.setXmlStandalone(true);
        dom.setXmlVersion("1.0");
        dom.appendChild(root);
        TransformerFactory tf = net.sf.saxon.TransformerFactoryImpl.newInstance();
        Transformer t = tf.newTransformer();
        response.setContentType("text/xml; charset=ISO-8859-1");
        t.transform(new DOMSource(dom), new StreamResult(response.getOutputStream()));
        return null;
    }
}
