📄 cmsbackupdriver.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/generic/CmsBackupDriver.java,v $
* Date : $Date: 2006/03/27 14:52:54 $
* Version: $Revision: 1.141 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (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, 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.db.generic;
import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.db.CmsDbConsistencyException;
import org.opencms.db.CmsDbContext;
import org.opencms.db.CmsDbEntryNotFoundException;
import org.opencms.db.CmsDbSqlException;
import org.opencms.db.CmsDbUtil;
import org.opencms.db.CmsDriverManager;
import org.opencms.db.I_CmsBackupDriver;
import org.opencms.db.I_CmsDriver;
import org.opencms.file.CmsBackupProject;
import org.opencms.file.CmsBackupResource;
import org.opencms.file.CmsDataAccessException;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
/**
* Generic (ANSI-SQL) database server implementation of the backup driver methods.<p>
*
* @author Thomas Weckert
* @author Michael Emmerich
* @author Carsten Weinholz
*
* @version $Revision: 1.141 $
*
* @since 6.0.0
*/
public class CmsBackupDriver implements I_CmsDriver, I_CmsBackupDriver {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(org.opencms.db.generic.CmsBackupDriver.class);
/** The driver manager instance. */
protected CmsDriverManager m_driverManager;
/** The SQL manager instance. */
protected org.opencms.db.generic.CmsSqlManager m_sqlManager;
/**
* @see org.opencms.db.I_CmsBackupDriver#createBackupPropertyDefinition(org.opencms.db.CmsDbContext, java.lang.String)
*/
public CmsPropertyDefinition createBackupPropertyDefinition(CmsDbContext dbc, String name)
throws CmsDataAccessException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_PROPERTYDEF_CREATE_BACKUP");
stmt.setString(1, new CmsUUID().toString());
stmt.setString(2, name);
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
return readBackupPropertyDefinition(dbc, name);
}
/**
* @see org.opencms.db.I_CmsBackupDriver#createBackupResource(java.sql.ResultSet, boolean)
*/
public CmsBackupResource createBackupResource(ResultSet res, boolean hasContent) throws SQLException {
byte[] content = null;
CmsUUID backupId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_BACKUP_ID")));
int versionId = res.getInt(m_sqlManager.readQuery("C_RESOURCES_VERSION_ID"));
int tagId = res.getInt(m_sqlManager.readQuery("C_RESOURCES_PUBLISH_TAG"));
CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_ID")));
CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_ID")));
String resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
int projectLastModified = res.getInt(m_sqlManager.readQuery("C_RESOURCES_PROJECT_LASTMODIFIED"));
int state = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
long dateCreated = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_CREATED"));
long dateLastModified = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
long dateReleased = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_RELEASED"));
long dateExpired = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_EXPIRED"));
int resourceSize = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIZE"));
CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
String userLastModifiedName = res.getString(m_sqlManager.readQuery("C_RESOURCES_LASTMODIFIED_BY_NAME"));
CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
String userCreatedName = res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED_NAME"));
CmsUUID contentId;
if (hasContent) {
content = m_sqlManager.getBytes(res, m_sqlManager.readQuery("C_RESOURCES_FILE_CONTENT"));
contentId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_CONTENT_ID")));
} else {
content = new byte[0];
contentId = CmsUUID.getNullUUID();
}
return new CmsBackupResource(
backupId,
tagId,
versionId,
structureId,
resourceId,
contentId,
resourcePath,
resourceType,
resourceFlags,
projectLastModified,
state,
dateCreated,
userCreated,
userCreatedName,
dateLastModified,
userLastModified,
userLastModifiedName,
dateReleased,
dateExpired,
resourceSize,
content);
}
/**
* @see org.opencms.db.I_CmsBackupDriver#deleteBackup(org.opencms.db.CmsDbContext, org.opencms.file.CmsBackupResource, int, int)
*/
public void deleteBackup(CmsDbContext dbc, CmsBackupResource resource, int tag, int versions)
throws CmsDataAccessException {
ResultSet res = null;
PreparedStatement stmt = null;
PreparedStatement stmt1 = null;
PreparedStatement stmt2 = null;
PreparedStatement stmt3 = null;
PreparedStatement stmt4 = null;
Connection conn = null;
List backupIds = new ArrayList();
// first get all backup ids of the entries which should be deleted
try {
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_READ_BACKUPID_FOR_DELETION");
stmt.setString(1, resource.getStructureId().toString());
stmt.setString(2, resource.getResourceId().toString());
stmt.setInt(3, versions);
stmt.setInt(4, tag);
res = stmt.executeQuery();
// now collect all backupId's for deletion
while (res.next()) {
backupIds.add(res.getString(1));
}
// we have all the nescessary information, so we can delete the old backups
stmt1 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_STRUCTURE_BYBACKUPID");
stmt2 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_RESOURCES_BYBACKUPID");
stmt3 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_CONTENTS_BYBACKUPID");
stmt4 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_PROPERTIES_BYBACKUPID");
Iterator i = backupIds.iterator();
while (i.hasNext()) {
String backupId = (String)i.next();
//delete the structure
stmt1.setString(1, backupId);
stmt1.addBatch();
//delete the resource
stmt2.setString(1, backupId);
stmt2.addBatch();
//delete the file
stmt3.setString(1, backupId);
stmt3.addBatch();
//delete the properties
stmt4.setString(1, backupId);
stmt4.addBatch();
}
// excecute them
stmt1.executeBatch();
stmt2.executeBatch();
stmt3.executeBatch();
stmt4.executeBatch();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, res);
m_sqlManager.closeAll(dbc, null, stmt1, null);
m_sqlManager.closeAll(dbc, null, stmt2, null);
m_sqlManager.closeAll(dbc, null, stmt3, null);
m_sqlManager.closeAll(dbc, null, stmt4, null);
}
}
/**
* @see org.opencms.db.I_CmsBackupDriver#deleteBackupPropertyDefinition(org.opencms.db.CmsDbContext, org.opencms.file.CmsPropertyDefinition)
*/
public void deleteBackupPropertyDefinition(CmsDbContext dbc, CmsPropertyDefinition metadef)
throws CmsDataAccessException {
Connection conn = null;
PreparedStatement stmt = null;
try {
if (internalCountProperties(dbc, metadef, CmsProject.ONLINE_PROJECT_ID) != 0
|| internalCountProperties(dbc, metadef, Integer.MAX_VALUE) != 0) {
throw new CmsDbConsistencyException(Messages.get().container(
Messages.ERR_ERROR_DELETING_PROPERTYDEF_1,
metadef.getName()));
}
// delete the backup propertydef
conn = m_sqlManager.getConnection(dbc);
stmt = m_sqlManager.getPreparedStatement(conn, "C_PROPERTYDEF_DELETE_BACKUP");
stmt.setString(1, metadef.getId().toString());
stmt.executeUpdate();
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(
Messages.ERR_GENERIC_SQL_1,
CmsDbSqlException.getErrorQuery(stmt)), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt, null);
}
}
/**
* @see org.opencms.db.I_CmsBackupDriver#deleteBackups(org.opencms.db.CmsDbContext, java.util.List, int)
*/
public void deleteBackups(CmsDbContext dbc, List existingBackups, int maxVersions) throws CmsDataAccessException {
PreparedStatement stmt1 = null;
PreparedStatement stmt2 = null;
PreparedStatement stmt3 = null;
PreparedStatement stmt4 = null;
Connection conn = null;
CmsBackupResource currentResource = null;
int count = existingBackups.size() - maxVersions;
try {
conn = m_sqlManager.getConnection(dbc);
stmt1 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_CONTENTS_BYBACKUPID");
stmt2 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_STRUCTURE_BYBACKUPID");
stmt3 = m_sqlManager.getPreparedStatement(conn, "C_BACKUP_DELETE_RESOURCES_BYBACKUPID");
stmt4 = m_sqlManager.getPreparedStatement(conn, "C_PROPERTIES_DELETEALL_BACKUP");
for (int i = 0; i < count; i++) {
currentResource = (CmsBackupResource)existingBackups.get(i);
// delete the resource
stmt1.setString(1, currentResource.getBackupId().toString());
stmt1.addBatch();
stmt2.setString(1, currentResource.getBackupId().toString());
stmt2.addBatch();
stmt3.setString(1, currentResource.getBackupId().toString());
stmt3.addBatch();
// delete the properties
stmt4.setString(1, currentResource.getBackupId().toString());
stmt4.setInt(2, currentResource.getTagId());
stmt4.setString(3, currentResource.getStructureId().toString());
stmt4.setInt(4, CmsProperty.STRUCTURE_RECORD_MAPPING);
stmt4.setString(5, currentResource.getResourceId().toString());
stmt4.setInt(6, CmsProperty.RESOURCE_RECORD_MAPPING);
stmt4.addBatch();
}
if (count > 0) {
stmt1.executeBatch();
stmt2.executeBatch();
stmt3.executeBatch();
stmt4.executeBatch();
}
} catch (SQLException e) {
throw new CmsDbSqlException(Messages.get().container(Messages.ERR_GENERIC_SQL_0), e);
} finally {
m_sqlManager.closeAll(dbc, conn, stmt1, null);
m_sqlManager.closeAll(dbc, null, stmt2, null);
m_sqlManager.closeAll(dbc, null, stmt3, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -