📄 cmsdbaccess.java
字号:
if (!content.isWriteable()) {
// no write access
throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
CmsUUID newMasterId = new CmsUUID();
int projectId = cms.getRequestContext().currentProject().getId();
CmsUUID currentUserId = cms.getRequestContext().currentUser().getId();
long currentTime = new java.util.Date().getTime();
// filling some default-values for new dataset's
dataset.m_masterId = newMasterId;
dataset.m_projectId = projectId;
dataset.m_lockedInProject = projectId;
dataset.m_state = CmsResource.STATE_NEW;
dataset.m_lockedBy = currentUserId;
dataset.m_lastModifiedBy = currentUserId;
dataset.m_dateCreated = currentTime;
dataset.m_dateLastModified = currentTime;
PreparedStatement stmt = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, "insert_offline");
sqlFillValues(stmt, content.getSubId(), dataset);
stmt.executeUpdate();
// after inserting the row, we have to update media and channel tables
updateMedia(dataset.m_masterId, mediaToAdd, new Vector(), new Vector());
updateChannels(cms, dataset.m_masterId, channelToAdd, new Vector());
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, null);
}
return newMasterId;
}
/**
* Updates the lockstate in the database.<p>
*
* @param cms the CmsObject to get access to cms resources.
* @param content the CmsMasterContent to write to the database.
* @param dataset the set of data for this contentdefinition.
*/
public void writeLockstate(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException {
if (isOnlineProject(cms)) {
// this is the onlineproject - don't write into this project directly
throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0));
}
if (!content.isWriteable()) {
// no write access
throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
if (!dataset.m_lockedBy.isNullUUID()) {
// lock the resource into the current project
dataset.m_lockedInProject = cms.getRequestContext().currentProject().getId();
}
PreparedStatement stmt = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, "update_lockstate_offline");
stmt.setString(1, dataset.m_lockedBy.toString());
stmt.setInt(2, dataset.m_lockedInProject);
stmt.setString(3, dataset.m_masterId.toString());
stmt.setInt(4, content.getSubId());
stmt.executeUpdate();
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, null);
}
}
/**
* Write the dataset to the database.<p>
*
* @param cms the CmsObject to get access to cms resources.
* @param content the CmsMasterContent to write to the database.
* @param dataset the set of data for this contentdefinition.
*/
public void write(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException {
if (isOnlineProject(cms)) {
// this is the onlineproject - don't write into this project directly
throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0));
}
if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) {
// this is not the online row - it was read from history
// don't write it!
throw new CmsLegacySecurityException("Can't update a cd with a backup cd", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
// read the lockstate
readLockstate(dataset, content.getSubId());
if (!dataset.m_lockedBy.equals(cms.getRequestContext().currentUser().getId())) {
// is not locked by this user
throw new CmsLegacySecurityException("Not locked by this user", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
if (dataset.m_lockedInProject != dataset.m_projectId) {
// not locked in this project
throw new CmsLegacySecurityException("Not locked in this project", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
if (!content.isWriteable()) {
// no write access
throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
long currentTime = new java.util.Date().getTime();
CmsUUID currentUserId = cms.getRequestContext().currentUser().getId();
// updateing some values for updated dataset
if (dataset.m_state != CmsResource.STATE_NEW) {
// if the state is not new then set the state to changed
dataset.m_state = CmsResource.STATE_CHANGED;
}
dataset.m_lastModifiedBy = currentUserId;
dataset.m_dateLastModified = currentTime;
PreparedStatement stmt = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, "update_offline");
int rowcounter = sqlFillValues(stmt, content.getSubId(), dataset);
stmt.setString(rowcounter++, dataset.m_masterId.toString());
stmt.setInt(rowcounter++, content.getSubId());
stmt.executeUpdate();
// after inserting the row, we have to update media and channel tables
updateMedia(dataset.m_masterId, dataset.m_mediaToAdd, dataset.m_mediaToUpdate, dataset.m_mediaToDelete);
updateChannels(cms, dataset.m_masterId, dataset.m_channelToAdd, dataset.m_channelToDelete);
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, null);
}
}
/**
* Read the dataset with the given UUID from the database.<p>
*
* @param cms the CmsObject to get access to cms-ressources.
* @param content the CmsMasterContent to write to the database.
* @param dataset the set of data for this contentdefinition.
* @param contentId the UUID of the contentdefinition.
*/
public void read(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset, CmsUUID contentId) throws CmsException {
if (!content.isReadable()) {
// no read access
throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
String statement_key = "read_offline";
if (isOnlineProject(cms)) {
statement_key = "read_online";
}
PreparedStatement stmt = null;
ResultSet res = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, statement_key);
stmt.setString(1, contentId.toString());
stmt.setInt(2, content.getSubId());
res = stmt.executeQuery();
if (res.next()) {
sqlFillValues(res, cms, dataset);
} else {
throw new CmsLegacyException("[" + this.getClass().getName() + ".read] no content found for CID:" + contentId + ", SID: " + content.getSubId() + ", statement: " + statement_key, CmsLegacyException.C_NOT_FOUND);
}
if (!checkAccess(content, false)) {
throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, res);
}
}
/**
* Read the lockstate from the database.
* We need this because of someone has maybe stolen the lock.<p>
*
* @param dataset the dataset to read the lockstate into.
* @param subId the subId of this cd
*/
protected void readLockstate(CmsMasterDataSet dataset, int subId) throws CmsException {
PreparedStatement stmt = null;
ResultSet res = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, "read_lockstate_offline");
stmt.setString(1, dataset.m_masterId.toString());
stmt.setInt(2, subId);
res = stmt.executeQuery();
if (res.next()) {
// update the values
dataset.m_lockedInProject = res.getInt(1);
dataset.m_lockedBy = new CmsUUID(res.getString(2));
} else {
// no values found - this is a new row
}
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, res);
}
}
/**
* Reads all media contents from the database.<p>
*
* @param cms the CmsObject to get access to cms resources.
* @param content the CmsMasterContent to write to the database.
* @return a Vector of media objects.
*/
public Vector readMedia(CmsObject cms, CmsMasterContent content) throws CmsException {
if (!content.isReadable()) {
// no read access
throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
Vector retValue = new Vector();
String statement_key = "read_media_offline";
if (isOnlineProject(cms)) {
statement_key = "read_media_online";
}
PreparedStatement stmt = null;
ResultSet res = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, statement_key);
stmt.setString(1, content.getId().toString());
res = stmt.executeQuery();
while (res.next()) {
int i = 1;
retValue.add(new CmsMasterMedia(res.getInt(i++), new CmsUUID(res.getString(i++)), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getString(i++), res.getInt(i++), res.getString(i++), res.getString(i++), res.getString(i++), res.getBytes(i++)));
}
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, res);
}
return retValue;
}
/**
* Reads all channels from the database.<p>
*
* @param cms the CmsObject to get access to cms resources.
* @param content the CmsMasterContent to write to the database.
* @return a Vector of channel names.
*/
public Vector readChannels(CmsObject cms, CmsMasterContent content) throws CmsException {
if (!content.isReadable()) {
// no read access
throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS);
}
Vector retValue = new Vector();
String statement_key = "read_channel_names_offline";
if (isOnlineProject(cms)) {
statement_key = "read_channel_names_online";
}
PreparedStatement stmt = null;
ResultSet res = null;
Connection conn = null;
try {
conn = m_sqlManager.getConnection();
stmt = m_sqlManager.getPreparedStatement(conn, statement_key);
stmt.setString(1, content.getId().toString());
res = stmt.executeQuery();
while (res.next()) {
// get the channel id
String channeldName = res.getString(1);
retValue.add(channeldName );
}
} catch (SQLException exc) {
throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc);
} finally {
m_sqlManager.closeAll(null, conn, stmt, res);
}
return retValue;
}
/**
* Reads all content definitions of a given channel.<p>
*
* @param cms the CmsObject to get access to cms resources.
* @param channelId the id of the channel.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -