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.CmsSetupDBWrapper;
031import org.opencms.setup.CmsSetupDb;
032import org.opencms.setup.db.A_CmsUpdateDBPart;
033
034import java.io.IOException;
035import java.sql.SQLException;
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.Iterator;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * This class converts the backup tables to history tables.<p>
044 *
045 * The following tables are converted
046 * CMS_BACKUP_PROJECTRESOURCES
047 * CMS_BACKUP_PROPERTIES
048 * CMS_BACKUP_PROPERTYDEF
049 * CMS_BACKUP_RESOURCES
050 * CMS_BACKUP_STRUCTURE
051 *
052 * The tables CMS_HISTORY_PRINCIPALS and CMS_HISTORY_PROJECTS are created in other classes.
053 *
054 * CMS_HISTORY_PRINCIPALS is a completely new table and is therefor handled by its own class.
055 *
056 * CMS_HISTORY_PROJECTS needs extra conversion beyond the execution of SQL statements and is
057 * also handled by a special class.
058 *
059 * @since 7.0.0
060 */
061public class CmsUpdateDBHistoryTables extends A_CmsUpdateDBPart {
062
063    /** Constant for the SQL query properties.<p> */
064    private static final String QUERY_PROPERTY_FILE = "cms_history_queries.properties";
065
066    /** Constant for the sql query to count the contents of a table.<p> */
067    private static final String QUERY_SELECT_COUNT_HISTORY_TABLE = "Q_SELECT_COUNT_HISTORY_TABLE";
068
069    /** Constant for the replacement of the tablename in the sql query. */
070    private static final String REPLACEMENT_TABLENAME = "${tablename}";
071
072    /**
073     * Constructor.<p>
074     *
075     * @throws IOException if the sql queries properties file could not be read
076     */
077    public CmsUpdateDBHistoryTables()
078    throws IOException {
079
080        super();
081        loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
082    }
083
084    /**
085     * @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
086     */
087    @Override
088    protected void internalExecute(CmsSetupDb dbCon) throws SQLException {
089
090        System.out.println(new Exception().getStackTrace()[0].toString());
091
092        List<String> elements = new ArrayList<String>();
093        elements.add("CMS_HISTORY_PROPERTYDEF");
094        if (isKeepHistory()) {
095            elements.add("CMS_HISTORY_PROJECTRESOURCES");
096            elements.add("CMS_HISTORY_PROPERTIES");
097            elements.add("CMS_HISTORY_RESOURCES");
098            elements.add("CMS_HISTORY_STRUCTURE");
099        }
100        for (Iterator<String> it = elements.iterator(); it.hasNext();) {
101            String table = it.next();
102            System.out.println("Updating table " + table);
103            if (dbCon.hasTableOrColumn(table, null)) {
104                Map<String, String> replacer = new HashMap<String, String>();
105                replacer.put(REPLACEMENT_TABLENAME, table);
106                CmsSetupDBWrapper db = null;
107                boolean update = false;
108                try {
109                    db = dbCon.executeSqlStatement(readQuery(QUERY_SELECT_COUNT_HISTORY_TABLE), replacer);
110
111                    if (db.getResultSet().next()) {
112                        if (db.getResultSet().getInt("COUNT") <= 0) {
113                            update = true;
114                        }
115                    }
116                } finally {
117                    if (db != null) {
118                        db.close();
119                    }
120                }
121                if (update) {
122                    String query = readQuery(table);
123                    dbCon.updateSqlStatement(query, null, null);
124                } else {
125                    System.out.println("table " + table + " already has data");
126                }
127            } else {
128                System.out.println("table " + table + " does not exists");
129            }
130        }
131    }
132}