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

📄 messagedaosql.java

📁 JiveJdon 3.0不只是一个论坛程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.dao.sql;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.jdon.jivejdon.Constants;
import com.jdon.jivejdon.dao.AccountDao;
import com.jdon.jivejdon.dao.MessageDao;
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.util.ToolsUtil;

/**
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */
public class MessageDaoSql implements MessageDao {
    private final static Logger logger = Logger.getLogger(MessageDaoSql.class);
    
    protected JdbcTempSource jdbcTempSource;
    
    private AccountDao accountDao;
    
    private Constants constants;
    
    public MessageDaoSql(JdbcTempSource jdbcTempSource,
            AccountDao accountDao, Constants constants) {
        this.jdbcTempSource = jdbcTempSource;
        this.accountDao = accountDao;
        this.constants = constants;
    }

    /* 
     * ForumMessage中包含的ForumThread Forum等对象只是包涵ID的空对象,完整对象需要
     * 前台再行处理.
     * 
     * @see com.jdon.jivejdon.dao.MessageDao#getMessage(java.lang.String)
     */
    public ForumMessage getMessage(Long messageId) {
        logger.debug("enter getMessage  for id:" + messageId);
        String LOAD_MESSAGE =
            "SELECT threadID, forumID, userID, subject, body, modValue, rewardPoints, "+
            "creationDate, modifiedDate FROM jiveMessage WHERE messageID=?";
        List queryParams = new ArrayList();
        queryParams.add(messageId);
        
        ForumMessage forumMessage = null;        
        try {
            List list = jdbcTempSource.getJdbcTemp().queryMultiObject(queryParams,
                    LOAD_MESSAGE);
            Iterator iter = list.iterator();
            if (iter.hasNext()) {
                forumMessage = new ForumMessage();
                Map map = (Map) iter.next();
                forumMessage.setMessageId(messageId);
                forumMessage.setSubject((String) map.get("subject"));
                forumMessage.setBody((String) map.get("body"));
                
                String saveDateTime = ((String) map.get("modifiedDate")).trim();
                String displayDateTime = constants.getDateTimeDisp(saveDateTime);                 
                forumMessage.setModifiedDate(displayDateTime);            
                
                saveDateTime = ((String) map.get("creationDate")).trim();
                displayDateTime = constants.getDateTimeDisp(saveDateTime);                                                 
                forumMessage.setCreationDate(displayDateTime);      
                //get the formatter so later can transfer String to Date
                forumMessage.setDateTime_formatter(constants.getDateTime_formatter());     
                
                ForumThread forumThread = new ForumThread();
                forumThread.setThreadId((Long) map.get("threadID"));
                forumMessage.setForumThread(forumThread);
                
                Forum forum  = new Forum();
                forum.setForumId((Long) map.get("forumID"));
                forumMessage.setForum(forum);
                
                com.jdon.jivejdon.model.Account account = new com.jdon.jivejdon.model.Account();
                forumMessage.setAccount(account);
                Object o = map.get("userID");
                if (o != null){
                    account.setUserIdLong((Long)o);
                }                
                //lazy load
                //forumMessage.setPropertys(propertyDaoSql.getAllPropertys(Constants.MESSAGE, messageId));
            }
        } catch (Exception e) {
            logger.error("messageId="+ messageId+ " happend  " + e);
        }
        logger.debug("getMessage end");
        return forumMessage;
    }
    
    
    /* (non-Javadoc)
     * @see com.jdon.jivejdon.dao.MessageDao#getThread(java.lang.String)
     */
    public ForumThread getThread(Long threadId) {
        logger.debug("enter getThread for id:" + threadId);
        String LOAD_THREAD =
            "SELECT forumID, rootMessageID, modValue, rewardPoints, creationDate, " +
            "modifiedDate FROM jiveThread WHERE threadID=?";
        List queryParams = new ArrayList();
        queryParams.add(threadId);
        
        ForumThread forumThread = null;        
        try {
            List list = jdbcTempSource.getJdbcTemp().queryMultiObject(queryParams,
                    LOAD_THREAD);
            Iterator iter = list.iterator();
            if (iter.hasNext()) {
                forumThread = new ForumThread();
                Map map = (Map) iter.next();
                forumThread.setThreadId(threadId);
                
                Forum forum  = new Forum();
                forum.setForumId((Long) map.get("forumID"));
                forumThread.setForum(forum);
                
                ForumMessage forumMessage = new ForumMessage();
                forumMessage.setMessageId((Long) map.get("rootMessageID"));
                forumThread.setRootMessage(forumMessage);
                
                String saveDateTime = ((String) map.get("modifiedDate")).trim();
                String displayDateTime = constants.getDateTimeDisp(saveDateTime);                 
                forumThread.setModifiedDate(displayDateTime);            
                
                saveDateTime = ((String) map.get("creationDate")).trim();
                displayDateTime = constants.getDateTimeDisp(saveDateTime);                                                 
                forumThread.setCreationDate(displayDateTime);
                                
                //forumThread.setPropertys(propertyDaoSql.getAllPropertys(Constants.THREAD, threadId));
                
            }
        } catch (Exception e) {
            logger.error("threadId="+ threadId+ " happend  " + e);
        }
        return forumThread;
    }    
        

    /*
     *topic message insert, no parentMessageID value,
     *so the table's field default value must be null 
     */
    public void createMessage(ForumMessage forumMessage) throws Exception {
        logger.debug("enter createTopicMessage for id:" + forumMessage.getMessageId());
        //differnce with createRpleyMessage: parentMessageID,
        
        String INSERT_MESSAGE =
            "INSERT INTO jiveMessage(messageID, threadID, forumID, " +
            "userID, subject, body, modValue, rewardPoints, creationDate, modifiedDate) " +
            "VALUES(?,?,?,?,?,?,?,?,?,?)";
        List queryParams = new ArrayList();
        queryParams.add(forumMessage.getMessageId());        
        queryParams.add(forumMessage.getForumThread().getThreadId());
        queryParams.add(forumMessage.getForum().getForumId());
        queryParams.add(forumMessage.getAccount().getUserId());
        queryParams.add(forumMessage.getSubject());
        queryParams.add(forumMessage.getBody());
        queryParams.add(new Integer(0));
        queryParams.add(new Integer(forumMessage.getRewardPoints()));
        
        long now = System.currentTimeMillis();   
        String saveDateTime = ToolsUtil.dateToMillis(now);
        String displayDateTime = constants.getDateTimeDisp(saveDateTime);
        queryParams.add(saveDateTime);
        forumMessage.setCreationDate(displayDateTime);
        

⌨️ 快捷键说明

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