📄 cmsdbaccess.java
字号:
CmsMasterDataSet dataset = new CmsMasterDataSet();
try { // to invoce the constructor to get a new empty instance
CmsMasterContent content = (CmsMasterContent)constructor.newInstance(new Object[]{cms, dataset});
sqlFillValues(res, cms, dataset);
// add the cd only if read (and visible) permissions are granted.
// the visible-permissens will be checked, if viewonly is set to true
// viewonly=true is needed for the backoffice
if(checkAccess(content, viewonly)) {
retValue.add(content);
}
} catch(Exception exc) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && CmsBase.isLogging()) {
CmsBase.log(I_CmsLogChannels.C_MODULE_DEBUG, "[CmsDbAccess] Cannot invoce constructor: " + exc.getMessage());
}
}
}
return retValue;
}
/**
* Returns a vector of contentdefinitions based on the sql resultset.
* @param datasets - the vector with the datasets.
* @param contentDefinitionClass - the class of the cd to create new instances.
* @param cms - the CmsObject to get access to cms-ressources.
* @throws SqlException if nothing could be read from the resultset.
*/
protected Vector createVectorOfCd(Vector datasets, Class contentDefinitionClass, CmsObject cms)
throws SQLException {
Constructor constructor;
Vector retValue = new Vector();
try { // to get the constructor to create an empty contentDefinition
constructor = contentDefinitionClass.getConstructor(new Class[]{CmsObject.class, CmsMasterDataSet.class});
} catch(NoSuchMethodException exc) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && CmsBase.isLogging()) {
CmsBase.log(I_CmsLogChannels.C_MODULE_DEBUG, "[CmsDbAccess] Cannot locate constructor: " + exc.getMessage());
}
// canno't fill the vector - missing constructor
return retValue;
}
// create content definition for each dataset
for(int i=0; i < datasets.size(); i++) {
CmsMasterDataSet dataset = (CmsMasterDataSet)datasets.elementAt(i);
try { // to invoce the constructor to get a new empty instance
CmsMasterContent content = (CmsMasterContent)constructor.newInstance(new Object[]{cms, dataset});
retValue.add(content);
} catch(Exception exc) {
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && CmsBase.isLogging()) {
CmsBase.log(I_CmsLogChannels.C_MODULE_DEBUG, "[CmsDbAccess] Cannot invoce constructor: " + exc.getMessage());
}
}
}
return retValue;
}
/**
* Checks if read (and visible) permissions are granted.
* the visible-permissens will be checked, if viewonly is set to true
* viewonly=true is needed for the backoffice
* @param cms - the CmsObject to get access to cms-ressources.
* @param content - the cd to check.
* @param viewonly - if set to true the v-Flag will be checked, too
*/
protected boolean checkAccess(CmsMasterContent content, boolean viewonly) {
if(!content.isReadable()) {
// was not readable
return false;
} else if(viewonly) {
// additional check for v-Flags
return content.isVisible();
} else {
// was readable - return true
return true;
}
}
/**
* Returns true, if this is the onlineproject
* @param cms - the CmsObject to get access to cms-ressources.
* @return true, if this is the onlineproject, else returns false
*/
protected boolean isOnlineProject(CmsObject cms) {
return cms.getRequestContext().currentProject().isOnlineProject();
}
/**
* Deletes all media lines for one master.
* @param masterId - the masterId to delete the media for
* @throws SQLException if an sql-error occur
*/
protected void deleteAllMedia(int masterId) throws SQLException {
String statement_key = "delete_all_media_offline";
PreparedStatement stmnt = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, statement_key);
stmnt.setInt(1, masterId);
stmnt.executeUpdate();
} finally {
sqlClose(con, stmnt, null);
}
}
/**
* Deletes all channel lines for one master.
* @param masterId - the masterId to delete the media for
* @throws SQLException if an sql-error occur
*/
protected void deleteAllChannels(int masterId) throws SQLException {
String statement_key = "delete_all_channel_offline";
PreparedStatement stmnt = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, statement_key);
stmnt.setInt(1, masterId);
stmnt.executeUpdate();
} finally {
sqlClose(con, stmnt, null);
}
}
/**
* Updates the media object of a content definition.
*
* @param masterId the content definition master id
* @param mediaToAdd vector of media objects to add
* @param mediaToUpdate vector of media objects to update
* @param mediaToDelete vector of media objects to delete
* @throws SQLException
* @throws CmsException
*/
protected void updateMedia(int masterId, Vector mediaToAdd,
Vector mediaToUpdate, Vector mediaToDelete)
throws SQLException, CmsException {
// add new media
PreparedStatement stmnt = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, "insert_media_offline");
for(int i = 0; i < mediaToAdd.size(); i++) {
CmsMasterMedia media = (CmsMasterMedia) mediaToAdd.get(i);
media.setId(CmsIdGenerator.nextId(m_poolName, "CMS_MODULE_MEDIA"));
media.setMasterId(masterId);
sqlFillValues(stmnt, media);
stmnt.executeUpdate();
}
} finally {
sqlClose(con, stmnt, null);
}
// update existing media
stmnt = null;
con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, "update_media_offline");
for(int i = 0; i < mediaToUpdate.size(); i++) {
CmsMasterMedia media = (CmsMasterMedia) mediaToUpdate.get(i);
media.setMasterId(masterId);
int rowCounter = sqlFillValues(stmnt, media);
stmnt.setInt(rowCounter++, media.getId());
stmnt.setInt(rowCounter++, masterId);
stmnt.executeUpdate();
}
} finally {
sqlClose(con, stmnt, null);
}
// delete unneeded media
stmnt = null;
con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, "delete_media_offline");
for(int i = 0; i < mediaToDelete.size(); i++) {
CmsMasterMedia media = (CmsMasterMedia) mediaToDelete.get(i);
stmnt.setInt(1, media.getId());
stmnt.setInt(2, masterId);
stmnt.executeUpdate();
}
} finally {
sqlClose(con, stmnt, null);
}
}
/**
* Updates the channels of a content definition.
*
* @param cms the current context object
* @param masterId the content definition master id
* @param channelToAdd vector of channels to add
* @param channelToDelete vector of channels to delete
* @throws SQLException
*/
protected void updateChannels(CmsObject cms, int masterId, Vector channelToAdd,
Vector channelToDelete) throws SQLException {
// add new channel
PreparedStatement stmnt = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, "insert_channel_offline");
for(int i = 0; i < channelToAdd.size(); i++) {
try {
stmnt.setInt(1, masterId);
cms.setContextToCos();
stmnt.setInt(2, Integer.parseInt(cms.readProperty(channelToAdd.get(i)+"",
I_CmsConstants.C_PROPERTY_CHANNELID)));
cms.setContextToVfs();
// stmnt.setInt(2, Integer.parseInt(cms.readProperty(C_COS_PREFIX + channelToAdd.get(i),
// I_CmsConstants.C_PROPERTY_CHANNELID)));
stmnt.executeUpdate();
} catch(CmsException exc) {
// no channel found - write to logfile
if(CmsBase.isLogging()) {
CmsBase.log(CmsBase.C_MODULE_DEBUG, "[CmsDbAccess] Couldn't find channel " + channelToAdd.get(i) + " errormessage: " + exc.getMessage());
}
}
}
} finally {
sqlClose(con, stmnt, null);
}
// delete unneeded channel
stmnt = null;
con = null;
try {
con = DriverManager.getConnection(m_poolName);
stmnt = sqlPrepare(con, "delete_channel_offline");
for(int i = 0; i < channelToDelete.size(); i++) {
try {
stmnt.setInt(1, masterId);
cms.setContextToCos();
stmnt.setInt(2, Integer.parseInt(cms.readProperty(channelToDelete.get(i)+"",
I_CmsConstants.C_PROPERTY_CHANNELID)));
cms.setContextToVfs();
// stmnt.setInt(2, Integer.parseInt(cms.readProperty(C_COS_PREFIX + channelToDelete.get(i),
// I_CmsConstants.C_PROPERTY_CHANNELID)));
stmnt.executeUpdate();
} catch(CmsException exc) {
// no channel found - write to logfile
if(CmsBase.isLogging()) {
CmsBase.log(CmsBase.C_MODULE_DEBUG, "[CmsDbAccess] Couldn't find channel " + channelToAdd.get(i) + " errormessage: " + exc.getMessage());
}
}
}
} finally {
sqlClose(con, stmnt, null);
}
}
/**
* Fills a prepared statement with media values.
*
* @param stmnt the statement to fill
* @param media the data to fill the statement with
* @return int the number of values set in the statement
* @throws SQLException if data could not be set in statement
*/
protected int sqlFillValues(PreparedStatement stmnt, CmsMasterMedia media)
throws SQLException {
int i = 1;
stmnt.setInt(i++, media.getId());
stmnt.setInt(i++, media.getMasterId());
stmnt.setInt(i++, media.getPosition());
stmnt.setInt(i++, media.getWidth());
stmnt.setInt(i++, media.getHeight());
stmnt.setInt(i++, media.getSize());
stmnt.setString(i++, media.getMimetype());
stmnt.setInt(i++, media.getType());
stmnt.setString(i++, media.getTitle());
stmnt.setString(i++, media.getName());
stmnt.setString(i++, media.getDescription());
stmnt.setBinaryStream(i++, new ByteArrayInputStream(media.getMedia()), media.getMedia().length);
//stmnt.setBytes(i++, media.getMedia());
return i;
}
/**
* Returns a vector with all version of a master in the backup
*
* @param cms The CmsObject
* @param masterId The id of the master
* @param subId The sub_id
* @return Vector A vector with all versions of the master
*/
public Vector getHistory(CmsObject cms, Class contentDefinitionClass, int masterId, int subId) throws CmsException{
Vector retVector = new Vector();
Vector allBackup = new Vector();
PreparedStatement stmnt = null;
ResultSet res = null;
Connection con = null;
try {
con = DriverManager.getConnection(m_backupPoolName);
stmnt = sqlPrepare(con, "read_all_backup");
stmnt.setInt(1, masterId);
stmnt.setInt(2, subId);
// gets all versions of the master in the backup table
res = stmnt.executeQuery();
while(res.next()) {
CmsMasterDataSet dataset = new CmsMasterDataSet();
sqlFillValues(res, cms, dataset);
dataset.m_versionId = res.getInt("VERSION_ID");
dataset.m_userName = res.getString("USER_NAME");
dataset.m_groupName
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -