📄 cmsdbaccess.java
字号:
}
return readSystemProperty(name);
}
/**
* Writes a user to the database.
*
* @param user the user to write
* @throws thorws CmsException if something goes wrong.
*/
public void writeUser(CmsUser user) throws CmsException {
byte[] value = null;
PreparedStatement statement = null;
PreparedStatement statement2 = null;
PreparedStatement nextStatement = null;
PreparedStatement trimStatement = null;
ResultSet res = null;
Connection con = null;
try {
// serialize the hashtable
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(user.getAdditionalInfo());
oout.close();
value = bout.toByteArray();
// write data to database
con = DriverManager.getConnection(m_poolName);
statement = con.prepareStatement(m_cq.get("C_ORACLE_USERSWRITE"));
statement.setString(1, checkNull(user.getDescription()));
statement.setString(2, checkNull(user.getFirstname()));
statement.setString(3, checkNull(user.getLastname()));
statement.setString(4, checkNull(user.getEmail()));
statement.setTimestamp(5, new Timestamp(user.getLastlogin()));
statement.setTimestamp(6, new Timestamp(user.getLastUsed()));
statement.setInt(7, user.getFlags());
statement.setInt(8, user.getDefaultGroupId());
statement.setString(9, checkNull(user.getAddress()));
statement.setString(10, checkNull(user.getSection()));
statement.setInt(11, user.getType());
statement.setInt(12, user.getId());
statement.executeUpdate();
statement.close();
// update user_info in this special way because of using blob
statement2 = con.prepareStatement(m_cq.get("C_ORACLE_USERSFORUPDATE"));
statement2.setInt(1, user.getId());
con.setAutoCommit(false);
res = statement2.executeQuery();
try {
while (res.next()) {
oracle.sql.BLOB blobnew = ((OracleResultSet) res).getBLOB("USER_INFO");
// first trim the blob to 0 bytes, otherwise ther could be left some bytes
// of the old content
//trimStatement = (OraclePreparedStatement) con.prepareStatement(cq.get("C_TRIMBLOB);
trimStatement = con.prepareStatement(m_cq.get("C_TRIMBLOB"));
//trimStatement.setBLOB(1, blobnew);
trimStatement.setBlob(1, blobnew);
trimStatement.setInt(2, 0);
trimStatement.execute();
trimStatement.close();
ByteArrayInputStream instream = new ByteArrayInputStream(value);
OutputStream outstream = blobnew.getBinaryOutputStream();
byte[] chunk = new byte[blobnew.getChunkSize()];
int i = -1;
while ((i = instream.read(chunk)) != -1) {
outstream.write(chunk, 0, i);
}
instream.close();
outstream.close();
}
// for the oracle-driver commit or rollback must be executed manually
// because setAutoCommit = false in CmsDbPool.CmsDbPool
nextStatement = con.prepareStatement(m_cq.get("C_COMMIT"));
nextStatement.execute();
nextStatement.close();
con.setAutoCommit(true);
} catch (IOException e) {
throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), e);
}
statement2.close();
res.close();
} catch (SQLException e) {
throw new CmsException("[" + this.getClass().getName() + "]" + e.getMessage(), CmsException.C_SQL_ERROR, e);
} catch (IOException e) {
throw new CmsException("[CmsAccessUserInfoMySql/addUserInformation(id,object)]:" + CmsException.C_SERIALIZATION, e);
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException exc) {
}
}
if (statement2 != null) {
try {
statement2.close();
} catch (SQLException exc) {
}
try {
nextStatement = con.prepareStatement(m_cq.get("C_ROLLBACK"));
nextStatement.execute();
} catch (SQLException se) {
}
}
if (nextStatement != null) {
try {
nextStatement.close();
} catch (SQLException exc) {
}
}
if (trimStatement != null) {
try {
trimStatement.close();
} catch (SQLException exc) {
}
}
if (con != null) {
try {
con.setAutoCommit(true);
} catch (SQLException se) {
}
try {
con.close();
} catch (SQLException se) {
}
}
if (res != null) {
try {
res.close();
} catch (SQLException se) {
}
}
}
}
/**
* Destroys this access-module
* @throws throws CmsException if something goes wrong.
*/
public void destroy()
throws CmsException {
try {
((com.opencms.dbpool.CmsDriver) DriverManager.getDriver(m_poolName)).destroy();
} catch(SQLException exc) {
// destroy not possible - ignoring the exception
}
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INIT, "[oraclesql.CmsDbAccess] Destroyed");
}
}
/**
* Creates the content entry for a file
*
* @param fileId The ID of the new file
* @param fileContent The content of the new file
* @param versionId For the content of a backup file you need to insert the versionId of the backup
* @param usedPool The name of the databasepool to use
* @param usedStatement Specifies which tables must be used: offline, online or backup
*
*/
protected void createFileContent(int fileId, byte[] fileContent, int versionId,String usedPool, String usedStatement) throws CmsException{
PreparedStatement statement = null;
Connection con = null;
try{
con = DriverManager.getConnection(usedPool);
// first insert new file without file_content, then update the file_content
// these two steps are necessary because of using BLOBs in the Oracle DB
statement = con.prepareStatement(m_cq.get("C_ORACLE_FILESFORINSERT"+usedStatement));
statement.setInt(1, fileId);
if("_BACKUP".equals(usedStatement)){
statement.setInt(2, versionId);
}
statement.executeUpdate();
} catch (SQLException e) {
throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
} finally {
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
}
}
}
// now update the file content
writeFileContent(fileId, fileContent, usedPool, usedStatement);
}
/**
* Writes the file content of an existing file
*
* @param fileId The ID of the file to update
* @param fileContent The new content of the file
* @param usedPool The name of the database pool to use
* @param usedStatement Specifies which tables must be used: offline, online or backup
*/
protected void writeFileContent(int fileId, byte[] fileContent, String usedPool, String usedStatement) throws CmsException{
PreparedStatement statement = null;
PreparedStatement nextStatement = null;
PreparedStatement trimStatement = null;
Connection con = null;
ResultSet res = null;
try{
// update the file content in the FILES database.
con = DriverManager.getConnection(usedPool);
statement = con.prepareStatement(m_cq.get("C_ORACLE_FILESFORUPDATE"+usedStatement));
statement.setInt(1, fileId);
con.setAutoCommit(false);
res = statement.executeQuery();
try {
while (res.next()) {
oracle.sql.BLOB blobnew = ((OracleResultSet) res).getBLOB("FILE_CONTENT");
// first trim the blob to 0 bytes, otherwise there could be left some bytes
// of the old content
trimStatement = con.prepareStatement(m_cq.get("C_TRIMBLOB"));
trimStatement.setBlob(1, blobnew);
trimStatement.setInt(2, 0);
trimStatement.execute();
ByteArrayInputStream instream = new ByteArrayInputStream(fileContent);
OutputStream outstream = blobnew.getBinaryOutputStream();
byte[] chunk = new byte[blobnew.getChunkSize()];
int i = -1;
while ((i = instream.read(chunk)) != -1) {
outstream.write(chunk, 0, i);
}
instream.close();
outstream.close();
}
// for the oracle-driver commit or rollback must be executed manually
// because setAutoCommit = false in CmsDbPool.CmsDbPool
nextStatement = con.prepareStatement(m_cq.get("C_COMMIT"));
nextStatement.execute();
nextStatement.close();
con.setAutoCommit(true);
} catch (IOException e) {
throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), e);
}
} catch (SQLException e) {
throw new CmsException("[" + this.getClass().getName() + "] " + e.getMessage(), CmsException.C_SQL_ERROR, e);
} finally {
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(nextStatement != null) {
try {
nextStatement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(trimStatement != null) {
try {
trimStatement.close();
} catch(SQLException exc) {
// nothing to do here
}
}
if(con != null) {
try {
con.close();
} catch(SQLException exc) {
// nothing to do here
}
}
}
}
/**
* Returns the bytes from a result set
*
* @param res The ResultSet to read from
* @param columnName The name of the column to read from
*
* @return The byte value from the column
*/
protected byte[] getBytesFromResultset(ResultSet res, String columnName) throws SQLException{
// read the bytes from an oracle blob
oracle.sql.BLOB blob = ((OracleResultSet) res).getBLOB(columnName);
byte[] content = new byte[(int) blob.length()];
content = blob.getBytes(1, (int) blob.length());
return content;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -