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

📄 javamailgateway.java

📁 jive 2.1.1 的源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

            ResultFilter parentCheck = new ResultFilter();
            parentCheck.setModerationRangeMin(Integer.MIN_VALUE+1);
            parentCheck.setCreationDateRangeMin(min);
            parentCheck.setCreationDateRangeMax(max);
            parentCheck.addProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(parentSubject));

            Forum forum = factory.getForum(forumID);
            if (forum.getMessageCount(parentCheck) > 0) {
                // we found a parent, set the parent messageId
                parentIterator = forum.messages(parentCheck);
                pMessage       = (ForumMessage) parentIterator.next();

                pMessageId = pMessage.getProperty(gatewayMessageId);

                // verify that this isn't the same message being imported yet again
                // if it isn't, update the parent header
                if (!pMessageId.equals(messageId)) {
                    index      = messages.indexOf(message);
                    found      = true;

                    try {
                        message.setProperty(gatewayParentId, pMessageId);
                        messages.set(index, message);
                    }
                    catch (UnauthorizedException ue) { /*ignore */ }
                }
            }

            // if we didn't find in the db, check the messages that
            // we are importing
            if (!found) {
                parentIterator = messages.listIterator();
                while (parentIterator.hasNext() && !found) {

                    pMessage = (ForumMessage) parentIterator.next();

                    // subject fields don't match, next parent
                    if (!pMessage.getSubject().trim().equals(parentSubject)) {
                        continue;
                    }

                    // we found a parent, set the parent messageId
                    pMessageId = pMessage.getProperty(gatewayMessageId);

                    // check to make sure we're not talking about the same
                    // message here
                    if (pMessageId.equals(messageId)) {
                        continue;
                    }

                    // don't allow parent to be younger than child - could
                    // cause circular parentage problems
                    if (pMessage.getCreationDate().compareTo(message.getCreationDate()) >= 0) {
                        continue;
                    }

                    index = messages.indexOf(message);
                    found = true;

                    try {
                        message.setProperty(gatewayParentId, pMessageId);
                        messages.set(index, message);
                    }
                    catch (UnauthorizedException ue) { /*ignore */ }
                }
            }
        }
    }

    /**
     * We need to check dates on all the messages to verify
     * that we don't have a child older than a parent, which Jive was
     * explicitly designed not to handle. Our solution to this problem
     * is to change the parent messages' creation date to 1 second
     * before the child's and store the original date in an extended
     * property.
     *
     * @param messages a List of ForumMessage objects
     */
    protected void correctMessageDates(List messages) {
        Iterator iter               = messages.listIterator();
        Iterator parentIterator     = null;
        ForumMessage message        = null;
        ForumMessage pMessage       = null;
        String messageId            = "";
        String pMessageId           = "";
        boolean parentDateCheckComplete = false;

        // repeatedly go through all the messages checking for
        // parents younger than children.
        while (!parentDateCheckComplete) {
            parentDateCheckComplete = true;

            while (iter.hasNext()) {
                message         = (ForumMessage) iter.next();
                messageId       = message.getProperty(gatewayMessageId);
                pMessageId      = message.getProperty(gatewayParentId);
                parentIterator  = messages.listIterator();

                // no parent, ignore
                if (pMessageId == null || pMessageId.length() < 1) {
                    continue;
                }

                // get the parent message
                while (parentIterator.hasNext()) {
                    pMessage      = (ForumMessage) parentIterator.next();
                    String temp   = pMessage.getProperty(gatewayMessageId);

                    if (!pMessageId.equals(temp)) {
                        pMessage = null;
                    }
                    else {
                        break;
                    }
                }

                // couldn't find the parent
                if (pMessage == null) {
                    continue;
                }

                Date messageDate  = message.getCreationDate();
                Date pMessageDate = pMessage.getCreationDate();
                long time         = pMessage.getCreationDate().getTime();
                String mTime      = String.valueOf(messageDate.getTime());
                int index         = messages.indexOf(message);

                if (pMessageDate.compareTo(messageDate) > 0) {
                    try {
                        // store the original date as a property
                        // set the child's creation date 1 second later
                        // than the parent's
                        message.setProperty(MESSAGE_DATE_HEADER, mTime);
                        message.setCreationDate(new Date(time+1));
                        messages.set(index, message);
                        parentDateCheckComplete = false;
                    }
                    catch (UnauthorizedException ue) { /* ignore */ }
                }
            }
        }
    }

    /**
     * Parse the JavaMail message object and return a ForumMessage object
     *
     * @param message the JavaMail Message object
     * @param afterDate the date we use to check to see if we should import this
     *      message or not
     * @return a ForumMessage object
     */
    protected ForumMessage parseMessage(Message message, Date afterDate) {

        // Create a message (anonymous author)
        ForumMessage forumMessage = factory.createMessage();
        try {
            // too old
            if (getDate(message).compareTo(afterDate) < 0) {
                return null;
            }

            // store all the parent message id's so we can use for threading
            // later
            parentMessageIDs.put(getMessageID(message), getParentMessageID(message));

            // set ForumMessage fields
            forumMessage.setSubject(getSubject(message));
            forumMessage.setProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(getSubject(message)));
            forumMessage.setCreationDate(getDate(message));
            forumMessage.setModifiedDate(getDate(message));
            forumMessage.setProperty(gatewayMessageId, getMessageID(message));
            forumMessage.setProperty("name", getFromName(message));
            forumMessage.setProperty("email", getFromEmail(message));
            forumMessage.setBody(getBody(message));
            if (!getParentMessageID(message)[0].equals("")) {
                forumMessage.setProperty(gatewayParentId, getParentMessageID(message)[0]);
            }
        }
        catch (MessagingException me) {
            me.printStackTrace();
            // fail this message
            return null;
        }
        catch (NullPointerException npe) {
            npe.printStackTrace();
            // fail this message
            return null;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            // fail this message
            return null;
        }
        catch (UnauthorizedException uae) {
            uae.printStackTrace();
            // fail this message
            return null;
        }

        return forumMessage;
    }

    /**
     * Import the List of messages into the forum specified
     *
     * One of the things we need to do in this method is check to see if this
     * message or it's parent already exist (or doesn't exist) in this forum.
     *
     * If a parent doesn't exist, we create a dummy message in its place. If a
     * parent does exist, we attach the message to the parent's thread.
     *
     * If a message already exists in the forum, we first check to see if it's
     * one of the dummy messages we previously created. If so, we modify it.
     * Otherwise, we don't insert the message.
     *
     * If a message doesn't already exist in the forum and it has no parent(s),
     * we create a new thread with this message as the root message
     *
     * @param messages   a list of messages to import into the forum
     */
    protected void importMessages(List messages)
            throws ForumNotFoundException, UnauthorizedException
    {
        Iterator messageIterator = messages.listIterator();
        Forum forum = factory.getForum(forumID);
        ForumMessage message     = null;
        ResultFilter existsCheck = null;
        String messageId         = "";
        String parentId          = "";
        ArrayList insertCache    = new ArrayList();

        // go through each message in the list
        while (messageIterator.hasNext()) {
            message = (ForumMessage) messageIterator.next();

            // failed message - likely couldn't parse a header
            if (message == null) {
                System.err.println("Unabled to import empty message");
                continue;
            }

            try {
                // test to see if this message already exists in this forum
                messageId = message.getProperty(gatewayMessageId);

                // verify that messageId is not null or empty
                // if it is (which it shouldn't be), don't import
                // because you're almost guaranteed to import it
                // more than once
                if (messageId == null || messageId.equals("")) {
                    continue;
                }

                if (!isMessageInDb(forum, gatewayMessageId, messageId)) {

                    // Now, see if the message states that it has a parent
                    parentId = message.getProperty(gatewayParentId);

                    // we have a parent
                    if (parentId != null && !parentId.equals("")) {

                        InsertCacheItem parentItem = null;

                        // first, check the cache
                        if ((parentItem = isMessageinInsertCache(insertCache, parentId)) != null) {

                            // cache insert
                            InsertCacheItem insertCacheItem =
                                    new InsertCacheItem(forum, null, null, message);
                            insertCache.add(insertCacheItem);

                            // update parent so it knows to set the
                            // thread and parent for the child
                            parentItem.addChild(insertCacheItem);
                        }
                        // verify that the parent exists in this forum
                        // yes
                        else if (isMessageInDb(forum, gatewayMessageId, parentId)) {
                            ResultFilter parentCheck = new ResultFilter();
                            parentCheck.setModerationRangeMin(Integer.MIN_VALUE+1);
                            parentCheck.addProperty(gatewayMessageId, parentId);

                            Iterator iter = forum.messages(parentCheck);
                            ForumMessage parent = (ForumMessage) iter.next();
                            ForumThread thread = parent.getForumThread();

                            // cache insert
                            InsertCacheItem insertCacheItem =
                                    new InsertCacheItem(forum, thread, parent, message);
                            insertCache.add(insertCacheItem);
                        }
                        // no
                        else {

                            // create a dummy message as a placeholder with a
                            // creation date 1 second earlier than the child's
                            ForumMessage pMessage = factory.createMessage();
                            long time             = message.getCreationDate().getTime();
                            Date parentDate       = new Date(time - 1);
                            String messageSubject = message.getSubject();
                            String[] parentIDs = (String[]) parentMessageIDs.get(messageId);

                            // find the subject of the parent that we want to create
                            int replyIndex = messageSubject.toLowerCase().indexOf(replyPrefix);

                            if (replyIndex == 0 && messageSubject.length() > 4) {
                                messageSubject = messageSubject.trim().substring(3).trim();
                            }

                            pMessage.setSubject(messageSubject);
                            pMessage.setProperty(SUBJECT_EXTENDED_PROPERTY,
                                                 StringUtils.hash(messageSubject));
                            pMessage.setCreationDate(parentDate);
                            pMessage.setModifiedDate(parentDate);
                            pMessage.setProperty(gatewayMessageId, parentId);
                            pMessage.setProperty(DUMMY_PARENT_HEADER, "true");
                            pMessage.setBody(temporaryParentBody);

                            // determine if this parent is itself a child
                            // yes
                            if (parentIDs.length > 1 &&
                                    isMessageInDb(forum, gatewayMessageId, parentIDs[1])) {
                                ResultFilter parentCheck = new ResultFilter();
                                parentCheck.setModerationRangeMin(Integer.MIN_VALUE+1);
                                parentCheck.addProperty(gatewayMessageId, parentIDs[1]);

                                Iterator pIter = forum.messages(parentCheck);
                                ForumMessage parent = (ForumMessage) pIter.next();
                                ForumThread thread  = parent.getForumThread();

                                // cache insert
                                InsertCacheItem insertCacheItem =
                                        new InsertCacheItem(forum, thread, parent, message);
                                insertCache.add(insertCacheItem);
                            }
                            // no
                            else {
                                // cache insert
                                InsertCacheItem insertCacheItem =

⌨️ 快捷键说明

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