📄 topicmodel.java
字号:
t.setHasAttach(rs.getInt("attach") > 0);
// First Post Time
t.setFirstPostTime(df.format(rs.getTimestamp("topic_time")));
// Last Post Time
t.setLastPostTime(df.format(rs.getTimestamp("post_time")));
t.setLastPostTimeInMillis(rs.getTimestamp("post_time"));
// Created by
User u = new User();
u.setId(rs.getInt("posted_by_id"));
u.setUsername(rs.getString("posted_by_username"));
t.setPostedBy(u);
// Last post by
u = new User();
u.setId(rs.getInt("last_post_by_id"));
u.setUsername(rs.getString("last_post_by_username"));
t.setLastPostBy(u);
l.add(t);
}
return l;
}
/**
* @see net.jforum.model.TopicModel#autoSetLastPostId(int)
*/
public int getMaxPostId(int topicId) throws Exception
{
int id = -1;
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMaxPostId"));
p.setInt(1, topicId);
ResultSet rs = p.executeQuery();
if (rs.next()) {
id = rs.getInt("post_id");
}
rs.close();
p.close();
return id;
}
/**
* @see net.jforum.model.TopicModel#getTotalPosts(int)
*/
public int getTotalPosts(int topicId) throws Exception
{
int total = 0;
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getTotalPosts"));
p.setInt(1, topicId);
ResultSet rs = p.executeQuery();
if (rs.next()) {
total = rs.getInt("total");
}
rs.close();
p.close();
return total;
}
/**
* @see net.jforum.model.TopicModel#selectLastN(int)
*/
public List selectLastN(int count) throws Exception
{
List topics = new ArrayList();
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectLastN"));
p.setInt(1, count);
ResultSet rs = p.executeQuery();
// If you want more fields here, just put the code. At the time
// this code was written, these were the only needed fields ;)
while (rs.next()) {
Topic t = new Topic();
t.setTitle(rs.getString("topic_title"));
t.setId(rs.getInt("topic_id"));
t.setTime(rs.getTimestamp("topic_time"));
t.setType(rs.getInt("topic_type"));
topics.add(t);
}
rs.close();
p.close();
return topics;
}
/**
* @see net.jforum.model.TopicModel#notifyUsers(int)
*/
public List notifyUsers(Topic topic) throws Exception
{
int posterId = SessionFacade.getUserSession().getUserId();
int anonUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
PreparedStatement stmt = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.notifyUsers"));
ResultSet rs = null;
stmt.setInt(1, topic.getId());
stmt.setInt(2, posterId); //don't notify the poster
stmt.setInt(3, anonUser); //don't notify the anonimous user
rs = stmt.executeQuery();
List users = new ArrayList();
while(rs.next()) {
User user = new User();
user.setId(rs.getInt("user_id"));
user.setEmail(rs.getString("user_email"));
user.setUsername(rs.getString("username"));
user.setLang(rs.getString("user_lang"));
users.add(user);
}
// Set read status to false
stmt = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.markAllAsUnread"));
stmt.setInt(1, topic.getId());
stmt.setInt(2, posterId); //don't notify the poster
stmt.setInt(3, anonUser); //don't notify the anonimous user
stmt.executeUpdate();
rs.close();
stmt.close();
return users;
}
/**
* @see net.jforum.model.TopicModel#subscribeUser(int, int)
*/
public void subscribeUser(int topicId, int userId) throws Exception
{
PreparedStatement stmt = JForum.getConnection(). prepareStatement( SystemGlobals.getSql("TopicModel.subscribeUser"));
try {
stmt.setInt(1, topicId);
stmt.setInt(2, userId);
stmt.executeUpdate();
} catch(SQLException e) {
// Ignore duplicate key warnings
if(e.getErrorCode() != DUPLICATE_KEY) {
throw e;
}
}
finally {
if(stmt != null) {
stmt.close();
}
}
}
/**
* @see net.jforum.model.TopicModel#isUserSubscribing(int, int)
*/
public boolean isUserSubscribed(int topicId, int userId) throws Exception
{
PreparedStatement stmt = JForum.getConnection(). prepareStatement( SystemGlobals.getSql("TopicModel.isUserSubscribed"));
ResultSet rs = null;
stmt.setInt(1, topicId);
stmt.setInt(2, userId);
rs = stmt.executeQuery();
boolean status = rs.next();
rs.close();
stmt.close();
return status;
}
/**
* @see net.jforum.model.TopicModel#removeSubscription(int, int)
*/
public void removeSubscription(int topicId, int userId) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscription"));
p.setInt(1, topicId);
p.setInt(2, userId);
p.executeUpdate();
p.close();
}
/**
* @see net.jforum.model.TopicModel#removeSubscriptionByTopic(int)
*/
public void removeSubscriptionByTopic(int topicId) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscriptionByTopic"));
p.setInt(1, topicId);
p.executeUpdate();
p.close();
}
/**
* @see net.jforum.model.TopicModel#updateReadStatus(int, int, boolean)
*/
public void updateReadStatus(int topicId, int userId, boolean read) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.updateReadStatus"));
p.setInt(1, read ? 1 : 0);
p.setInt(2, topicId);
p.setInt(3, userId);
p.executeUpdate();
p.close();
}
/**
* @see net.jforum.model.TopicModel#lockUnlock(int, int)
*/
public void lockUnlock(int[] topicId, int status) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.lockUnlock"));
p.setInt(1, status);
for (int i = 0; i < topicId.length; i++) {
p.setInt(2, topicId[i]);
p.executeUpdate();
}
p.close();
}
/**
* @see net.jforum.model.TopicModel#selectRecentTopics(int)
*/
public List selectRecentTopics (int limit) throws Exception
{
List l = new ArrayList();
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectRecentTopicsByLimit"));
p.setInt(1, limit);
ResultSet rs = p.executeQuery();
l = this.fillTopicsData(rs);
rs.close();
p.close();
return l;
}
/**
* @see net.jforum.model.TopicModel#setFirstPostId(int, int)
*/
public void setFirstPostId(int topicId, int postId) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setFirstPostId"));
p.setInt(1, postId);
p.setInt(2, topicId);
p.executeUpdate();
p.close();
}
/**
* @see net.jforum.model.TopicModel#getMinPostId(int)
*/
public int getMinPostId(int topicId) throws Exception
{
int id = -1;
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMinPostId"));
p.setInt(1, topicId);
ResultSet rs = p.executeQuery();
if (rs.next()) {
id = rs.getInt("post_id");
}
rs.close();
p.close();
return id;
}
/**
* @see net.jforum.model.TopicModel#setModerationStatus(int, boolean)
*/
public void setModerationStatus(int forumId, boolean status) throws Exception
{
PreparedStatement p = JForum.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setModerationStatus"));
p.setInt(1, status ? 1 : 0);
p.setInt(2, forumId);
p.executeUpdate();
p.close();
}
/**
* @see net.jforum.model.TopicModel#selectTopicTitlesByIds(java.util.Collection)
*/
public List selectTopicTitlesByIds(Collection idList) throws Exception
{
List l = new ArrayList();
String sql = SystemGlobals.getSql("TopicModel.selectTopicTitlesByIds");
StringBuffer sb = new StringBuffer(idList.size() * 2);
for (Iterator iter = idList.iterator(); iter.hasNext(); ) {
sb.append(iter.next()).append(",");
}
int len = sb.length();
sql = sql.replaceAll(":ids:", len > 0 ? sb.toString().substring(0, len - 1) : "0");
PreparedStatement p = JForum.getConnection().prepareStatement(sql);
ResultSet rs = p.executeQuery();
while (rs.next()) {
Map m = new HashMap();
m.put("id", new Integer(rs.getInt("topic_id")));
m.put("title", rs.getString("topic_title"));
l.add(m);
}
rs.close();
p.close();
return l;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -