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.Collections; 037import java.util.HashMap; 038import java.util.Map; 039 040/** 041 * This class creates the table CMS_CONTENTS and fills it with data from the tables CMS_BACKUP_CONTENTS and CMS_ONLINE_CONTENTS.<p> 042 * 043 * @since 7.0.0 044 */ 045public class CmsUpdateDBContentTables extends A_CmsUpdateDBPart { 046 047 /** Constant for the sql query to create the CMS_CONTENTS table.<p> */ 048 protected static final String QUERY_CREATE_CMS_CONTENTS_TABLE = "Q_CREATE_CMS_CONTENTS_TABLE"; 049 050 /** Constant for the sql query to transfer the online contents.<p> */ 051 protected static final String QUERY_TRANSFER_ONLINE_CONTENTS = "Q_TRANSFER_ONLINE_CONTENTS"; 052 053 /** Constant for the table CMS_CONTENTS.<p> */ 054 protected static final String TABLE_CMS_CONTENTS = "CMS_CONTENTS"; 055 056 /** Constant for the sql query to drop a table.<p> */ 057 private static final String QUERY_DROP_TABLE = "Q_DROP_TABLE"; 058 059 /** Constant for the SQL query properties.<p> */ 060 private static final String QUERY_PROPERTY_FILE = "cms_content_table_queries.properties"; 061 062 /** Constant for the sql query to read the max publish tag.<p> */ 063 private static final String QUERY_READ_MAX_PUBTAG = "Q_READ_MAX_PUBTAG"; 064 065 /** Constant for the sql query to transfer the backup contents.<p> */ 066 private static final String QUERY_TRANSFER_BACKUP_CONTENTS = "Q_TRANSFER_BACKUP_CONTENTS"; 067 068 /** Constant for the replacement in the SQL query for the tablename.<p> */ 069 private static final String REPLACEMENT_TABLENAME = "${tablename}"; 070 071 /** Constant for the table CMS_BACKUP_CONTENTS.<p> */ 072 private static final String TABLE_CMS_BACKUP_CONTENTS = "CMS_BACKUP_CONTENTS"; 073 074 /** Constant for the table CMS_ONLINE_CONTENTS.<p> */ 075 private static final String TABLE_CMS_ONLINE_CONTENTS = "CMS_ONLINE_CONTENTS"; 076 077 /** 078 * Constructor.<p> 079 * 080 * @throws IOException if the query properties cannot be read 081 */ 082 public CmsUpdateDBContentTables() 083 throws IOException { 084 085 super(); 086 loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE); 087 } 088 089 /** 090 * Creates the CMS_CONTENTS table if it does not exist yet.<p> 091 * 092 * @param dbCon the db connection interface 093 * 094 * @throws SQLException if something goes wrong 095 */ 096 protected void createContentsTable(CmsSetupDb dbCon) throws SQLException { 097 098 System.out.println(new Exception().getStackTrace()[0].toString()); 099 if (!dbCon.hasTableOrColumn(TABLE_CMS_CONTENTS, null)) { 100 String query = readQuery(QUERY_CREATE_CMS_CONTENTS_TABLE); 101 dbCon.updateSqlStatement(query, null, null); 102 } else { 103 System.out.println("table " + TABLE_CMS_CONTENTS + " already exists"); 104 } 105 } 106 107 /** 108 * @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb) 109 */ 110 @Override 111 protected void internalExecute(CmsSetupDb dbCon) throws SQLException { 112 113 System.out.println(new Exception().getStackTrace()[0].toString()); 114 createContentsTable(dbCon); 115 116 // Transfer the online contents if the table exists 117 if (dbCon.hasTableOrColumn(TABLE_CMS_ONLINE_CONTENTS, null)) { 118 int pubTag = 1; 119 String query = readQuery(QUERY_READ_MAX_PUBTAG); 120 CmsSetupDBWrapper db = null; 121 try { 122 db = dbCon.executeSqlStatement(query, null); 123 if (db.getResultSet().next()) { 124 pubTag = db.getResultSet().getInt(1); 125 } 126 } finally { 127 if (db != null) { 128 db.close(); 129 } 130 } 131 transferOnlineContents(dbCon, pubTag); 132 } else { 133 System.out.println("no table " + TABLE_CMS_ONLINE_CONTENTS + " found"); 134 } 135 136 if (isKeepHistory()) { 137 // Transfer the backup contents if the table exists 138 if (dbCon.hasTableOrColumn(TABLE_CMS_BACKUP_CONTENTS, null)) { 139 String query = readQuery(QUERY_TRANSFER_BACKUP_CONTENTS); 140 dbCon.updateSqlStatement(query, null, null); 141 } else { 142 System.out.println("no table " + TABLE_CMS_BACKUP_CONTENTS + " found"); 143 } 144 } 145 146 // Drop the tables CMS_BACKUP_CONTENTS and CMS_ONLINE_CONTENTS 147 cleanUpContentsTables(dbCon); 148 } 149 150 /** 151 * Transfers the online content.<p> 152 * 153 * @param dbCon the db connection interface 154 * @param pubTag the publish tag to use 155 * 156 * @throws SQLException if something goes wrong 157 */ 158 protected void transferOnlineContents(CmsSetupDb dbCon, int pubTag) throws SQLException { 159 160 String query = readQuery(QUERY_TRANSFER_ONLINE_CONTENTS); 161 Map<String, String> replacer = Collections.singletonMap("${pubTag}", "" + pubTag); 162 dbCon.updateSqlStatement(query, replacer, null); 163 } 164 165 /** 166 * After the transfer the tables CMS_ONLINE_CONTENTS and CMS_BACKUP contents are dropped as they are no longer needed.<p> 167 * 168 * @param dbCon the db connection interface 169 * 170 * @throws SQLException if something goes wrong 171 */ 172 private void cleanUpContentsTables(CmsSetupDb dbCon) throws SQLException { 173 174 System.out.println(new Exception().getStackTrace()[0].toString()); 175 String query = readQuery(QUERY_DROP_TABLE); 176 HashMap<String, String> replacers = new HashMap<String, String>(); 177 // Drop the CMS_ONLINE_CONTENTS table 178 replacers.put(REPLACEMENT_TABLENAME, TABLE_CMS_ONLINE_CONTENTS); 179 dbCon.updateSqlStatement(query, replacers, null); 180 181 replacers.clear(); 182 // Drop the CMS_BACKUP_CONTENTS table 183 replacers.put(REPLACEMENT_TABLENAME, TABLE_CMS_BACKUP_CONTENTS); 184 dbCon.updateSqlStatement(query, replacers, null); 185 } 186}