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

📄 messagekernel.java

📁 非常有影响的 j道 论 坛 源码 国外很有明的专家编写的 ....对java爱好者很有参考价值
💻 JAVA
字号:
/*
 * Copyright 2003-2005 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.service.imp.message;

import javax.transaction.TransactionManager;

import org.apache.log4j.Logger;

import com.jdon.controller.events.EventModel;
import com.jdon.controller.model.PageIterator;
import com.jdon.jivejdon.manager.TreeManager;
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.model.query.MultiCriteria;
import com.jdon.jivejdon.repository.MessageRepository;
import com.jdon.jivejdon.repository.builder.ForumAbstractFactory;
import com.jdon.jivejdon.service.ForumMessageQueryService;
import com.jdon.jivejdon.service.util.JtaTransactionUtil;
import com.jdon.jivejdon.util.ToolsUtil;

/**
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 * 
 */
public class MessageKernel {
	private final static Logger logger = Logger.getLogger(MessageKernel.class);

	protected MessageRepository messageRepository;

	protected ForumMessageQueryService forumMessageQueryService;

	protected ForumAbstractFactory forumAbstractFactory;

	protected TreeManager treeManager;

	protected JtaTransactionUtil jtaTransactionUtil;
	
	public MessageKernel(MessageRepository messageRepository, ForumMessageQueryService forumMessageQueryService, ForumAbstractFactory forumAbstractFactory, TreeManager treeManager,
			JtaTransactionUtil jtaTransactionUtil) {
		this.messageRepository = messageRepository;
		this.forumMessageQueryService = forumMessageQueryService;
		this.forumAbstractFactory = forumAbstractFactory;
		this.treeManager = treeManager;
		this.jtaTransactionUtil = jtaTransactionUtil;

	}

	/**
	 * get the full forum in forumMessage, and return it.
	 */
	public ForumMessage initMessage(EventModel em) {
		logger.debug("enter initMessage");
		return messageRepository.initMessage(em);
	}

	public ForumMessage initReplyMessage(EventModel em) {
		logger.debug("enter initReplyMessage");
		return messageRepository.initReplyMessage(em);
	}

	/*
	 * return a full ForumMessage need solve the relations with Forum ForumThread parentMessage
	 */
	public ForumMessage getMessage(Long messageId) {
		logger.debug("enter MessageServiceImp's getMessage");
		return forumAbstractFactory.getMessage(messageId);
	}

	public ForumMessage getMessageWithPropterty(Long messageId) {
		return forumAbstractFactory.getMessageWithPropterty(messageId);
	}

	/**
	 * return a full ForumThread one ForumThread has one rootMessage need solve the realtion with Forum rootForumMessage lastPost
	 * 
	 * @param threadId
	 * @return
	 */
	public ForumThread getThread(Long threadId) {
		logger.debug("enter getThread");
		return forumAbstractFactory.getThread(threadId);

	}

	public  Long getNextId(final int idType) throws Exception {
		try {
			return messageRepository.getNextId(idType);
		} catch (Exception e) {
			String error = e + " getNextId ";
			logger.error(error);
			throw new Exception(error);
		}

	}

	/*
	 * create the topic message
	 */
	public void createTopicMessage(EventModel em) throws Exception {
		logger.debug("enter createTopicMessage");
		ForumMessage forumMessage = (ForumMessage) em.getModelIF();
		TransactionManager tx = jtaTransactionUtil.getTransactionManager();
		try {
			tx.begin();
			messageRepository.createTopicMessage(forumMessage);
			logger.debug("createTopicMessage ok!");
			tx.commit();
		} catch (Exception e) {
			jtaTransactionUtil.rollback(tx);
			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(EventModel em) throws Exception {
		logger.debug("enter createReplyMessage");
		ForumMessageReply forumMessageReply = (ForumMessageReply) em.getModelIF();
		if ((forumMessageReply.getParentMessage() == null) || (forumMessageReply.getParentMessage().getMessageId() == null)) {
			logger.error("parentMessage is null, this is not reply message!");
			return;
		}
		TransactionManager tx = jtaTransactionUtil.getTransactionManager();
		try {
			tx.begin();
			messageRepository.createReplyMessage(forumMessageReply);
			logger.debug("createReplyMessage ok!");
			tx.commit();
		} catch (Exception e) {
			jtaTransactionUtil.rollback(tx);
			String error = e + " createTopicMessage forumMessageId=" + forumMessageReply.getParentMessage().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(EventModel em) throws Exception {
		logger.debug("enter updateMessage");
		ForumMessage newForumMessageInputparamter = (ForumMessage) em.getModelIF();
		ForumMessage forumMessage = getMessage(newForumMessageInputparamter.getMessageId());
		TransactionManager tx = jtaTransactionUtil.getTransactionManager();
		try {
			// merge
			tx.begin();
			// merge
			// replace old forumMessage with modified new content
			forumMessage.setSubject(newForumMessageInputparamter.getSubject());
			forumMessage.setBody(newForumMessageInputparamter.getBody());
			forumMessage.setUploadFiles(newForumMessageInputparamter.getUploadFiles());
			forumMessage.setOperator(newForumMessageInputparamter.getOperator());
			forumMessage.setMasked(newForumMessageInputparamter.isMasked());
			forumMessage.setTagTitle(newForumMessageInputparamter.getTagTitle());
			Long forumId = newForumMessageInputparamter.getForum().getForumId();
			if (forumId != null && forumId.intValue() != 0 && forumMessage.getForum().getForumId() != forumId) {
				// the message has been moved to a new forum
				Forum newForum = forumAbstractFactory.getForum(forumId);
				if (newForum != null)
					forumMessage.setForum(newForum);
			}
			long now = System.currentTimeMillis();
			String saveDateTime = ToolsUtil.dateToMillis(now);
			forumMessage.setModifiedDate(saveDateTime);
			
			messageRepository.updateMessage(forumMessage);
			// update the forumThread's updatetime
			messageRepository.updateThread(forumMessage.getForumThread());
			logger.debug("updateMessage ok!");
			tx.commit();
		} catch (Exception e) {
			jtaTransactionUtil.rollback(tx);
			String error = e + " updateMessage forumMessageId=" + newForumMessageInputparamter.getMessageId();
			logger.error(error);
			throw new Exception(error);
		}
	}

	/*
	 * delete a message and not inlcude its childern
	 */
	public void deleteMessage(ForumMessage delforumMessage) throws Exception {
		logger.debug("enter deleteMessage");
		TransactionManager tx = jtaTransactionUtil.getTransactionManager();
		try {
			tx.begin();
			this.messageRepository.deleteMessageComposite(delforumMessage);
			tx.commit();
		} catch (Exception e) {
			jtaTransactionUtil.rollback(tx);
			String error = e + " deleteMessage forumMessageId=" + delforumMessage.getMessageId();
			logger.error(error);
			throw new Exception(error);

		}
	}

	public void deleteUserMessages(String username) throws Exception {
		logger.debug("enter userMessageListDelete username=" + username);
		MultiCriteria mqc = new MultiCriteria("1970/01/01");
		mqc.setUsername(username);

		// iterate all messages
		int oneMaxSize = 100;
		PageIterator pi = forumMessageQueryService.getMessages(mqc, 0, oneMaxSize);
		int allCount = pi.getAllCount();

		int wheelCount = allCount / oneMaxSize;
		int start = 0;
		int end = 0;
		for (int i = 0; i <= wheelCount; i++) {
			end = oneMaxSize + oneMaxSize * i;
			logger.debug("start = " + start + " end = " + end);
			if (pi == null)
				pi = forumMessageQueryService.getMessages(mqc, start, end);
			messagesDelete(pi, username);
			pi = null;
			start = end;
		}
	}

	private void messagesDelete(PageIterator pi, String username) throws Exception {
		Object[] keys = pi.getKeys();
		for (int i = 0; i < keys.length; i++) {
			Long messageId = (Long) keys[i];
			logger.debug("delete messageId =" + messageId);
			ForumMessage message = getMessage(messageId);
			if (message.getAccount().getUsername().equals(username)) {
				deleteMessage(message);
			}
		}
	}

	public MessageRepository getMessageRepository() {
		return messageRepository;
	}

	public void setMessageRepository(MessageRepository messageRepository) {
		this.messageRepository = messageRepository;
	}

	public ForumMessageQueryService getForumMessageQueryService() {
		return forumMessageQueryService;
	}

	public void setForumMessageQueryService(ForumMessageQueryService forumMessageQueryService) {
		this.forumMessageQueryService = forumMessageQueryService;
	}

}

⌨️ 快捷键说明

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