001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.setup.db.update6to7;
029
030import org.opencms.setup.CmsSetupDb;
031import org.opencms.setup.db.A_CmsUpdateDBPart;
032
033import java.io.IOException;
034import java.sql.SQLException;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.HashMap;
038import java.util.Iterator;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * This class drops the outdated tables from the OpenCms database.<p>
044 *
045 * These tables are
046 * CMS_SYSTEMID
047 * CMS_TASK
048 * CMS_TASKLOG
049 * CMS_TASKPAR
050 * CMS_TASKTYPE
051 * TEMP_PROJECT_UUIDS
052 *
053 * @since 7.0.0
054 */
055public class CmsUpdateDBDropUnusedTables extends A_CmsUpdateDBPart {
056
057    /** Constant for the SQL query to drop a table.<p> */
058    private static final String QUERY_DROP_TABLE = "Q_DROP_TABLE";
059
060    /** Constant for the SQL query properties.<p> */
061    private static final String QUERY_PROPERTY_FILE = "cms_drop_unused_tables_queries.properties";
062
063    /** Constant for the replacement of the tablename in the sql query.<p> */
064    private static final String REPLACEMENT_TABLENAME = "${tablename}";
065
066    /** Constant array with the unused tables.<p> */
067    private static final String[] UNUSED_TABLES = {
068        "CMS_SYSTEMID",
069        "CMS_TASK",
070        "CMS_TASKLOG",
071        "CMS_TASKPAR",
072        "CMS_TASKTYPE",
073        "TEMP_PROJECT_UUIDS"};
074
075    /** Constant ArrayList of the unused tables that are to be dropped.<p> */
076    private static final List<String> UNUSED_TABLES_LIST = Collections.unmodifiableList(Arrays.asList(UNUSED_TABLES));
077
078    /**
079     * Constructor.<p>
080     *
081     * @throws IOException if the sql queries properties file could not be read
082     */
083    public CmsUpdateDBDropUnusedTables()
084    throws IOException {
085
086        super();
087        loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
088    }
089
090    /**
091     * @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
092     */
093    @Override
094    protected void internalExecute(CmsSetupDb dbCon) throws SQLException {
095
096        System.out.println(new Exception().getStackTrace()[0].toString());
097        for (Iterator<String> it = UNUSED_TABLES_LIST.iterator(); it.hasNext();) {
098            String table = it.next();
099            // Check if the table to drop exists
100            if (dbCon.hasTableOrColumn(table, null)) {
101                Map<String, String> replacer = new HashMap<String, String>();
102                replacer.put(REPLACEMENT_TABLENAME, table);
103                dbCon.updateSqlStatement(readQuery(QUERY_DROP_TABLE), replacer, null);
104            } else {
105                System.out.println("table " + table + " not found");
106            }
107        }
108    }
109}