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.mysql;
029
030import org.opencms.setup.CmsSetupDb;
031
032import java.io.IOException;
033import java.sql.SQLException;
034import java.util.Collections;
035import java.util.Map;
036
037/**
038 * This class updates the project ids from integer values to CmsUUIDs in all existing database tables.<p>
039 *
040 * It creates new UUIDs for each existing project and stores it into a temporary table.<p>
041 *
042 * For each table using a project id a new column for the UUID is added and the according data is transferred.<p>
043 * After that the original indexes and the column for the project id index is dropped and the new column with the
044 * project uuid becomes the primary key.<p>
045 */
046public class CmsUpdateDBProjectId extends org.opencms.setup.db.update6to7.CmsUpdateDBProjectId {
047
048    /** Constant for the sql query to create the new CMS_HISTORY_PROJECTS table.<p> */
049    private static final String QUERY_CREATE_HISTORY_PROJECTS_TABLE_MYSQL = "Q_CREATE_HISTORY_PROJECTS_TABLE_MYSQL";
050
051    /** Constant for the sql query to create the temporary table.<p> */
052    private static final String QUERY_CREATE_TEMP_TABLE_UUIDS_MYSQL = "Q_CREATE_TEMPORARY_TABLE_UUIDS_MYSQL";
053
054    /** Constant for the SQL query properties.<p> */
055    private static final String QUERY_PROPERTY_FILE = "cms_projectid_queries.properties";
056
057    /**
058     * Constructor.<p>
059     *
060     * @throws IOException if the query properties cannot be read
061     */
062    public CmsUpdateDBProjectId()
063    throws IOException {
064
065        super();
066        loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
067    }
068
069    /**
070     * Creates the CMS_HISTORY_PROJECTS table if it does not exist yet.<p>
071     *
072     * @param dbCon the db connection interface
073     *
074     * @throws SQLException if something goes wrong
075     */
076    @Override
077    protected void createHistProjectsTable(CmsSetupDb dbCon) throws SQLException {
078
079        System.out.println(new Exception().getStackTrace()[0].toString());
080        if (!dbCon.hasTableOrColumn(HISTORY_PROJECTS_TABLE, null)) {
081            String createStatement = readQuery(QUERY_CREATE_HISTORY_PROJECTS_TABLE_MYSQL);
082            Map<String, String> replacer = Collections.singletonMap("${tableEngine}", m_poolData.get("engine"));
083            dbCon.updateSqlStatement(createStatement, replacer, null);
084            transferDataToHistoryTable(dbCon);
085        } else {
086            System.out.println("table " + HISTORY_PROJECTS_TABLE + " already exists");
087        }
088    }
089
090    /**
091     * Creates the temp table for project ids if it does not exist yet.<p>
092     *
093     * @param dbCon the db connection interface
094     *
095     * @throws SQLException if something goes wrong
096     */
097    @Override
098    protected void createTempTable(CmsSetupDb dbCon) throws SQLException {
099
100        System.out.println(new Exception().getStackTrace()[0].toString());
101        if (!dbCon.hasTableOrColumn(TEMPORARY_TABLE_NAME, null)) {
102            String createStatement = readQuery(QUERY_CREATE_TEMP_TABLE_UUIDS_MYSQL);
103            Map<String, String> replacer = Collections.singletonMap("${tableEngine}", m_poolData.get("engine"));
104            dbCon.updateSqlStatement(createStatement, replacer, null);
105        } else {
106            System.out.println("table " + TEMPORARY_TABLE_NAME + " already exists");
107        }
108    }
109
110}