/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *  2009 Laurent Meckert <laurent.meckert@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.cp.xemelios.controls.editors;

import java.util.Hashtable;
import java.math.BigDecimal;
import java.io.FileOutputStream;







import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;



import java.io.File;

/**
 *
 * @author laurent.meckert
 */
public class ControllerTester {

   protected static Hashtable<String, Object> getControlParameters() {
      // in control config file, param types are
      //  string  -> java.lang.String
      //  boolean -> java.lang.Boolean
      //  decimal -> java.math.BigDecimal
      //  integer -> java.lang.Integer
      //
      // multi-valued params are String arrays (java.lang.String[]) values


      // here, filled with default values from PES as an example
      Hashtable<String, Object> ret = new Hashtable<String, Object>();
      ret.put("nomencl", "M14");   // values can be M14, M21, M22, M31, M52, M61, M71
      ret.put("typnomencl", "Nature"); // values can be Nature, Fonction, Nature-ref-fonct
      ret.put("votop", Boolean.FALSE);
      ret.put("suivdgp", Boolean.TRUE);
      ret.put("dureedgp", new Integer(45));
      ret.put("coltva", Boolean.TRUE);
      ret.put("servTVA", new String[]{}); // list of services
      ret.put("compteTVA", new String[]{}); // list of comptes
      ret.put("tauxTVA", new String[]{"2.1", "5.5", "19.6"}); // String array of VAT rates
      ret.put("seuilMax", new BigDecimal(90000));
      ret.put("seuilEmission", new BigDecimal(10));
      ret.put("codProd", new String[]{"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80",
                 "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
                 "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "248", "300", "302", "305",
                 "306", "307", "308"}); // String array of product codes
      return ret;
   }

   public static void main(String[] args) {
      try {
         File fileToRead = new File("P:/laurent.meckert/FichiersPES/PES_DEP_exemple_1000.xml");
         Controller controller = new Controller("PES_Aller");
         Hashtable<String, Document> result = controller.controle(fileToRead, getControlParameters());

         for (String k : result.keySet()) {
            Document doc = result.get(k);
          
            try {

               FileOutputStream f = new FileOutputStream(k + ".xml");

               TransformerFactory tf = TransformerFactory.newInstance();
               Transformer t = tf.newTransformer();
               t.setOutputProperty("encoding", "ISO-8859-1");
               t.setOutputProperty("method", "xml");
               t.setOutputProperty("indent", "yes");
               t.transform(new DOMSource(doc), new StreamResult(f));

               f.flush();
               f.close();

            } catch (Exception e) {
               e.printStackTrace();
         //

            } catch (Error e) {
               e.printStackTrace();


            }
         }

      } catch (Throwable t) {
         t.printStackTrace();
      }
   }
}
