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

📄 cmsdbaccess.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        } else {
            // was readable - return true
            return true;
        }
    }

    /**
     * Returns true, if the current project is the online project.<p>
     * 
     * @param cms the CmsObject to get access to cms resources.
     * @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 a master content definition.<p>
     * 
     * @param masterId - the masterId to delete the media for.
     * @throws SQLException if an sql error occurs.
     */
    protected void deleteAllMedia(CmsUUID masterId) throws SQLException {
        String statement_key = "delete_all_media_offline";
        PreparedStatement stmt = null;
        Connection conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, statement_key);
            stmt.setString(1, masterId.toString());
            stmt.executeUpdate();
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
        }
    }

    /**
     * Deletes all channel lines for one master.<p>
     * 
     * @param masterId - the masterId to delete the channels for.
     * @throws SQLException if an sql error occurs.
     */
    protected void deleteAllChannels(CmsUUID masterId) throws SQLException {
        String statement_key = "delete_all_channel_offline";
        PreparedStatement stmt = null;
        Connection conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, statement_key);
            stmt.setString(1, masterId.toString());
            stmt.executeUpdate();
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
        }
    }

    /**
     * Updates the media object of a content definition.<p>
     * 
     * @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 if an sql error occurs.
     * @throws CmsException if an error occurs.
     */
    protected void updateMedia(CmsUUID masterId, Vector mediaToAdd, Vector mediaToUpdate, Vector mediaToDelete) throws SQLException, CmsException {
        // add new media
        PreparedStatement stmt = null;
        Connection conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "insert_media_offline");
            for (int i = 0; i < mediaToAdd.size(); i++) {
                CmsMasterMedia media = (CmsMasterMedia)mediaToAdd.get(i);
                media.setId(CmsDbUtil.nextId(m_poolUrl, "CMS_MODULE_MEDIA"));
                media.setMasterId(masterId);
                sqlFillValues(stmt, media);
                stmt.executeUpdate();
            }
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
        }

        // update existing media
        stmt = null;
        conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "update_media_offline");
            for (int i = 0; i < mediaToUpdate.size(); i++) {
                CmsMasterMedia media = (CmsMasterMedia)mediaToUpdate.get(i);
                media.setMasterId(masterId);
                int rowCounter = sqlFillValues(stmt, media);
                stmt.setInt(rowCounter++, media.getId());
                stmt.setString(rowCounter++, masterId.toString());
                stmt.executeUpdate();
            }
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
        }
        // delete unneeded media
        stmt = null;
        conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "delete_media_offline");
            for (int i = 0; i < mediaToDelete.size(); i++) {
                CmsMasterMedia media = (CmsMasterMedia)mediaToDelete.get(i);
                stmt.setInt(1, media.getId());
                stmt.setString(2, masterId.toString());
                stmt.executeUpdate();
            }
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
        }
    }

    /**
     * Updates the channels of a content definition.<p>
     * 
     * @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 if an sql error occurs.
     */
    protected void updateChannels(CmsObject cms, CmsUUID masterId, Vector channelToAdd, Vector channelToDelete) throws SQLException {
        // add new channel
        PreparedStatement stmt = null;
        Connection conn = null;
        cms.getRequestContext().saveSiteRoot();
        cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
           conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "insert_channel_offline");
            for (int i = 0; i < channelToAdd.size(); i++) {
                try {
                    stmt.setString(1, masterId.toString());


                    //int id=Integer.parseInt(cms.readProperty(channelToAdd.get(i) + "", I_CmsConstants.C_PROPERTY_CHANNELID));
                    //stmt.setInt(2, id);
                    CmsResource channel=cms.readFolder((String)channelToAdd.get(i));
                    String id=channel.getResourceId().toString();
                   
                    stmt.setString(2, id);  
                    stmt.setString(3,(String)channelToAdd.get(i));
                    //    I_CmsConstants.C_PROPERTY_CHANNELID)));
                    stmt.executeUpdate();
                } catch (CmsException exc) {
                    // no channel found - write to logfile
                    if (CmsLog.getLog(this).isWarnEnabled()) {
                        CmsLog.getLog(this).warn("Couldn't find channel " + channelToAdd.get(i), exc);
                    }
                }
            }
            m_sqlManager.closeAll(null, conn, stmt, null);
            
        // delete unneeded channel
        stmt = null;
        conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "delete_channel_offline");
            for (int i = 0; i < channelToDelete.size(); i++) {
                //try {
                    stmt.setString(1, masterId.toString());
                    stmt.setString(2, (String)channelToDelete.get(i) );
                    // stmnt.setInt(2, Integer.parseInt(cms.readProperty(C_COS_PREFIX + channelToDelete.get(i),
                    //     I_CmsConstants.C_PROPERTY_CHANNELID)));
                    stmt.executeUpdate();
                /*} catch (CmsException exc) {
                    // no channel found - write to logfile
                    if (CmsLog.getLog(this).isWarnEnabled()) {
                        CmsLog.getLog(this).warn("Couldn't find channel " + channelToAdd.get(i), exc);
                    }
                }*/
            }
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, null);
            cms.getRequestContext().restoreSiteRoot();
        }
    }

    /**
     * Fills a prepared statement with media values.<p>
     * 
     * @param stmt 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 stmt, CmsMasterMedia media) throws SQLException {
        int i = 1;
        stmt.setInt(i++, media.getId());
        stmt.setString(i++, media.getMasterId().toString());
        stmt.setInt(i++, media.getPosition());
        stmt.setInt(i++, media.getWidth());
        stmt.setInt(i++, media.getHeight());
        stmt.setInt(i++, media.getSize());
        stmt.setString(i++, media.getMimetype());
        stmt.setInt(i++, media.getType());
        stmt.setString(i++, media.getTitle());
        stmt.setString(i++, media.getName());
        stmt.setString(i++, media.getDescription());
        stmt.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 table.<p>
     *
     * @param cms the CmsObject.
     * @param contentDefinitionClass the Class of the current master.
     * @param masterId the id of the master.
     * @param subId the sub id of the master.
     * @return Vector a vector with all versions of the master.
     */
    public Vector getHistory(CmsObject cms, Class contentDefinitionClass, CmsUUID masterId, int subId) throws CmsException {
        Vector retVector = new Vector();
        Vector allBackup = new Vector();
        PreparedStatement stmt = null;
        ResultSet res = null;
        Connection conn = null;
        try {
            conn = m_sqlManager.getConnection();
            stmt = m_sqlManager.getPreparedStatement(conn, "read_all_backup");
            stmt.setString(1, masterId.toString());
            stmt.setInt(2, subId);
            // gets all versions of the master in the backup table
            res = stmt.executeQuery();
            while (res.next()) {
                CmsMasterDataSet dataset = new CmsMasterDataSet();
                sqlFillValues(res, cms, dataset);
                dataset.m_versionId = res.getInt("TAG_ID");
                dataset.m_userName = res.getString("USER_NAME");
                dataset.m_groupName = res.getString("GROUP_NAME");
                dataset.m_lastModifiedByName = res.getString("LASTMODIFIED_BY_NAME");
                allBackup.add(dataset);
            }
            retVector = createVectorOfCd(allBackup, contentDefinitionClass, cms);
        } catch (SQLException e) {
            throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, e);
        } finally {
            m_sqlManager.closeAll(null, conn, stmt, res);
        }
        return retVector;
    }

    /**
     * Returns the version of a master in the backup.<p>
     *
     * @param cms The CmsObject.
     * @param contentDefinitionClass The class of the content definition.
     * @param masterId the id of the master.
     * @param subId the sub id.
     * @param versionId the version id.
     * @return CmsMasterContent a content definition of the version.
     */
    public CmsMasterContent getVersionFromHistory(CmsObject cms, Class contentDefinitionClass, CmsUUID masterId, int subId, int versionId) throws CmsException {
        CmsMasterContent content = null;
        CmsMasterDataSet dataset = this.getVersionFromHistory(cms, masterId, subId, versionId);
        Constructor constructor;
        try { // to get the constructor to create an empty contentDefinition
            constructor = contentDefinitionClass.getConstructor(new Class[] { CmsObject.class, CmsMasterDataSet.class });
        } catch (NoSuchMethodException exc) {
            if (CmsLog.getLog(this).isWarnEnabled()) {
                CmsLog.getLog(this).warn("Cannot locate constructor", exc);
            }
            // canno't fill the vector - missing constructor
            return content;
        }
        // create content definition for each dataset
        if (dataset != null) {
            try { // to invoce the constructor to get a new empty instance
                content = (CmsMasterContent)constructor.newInstance(new Object[] { cms, dataset });
            } catch (Exception exc) {
                if (CmsLog.getLog(this).isWarnEnabled()) {
                    CmsLog.getLog(this).warn("Cannot invoke constructor", exc);
                }
            }
        }
        return content;
    }

    /**
     * Returns the version of a maste

⌨️ 快捷键说明

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