OF33.java 9.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.database.bugfix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.SchemaManager;
import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.util.JiveConstants;

/**
 * This class implements a fix for a problem identified as issue OF-33 in the
 * bugtracker of Openfire.
 * <p>
 * The code in this class is intended to be executed only once, under very
 * strict circumstances. The only class responsible for calling this code should
 * be an instance of {@link SchemaManager}. The database update version
 * corresponding to this fix is 21.
 * 
 * @author G&uuml;nther Nie&szlig;
 * @see <a href="http://www.igniterealtime.org/issues/browse/OF-33">Openfire
 *      bugtracker: OF-33</a>
 */
public final class OF33 {

	/**
	 * Check and repair the serviceIDs for the ofMucService table.
	 * 
	 * @param con
	 *            the database connection to use to check the MultiUserChat
	 *            services.
	 */
	public static void executeFix(Connection con) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String answer = null;
		try {
			// Get the current ID for MUC services
			pstmt = con.prepareStatement("SELECT id FROM ofID WHERE idType="
					+ JiveConstants.MUC_SERVICE);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				answer = rs.getString(1);
			}
			rs.close();
			if (answer == null) {
				// No MultiUserService entries are found, so nothing to do.
				return;
			}
			if (answer.equals("1")) {
				// The initial configuration wasn't modified. We must only
				// update the ID.
				pstmt = con
						.prepareStatement("UPDATE ofID SET id=2 WHERE idType="
								+ JiveConstants.MUC_SERVICE);
				pstmt.executeUpdate();
				return;
			} else {
				// Check for duplicated entries with ID=1.
				String subdomain = null;
				try {
					pstmt = con
							.prepareStatement("SELECT subdomain FROM ofMucService WHERE serviceID=1");
					rs = pstmt.executeQuery();
					if (rs.next()) {
						subdomain = rs.getString(1);
					}
					if (subdomain == null || !rs.next()) {
						// No duplicated entries found, so nothing to do.
						return;
					}
					subdomain = rs.getString(1);
				} finally {
					rs.close();
				}
				// Set new serviceID for duplicated MUC service
				long newID = SequenceManager.nextID(JiveConstants.MUC_SERVICE);
				pstmt = con
						.prepareStatement("UPDATE ofMucService SET serviceID=? WHERE serviceID=1 AND subdomain=?");
				pstmt.setLong(1, newID);
				pstmt.setString(2, subdomain);
				pstmt.executeUpdate();
				// Copy service properties.
				try {
					pstmt = con
							.prepareStatement("SELECT name, propValue FROM ofMucServiceProp WHERE serviceID=1");
					rs = pstmt.executeQuery();
					String name = null;
					String value = null;
					while (rs.next()) {
						name = rs.getString(1);
						value = rs.getString(2);
						if (name != null && value != null) {
							pstmt = con
									.prepareStatement("INSERT INTO ofMucServiceProp(serviceID, name, propValue) "
											+ "VALUES(?,?,?)");
							pstmt.setLong(1, newID);
							pstmt.setString(2, name);
							pstmt.setString(3, value);
							pstmt.executeUpdate();
						}
					}
				} finally {
					rs.close();
				}
				// Copy rooms.
				try {
					Long roomID, newRoomID;
					ResultSet roomRS = null;
					pstmt = con
							.prepareStatement("SELECT roomID, creationDate, modificationDate, "
									+ "name, naturalName, description, lockedDate, emptyDate, "
									+ "canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, "
									+ "canInvite, roomPassword, canDiscoverJID, logEnabled, subject, "
									+ "rolesToBroadcast, useReservedNick, canChangeNick, canRegister "
									+ "FROM ofMucRoom WHERE serviceID=1");
					rs = pstmt.executeQuery();
					while (rs.next()) {
						roomID = rs.getLong(1);
						newRoomID = SequenceManager
								.nextID(JiveConstants.MUC_ROOM);
						pstmt = con
								.prepareStatement("INSERT INTO ofMucRoom (serviceID, roomID, "
										+ "creationDate, modificationDate, name, naturalName, description, "
										+ "lockedDate, emptyDate, canChangeSubject, maxUsers, publicRoom, "
										+ "moderated, membersOnly, canInvite, roomPassword, canDiscoverJID, "
										+ "logEnabled, subject, rolesToBroadcast, useReservedNick, "
										+ "canChangeNick, canRegister) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
						pstmt.setLong(1, newID);
						pstmt.setLong(2, newRoomID);
						pstmt.setString(3, rs.getString(2));
						pstmt.setString(4, rs.getString(3));
						pstmt.setString(5, rs.getString(4));
						pstmt.setString(6, rs.getString(5));
						pstmt.setString(7, rs.getString(6));
						pstmt.setString(8, rs.getString(7));
						pstmt.setString(9, rs.getString(8));
						pstmt.setInt(10, rs.getInt(9));
						pstmt.setInt(11, rs.getInt(10));
						pstmt.setInt(12, rs.getInt(11));
						pstmt.setInt(13, rs.getInt(12));
						pstmt.setInt(14, rs.getInt(13));
						pstmt.setInt(15, rs.getInt(14));
						pstmt.setString(16, rs.getString(15));
						pstmt.setInt(17, rs.getInt(16));
						pstmt.setInt(18, rs.getInt(17));
						pstmt.setString(19, rs.getString(18));
						pstmt.setInt(20, rs.getInt(19));
						pstmt.setInt(21, rs.getInt(20));
						pstmt.setInt(22, rs.getInt(21));
						pstmt.setInt(23, rs.getInt(22));
						pstmt.executeUpdate();
						// Copy room properties.
						try {
							pstmt = con
									.prepareStatement("SELECT name, propValue FROM ofMucRoomProp WHERE roomID=?");
							pstmt.setLong(1, roomID);
							roomRS = pstmt.executeQuery();
							String name = null;
							String value = null;
							while (roomRS.next()) {
								name = roomRS.getString(1);
								value = roomRS.getString(2);
								if (name != null && value != null) {
									pstmt = con
											.prepareStatement("INSERT INTO ofMucRoomProp(roomID, name, propValue) "
													+ "VALUES(?,?,?)");
									pstmt.setLong(1, newRoomID);
									pstmt.setString(2, name);
									pstmt.setString(3, value);
									pstmt.executeUpdate();
								}
							}
						} finally {
							roomRS.close();
						}
						// Copy affiliations.
						try {
							pstmt = con
									.prepareStatement("SELECT jid, affiliation FROM ofMucAffiliation WHERE roomID=?");
							pstmt.setLong(1, roomID);
							roomRS = pstmt.executeQuery();
							while (roomRS.next()) {
								pstmt = con
										.prepareStatement("INSERT INTO ofMucAffiliation(roomID, jid, affiliation) "
												+ "VALUES(?,?,?)");
								pstmt.setLong(1, newRoomID);
								pstmt.setString(2, roomRS.getString(1));
								pstmt.setInt(3, roomRS.getInt(2));
								pstmt.executeUpdate();
							}
						} finally {
							roomRS.close();
						}
						// Copy members.
						try {
							pstmt = con
									.prepareStatement("SELECT jid, nickname, firstName, lastName, url, email, faqentry "
											+ "FROM ofMucMember WHERE roomID=?");
							pstmt.setLong(1, roomID);
							roomRS = pstmt.executeQuery();
							while (roomRS.next()) {
								pstmt = con
										.prepareStatement("INSERT INTO ofMucMember(roomID, jid, nickname, firstName, "
												+ "lastName, url, email, faqentry) VALUES(?,?,?,?,?,?,?,?)");
								pstmt.setLong(1, newRoomID);
								pstmt.setString(2, roomRS.getString(1));
								pstmt.setString(3, roomRS.getString(2));
								pstmt.setString(4, roomRS.getString(3));
								pstmt.setString(5, roomRS.getString(4));
								pstmt.setString(6, roomRS.getString(5));
								pstmt.setString(7, roomRS.getString(6));
								pstmt.setString(8, roomRS.getString(7));
								pstmt.executeUpdate();
							}
						} finally {
							roomRS.close();
						}
						// Copy conversation history.
						try {
							pstmt = con
									.prepareStatement("SELECT sender, nickname, logTime, subject, body "
											+ "FROM ofMucConversationLog WHERE roomID=?");
							pstmt.setLong(1, roomID);
							roomRS = pstmt.executeQuery();
							while (roomRS.next()) {
								pstmt = con
										.prepareStatement("INSERT INTO ofMucConversationLog(roomID, "
												+ "sender, nickname, logTime, subject, body VALUES(?,?,?,?,?,?)");
								pstmt.setLong(1, newRoomID);
								pstmt.setString(2, roomRS.getString(1));
								pstmt.setString(3, roomRS.getString(2));
								pstmt.setString(4, roomRS.getString(3));
								pstmt.setString(5, roomRS.getString(4));
								pstmt.setString(6, roomRS.getString(5));
								pstmt.executeUpdate();
							}
						} finally {
							roomRS.close();
						}
					}
				} finally {
					rs.close();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DbConnectionManager.closeStatement(pstmt);
		}
	}

}