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

📄 dbforum.java

📁 java开发的一套非常好用的oa系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  stmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            // Add the thread count to cache
            threadCountCache.add(query, new CacheableInt(threadCount));
            return threadCount;
        }
    }

    public int getMessageCount() {
        return getMessageCount(DEFAULT_MESSAGE_FILTER);
    }

    public int getMessageCount(ResultFilter resultFilter) {
        String query = getMessageListSQL(resultFilter, true);
        CacheableInt count = (CacheableInt)messageCountCache.get(query);
        // If already in cache, return the count.
        if (count != null) {
            return count.getInt();
        }
        // Otherwise, we have to load the count from the db.
        else {
            int messageCount = 0;
            Connection con = null;
            Statement stmt = null;
            try {
                con = ConnectionManager.getConnection();
                stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                rs.next();
                messageCount = rs.getInt(1);
            }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  stmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            // Add the thread count to cache
            messageCountCache.add(query, new CacheableInt(messageCount));
            return messageCount;
        }
    }

    public Query createQuery() {
        return new DbQuery(new Forum [] { this }, factory);
    }

    public FilterManager getFilterManager() {
        return filterManager;
    }

//    public GatewayManager getGatewayManager() {
        //GatewayManager manager = (GatewayManager)factory.gatewayManagers.get(this.id);
        // If there is no entry for the manager, add it.
        //if (manager == null) {
            //manager = new GatewayManager(factory, this);
            //factory.gatewayManagers.put(this.id, manager);
        //}
        //return manager;
    //}

    public PermissionsManager getPermissionsManager() {
        return new DbPermissionsManager(id, factory);
    }

    public ForumPermissions getPermissions(Authorization authorization) {
        long userID = authorization.getUserID();
        return factory.permissionsManager.getFinalUserPerms(id, userID);
    }

    public boolean hasPermission(int type) {
        return true;
    }

    //FROM THE CACHEABLE INTERFACE//

    public int getSize() {
        // Approximate the size of the object in bytes by calculating the size
        // of each field.
        int size = 0;
        size += CacheSizes.sizeOfObject();              // overhead of object
        size += CacheSizes.sizeOfObject();              // factory
        size += CacheSizes.sizeOfLong();                // id
        size += CacheSizes.sizeOfString(name);          // name
        size += CacheSizes.sizeOfString(description);   // description
        size += CacheSizes.sizeOfDate();                // creation date
        size += CacheSizes.sizeOfDate();                // modified date
        size += CacheSizes.sizeOfMap(properties);       // properties
        size += threadListCache.getMaxSize();           // thread list cache
        size += messageListCache.getMaxSize();          // message list cache
        size += threadCountCache.getMaxSize();          // thread count cache
        size += messageCountCache.getMaxSize();         // message count cache
        size += CacheSizes.sizeOfObject() +
                CacheSizes.sizeOfLong() * 5;            // approx of pop threads
        size += CacheSizes.sizeOfInt() * 4;             // moderation values
        size += CacheSizes.sizeOfObject();              // filter manager

        return size;
    }

    //OTHER METHODS

    /**
     * Returns a String representation of the Forum object using the forum name.
     *
     * @return a String representation of the Forum object.
     */
    public String toString() {
        return name;
    }

    public int hashCode() {
        return (int)id;
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object != null && object instanceof DbForum) {
            return id == ((DbForum)object).getID();
        }
        else {
            return false;
        }
    }

    /**
     * Updates the modified date. It accepts a Connection so that it can
     * participate in trasactions.
     */
    protected void updateModifiedDate(long date, Connection con)
            throws SQLException
    {
        this.modifiedDate.setTime(date);
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE);
            pstmt.setString(1, StringUtils.dateToMillis(modifiedDate));
            pstmt.setLong(2, id);
            pstmt.executeUpdate();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Does the actual work of deleteting a thread. It accepts a Connection so
     * that it can participate in transactions.
     */
    protected void deleteThread(DbForumThread thread, Connection con)
            throws SQLException, ForumMessageNotFoundException
    {
        // Delete all messages from the thread. Deleting the root
        // message will delete all submessages.
        thread.deleteMessage(thread.getRootMessage(), con);

        // Delete any watches on the thread.
        factory.watchManager.deleteThreadWatches(thread, con);

        // Now delete thread db entry and thread properties.
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(DELETE_THREAD_PROPERTIES);
            pstmt.setLong(1,thread.getID());
            pstmt.execute();
            pstmt.close();

            pstmt = con.prepareStatement(DELETE_THREAD);
            pstmt.setLong(1,thread.getID());
            pstmt.execute();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Returns an array of all the thread id's in the forum. This method
     * is primarily useful for the data export class, which needs a full list
     * of all the id's for maximum efficiency.
     *
     * @return an array of all thread ID's in the forum.
     */
    protected long[] getAllThreads() {
        LongList threads = new LongList(getThreadCount());
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(ALL_THREADS);
            pstmt.setLong(1, this.id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                threads.add(rs.getLong(1));
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return threads.toArray();
    }

    /**
     * Resets the object.
     */
    protected void reset() {
        // Reload the actual forum data.
        try {
            loadFromDb();
        } catch (Exception e) { }
        // Clear out the message list and message count cache.
        threadListCache.clear();
        threadCountCache.clear();
        messageListCache.clear();
        messageCountCache.clear();
        // Reset popular threads
        this.popularThreads = null;
    }

    /**
     * Returns the SQL statement corresponding to a ResultFilter for threads.
     */
    protected String getThreadListSQL(ResultFilter resultFilter, boolean countQuery) {
        int sortField = resultFilter.getSortField();

        // Make sure the sort field is valid.
        if (!countQuery && ! ( sortField == JiveGlobals.MODIFIED_DATE ||
                sortField == JiveGlobals.CREATION_DATE ||
                sortField == JiveGlobals.THREAD_NAME ||
                ( sortField == JiveGlobals.EXTENDED_PROPERTY &&
                  resultFilter.getSortPropertyName() != null
                )
              )
            )
        {
            throw new IllegalArgumentException("The specified sort field is not valid.");
        }

        StringBuffer query = new StringBuffer(80);
        if (!countQuery) {
            query.append("SELECT jiveThread.threadID");
        }
        else {
            query.append("SELECT count(1)");
        }

        boolean filterUser = resultFilter.getUserID() != ResultFilter.NULL_INT;
        boolean filterCreationDate = resultFilter.getCreationDateRangeMin() != null ||
                                     resultFilter.getCreationDateRangeMax() != null;
        boolean filterModifiedDate = resultFilter.getModifiedDateRangeMin() != null ||
                                     resultFilter.getModifiedDateRangeMax() != null;
        int propertyCount = resultFilter.getPropertyCount();

        // SELECT -- need to add value that we sort on
        if (!countQuery) {
            switch(sortField) {
                case JiveGlobals.THREAD_NAME:
                    query.append(", jiveMessage.subject");
                    break;
                case JiveGlobals.MODIFIED_DATE:
                    query.append(", jiveThread.modifiedDate");
                    break;
                case JiveGlobals.CREATION_DATE:
                    query.append(", jiveThread.creationDate");
                    break;
                case JiveGlobals.EXTENDED_PROPERTY:
                    query.append(", propTable.propValue");
                    break;
            }
        }

        // FROM -- values (in addition to jiveThread)
        query.append(" FROM jiveThread");
        if (filterUser ||
                (!countQuery && resultFilter.getSortField() == JiveGlobals.THREAD_NAME))
        {
            query.append(", jiveMessage");
        }
        for (int i=0; i<propertyCount; i++) {
            query.append(", jiveThreadProp p").append(i);
        }
        if (!countQuery &&
                resultFilter.getSortField() == JiveGlobals.EXTENDED_PROPERTY)
        {
            query.append(", jiveThreadProp propTable");
        }

        // WHERE BLOCK
        query.append(" WHERE jiveThread.forumID=").append(this.id);
        // User
        if (filterUser || (!countQuery && sortField == JiveGlobals.THREAD_NAME)) {
            query.append(" AND jiveThread.threadID=jiveMessage.threadID");
            query.append(" AND jiveMessage.parentMessageID IS NULL");
        }
        if (filterUser) {
            query.append(" AND jiveMessage.userID=").append(resultFilter.getUserID());
        }
        // Properties
        for (int i=0; i<propertyCount; i++) {
            query.append(" AND jiveThread.threadID=p").append(i).append(".threadID");
            query.append(" AND p").append(i).append(".name='");
            query.append(resultFilter.getPropertyName(i));
            query.append("' AND p").append(i).append(".propValue='");
            query.append(resultFilter.getPropertyValue(i)).append("'");
        }
        // Sort on thread name

⌨️ 快捷键说明

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