⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        }
    }

    /**
     * @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.
     */
    public void read(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset, int masterId)
        throws CmsException {
        if(!content.isReadable()) {
            // no read access
            throw new CmsException("Not readable", CmsException.C_NO_ACCESS);
        }
        String statement_key = "read_offline";
        String poolToUse = m_poolName;
        if(isOnlineProject(cms)) {
            statement_key = "read_online";
            poolToUse = m_onlinePoolName;
        }

        PreparedStatement stmnt = null;
        ResultSet res = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(poolToUse);
            stmnt = sqlPrepare(con, statement_key);
            stmnt.setInt(1, masterId);
            stmnt.setInt(2, content.getSubId());
            res = stmnt.executeQuery();
            if(res.next()) {
                sqlFillValues(res, cms, dataset);
            } else {
                throw new CmsException("Row not found: " + masterId + " " + content.getSubId(), CmsException.C_NOT_FOUND);
            }
            if(!checkAccess(cms, content, false)) {
                throw new CmsException("Not readable", CmsException.C_NO_ACCESS);
            }
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, res);
        }
    }

    /**
     * Read the lockstate from the database.
     * We nned this, because of someone has maybe stolen the lock.
     * @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 stmnt = null;
        ResultSet res = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "read_lockstate_offline");
            stmnt.setInt(1, dataset.m_masterId);
            stmnt.setInt(2, subId);
            res = stmnt.executeQuery();
            if(res.next()) {
                // update the values
                dataset.m_lockedInProject = res.getInt(1);
                dataset.m_lockedBy = res.getInt(2);
            } else {
                // no values found - this is a new row
            }
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, res);
        }
    }

    /**
     * Reads all media from the database.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @returns a Vector of media objects.
     */
    public Vector readMedia(CmsObject cms, CmsMasterContent content)
        throws CmsException {
        if(!content.isReadable()) {
            // no read access
            throw new CmsException("Not readable", CmsException.C_NO_ACCESS);
        }
        Vector retValue = new Vector();
        String statement_key = "read_media_offline";
        String poolToUse = m_poolName;
        if(isOnlineProject(cms)) {
            statement_key = "read_media_online";
            poolToUse = m_onlinePoolName;
        }

        PreparedStatement stmnt = null;
        ResultSet res = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(poolToUse);
            stmnt = sqlPrepare(con, statement_key);
            stmnt.setInt(1, content.getId());
            res = stmnt.executeQuery();
            while(res.next()) {
                int i = 1;
                retValue.add(new CmsMasterMedia (
                    res.getInt(i++),
                    res.getInt(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 CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, res);
        }
        return retValue;
    }

    /**
     * Reads all channels from the database.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param content the CmsMasterContent to write to the database.
     * @returns a Vector of channel names.
     */
    public Vector readChannels(CmsObject cms, CmsMasterContent content)
        throws CmsException {
        if(!content.isReadable()) {
            // no read access
            throw new CmsException("Not readable", CmsException.C_NO_ACCESS);
        }
        Vector retValue = new Vector();
        String statement_key = "read_channel_offline";
        String poolToUse = m_poolName;
        if(isOnlineProject(cms)) {
            statement_key = "read_channel_online";
            poolToUse = m_onlinePoolName;
        }

        PreparedStatement stmnt = null;
        ResultSet res = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(poolToUse);
            stmnt = sqlPrepare(con, statement_key);
            stmnt.setInt(1, content.getId());
            res = stmnt.executeQuery();
            while(res.next()) {
                // get the channel id
                int channeldId = res.getInt(1);
                // read the resource by property "channelid"
                cms.setContextToCos();
                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 - switch to next channel
                }
                cms.setContextToVfs();
                if(resources.size() >= 1) {
                    // add the name of the channel to the ret-value
                    retValue.add(((CmsResource)resources.get(0)).getAbsolutePath());
                }
            }
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, res);
        }
        return retValue;
    }

    /**
     * Reads all content definitions of a given channel
     * @param cms the CmsObject to get access to cms-ressources.
     * @param channelId the id of the channel.
     * @return Vector The datasets of the contentdefinitions in the channel
     */
    public Vector readAllByChannel(CmsObject cms, int channelId, int subId)
        throws CmsException {
        Vector theDataSets = new Vector();
        String statement_key = "readallbychannel_offline";
        String poolToUse = m_poolName;
        if(isOnlineProject(cms)) {
            statement_key = "readallbychannel_online";
            poolToUse = m_onlinePoolName;
        }

        PreparedStatement stmnt = null;
        ResultSet res = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(poolToUse);
            stmnt = sqlPrepare(con, statement_key);
            stmnt.setInt(1, subId);
            stmnt.setInt(2, channelId);
            res = stmnt.executeQuery();
            while(res.next()) {
                CmsMasterDataSet dataset = new CmsMasterDataSet();
                sqlFillValues(res, cms, dataset);
                theDataSets.add(dataset);
            }
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, res);
        }
        return theDataSets;
    }

    /**
     * @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.
     */
    public void delete(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't delete from the online project", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_versionId != I_CmsConstants.C_UNKNOWN_ID) {
            // this is not the online row - it was read from history
            // don't delete it!
            throw new CmsException("Can't delete a backup cd ", CmsException.C_NO_ACCESS);
        }
        // read the lockstate
        readLockstate(dataset, content.getSubId());
        if((dataset.m_lockedBy != cms.getRequestContext().currentUser().getId())) {
            // is not locked by this user
            throw new CmsException("Not locked by this user", CmsException.C_NO_ACCESS);
        }
        if(dataset.m_lockedInProject != dataset.m_projectId) {
            // not locked in this project
            throw new CmsException("Not locked in this project", CmsException.C_NO_ACCESS);
        }
        if(!content.isWriteable()) {
            // no write access
            throw new CmsException("Not writeable", CmsException.C_NO_ACCESS);
        }

        if(dataset.m_state == I_CmsConstants.C_STATE_NEW) {
            // this is a new line in this project and can be deleted
            String statement_key = "delete_offline";
            PreparedStatement stmnt = null;
            Connection con = null;
            try {
                con = DriverManager.getConnection(m_poolName);
                stmnt = sqlPrepare(con, statement_key);
                stmnt.setInt(1, dataset.m_masterId);
                stmnt.setInt(2, content.getSubId());
                if(stmnt.executeUpdate() != 1) {
                    // no line deleted - row wasn't found
                    throw new CmsException("Row not found: " + dataset.m_masterId + " " + content.getSubId(), CmsException.C_NOT_FOUND);
                }
                // after deleting the row, we have to delete media and channel rows
                deleteAllMedia(dataset.m_masterId);
                deleteAllChannels(dataset.m_masterId);
            } catch(SQLException exc) {
                throw new CmsException(CmsException.C_SQL_ERROR, exc);
            } finally {
                sqlClose(con, stmnt, null);
            }
        } else {
            // set state to deleted and update the line
            String statement_key = "update_offline";
            dataset.m_state = I_CmsConstants.C_STATE_DELETED;
            dataset.m_lockedBy = I_CmsConstants.C_UNKNOWN_ID;
            PreparedStatement stmnt = null;
            Connection con = null;
            try {
                con = DriverManager.getConnection(m_poolName);
                stmnt = sqlPrepare(con, "update_offline");
                int rowcounter = sqlFillValues(stmnt, content.getSubId(), dataset);
                stmnt.setInt(rowcounter++, dataset.m_masterId);
                stmnt.setInt(rowcounter++, content.getSubId());
                stmnt.executeUpdate();
            } catch(SQLException exc) {
                throw new CmsException(CmsException.C_SQL_ERROR, exc);
            } finally {
                sqlClose(con, stmnt, null);
            }
        }
    }

    /**
     * @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.
     */
    public void undelete(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset)
        throws CmsException {
        if(isOnlineProject(cms)) {
            // this is the onlineproject - don't write into this project directly
            throw new CmsException("Can't undelete from the online project", CmsException.C_NO_ACCESS);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -