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

📄 messagerepository.java

📁 非常有影响的 j道 论 坛 源码 国外很有明的专家编写的 ....对java爱好者很有参考价值
💻 JAVA
字号:
/*
 * Copyright 2003-2006 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.jdon.jivejdon.repository;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.jdon.controller.events.EventModel;
import com.jdon.jivejdon.Constants;
import com.jdon.jivejdon.dao.MessageDaoFacade;
import com.jdon.jivejdon.dao.PropertyDao;
import com.jdon.jivejdon.manager.MessageDeletor;
import com.jdon.jivejdon.model.Forum;
import com.jdon.jivejdon.model.ForumMessage;
import com.jdon.jivejdon.model.ForumMessageReply;
import com.jdon.jivejdon.model.ForumThread;
import com.jdon.jivejdon.repository.builder.ForumAbstractFactory;
import com.jdon.jivejdon.service.util.ContainerUtil;
import com.jdon.treepatterns.TreeNodeFactory;
import com.jdon.treepatterns.TreeNodeVisitable;
import com.jdon.treepatterns.TreeVisitor;
import com.jdon.treepatterns.model.TreeModel;

/**
 * Kernel of Message business operations
 * 
 * @author banq(http://www.jdon.com)
 * 
 */
public class MessageRepository extends ThreadRepository {
	private final static Logger logger = Logger.getLogger(MessageRepository.class);

	protected MessageDaoFacade messageDaoFacade;

	protected ForumAbstractFactory forumBuilder;

	private TagRepository tagRepository;

	protected UploadRepository uploadRepository;

	protected PropertyDao propertyDao;

	public MessageRepository(MessageDaoFacade messageDaoFacade, ForumAbstractFactory forumBuilder, ContainerUtil containerUtil, TagRepository tagRepository, UploadRepository uploadRepository,
			PropertyDao propertyDao) {
		super(messageDaoFacade);
		this.messageDaoFacade = messageDaoFacade;
		this.forumBuilder = forumBuilder;
		this.tagRepository = tagRepository;
		this.uploadRepository = uploadRepository;
		this.propertyDao = propertyDao;
	}

	/**
	 * get the full forum in forumMessage, and return it.
	 */
	public ForumMessage initMessage(EventModel em) {
		logger.debug(" enter service: initMessage ");
		ForumMessage forumMessage = (ForumMessage) em.getModelIF();
		try {
			if (forumMessage.getForum() == null) {
				logger.error(" no Forum in this ForumMessage");
				return forumMessage;
			}
			Long forumId = forumMessage.getForum().getForumId();
			logger.debug(" paremter forumId =" + forumId);
			Forum forum = forumBuilder.getForum(forumId);
			forumMessage.setForum(forum);
		} catch (Exception e) {
			logger.error(e);
		}
		return forumMessage;
	}

	public ForumMessage initReplyMessage(EventModel em) {
		logger.debug(" enter service: initReplyMessage ");
		ForumMessageReply forumMessageReply = (ForumMessageReply) initMessage(em);
		try {
			Long pmessageId = forumMessageReply.getParentMessage().getMessageId();
			if (pmessageId == null) {
				logger.error(" no the parentMessage.messageId parameter");
				return null;
			}
			ForumMessage pMessage = forumBuilder.getMessage(pmessageId);
			forumMessageReply.setParentMessage(pMessage);
			forumMessageReply.setSubject("回复:" + pMessage.getSubject());
		} catch (Exception e) {
			logger.error(e);
		}
		return forumMessageReply;
	}

	/*
	 * create the topic message
	 */
	public void createTopicMessage(ForumMessage forumMessage) throws Exception {
		try {
			logger.debug(" enter service: createMessage ");
			Forum forum = forumBuilder.getForum(forumMessage.getForum().getForumId());
			if (forum == null) {
				logger.error(" no this forum, forumId = " + forumMessage.getForum().getForumId());
				return;
			}
			forumMessage.setForum(forum);

			ForumThread forumThread = super.createThread(forumMessage);
			forumMessage.setForumThread(forumThread);
			// tag
			tagRepository.saveTagTitle(forumMessage.getForumThread(), forumMessage.getTagTitle());

			messageDaoFacade.getMessageDao().createMessage(forumMessage);

			uploadRepository.saveAllUploadFiles(forumMessage.getMessageId().toString(), forumMessage.getUploadFiles());

			propertyDao.updateProperties(Constants.MESSAGE, forumMessage.getMessageId(), forumMessage.getPropertys());
		} catch (Exception e) {
			String error = e + " createTopicMessage forumMessageId=" + forumMessage.getMessageId();
			logger.error(error);
			throw new Exception(error);
		}
	}

	/**
	 * the relation about creating reply forumMessage only need a parameter : parent message. we can get the Forum or ForumThread from the parent message. the hypelink parameter in jsp must be a
	 * paremeter: the Id of parent message.
	 * 
	 */
	public void createReplyMessage(ForumMessageReply forumMessageReply) throws Exception {
		try {
			logger.debug(" enter service: createReplyMessage ....");
			
			// verify the parentMessageId existed.
			ForumMessage parentMessage = this.forumBuilder.getMessage(forumMessageReply.getParentMessage().getMessageId());
			if (parentMessage == null) {
				logger.error("not this parent Message: " + forumMessageReply.getParentMessage().getMessageId());
				return;
			}
			forumMessageReply.setParentMessage(parentMessage);
			forumMessageReply.setForum(parentMessage.getForum());
			forumMessageReply.setForumThread(parentMessage.getForumThread());

			// update thread ModifiedDate
			super.updateThread(forumMessageReply.getForumThread());
			logger.debug(" updateThread for forumThread=" + forumMessageReply.getForumThread().getThreadId() + " hashCode=" + forumMessageReply.getForumThread().hashCode());

			// create
			messageDaoFacade.getMessageDao().createMessageReply(forumMessageReply);

			uploadRepository.saveAllUploadFiles(forumMessageReply.getMessageId().toString(), forumMessageReply.getUploadFiles());

			propertyDao.updateProperties(Constants.MESSAGE, forumMessageReply.getMessageId(), forumMessageReply.getPropertys());
		} catch (Exception e) {
			String error = e + " createReplyMessage forumMessageId=" + forumMessageReply.getMessageId();
			logger.error(error);
			throw new Exception(error);
		}
	}

	/*
	 * update the message, update the message's subject and body we must mark the message that has been updated. there are two kinds of parameters: the primary key /new entity data in DTO ForumMessage
	 * of the method patameter
	 * 
	 */
	public void updateMessage(ForumMessage forumMessage) throws Exception {
		logger.debug(" enter updateMessage id =" + forumMessage.getMessageId());
		try {
			messageDaoFacade.getMessageDao().updateMessage(forumMessage);

			// update moving a forum , when it is root message and has no reply.
			if ((forumMessage.isRoot()) && (forumMessage.isLeaf())) {
				Long messageId = forumMessage.getMessageId();
				Long threadId = forumMessage.getForumThread().getThreadId();
				Long forumId = forumMessage.getForum().getForumId();
				messageDaoFacade.getMessageDao().updateMovingForum(messageId, threadId, forumId);
				forumMessage.getForumThread().setForum(forumMessage.getForum());
			}

			uploadRepository.updateAllUploadFiles(forumMessage.getMessageId(), forumMessage.getUploadFiles());

			tagRepository.mergeTagTitle(forumMessage.getForumThread(), forumMessage.getTagTitle());

			propertyDao.deleteProperties(Constants.MESSAGE, forumMessage.getMessageId());
			propertyDao.updateProperties(Constants.MESSAGE, forumMessage.getMessageId(), forumMessage.getPropertys());
		} catch (Exception e) {
			String error = e + " updateMessage forumMessageId=" + forumMessage.getMessageId();
			logger.error(error);
			throw new Exception(error);
		}

	}

	/**
	 * 
	 * Composite pattern
	 * 
	 * delete a node or its all childern and refresh the cache.
	 * 
	 * @param forumMessage
	 */
	public void deleteMessageComposite(ForumMessage delforumMessage) throws Exception {
		Long key = delforumMessage.getMessageId();
		logger.debug("deleteNode messageId =" + key);
		try {
			ForumThread forumThread = forumBuilder.getThread(delforumMessage.getForumThread().getThreadId());
			
			// because forumMessage can be cached, so we do need create a node every time.
			TreeModel treeModel = forumThread.getState().getTreeModel();
			TreeNodeFactory TreeNodeFactory = new TreeNodeFactory(treeModel);
			TreeNodeVisitable treeNode = TreeNodeFactory.createNode(key);

			TreeVisitor messageDeletor = new MessageDeletor(this);
			logger.debug(" begin to walk into tree, and delete them");
			treeNode.accept(messageDeletor);

			// if the root message was deleted, the thread that it be in
			// will all be deleted
			if (delforumMessage.isRoot()) {
				logger.debug("1. it is a root message, delete the forumThread");
				tagRepository.deleteTagTitle(delforumMessage.getForumThread());
				super.deleteThread(forumThread);
			}
		} catch (Exception e) {
			String error = e + " deleteMessageComposite forumMessageId=" + delforumMessage.getMessageId();
			logger.error(error);
			throw new Exception(error);
		}
	}

	public void deleteMessage(Long messageId) throws Exception {
		try {
			ForumMessage message = messageDaoFacade.getMessageDao().getMessage(messageId);
			if (message == null) return;//it is delete
			messageDaoFacade.getMessageDao().deleteMessage(messageId);
			uploadRepository.deleteAllUploadFiles(messageId);
			propertyDao.deleteProperties(Constants.MESSAGE, messageId);
		} catch (Exception e) {
			String error = e + " deleteMessage forumMessageId=" + messageId;
			logger.error(error);
			throw new Exception(error);
		}

	}

	public synchronized Long getNextId(int idType) throws Exception {
		Long mIDInt = new Long(0);
		try {
			mIDInt = messageDaoFacade.getSequenceDao().getNextId(Constants.MESSAGE);
		} catch (SQLException e) {
			logger.error(e);
			throw new Exception(e);
		}
		return mIDInt;
	}

	/**
	 * @return Returns the forumBuilder.
	 */
	public ForumAbstractFactory getForumBuilder() {
		return forumBuilder;
	}

	/**
	 * @return Returns the messageDaoFacade.
	 */
	public MessageDaoFacade getMessageDaoFacade() {
		return messageDaoFacade;
	}

}

⌨️ 快捷键说明

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