📄 cmsvfsdriver.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/generic/CmsVfsDriver.java,v $
* Date : $Date: 2006/04/28 15:20:57 $
* Version: $Revision: 1.261 $
*
* 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_CmsDriver;
import org.opencms.db.I_CmsVfsDriver;
import org.opencms.file.CmsDataAccessException;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsFolder;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsVfsException;
import org.opencms.file.CmsVfsResourceAlreadyExistsException;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
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.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
/**
* Generic (ANSI-SQL) database server implementation of the VFS driver methods.<p>
*
* @author Thomas Weckert
* @author Michael Emmerich
*
* @version $Revision: 1.261 $
*
* @since 6.0.0
*/
public class CmsVfsDriver implements I_CmsDriver, I_CmsVfsDriver {
/** Operator to concatenate exclude conditions. */
protected static final String BEGIN_EXCLUDE_CONDITION = " AND NOT (";
/** Operator to concatenate include conditions. */
protected static final String BEGIN_INCLUDE_CONDITION = " AND (";
/** String to end a single condition. */
protected static final String END_CONDITION = ") ";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(org.opencms.db.generic.CmsVfsDriver.class);
/** The driver manager. */
protected CmsDriverManager m_driverManager;
/** The sql manager. */
protected org.opencms.db.generic.CmsSqlManager m_sqlManager;
/**
* Adds a trailing separator to a path if required.<p>
*
* @param path the path to add the trailing separator to
* @return the path with a trailing separator
*/
private static String addTrailingSeparator(String path) {
int l = path.length();
if ((l == 0) || (path.charAt(l - 1) != '/')) {
return path.concat("/");
} else {
return path;
}
}
/**
* Removes a trailing separater from a path if required.<p>
*
* @param path the path to remove the trailing separator from
* @return the path without a trailing separator
*/
private static String removeTrailingSeparator(String path) {
int l = path.length();
if ((l <= 1) || (path.charAt(l - 1) != '/')) {
return path;
} else {
return path.substring(0, l - 1);
}
}
/**
* @see org.opencms.db.I_CmsVfsDriver#createContent(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID, byte[], int)
*/
public void createContent(CmsDbContext dbc, CmsProject project, CmsUUID resourceId, byte[] content, int versionId)
throws CmsDataAccessException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = m_sqlManager.getConnection(dbc, project.getId());
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_CONTENTS_WRITE");
stmt.setString(1, new CmsUUID().toString());
stmt.setString(2, resourceId.toString());
if (content.length < 2000) {
stmt.setBytes(3, content);
} else {
stmt.setBinaryStream(3, new ByteArrayInputStream(content), content.length);
}
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_CmsVfsDriver#createFile(java.sql.ResultSet, int)
*/
public CmsFile createFile(ResultSet res, int projectId) throws SQLException {
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")));
int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
String resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
int resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_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 userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
CmsUUID contentId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_CONTENT_ID")));
byte[] content = m_sqlManager.getBytes(res, m_sqlManager.readQuery("C_RESOURCES_FILE_CONTENT"));
int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
// calculate the overall state
int newState = (structureState > resourceState) ? structureState : resourceState;
// in case of folder type ensure, that the root path has a trailing slash
if (CmsFolder.isFolderType(resourceType)) {
resourcePath = addTrailingSeparator(resourcePath);
}
return new CmsFile(
structureId,
resourceId,
contentId,
resourcePath,
resourceType,
resourceFlags,
projectId,
newState,
dateCreated,
userCreated,
dateLastModified,
userLastModified,
dateReleased,
dateExpired,
siblingCount,
resourceSize,
content);
}
/**
* @see org.opencms.db.I_CmsVfsDriver#createFile(java.sql.ResultSet, int, boolean)
*/
public CmsFile createFile(ResultSet res, int projectId, boolean hasFileContentInResultSet) throws SQLException {
byte[] content = null;
int resProjectId = CmsDbUtil.UNKNOWN_ID;
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 resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_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 userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
int lockedInProject = res.getInt("LOCKED_IN_PROJECT");
int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
// in case of folder type ensure, that the root path has a trailing slash
if (CmsFolder.isFolderType(resourceType)) {
resourcePath = addTrailingSeparator(resourcePath);
}
CmsUUID contentId;
if (hasFileContentInResultSet) {
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();
}
resProjectId = lockedInProject;
int newState = (structureState > resourceState) ? structureState : resourceState;
return new CmsFile(
structureId,
resourceId,
contentId,
resourcePath,
resourceType,
resourceFlags,
resProjectId,
newState,
dateCreated,
userCreated,
dateLastModified,
userLastModified,
dateReleased,
dateExpired,
siblingCount,
resourceSize,
content);
}
/**
* @see org.opencms.db.I_CmsVfsDriver#createFolder(java.sql.ResultSet, int, boolean)
*/
public CmsFolder createFolder(ResultSet res, int projectId, boolean hasProjectIdInResultSet) throws SQLException {
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 resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_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"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -