/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2009 c.Marhcand - 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.edmn.patchers;

import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.common.FileInfo;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import fr.gouv.finances.dgfip.xemelios.data.DataImpl;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TDocument;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TEtat;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TTable;
import fr.gouv.finances.dgfip.xemelios.importers.AbstractImportPatcherImpl;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;

public class RepositoryDropper extends AbstractImportPatcherImpl {

    private static final Logger logger = Logger.getLogger(RepositoryDropper.class);
    public FileInfo run() {
        try {
            DataImpl di = DataLayerManager.getImplementation();
            DocumentModel dm = Loader.getDocumentsInfos(null).getDocumentById("edmn");
            XemeliosUser user = new User();
            TDocument doc = di.getPersistenceConfig(dm, user).getLayer(di.getLayerName()).getDocument("edmn");
            for(TEtat etat:doc.getEtats()) {
                if(!etat.getId().equals("etatEdmn")) {
                    logger.debug("dropping tables for "+etat.getId());
                    recreate(etat.getDocumentTable());
                    for(TTable table:etat.getIndexTables()) recreate(table);
                } else {
                    logger.debug("skipping "+etat.getId());
                }
            }
        } catch (Exception ex) {
//            throw ex;
        }
        return new FileInfo();
    }

    private void recreate(TTable table) throws SQLException {
        Connection con = null;
        try {
            con = PoolManager.getInstance().getConnection();
            con.createStatement().execute("DROP TABLE "+table.getBaseName());
            con.createStatement().execute(table.getScript());
            con.createStatement().execute("UPDATE TABLES_VERSIONS SET VERSION="+table.getVersion()+" WHERE TABLE_NAME='"+table.getBaseName()+"'");
        } catch(SQLException sqlEx) {
            throw sqlEx;
        } finally {
            if(con!=null) PoolManager.getInstance().releaseConnection(con);
            con = null;
        }
    }

    private class User implements XemeliosUser {

        public String getId() {
            return "importer";
        }

        public String getDisplayName() {
            return getId();
        }

        public boolean hasRole(String role) {
            return true;
        }

        public boolean hasDocument(String document) {
            return true;
        }

        public boolean hasCollectivite(String collectivite, DocumentModel dm) {
            return true;
        }

    }

}
