/*
 * Copyright
 *   2007 axYus - www.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.batch.exports.postProcessors;

import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
import java.io.File;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;

/**
 *
 * @author Christophe Marchand <christophe.marchand@axyus.com>
 */
public class ExcelRunner implements PostProcessor {
    private static final Logger logger = Logger.getLogger(ExcelRunner.class);
    private ExportJob job;

    public ExcelRunner() {
        super();
    }

    public void setConfigData(Object data) {
        job = (ExportJob)data;
    }

    public void postProcess() throws Exception {
        if(job==null) return;
        File f = new File(job.getGeneratedFileName());
        if(!f.exists()) return;
        if(!f.getName().toUpperCase().endsWith(".XLS")) return;

        String osName = System.getProperty("os.name");
        String url = f.toURL().toExternalForm();
        try {
            if (osName.startsWith("Mac OS")) {
                // Mac OS X can handle file types itself (thank  goodness!)
                Class fileMgr = Class.forName("com.apple.eio.FileManager");
                Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] {String.class});
                openURL.invoke(null, new Object[] {url});
            } else if (osName.startsWith("Windows")) { // who knows  what Windows does ...
                // il y a un bug avec Windows 2000, il faut que le fichier commence par file:// au lieu de file:/
                if("Windows 2000".equals(System.getProperty("os.name")) || "Windows 98".equals(System.getProperty("os.name"))) {
                    if(url.startsWith("file:/") && !url.startsWith("file://"))
                        url = "file://".concat(url.substring(6));
                }
                Process p = Runtime.getRuntime().exec("rundll32  url.dll,FileProtocolHandler " + url);
                // we must synchronize this
                p.waitFor();
            } else {
                logger.error("This post processor - ExcelRunner - can only be used on Windows and MacOS. You are using "+osName);
            }
        } catch (Exception e) {
            logger.error(e);
            throw e;
        }

    }

}
