/*
 * Copyright
 *  2010 axYus - http://www.axyus.com
 *  2010 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS_NB 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.patches;

import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;
import fr.gouv.finances.cp.xemelios.updater.AbstractConfigurator;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.common.config.ConfigModelPatcher;
import fr.gouv.finances.dgfip.xemelios.common.config.PersonnalConfigParser;
import fr.gouv.finances.dgfip.xemelios.common.config.PersonnalConfigurationModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 * Corrige l'escape dans les recherches enregistrées. Uniquement pour les fichiers
 * @author cmarchand
 */
public class Core_4_0_0_42 extends AbstractConfigurator {
    private static final Logger logger = Logger.getLogger(Core_4_0_0_42.class);

    @Override
    public void run() throws Exception {
        File f = new File(System.getProperty("user.home"));
        File fProp = new File(f, Constants.PERSONNAL_CONFIG_FILENAME);
        ArrayList<File> filesToPatch = new ArrayList<File>();
        if (fProp.exists()) {
            filesToPatch.add(fProp);
        }
        File[] sharedFiles = f.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.startsWith(Constants.PERSONNAL_CONFIG_FILENAME.substring(0, Constants.PERSONNAL_CONFIG_FILENAME.indexOf('.'))+"-");
            }
        });
        for(File share:sharedFiles) {
            filesToPatch.add(share);
        }
        for(File ftp:filesToPatch) patchFile(ftp);
        if(MainWindow.instance!=null) MainWindow.instance.reloadPersonnalConfig();
        setPreviousConfigurator(new Core4_0_0_21());
    }

    private void patchFile(File f) {
        try {
            PersonnalConfigParser parser = new PersonnalConfigParser();
            parser.parse(f);
            PersonnalConfigurationModel sharedConfig = (PersonnalConfigurationModel) parser.getMarshallable();
            ConfigModelPatcher.patchSharedConfig(sharedConfig);
            Charset cs = Charset.forName("ISO-8859-1");
            FileOutputStream fos = new FileOutputStream(f);
            OutputStreamWriter osw = new OutputStreamWriter(fos,cs);
            XmlOutputter output = new XmlOutputter(osw, cs.name());
            sharedConfig.marshall(output);
            osw.flush();
            osw.close();
        } catch(Exception ex) {
            logger.error("patching "+f.getAbsolutePath(),ex);
        }
    }


}
