package j2eebbs;
import java.sql.ResultSet;
import java.util.Vector;
public class Forum {
private int id;
private String forumname;
private String manager;
private int topicNum;
private int lastTopicId;
private String lastTopicTitle = "";
private String lastTopicAuthor = "";
private String lastTopicTime = "";
public String getForumname() {
return forumname;
}
public void setForumname(String forumname) {
this.forumname = forumname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastTopicAuthor() {
return lastTopicAuthor;
}
public void setLastTopicAuthor(String lastTopicAuthor) {
this.lastTopicAuthor = lastTopicAuthor;
}
public int getLastTopicId() {
return lastTopicId;
}
public void setLastTopicId(int lastTopicId) {
this.lastTopicId = lastTopicId;
}
public String getLastTopicTime() {
return lastTopicTime;
}
public void setLastTopicTime(String lastTopicTime) {
this.lastTopicTime = lastTopicTime;
}
public String getLastTopicTitle() {
return lastTopicTitle;
}
public void setLastTopicTitle(String lastTopicTitle) {
this.lastTopicTitle = lastTopicTitle;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public int getTopicNum() {
return topicNum;
}
public void setTopicNum(int topicNum) {
this.topicNum = topicNum;
}
public Forum(int id, String forumname, String manager, int topicNum,
int lastTopicId, String lastTopicTitle, String lastTopicAuthor,
String lastTopicTime) {
this.id = id;
this.forumname = forumname;
this.manager = manager;
this.topicNum = topicNum;
this.lastTopicId = lastTopicId;
this.lastTopicTitle = lastTopicTitle;
this.lastTopicAuthor = lastTopicAuthor;
this.lastTopicTime = lastTopicTime;
}
public Forum(int id, String forumname, String manager) {
this.id = id;
this.forumname = forumname;
this.manager = manager;
}
public static Vector search(DB db) throws Exception {
int topicNum = 0;
int lastTopicId;
String lastTopicTitle;
String lastTopicAuthor;
String lastTopicTime;
Vector forumVector = new Vector();
ResultSet rs, rsTopic;
String strSql;
int forumid;
strSql = "select * from forum";
rs = db.OpenSql(strSql);
while (rs.next()) {
lastTopicId = 0;
lastTopicTitle = null;
lastTopicAuthor = null;
lastTopicTime = null;
forumid = rs.getInt("id");
strSql = "select count(*) from topic where forumid=" + forumid;
rsTopic = db.OpenSql(strSql);
if (rsTopic.next()) {
topicNum = rsTopic.getInt(1);
}
strSql = "select * from topic where forumid=" + forumid
+ " order by forumid desc";
rsTopic = db.OpenSql(strSql);
if (rsTopic.next()) {
lastTopicId = rsTopic.getInt("id");
lastTopicTitle = rsTopic.getString("title");
lastTopicTime = rsTopic.getString("submittime");
lastTopicAuthor = rsTopic.getString("author");
}
forumVector.add(new Forum(forumid, rs.getString("forumname"), rs
.getString("manager"), topicNum, lastTopicId,
lastTopicTitle, lastTopicTime, lastTopicAuthor));
}
return forumVector;
}
public static Vector searchAllForums(DB db) throws Exception {
int forumid = 0;
String forumname = null;
String manager = null;
Vector forumVector = new Vector();
ResultSet rs;
String strSql;
strSql = "select * from forum";
rs = db.OpenSql(strSql);
while (rs.next()) {
forumid = rs.getInt("id");
forumname = rs.getString("forumname");
manager = rs.getString("manager");
forumVector.add(new Forum(forumid, forumname, manager));
}
return forumVector;
}
public static boolean insert(DB db, String forumname) throws Exception {
String strSql;
ResultSet rs;
int iMaxId;
strSql = "select max(id) from forum";
rs = db.OpenSql(strSql);
if (rs.next()) {
iMaxId = rs.getInt(1) + 1;
} else {
iMaxId = 1;
}
strSql = "insert into forum values('" + iMaxId + "','" + forumname
+ "','')";
if (db.ExecSql(strSql) == 0) {
return false;
} else {
return true;
}
}
public static boolean delete(DB db, String forumid) throws Exception {
String strSql;
strSql = "delete from forum where id=" + forumid;
if (db.ExecSql(strSql) == 0) {
return false;
} else {
return true;
}
}
public static boolean edit(DB db, String forumid, String forumname,
String manager) throws Exception {
String strSql;
strSql = "update forum set forumname='" + forumname + "',manager='"
+ manager + "'where id=" + forumid;
if (db.ExecSql(strSql) == 0) {
return false;
} else {
return true;
}
}
}