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 creates all the indexes that are used in the database version 7.<p> 044 * 045 * @since 7.0.0 046 */ 047public class CmsUpdateDBCreateIndexes7 extends A_CmsUpdateDBPart { 048 049 /** Constant for the sql query to read the indexes.<p> */ 050 protected static final String QUERY_SHOW_INDEX = "QUERY_SHOW_INDEX"; 051 052 /** Constant for the replacement of the tablename in the sql query.<p> */ 053 protected static final String REPLACEMENT_TABLENAME = "${tablename}"; 054 055 /** Constant for the field of the index name.<p> */ 056 private static final String FIELD_INDEX = "KEY_NAME"; 057 058 /** Constant for the primary key.<p> */ 059 private static final String PRIMARY_KEY = "PRIMARY"; 060 061 /** Constant for the SQL query properties.<p> */ 062 private static final String QUERY_PROPERTY_FILE = "cms_add_new_indexes_queries.properties"; 063 064 /** Constant for the replacement of the indexes to drop.<p> */ 065 private static final String REPLACEMENT_INDEXES = "${dropindexes}"; 066 067 /** 068 * Constructor.<p> 069 * 070 * @throws IOException if the query properties cannot be read 071 */ 072 public CmsUpdateDBCreateIndexes7() 073 throws IOException { 074 075 super(); 076 loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE); 077 } 078 079 /** 080 * @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb) 081 */ 082 @Override 083 protected void internalExecute(CmsSetupDb dbCon) { 084 085 System.out.println(new Exception().getStackTrace()[0].toString()); 086 List<String> elements = new ArrayList<String>(); 087 elements.add("CMS_CONTENTS"); 088 elements.add("CMS_GROUPS"); 089 elements.add("CMS_GROUPUSERS"); 090 elements.add("CMS_OFFLINE_ACCESSCONTROL"); 091 elements.add("CMS_OFFLINE_CONTENTS"); 092 elements.add("CMS_OFFLINE_PROPERTIES"); 093 elements.add("CMS_OFFLINE_PROPERTYDEF"); 094 elements.add("CMS_OFFLINE_RESOURCES"); 095 elements.add("CMS_OFFLINE_STRUCTURE"); 096 elements.add("CMS_ONLINE_ACCESSCONTROL"); 097 elements.add("CMS_ONLINE_PROPERTIES"); 098 elements.add("CMS_ONLINE_PROPERTYDEF"); 099 elements.add("CMS_ONLINE_RESOURCES"); 100 elements.add("CMS_ONLINE_STRUCTURE"); 101 elements.add("CMS_PROJECTRESOURCES"); 102 elements.add("CMS_PROJECTS"); 103 elements.add("CMS_PUBLISH_HISTORY"); 104 elements.add("CMS_STATICEXPORT_LINKS"); 105 elements.add("CMS_USERS"); 106 107 // iterate the queries 108 for (Iterator<String> it = elements.iterator(); it.hasNext();) { 109 String query = it.next(); 110 // Check if the table exists 111 if (dbCon.hasTableOrColumn(query, null)) { 112 HashMap<String, String> replacer = new HashMap<String, String>(); 113 replacer.put(REPLACEMENT_INDEXES, getIndexesToDrop(dbCon, query)); 114 try { 115 dbCon.updateSqlStatement(readQuery(query), replacer, null); 116 } catch (SQLException e) { 117 e.printStackTrace(); 118 } 119 } else { 120 System.out.println("Table " + query + "does not exist."); 121 } 122 } 123 } 124 125 /** 126 * Returns the string of the indexes that shall be dropped before adding the final new indexes.<p> 127 * 128 * @param dbCon the connection to the database 129 * @param tablename the table to drop the indexes from 130 * 131 * @return the string to drop the temporary indexes 132 */ 133 private String getIndexesToDrop(CmsSetupDb dbCon, String tablename) { 134 135 List<String> indexes = new ArrayList<String>(); 136 String tableIndex = readQuery(QUERY_SHOW_INDEX); 137 Map<String, String> replacer = new HashMap<String, String>(); 138 replacer.put(REPLACEMENT_TABLENAME, tablename); 139 CmsSetupDBWrapper db = null; 140 try { 141 db = dbCon.executeSqlStatement(tableIndex, replacer); 142 while (db.getResultSet().next()) { 143 String index = db.getResultSet().getString(FIELD_INDEX); 144 if (!indexes.contains(index)) { 145 indexes.add(index); 146 } 147 } 148 149 } catch (SQLException e) { 150 e.printStackTrace(); 151 } finally { 152 if (db != null) { 153 db.close(); 154 } 155 } 156 157 StringBuffer dropIndex = new StringBuffer(); 158 for (Iterator<String> it = indexes.iterator(); it.hasNext();) { 159 String index = it.next(); 160 if (index.equals(PRIMARY_KEY)) { 161 dropIndex.append("DROP PRIMARY KEY, "); 162 } else { 163 dropIndex.append("DROP INDEX "); 164 dropIndex.append(index); 165 dropIndex.append(", "); 166 } 167 } 168 return dropIndex.toString(); 169 } 170 171}