/*
 * Copyright
 *   2009 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 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.dgfip.xemelios.batch.imports;

import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOFull;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOWrapper;
import fr.gouv.finances.dgfip.xemelios.common.PJRef;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.log4j.Logger;

/**
 *
 * @author chm
 */
public class PostImportCleaner extends Batch {
    public static final int MAX_CONCURRENT_THREADS = 3;
    private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
    private String importDirectory = null;

    public PostImportCleaner(String args[]) {
        super(args);
    }

    @Override
    protected void initialize() throws Exception {
        super.initialize();
        String sTmp = getProps().getProperty("max.concurrent.threads");
        if(sTmp!=null && sTmp.length()>0) {
            try {
                maxConcurrentThreads = Integer.parseInt(sTmp);
            } catch(Throwable t) {}
        }
    }



    @Override
    protected void doProcess() throws Exception {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<PostImportCleanerImpl> threads = new ArrayList<PostImportCleanerImpl>();
        try {
            con = PoolManager.getInstance().getConnection();
            String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
            st = con.createStatement();
            rs = st.executeQuery(sqlParam);
            if(rs.next()) {
                importDirectory = rs.getString(1);
                rs.close();
            } else {
                throw new Exception("parameter 'import.directory' not set !");
            }
            String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS IN (4,5) AND CLEANED IS NULL";
            rs = st.executeQuery(sql);
            while(rs.next()) {
                threads.add(new PostImportCleanerImpl(rs.getString(1),importDirectory));
            }
            ThreadPool pool = new ThreadPool(threads);
            pool.start(maxConcurrentThreads);
        } catch(SQLException sqlEx) {
            // TODO
        } finally {
            if(con!=null) PoolManager.getInstance().releaseConnection(con);
        }
    }

    @Override
    public String getResumeTraitement() {
        return "post-import.cleaner.resume";
    }

    @Override
    public String typeTraitementRefCode() {
        return "post-import.cleaner";

    }

    private static class PostImportCleanerImpl extends Thread {
        private static final Logger logger = Logger.getLogger(PostImportCleanerImpl.class);
        private String importId;
        private String importDirectory;
        // utilities
        private ThreadPool notifier = null;
        private ImportVOFull importFull;

        public PostImportCleanerImpl(String importId, String importDirectory) {
            super();
            this.importDirectory=importDirectory;
            this.importId=importId;
        }

        @Override
        public void run() {
            Connection con = null;
            try {
                con = PoolManager.getInstance().getConnection();
                loadImportInfos(con);
                File directory = new File(new File(importDirectory),importFull.getImportId());
                File sourceFile = new File(directory,importFull.getFileName());
                ArrayList<File> excludeFiles = new ArrayList<File>();
                if(importFull.getStatus()==5) {
                    excludeFiles.add(sourceFile);
                }
                cleanDirectory(directory,excludeFiles);
                JdbcUtils.executeUpdate(con,"UPDATE XEM_IMPORTS SET CLEANED=1 WHERE IMPORT_ID=?",new PStmtBinder.SimplePStmtBinderBuilder().add(importId).toPStmtBinder());
            } catch(SQLException sqlEx) {
                logger.error("PostImportCleanerImpl.run()",sqlEx);
            } finally {
                if(con!=null) {
                    PoolManager.getInstance().releaseConnection(con);
                }
            }
        }

        private static void cleanDirectory(File directory, List<File> excludes) {
            for(File f:directory.listFiles()) {
                if(excludes.contains(f)) continue;
                else if(f.isDirectory()) {
                    cleanDirectory(f, excludes);
                } else {
                    f.delete();
                }
            }
            if(directory.listFiles().length==0 && !excludes.contains(directory)) {
                directory.delete();
            }
        }

        @SuppressWarnings("unchecked")
        public void loadImportInfos(Connection con) throws SQLException {
            ImportVOPK pk = new ImportVOPK(importId);
            importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
//            files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class, "SELECT "+XemImportsFilesVOWrapper.getSelectFieldsClause()+" FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='"+importId+"'");
        }

        private final void setNotifier(ThreadPool pool) {
            this.notifier=pool;
        }

    }

    private static class ThreadPool {
        private static final Logger logger = Logger.getLogger(ThreadPool.class);
        private List<PostImportCleanerImpl> threads;
        private int maxConcurrentThreads;
        private int runningThreads = 0;
        public ThreadPool(List<PostImportCleanerImpl> threads) {
            super();
            this.threads=threads;
        }
        public void notifyThreadStop(PostImportCleanerImpl thread) {
            threads.remove(thread);
            runningThreads--;
            synchronized(this) {
                for(PostImportCleanerImpl aThread:threads) {
                    if(!aThread.isAlive()) {
                        aThread.setNotifier(this);
                        aThread.start();
                    }
                }
            }
        }
        public void start(int maxConcurrentThreads) {
            logger.debug("starting with "+maxConcurrentThreads+" max threads");
            this.maxConcurrentThreads=maxConcurrentThreads;
            while(runningThreads<maxConcurrentThreads && threads.size()>0) {
                for(int i=0;i<threads.size();i++) {
                    if(!threads.get(i).isAlive()) {
                        threads.get(i).setNotifier(this);
                        threads.get(i).start();
                        runningThreads++;
                    }
                }
            }
        }
    }

}
