📄 cmsdbaccess.java
字号:
int parentId,
String destination)
throws CmsException {
CmsFile file;
// read sourcefile
file=readFile(project.getId(),onlineProject.getId(),source);
// create destination file
createFile(project,onlineProject,file,userId,parentId,destination, true);
}
/**
* Counts the locked resources in this project.
*
* @param project The project to be unlocked.
* @return the amount of locked resources in this project.
*
* @exception CmsException Throws CmsException if something goes wrong.
*/
public int countLockedResources(CmsProject project)
throws CmsException {
Connection con = null;
PreparedStatement statement = null;
ResultSet res = null;
int retValue;
String usedPool;
String usedStatement;
//int onlineProject = getOnlineProject(project.getId()).getId();
int onlineProject = I_CmsConstants.C_PROJECT_ONLINE_ID;
if (project.getId() == onlineProject){
usedPool = m_poolNameOnline;
usedStatement = "_ONLINE";
} else {
usedPool = m_poolName;
usedStatement = "";
}
try {
// create the statement
con = DriverManager.getConnection(usedPool);
statement = con.prepareStatement(m_cq.get("C_RESOURCES_COUNTLOCKED"+usedStatement));
statement.setInt(1,project.getId());
res = statement.executeQuery();
if(res.next()) {
retValue = res.getInt(1);
} else {
retValue=0;
}
} catch( Exception exc ) {
throw new CmsException("[" + this.getClass().getName() + "] " + exc.getMessage(),
CmsException.C_SQL_ERROR, exc);
} finally {
// close all db-resources
if(res != null) {
try {
res.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(statement != null) {
try {
statement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
return retValue;
}
/**
* Returns the amount of properties for a propertydefinition.
*
* @param metadef The propertydefinition to test.
*
* @return the amount of properties for a propertydefinition.
*
* @exception CmsException Throws CmsException if something goes wrong.
*/
protected int countProperties(CmsPropertydefinition metadef)
throws CmsException {
ResultSet result = null;
PreparedStatement statement = null;
Connection con = null;
int returnValue;
try {
// create statement
con = DriverManager.getConnection(m_poolName);
statement = con.prepareStatement(m_cq.get("C_PROPERTIES_READALL_COUNT"));
statement.setInt(1, metadef.getId());
result = statement.executeQuery();
if( result.next() ) {
returnValue = result.getInt(1) ;
} else {
throw new CmsException("[" + this.getClass().getName() + "] " + metadef.getName(),
CmsException.C_UNKNOWN_EXCEPTION);
}
} catch(SQLException exc) {
throw new CmsException("[" + this.getClass().getName() + "] " + exc.getMessage(),
CmsException.C_SQL_ERROR, exc);
}finally {
// close all db-resources
if(result != null) {
try {
result.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(statement != null) {
try {
statement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
return returnValue;
}
/**
* Creates a new file from an given CmsFile object and a new filename.
*
* @param project The project in which the resource will be used.
* @param onlineProject The online project of the OpenCms.
* @param file The file to be written to the Cms.
* @param user The Id of the user who changed the resourse.
* @param parentId The parentId of the resource.
* @param filename The complete new name of the file (including pathinformation).
*
* @return file The created file.
*
* @exception CmsException Throws CmsException if operation was not succesful
*/
public CmsFile createFile(CmsProject project,
CmsProject onlineProject,
CmsFile file,
int userId,
int parentId, String filename, boolean copy)
throws CmsException {
String usedPool = null;
String usedStatement = null;
Connection con = null;
PreparedStatement statement = null;
// check the resource name
if (filename.length() > C_MAX_LENGTH_RESOURCE_NAME){
throw new CmsException("["+this.getClass().getName()+"] "+"Resourcename too long(>"+C_MAX_LENGTH_RESOURCE_NAME+") ",CmsException.C_BAD_NAME);
}
int state=0;
int modifiedBy = userId;
long dateModified = System.currentTimeMillis();
if (project.equals(onlineProject)) {
state= file.getState();
usedPool = m_poolNameOnline;
usedStatement = "_ONLINE";
modifiedBy = file.getResourceLastModifiedBy();
dateModified = file.getDateLastModified();
} else {
state=C_STATE_NEW;
usedPool = m_poolName;
usedStatement = "";
}
// Test if the file is already there and marked as deleted.
// If so, delete it.
// If the file exists already and is not marked as deleted then throw exception
try {
CmsFile exFile = readFileHeader(project.getId(),filename);
throw new CmsException("["+this.getClass().getName()+"] ",CmsException.C_FILE_EXISTS);
} catch (CmsException e) {
// if the file is marked as deleted remove it!
if (e.getType() == CmsException.C_RESOURCE_DELETED) {
removeFile(project.getId(), filename);
state=C_STATE_CHANGED;
//throw new CmsException("["+this.getClass().getName()+"] ",CmsException.C_FILE_EXISTS);
}
if (e.getType() == CmsException.C_FILE_EXISTS) {
throw e;
}
}
// first write the file content
int newFileId = nextId(m_cq.get("C_TABLE_FILES"+usedStatement));
int resourceId = nextId(m_cq.get("C_TABLE_RESOURCES"+usedStatement));
// now write the resource
try {
con = DriverManager.getConnection(usedPool);
// first write the file content
try {
statement = con.prepareStatement(m_cq.get("C_FILES_WRITE"+usedStatement));
statement.setInt(1, newFileId);
statement.setBytes(2, file.getContents());
statement.executeUpdate();
statement.close();
} catch (SQLException se) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
A_OpenCms.log(C_OPENCMS_CRITICAL, "[CmsAccessFileMySql] " + se.getMessage());
}
} finally {
if(statement != null) {
try {
statement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
// now write the file header
statement = con.prepareStatement(m_cq.get("C_RESOURCES_WRITE"+usedStatement));
statement.setInt(1, resourceId);
statement.setInt(2, parentId);
statement.setString(3, filename);
statement.setInt(4, file.getType());
statement.setInt(5, file.getFlags());
statement.setInt(6, file.getOwnerId());
statement.setInt(7, file.getGroupId());
statement.setInt(8, project.getId());
statement.setInt(9, newFileId);
statement.setInt(10, file.getAccessFlags());
statement.setInt(11, state);
statement.setInt(12, file.isLockedBy());
statement.setInt(13, file.getLauncherType());
statement.setString(14, file.getLauncherClassname());
statement.setTimestamp(15, new Timestamp(file.getDateCreated()));
statement.setTimestamp(16, new Timestamp(dateModified));
statement.setInt(17, file.getLength());
statement.setInt(18, modifiedBy);
statement.executeUpdate();
statement.close();
} catch (SQLException e){
throw new CmsException("["+this.getClass().getName()+"] "+e.getMessage(),CmsException.C_SQL_ERROR, e);
} finally {
// close all db-resources
if(statement != null) {
try {
statement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
return readFile(project.getId(), onlineProject.getId(), filename);
}
/**
* Creates a new file with the given content and resourcetype.
*
* @param user The user who wants to create the file.
* @param project The project in which the resource will be used.
* @param onlineProject The online project of the OpenCms.
* @param filename The complete name of the new file (including pathinformation).
* @param flags The flags of this resource.
* @param parentId The parentId of the resource.
* @param contents The contents of the new file.
* @param resourceType The resourceType of the new file.
*
* @return file The created file.
*
* @exception CmsException Throws CmsException if operation was not succesful
*/
public CmsFile createFile(CmsUser user, CmsProject project, CmsProject onlineProject, String filename, int flags, int parentId, byte[] contents, I_CmsResourceType resourceType) throws CmsException {
String usedPool = null;
String usedStatement = null;
//check the resource name
if (filename.length() > C_MAX_LENGTH_RESOURCE_NAME){
throw new CmsException("["+this.getClass().getName()+"] "+"Resourcename too long(>"+C_MAX_LENGTH_RESOURCE_NAME+") ",CmsException.C_BAD_NAME);
}
// it is not allowed, that there is no content in the file
// TODO: check if this can be done in another way:
if (contents.length == 0) {
contents = " ".getBytes();
}
int state = C_STATE_NEW;
if (project.equals(onlineProject)) {
usedPool = m_poolNameOnline;
usedStatement = "_ONLINE";
} else {
usedPool = m_poolName;
usedStatement = "";
}
// Test if the file is already there and marked as deleted.
// If so, delete it
try {
readFileHeader(project.getId(), filename);
throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
} catch (CmsException e) {
// if the file is maked as deleted remove it!
if (e.getType() == CmsException.C_RESOURCE_DELETED) {
removeFile(project.getId(), filename);
state = C_STATE_CHANGED;
//throw new CmsException("[" + this.getClass().getName() + "] ", CmsException.C_FILE_EXISTS);
}
if (e.getType() == CmsException.C_FILE_EXISTS) {
throw e;
}
}
int resourceId = nextId(m_cq.get("C_TABLE_RESOURCES"+usedStatement));
int fileId = nextId(m_cq.get("C_TABLE_FILES"+usedStatement));
Connection con = null;
PreparedStatement statement = null;
PreparedStatement statementFileWrite = null;
try {
con = DriverManager.getConnection(usedPool);
statement = con.prepareStatement(m_cq.get("C_RESOURCES_WRITE"+usedStatement));
// write new resource to the database
statement.setInt(1, resourceId);
statement.setInt(2, parentId);
statement.setString(3, filename);
statement.setInt(4, resourceType.getResourceType());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -