📄 cmsdbaccess.java
字号:
/**
* Inserts all values to the statement for insert and update.
* @param res the Resultset read the values from.
* @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.
* @returns the actual rowcounter.
*/
protected int sqlFillValues(ResultSet res, CmsObject cms, CmsMasterDataSet dataset)
throws SQLException {
// columncounter
int i = 1;
//// COREDATA ////
dataset.m_masterId = res.getInt(i++);
res.getInt(i++); // we don't have to store the sub-id
dataset.m_userId = res.getInt(i++);
dataset.m_groupId = res.getInt(i++);
dataset.m_lockedInProject = res.getInt(i++);
// compute project based on the current project and the channels
dataset.m_projectId = computeProjectId(cms, dataset);
dataset.m_accessFlags = res.getInt(i++);
dataset.m_state = res.getInt(i++);
dataset.m_lockedBy =res.getInt(i++);
dataset.m_lastModifiedBy = res.getInt(i++);
dataset.m_dateCreated = res.getTimestamp(i++).getTime();
dataset.m_dateLastModified = res.getTimestamp(i++).getTime();
//// USERDATA ////
dataset.m_publicationDate = res.getTimestamp(i++).getTime();
dataset.m_purgeDate = res.getTimestamp(i++).getTime();
dataset.m_flags = res.getInt(i++);
dataset.m_feedId = res.getInt(i++);
dataset.m_feedReference = res.getInt(i++);
dataset.m_feedFilename = res.getString(i++);
dataset.m_title = res.getString(i++);;
//// GENERIC DATA ////
i = sqlSetTextArray(res, dataset.m_dataBig, i);
i = sqlSetTextArray(res, dataset.m_dataMedium, i);
i = sqlSetTextArray(res, dataset.m_dataSmall, i);
i = sqlSetIntArray(res, dataset.m_dataInt, i);
i = sqlSetIntArray(res, dataset.m_dataReference, i);
i = sqlSetDateArray(res, dataset.m_dataDate, i);
return i;
}
/**
* Computes the correct project id based on the channels.
*/
protected int computeProjectId(CmsObject cms, CmsMasterDataSet dataset) throws SQLException {
int onlineProjectId = I_CmsConstants.C_UNKNOWN_ID;
int offlineProjectId = I_CmsConstants.C_UNKNOWN_ID;
try {
offlineProjectId = cms.getRequestContext().currentProject().getId();
onlineProjectId = cms.onlineProject().getId();
} catch(CmsException exc) {
// ignore the exception
}
if(!isOnlineProject(cms)) {
// this is a offline project -> compute if we have to return the
// online project id or the offline project id
// the owner and the administrtor has always access
try {
if( (cms.getRequestContext().currentUser().getId() == dataset.m_userId) ||
cms.isAdmin()) {
return offlineProjectId;
}
} catch(CmsException exc) {
// ignore the exception -> we are not admin
}
String statement_key = "read_channel_offline";
String poolToUse = m_poolName;
PreparedStatement stmnt = null;
ResultSet res = null;
Connection con = null;
try {
cms.setContextToCos();
con = DriverManager.getConnection(poolToUse);
stmnt = sqlPrepare(con, statement_key);
stmnt.setInt(1, dataset.m_masterId);
res = stmnt.executeQuery();
while(res.next()) {
// get the channel id
int channeldId = res.getInt(1);
// read the resource by property "channelid"
Vector resources = new Vector();
try {
resources = cms.getResourcesWithProperty(I_CmsConstants.C_PROPERTY_CHANNELID, channeldId+"", I_CmsConstants.C_TYPE_FOLDER);
} catch(CmsException exc) {
// ignore the exception - read the next one
}
if(resources.size() >= 1) {
int resProjectId = ((CmsResource)resources.get(0)).getProjectId();
if(resProjectId == offlineProjectId) {
// yes - we have found a chanel that belongs to
// the current offlineproject -> we can return the
// offline project id as computed project id
return offlineProjectId;
}
}
}
} finally {
cms.setContextToVfs();
sqlClose(con, stmnt, res);
}
}
// no channel found, that belongs to the offlineproject ->
// return the online project id.
return onlineProjectId;
}
/**
* Sets an array of strings into the stmnt.
* @param stmnt the PreparedStatement to set the values into.
* @param array the array of strings to set.
* @param the columnscounter for the stmnt.
* @returns the increased columnscounter;
*/
protected int sqlSetTextArray(PreparedStatement stmnt, String[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
if(array[j] == null) {
stmnt.setNull(columnscounter++,Types.LONGVARCHAR);
} else {
stmnt.setString(columnscounter++,array[j]);
}
}
return columnscounter;
}
/**
* Sets an array of strings from the resultset.
* @param res the ResultSet to get the values from.
* @param array the array of strings to set.
* @param the columnscounter for the res.
* @returns the increased columnscounter;
*/
protected int sqlSetTextArray(ResultSet res, String[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
array[j] = res.getString(columnscounter++);
}
return columnscounter;
}
/**
* Sets an array of ints into the stmnt.
* @param stmnt the PreparedStatement to set the values into.
* @param array the array of ints to set.
* @param the columnscounter for the stmnt.
* @returns the increased columnscounter;
*/
protected int sqlSetIntArray(PreparedStatement stmnt, int[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
stmnt.setInt(columnscounter++,array[j]);
}
return columnscounter;
}
/**
* Sets an array of ints from the resultset.
* @param res the ResultSet to get the values from.
* @param array the array of ints to set.
* @param the columnscounter for the res.
* @returns the increased columnscounter;
*/
protected int sqlSetIntArray(ResultSet res, int[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
array[j] = res.getInt(columnscounter++);
}
return columnscounter;
}
/**
* Sets an array of ints into the stmnt.
* @param stmnt the PreparedStatement to set the values into.
* @param array the array of longs to set.
* @param the columnscounter for the stmnt.
* @returns the increased columnscounter;
*/
protected int sqlSetDateArray(PreparedStatement stmnt, long[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
stmnt.setTimestamp(columnscounter++,new Timestamp(array[j]));
}
return columnscounter;
}
/**
* Sets an array of ints from the resultset.
* @param res the ResultSet to get the values from.
* @param array the array of longs to set.
* @param the columnscounter for the res.
* @returns the increased columnscounter;
*/
protected int sqlSetDateArray(ResultSet res, long[] array, int columnscounter)
throws SQLException {
for(int j = 0; j < array.length; j++) {
array[j] = res.getTimestamp(columnscounter++).getTime();
}
return columnscounter;
}
/**
* Returns a vector of contentdefinitions based on the sql resultset.
* Never mind about the visible flag.
* @param res - the ResultSet to get data-lines from.
* @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(ResultSet res, Class contentDefinitionClass, CmsObject cms)
throws SQLException {
return createVectorOfCd(res, contentDefinitionClass, cms, false);
}
/**
* Returns a vector of contentdefinitions based on the sql resultset.
* @param res - the ResultSet to get data-lines from.
* @param contentDefinitionClass - the class of the cd to create new instances.
* @param cms - the CmsObject to get access to cms-ressources.
* @param viewonly - decides, if only the ones that are visible should be returned
* @throws SqlException if nothing could be read from the resultset.
*/
protected Vector createVectorOfCd(ResultSet res, Class contentDefinitionClass, CmsObject cms, boolean viewonly)
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;
}
while(res.next()) { // while there is data in the resultset
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(cms, 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -