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

📄 cmsdbaccess.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        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 undelete a backup cd ", CmsException.C_NO_ACCESS);
        }
        if(!content.isWriteable()) {
            // no write access
            throw new CmsException("Not writeable", CmsException.C_NO_ACCESS);
        }
        // set state to deleted and update the line
        String statement_key = "update_offline";
        dataset.m_state = I_CmsConstants.C_STATE_CHANGED;
        dataset.m_lockedBy = cms.getRequestContext().currentUser().getId();
        dataset.m_lockedInProject = cms.getRequestContext().currentProject().getId();
        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);
        }
    }

    /**
     * Changes the perrmissions of the Master
     *
     * @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 changePermissions(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 change permissions in 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 change permissions of 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){
            dataset.m_state = I_CmsConstants.C_STATE_CHANGED;
        }
        dataset.m_dateLastModified = System.currentTimeMillis();
        dataset.m_lastModifiedBy = cms.getRequestContext().currentUser().getId();
        // update the line
        PreparedStatement stmnt = null;
        Connection con = null;
        try {
            con = DriverManager.getConnection(m_poolName);
            stmnt = sqlPrepare(con, "update_permissions_offline");
            stmnt.setInt(1, dataset.m_userId);
            stmnt.setInt(2, dataset.m_groupId);
            stmnt.setInt(3, dataset.m_accessFlags);
            stmnt.setInt(4, dataset.m_state);
            stmnt.setInt(5, dataset.m_lastModifiedBy);
            stmnt.setTimestamp(6, new Timestamp(dataset.m_dateLastModified));
            stmnt.setInt(7, dataset.m_masterId);
            stmnt.setInt(8, content.getSubId());
            stmnt.executeUpdate();
        } catch(SQLException exc) {
            throw new CmsException(CmsException.C_SQL_ERROR, exc);
        } finally {
            sqlClose(con, stmnt, null);
        }
    }

    /**
     * Returns a string representation of this instance.
     * This can be used for debugging.
     * @returns the string representation of this instance.
     */
    public String toString() {
        StringBuffer returnValue = new StringBuffer();
        returnValue.append(this.getClass().getName() + "{");
        returnValue.append("m_poolName="+m_poolName+";");
        returnValue.append("m_backupPoolName="+m_backupPoolName+";");
        returnValue.append("m_onlinePoolName="+m_onlinePoolName+";");
        returnValue.append("m_queries="+m_queries + "}");
        return returnValue.toString();
    }

    /**
     * Loads recursively all query.properties from all packages of the
     * superclasses. This method calls recuresively itself with the superclass
     * (if exists) as parameter.
     *
     * @param the currentClass of the dbaccess module.
     */
    private void loadQueries(Class currentClass) {
        // creates the queryFilenam from the packagename and
        // filename query.properties
        String className = currentClass.getName();
        String queryFilename = className.substring(0, className.lastIndexOf('.'));
        queryFilename = queryFilename.replace('.','/') + "/query.properties";
        // gets the superclass and calls this method recursively
        Class superClass = currentClass.getSuperclass();
        if(superClass != java.lang.Object.class) {
            loadQueries(superClass);
        }
        try {
            // load the queries. Entries of the most recent class will overwrite
            // entries of superclasses.
            m_queries.load(getClass().getClassLoader().getResourceAsStream(queryFilename));
        } catch(Exception exc) {
            // no query.properties found - write to logstream.
            if(CmsBase.isLogging()) {
                CmsBase.log(CmsBase.C_MODULE_DEBUG, "[CmsDbAccess] Couldn't load " + queryFilename + " errormessage: " + exc.getMessage());
            }
        }
    }

    /**
     * Combines the queries in the properties to complete quereis. Therefor a
     * replacement is needed: The follwing Strings will be replaces
     * automatically by the corresponding property-entrys:
     * ${property_key}
     */
    private void combineQueries() {
        Enumeration keys = m_queries.keys();
        while(keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            // replace while there has benn repacements performaend
            while(replace(key));
        }
    }

    /**
     * Computes one run of the replacement for one query.
     * Stores the new value into m_queries.
     * @param key the key for the query to compute.
     * @returns if in this run replacements are done.
     */
    private boolean replace(String key) {
        boolean retValue = false;
        String value = m_queries.getProperty(key);
        String newValue = new String();
        int index = 0;
        int lastIndex = 0;
        // run as long as there are "${" strings found
        while(index != -1) {
            index = value.indexOf("${", lastIndex);
            if(index != -1) {
                retValue = true;
                int nextIndex = value.indexOf('}', index);
                if(nextIndex != -1) {
                    // get the replacer key
                    String replacer = value.substring(index+2, nextIndex);
                    // copy the first part of the query
                    newValue += value.substring(lastIndex, index);
                    // copy the replcement-value
                    newValue += m_queries.getProperty(replacer, "");
                    // set up lastindex
                    lastIndex = nextIndex+1;
                } else {
                    // no key found, just copy the query-part
                    newValue += value.substring(lastIndex, index+2);
                    // set up lastindex
                    lastIndex = index+2;
                }
            } else {
                // end of the string, copy the tail into new value
                newValue += value.substring(lastIndex);
            }
        }
        // put back the new query to the queries
        m_queries.put(key, newValue);
        // returns true, if replacements were made in this run
        return retValue;
    }

    /**
     * Creates a new connection and prepares a stement.
     * @param con the Connection to use.
     * @param queryKey the key for the query to use. The query will be get
     * by m_queries.getParameter(key)
     */
    protected PreparedStatement sqlPrepare(Connection con, String queryKey) throws SQLException {
        return con.prepareStatement(m_queries.getProperty(queryKey, ""));
    }

    /**
     * Creates a new connection and prepares a stement.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param con the Connection to use.
     * @param queryKey the key for the query to use. The query will be get
     * by m_queries.getParameter(key)
     */
    protected PreparedStatement sqlPrepare(CmsObject cms, Connection con, String queryKey) throws SQLException {
        String statement = m_queries.getProperty(queryKey, "");
        String moduleMaster;
        String channelRel;
        String media;
        if(isOnlineProject(cms)) {
            moduleMaster = "CMS_MODULE_ONLINE_MASTER";
            channelRel = "CMS_MODULE_ONLINE_CHANNEL_REL";
            media = "CMS_MODULE_ONLINE_MEDIA";
        } else {
            moduleMaster = "CMS_MODULE_MASTER";
            channelRel = "CMS_MODULE_CHANNEL_REL";
            media = "CMS_MODULE_MEDIA";
        }
        statement = Utils.replace(statement, "$CMS_MODULE_MASTER", moduleMaster);
        statement = Utils.replace(statement, "$CMS_MODULE_CHANNEL_REL", channelRel);
        statement = Utils.replace(statement, "$CMS_MODULE_MEDIA", media);
        return con.prepareStatement(statement);
    }

    /**
     * Closes all sql ressources.
     * @param con the Connection to close.
     * @param stmnt the SqlStatement to close.
     * @param resr the ResultSet to close.
     */
    protected void sqlClose(Connection con, Statement stmnt, ResultSet res) {
        try {
            res.close();
        } catch(Exception exc) {
            // ignore the exception
            // this was null or already closed
        }
        try {
            stmnt.close();
        } catch(Exception exc) {
            // ignore the exception
            // this was null or already closed
        }
        try {
        con.close();
        } catch(Exception exc) {
            // ignore the exception
            // this was null or already closed
        }
    }

    /**
     * Inserts all values to the statement for insert and update.
     * @param stmnt the Statement to fill the values to.
     * @param cms the CmsObject to get access to cms-ressources.
     * @param subId the subid of this module.
     * @param dataset the set of data for this contentdefinition.
     * @returns the actual rowcounter.
     */
    protected int sqlFillValues(PreparedStatement stmnt, int subId, CmsMasterDataSet dataset)
        throws SQLException {
        // columncounter
        int i = 1;
        //// COREDATA ////
        stmnt.setInt(i++,dataset.m_masterId);
        stmnt.setInt(i++,subId);
        stmnt.setInt(i++,dataset.m_userId);
        stmnt.setInt(i++,dataset.m_groupId);
        stmnt.setInt(i++,dataset.m_lockedInProject);
        stmnt.setInt(i++,dataset.m_accessFlags);
        stmnt.setInt(i++,dataset.m_state);
        stmnt.setInt(i++,dataset.m_lockedBy);
        stmnt.setInt(i++,dataset.m_lastModifiedBy);
        stmnt.setTimestamp(i++,new Timestamp(dataset.m_dateCreated));
        stmnt.setTimestamp(i++,new Timestamp(dataset.m_dateLastModified));
        //// USERDATA ////
        stmnt.setTimestamp(i++,new Timestamp(dataset.m_publicationDate));
        stmnt.setTimestamp(i++,new Timestamp(dataset.m_purgeDate));
        stmnt.setInt(i++,dataset.m_flags);
        stmnt.setInt(i++,dataset.m_feedId);
        stmnt.setInt(i++,dataset.m_feedReference);
        if(dataset.m_feedFilename == null){
            stmnt.setNull(i++,Types.VARCHAR);
        } else {
            stmnt.setString(i++,dataset.m_feedFilename);
        }
        if(dataset.m_title == null){
            stmnt.setNull(i++,Types.VARCHAR);
        } else {
            stmnt.setString(i++,dataset.m_title);
        }
        //// GENERIC DATA ////
        i = sqlSetTextArray(stmnt, dataset.m_dataBig, i);
        i = sqlSetTextArray(stmnt, dataset.m_dataMedium, i);
        i = sqlSetTextArray(stmnt, dataset.m_dataSmall, i);
        i = sqlSetIntArray(stmnt, dataset.m_dataInt, i);
        i = sqlSetIntArray(stmnt, dataset.m_dataReference, i);
        i = sqlSetDateArray(stmnt, dataset.m_dataDate, i);
        return i;
    }

⌨️ 快捷键说明

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