📄 messagequerydaosql.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.dao.sql;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.jdon.controller.model.PageIterator;
import com.jdon.jivejdon.dao.MessageQueryDao;
import com.jdon.jivejdon.dao.util.MessagePageIteratorSolver;
import com.jdon.jivejdon.dao.util.ToolsUtil;
import com.jdon.jivejdon.model.ForumMessage;
import com.jdon.jivejdon.model.ForumThread;
import com.jdon.jivejdon.model.ForumThreadState;
import com.jdon.jivejdon.model.query.MultiCriteria;
import com.jdon.jivejdon.model.query.QueryCriteria;
import com.jdon.model.query.PageIteratorSolver;
import com.jdon.model.query.block.Block;
import com.jdon.treepatterns.model.TreeModel;
/**
*
* @author <a href="mailto:banqiao@jdon.com">banq</a>
*
*/
public class MessageQueryDaoSql implements MessageQueryDao{
private final static Logger logger = Logger.getLogger(MessageQueryDaoSql.class);
protected JdbcTempSource jdbcTempSource;
protected PageIteratorSolver pageIteratorSolver;
/**
* @param jdbcTempSource
*/
public MessageQueryDaoSql(JdbcTempSource jdbcTempSource, MessagePageIteratorSolver messagePageIteratorSolver) {
this.pageIteratorSolver = messagePageIteratorSolver.getPageIteratorSolver();
this.jdbcTempSource = jdbcTempSource;
}
public int getMessageCountOfUser(Long userId){
logger.debug("enter getMessageCountOfUser for userId:" + userId);
String USER_MESSAGE_COUNT =
"SELECT count(1) FROM jiveMessage WHERE jiveMessage.userID=?";
List queryParams = new ArrayList();
queryParams.add(userId);
return pageIteratorSolver.getDatasAllCount(queryParams, USER_MESSAGE_COUNT);
}
public ForumThreadState getForumThreadState(ForumThread forumThread){
logger.debug("getForumThreadState from jdbc for threadId=" + forumThread.getThreadId());
ForumThreadState forumThreadState = new ForumThreadState();
int count = getMessageCount(forumThread.getForum().getForumId(), forumThread.getThreadId());
forumThreadState.setMessageCount(count);
Long lastMessageId = getLastPostMessageId(forumThread.getForum().getForumId(), forumThread.getThreadId());
if (lastMessageId == null)
logger.warn("maybe first running, not found lastMessageId for forumId: " + forumThread.getForum().getForumId());
else{
ForumMessage forumMessage = new ForumMessage();
forumMessage.setMessageId(lastMessageId);
forumThreadState.setLastPost(forumMessage);
}
return forumThreadState;
}
private int getMessageCount(Long forumId, Long threadId){
logger.debug("enter getMessageCount for threadId:" + threadId);
String ALL_THREADS =
"SELECT count(1) from jiveMessage WHERE forumID = ? and threadID=? ";
List queryParams = new ArrayList();
queryParams.add(forumId);
queryParams.add(threadId);
Integer count = null;
try {
count = (Integer)jdbcTempSource.getJdbcTemp().querySingleObject(queryParams, ALL_THREADS);
} catch (Exception e) {
logger.error(e);
}
return count.intValue();
}
/**
* get laste message from the message db
*
*/
private Long getLastPostMessageId(Long forumId, Long threadId){
logger.debug("enter getLastPostMessageId for threadId:" + threadId);
String LAST_MESSAGES =
"SELECT messageID from jiveMessage WHERE forumID=? and threadID = ? ORDER BY modifiedDate DESC";
List queryParams2 = new ArrayList();
queryParams2.add(forumId);
queryParams2.add(threadId);
Long messageId = null;
try {
List list = jdbcTempSource.getJdbcTemp().queryMultiObject(queryParams2, LAST_MESSAGES);
Iterator iter = list.iterator();
if (iter.hasNext()) {
Map map = (Map) iter.next();
messageId = (Long)map.get("messageID");
}
} catch (Exception e) {
logger.error(e);
}
return messageId;
}
public TreeModel getTreeModel(ForumThread forumThread){
logger.debug("getTreeModel from jdbc for threadId=" + forumThread.getThreadId());
Long threadId = forumThread.getThreadId();
int messagesCount = forumThread.getForumThreadState().getMessageCount();
Long rootId = forumThread.getRootMessage().getMessageId();
TreeModel treeModel = new TreeModel(rootId.longValue(), messagesCount + 1);
//get the messagId collection only except the root messageId
String SQL =
"SELECT messageID, parentMessageID, creationDate FROM jiveMessage " +
"WHERE threadID=? AND parentMessageID IS NOT NULL " +
"ORDER BY creationDate ASC";
//parentMessageID IS NOT NULL or parentMessageID != 0 ?
List queryParams = new ArrayList();
queryParams.add(threadId);
try {
List list = jdbcTempSource.getJdbcTemp().queryMultiObject(queryParams, SQL);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Map map = (Map) iter.next();
Long messageID = (Long) map.get("messageID");
Long parentMessageID = (Long) map.get("parentMessageID");
treeModel.addChild(parentMessageID.longValue(), messageID.longValue());
}
} catch (Exception e) {
logger.error(e);
}
return treeModel;
}
/*
* (non-Javadoc)
*
* @see com.jdon.jivejdon.dao.MessageDao#getMessages(java.lang.String, int,
* int)
*/
public PageIterator getMessages(Long threadId, int start, int count) {
String GET_ALL_ITEMS_ALLCOUNT = "select count(1) from jiveMessage where threadID=? ";
String GET_ALL_ITEMS = "select messageID from jiveMessage WHERE threadID=? ORDER BY creationDate ASC";
Collection params = new ArrayList(1);
params.add(threadId);
return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, params, start, count);
}
/* (non-Javadoc)
* @see com.jdon.jivejdon.dao.MessageDao#getThreads(java.lang.String, int, int)
*/
public PageIterator getThreads(Long forumId, int start, int count) {
String GET_ALL_ITEMS_ALLCOUNT = "select count(1) from jiveThread where forumId=?";
String GET_ALL_ITEMS = "select threadID from jiveThread WHERE forumId=? ORDER BY modifiedDate DESC ";
Collection params = new ArrayList(1);
params.add(forumId);
return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, params, start, count);
}
/*
* get the threads collection include prev/cuurent/next threads.
*/
public List getThreadsPrevNext(Long forumId, Long currentThreadId) {
String GET_ALL_ITEMS = "select threadID from jiveThread WHERE forumId=? ORDER BY modifiedDate DESC ";
Collection params = new ArrayList(1);
params.add(forumId);
Block block = pageIteratorSolver.locate(GET_ALL_ITEMS, params, currentThreadId);
if (block == null){
return null;
}
return block.getList();
}
public PageIterator getHotThreads(String fromDate, String toDate, int start, int count){
try {
String GET_ALL_ITEMS_ALLCOUNT =
"SELECT count(1) AS msgCount FROM jiveMessage " +
"WHERE modifiedDate >= ? and modifiedDate <= ? AND parentMessageID IS NULL";
String GET_ALL_ITEMS =
"SELECT threadID, count(1) AS msgCount FROM jiveMessage " +
"WHERE modifiedDate >= ? and modifiedDate <= ? AND parentMessageID IS NULL GROUP BY threadID ORDER BY msgCount DESC";
Collection params = new ArrayList(1);
params.add(fromDate);
params.add(toDate);
return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, params, start, count);
} catch (Exception e) {
e.printStackTrace();
return new PageIterator();
}
}
public PageIterator getThreads(QueryCriteria mqc, int start, int count){
logger.debug("enter getThreads for QueryCriteria" );
try {
StringBuffer where = new StringBuffer( " WHERE modifiedDate >= ? and modifiedDate <= ? AND parentMessageID IS NULL ");
Collection params = new ArrayList(6);
String fromDate = ToolsUtil.dateToMillis(mqc.getFromDate().getTime());
String toDate = ToolsUtil.dateToMillis(mqc.getToDate().getTime());
params.add(fromDate);
params.add(toDate);
if (mqc instanceof MultiCriteria){
MultiCriteria mmqc = (MultiCriteria)mqc;
if (mmqc.getUserID() != null){
where.append(" and userID = ?");
params.add(mmqc.getUserID());
}
if (mmqc.getForumId() != null){
where.append(" and forumID = ? ");
params.add(new Long(mmqc.getForumId()));
}
}
String GET_ALL_ITEMS_ALLCOUNT =
"SELECT count(1) FROM jiveMessage " + where.toString();
String GET_ALL_ITEMS =
"SELECT threadID FROM jiveMessage " + where.toString() + mqc.toResultSortSql();
logger.debug("GET_ALL_ITEMS=" + GET_ALL_ITEMS);
return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT,
GET_ALL_ITEMS, params, start, count);
} catch (Exception e) {
e.printStackTrace();
return new PageIterator();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -