org.opencms.setup.db.update6to7.CmsUpdateDBCmsUsers Maven / Gradle / Ivy
Show all versions of opencms-test Show documentation
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH & Co. KG, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.setup.db.update6to7;
import org.opencms.setup.CmsSetupDBWrapper;
import org.opencms.setup.CmsSetupDb;
import org.opencms.setup.db.A_CmsUpdateDBPart;
import org.opencms.util.CmsCollectionsGenericWrapper;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* This class makes an update of the CMS_USERS table splitting it up into CMS_USERS and CMS_USERDATA.
* Unnecessary colums from CMS_USERS will be deleted and the new column USER_DATECREATED is added.
*
* @since 7.0.0
*/
public class CmsUpdateDBCmsUsers extends A_CmsUpdateDBPart {
/** Constant for the query to create the user data table.
*/
protected static final String QUERY_CREATE_TABLE_USERDATA = "Q_CREATE_TABLE_USERDATA";
/** Constant for the query to insert the new user data into the new table CMS_USERDATA.
*/
protected static final String QUERY_INSERT_CMS_USERDATA = "Q_INSERT_CMS_USERDATA";
/** Constant for the table CMS_USERDATA.
*/
private static final String CHECK_CMS_USERDATA = "CMS_USERDATA";
/** Constant for the table name of CMS_USERS.
*/
private static final String CMS_USERS_TABLE = "CMS_USERS";
/** Constant for the sql query to add the USER_DATECREATED column to CMS_USERS.
*/
private static final String QUERY_ADD_USER_DATECREATED_COLUMN = "Q_ADD_USER_DATECREATED";
/** Constant for the sql query to add all webusers to the group with the given id.
*/
private static final String QUERY_ADD_WEBUSERS_TO_GROUP = "Q_ADD_WEBUSERS_TO_GROUP";
/** Constant for the sql query to create a new group in the CMS_GROUPS table for the webusers.
*/
private static final String QUERY_CREATE_WEBUSERS_GROUP = "Q_CREATE_WEBUSERS_GROUP";
/** Constant for the sql query to drop the USER_ADDRESS column from CMS_USERS.
*/
private static final String QUERY_DROP_USER_ADDRESS_COLUMN = "Q_DROP_USER_ADDRESS_COLUMN";
/** Constant for the sql query to drop the USER_DESCRIPTION column from CMS_USERS.
*/
private static final String QUERY_DROP_USER_DESCRIPTION_COLUMN = "Q_DROP_USER_DESCRIPTION_COLUMN";
/** Constant for the sql query to drop the USER_INFO column from CMS_USERS.
*/
private static final String QUERY_DROP_USER_INFO_COLUMN = "Q_DROP_USER_INFO_COLUMN";
/** Constant for the sql query to drop the USER_TYPE column from CMS_USERS.
*/
private static final String QUERY_DROP_USER_TYPE_COLUMN = "Q_DROP_USER_TYPE_COLUMN";
/** Constant for the SQL query properties.
*/
private static final String QUERY_PROPERTY_FILE = "cms_users_queries.properties";
/** Constant for the query to the select the user infos for a user.
*/
private static final String QUERY_SELECT_USER_DATA = "Q_SELECT_USER_DATA";
/** Constant for the sql query to set the USER_DATECREATED value.
*/
private static final String QUERY_SET_USER_DATECREATED = "Q_SET_USER_DATECREATED";
/** Constant for the columnname USER_ID of the resultset.
*/
private static final String RESULTSET_USER_ID = "USER_ID";
/** Constant for the columnname USER_INFO of the resultset.
*/
private static final String RESULTSET_USER_INFO = "USER_INFO";
/** Constant for the columnname USER_ADDRESS of the resultset.
*/
private static final String USER_ADDRESS = "USER_ADDRESS";
/** Constant for the columnname USER_DATECREATED.
*/
private static final String USER_DATECREATED = "USER_DATECREATED";
/** Constant for the columnname USER_DESCRIPTION of the resultset.
*/
private static final String USER_DESCRIPTION = "USER_DESCRIPTION";
/** Constant for the columnname USER_INFO.
*/
private static final String USER_INFO = "USER_INFO";
/** Constant for the columnname USER_TYPE.
*/
private static final String USER_TYPE = "USER_TYPE";
/**
* Default constructor.
*
* @throws IOException if the default sql queries property file could not be read
*/
public CmsUpdateDBCmsUsers()
throws IOException {
super();
loadQueryProperties(getPropertyFileLocation() + QUERY_PROPERTY_FILE);
}
/**
* @see org.opencms.setup.db.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
*/
@Override
protected void internalExecute(CmsSetupDb dbCon) {
System.out.println(new Exception().getStackTrace()[0].toString());
try {
if (dbCon.hasTableOrColumn(CMS_USERS_TABLE, USER_TYPE)) {
CmsUUID id = createWebusersGroup(dbCon);
addWebusersToGroup(dbCon, id);
} else {
System.out.println("table " + CHECK_CMS_USERDATA + " already exists");
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
// Check if the CMS_USERDATA table exists
if (!checkUserDataTable(dbCon)) {
createUserDataTable(dbCon); // Could throw Exception during table creation
String query = readQuery(QUERY_SELECT_USER_DATA);
CmsSetupDBWrapper db = null;
try {
db = dbCon.executeSqlStatement(query, null);
while (db.getResultSet().next()) {
String userID = (String)db.getResultSet().getObject(RESULTSET_USER_ID);
System.out.println("UserId: " + userID);
try {
Blob blob = db.getResultSet().getBlob(RESULTSET_USER_INFO);
ByteArrayInputStream bin = new ByteArrayInputStream(blob.getBytes(1, (int)blob.length()));
ObjectInputStream oin = new ObjectInputStream(bin);
Map infos = CmsCollectionsGenericWrapper.map(oin.readObject());
if (infos == null) {
infos = new HashMap();
}
// Add user address and user description of the current user
String userAddress = (String)db.getResultSet().getObject(USER_ADDRESS);
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userAddress)) {
infos.put(USER_ADDRESS, userAddress);
}
String userDescription = (String)db.getResultSet().getObject(USER_DESCRIPTION);
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userDescription)) {
infos.put(USER_DESCRIPTION, userDescription);
}
// Write the user data to the table
writeAdditionalUserInfo(dbCon, userID, infos);
} catch (Throwable e) {
e.printStackTrace();
}
}
} finally {
if (db != null) {
db.close();
}
}
// add the column USER_DATECREATED
addUserDateCreated(dbCon);
// remove the unnecessary columns from CMS_USERS
removeUnnecessaryColumns(dbCon);
} else {
System.out.println("table " + CHECK_CMS_USERDATA + " already exists");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Adds the new column USER_DATECREATED to the CMS_USERS table.
*
* @param dbCon the db connection interface
*
* @throws SQLException if something goes wrong
*/
protected void addUserDateCreated(CmsSetupDb dbCon) throws SQLException {
System.out.println(new Exception().getStackTrace()[0].toString());
// Add the column to the table if necessary
if (!dbCon.hasTableOrColumn(CMS_USERS_TABLE, USER_DATECREATED)) {
String addUserDateCreated = readQuery(QUERY_ADD_USER_DATECREATED_COLUMN);
dbCon.updateSqlStatement(addUserDateCreated, null, null);
String setUserDateCreated = readQuery(QUERY_SET_USER_DATECREATED);
List