📄 blogdb.java
字号:
package com.redmoon.blog;
import java.sql.*;
import java.util.*;
import java.util.Date;
import cn.js.fan.base.*;
import cn.js.fan.db.*;
import cn.js.fan.util.*;
public class BlogDb extends ObjectDb {
public static final int ID = 1;
private int id = ID;
private int todayCount = 0;
private int userCount = 0;
private int topicCount = 0;
private int postCount = 0;
private int yestodayCount = 0;
private String newBlogUserName;
public BlogDb() {
id = ID;
init();
}
public BlogDb(int id) {
this.id = ID;
init();
load();
}
/**
* 取得博客最新发表的n条贴子
* @return long[]
*/
public long[] getNewBlogMsgs(int n) {
BlogCache fc = new BlogCache(this);
return fc.getNewBlogMsgs(n);
}
public long[] getHotBlogMsgs(int n) {
BlogCache fc = new BlogCache(this);
return fc.getHotBlogMsgs(n);
}
public String[] getPostRank(int n) {
BlogCache fc = new BlogCache(this);
return fc.getPostRank(n);
}
public String[] getNewBlogs(int n) {
BlogCache fc = new BlogCache(this);
return fc.getNewBlogs(n);
}
/**
* 推荐Blog - mzlkr
* @param n int
* @return String[]
*/
public String[] getCommendBlogs(int n) {
BlogCache fc = new BlogCache(this);
return fc.getCommendBlogs(n);
}
public int[] getNewPhotos(int n) {
BlogCache fc = new BlogCache(this);
return fc.getNewPhotos(n);
}
/**
* 取得博客类别中某一类文章的前n篇
* @param n int 前n篇
* @param kind String 博客类别(博客类别节点的父结点为root)
* @return long[] 贴子ID数组
*/
public long[] getBlogMsgsOfKind(int n, String kind) {
BlogCache fc = new BlogCache(this);
return fc.getBlogMsgsOfKind(n, kind);
}
public ObjectDb getObjectDb(Object primaryKeyValue) {
BlogCache fc = new BlogCache(this);
primaryKey.setValue(primaryKeyValue);
return (BlogDb)fc.getObjectDb(primaryKey);
}
public BlogDb getBlogDb() {
return (BlogDb)getObjectDb(new Integer(ID));
}
public boolean del() {
return true;
}
public int getObjectCount(String sql) {
return 0;
}
public ObjectDb getObjectRaw(PrimaryKey pk) {
return new BlogDb(pk.getIntValue());
}
public void setQueryCreate() {
}
public void setQuerySave() {
this.QUERY_SAVE =
"update blog set blogCount=?,newBlogUserName=?, todayCount=?, topicCount=?, postCount=?, yestodayCount=?, todayDate=?,star=?,homeClasses=?,recommandBlogs=? where id=?";
}
public void setQueryDel() {
}
public void setQueryLoad() {
this.QUERY_LOAD =
"select blogCount, newBlogUserName, todayCount, topicCount, postCount, yestodayCount, todayDate,star,homeClasses,recommandBlogs from blog where id=?";
}
public void setQueryList() {
}
public boolean save() {
int rowcount = 0;
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement ps = conn.prepareStatement(this.QUERY_SAVE);
ps.setInt(1, blogCount);
ps.setString(2, newBlogUserName);
ps.setInt(3, todayCount);
ps.setInt(4, topicCount);
ps.setInt(5, postCount);
ps.setInt(6, yestodayCount);
ps.setString(7, DateUtil.toLongString(todayDate));
ps.setString(8, star);
ps.setString(9, homeClasses);
ps.setString(10, recommandBlogs);
ps.setInt(11, id);
rowcount = conn.executePreUpdate();
BlogCache uc = new BlogCache(this);
primaryKey.setValue(new Integer(this.id));
uc.refreshSave(primaryKey);
} catch (SQLException e) {
logger.error(e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return rowcount>0? true:false;
}
public void load() {
ResultSet rs = null;
Conn conn = new Conn(connname);
try {
PreparedStatement ps = conn.prepareStatement(this.QUERY_LOAD);
ps.setInt(1, id);
primaryKey.setValue(new Integer(id));
rs = conn.executePreQuery();
if (rs.next()) {
blogCount = rs.getInt(1);
newBlogUserName = rs.getString(2);
todayCount = rs.getInt(3);
topicCount = rs.getInt(4);
postCount = rs.getInt(5);
yestodayCount = rs.getInt(6);
todayDate = DateUtil.parse(rs.getString(7));
star = rs.getString(8);
homeClasses = rs.getString(9);
recommandBlogs = rs.getString(10);
loaded = true;
}
} catch (SQLException e) {
logger.error("load:" + e.getMessage());
}
finally {
if (conn!=null) {
conn.close();
conn = null;
}
}
}
public Object[] getObjectBlock(String query, int startIndex) {
return null;
}
public void setPrimaryKey() {
primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT);
}
public void setId(int id) {
this.id = id;
}
public void setTodayCount(int todayCount) {
this.todayCount = todayCount;
}
public void setUserCount(int userCount) {
this.userCount = userCount;
}
public void setTopicCount(int topicCount) {
this.topicCount = topicCount;
}
public void setPostCount(int postCount) {
this.postCount = postCount;
}
public void setYestodayCount(int yestodayCount) {
this.yestodayCount = yestodayCount;
}
public int getTodayCount() {
return todayCount;
}
public int getUserCount() {
return userCount;
}
public int getTopicCount() {
return topicCount;
}
public int getPostCount() {
return postCount;
}
public int getYestodayCount() {
return yestodayCount;
}
public Date getTodayDate() {
return todayDate;
}
public int getBlogCount() {
return blogCount;
}
public String getNewBlogUserName() {
return newBlogUserName;
}
public String getStar() {
return star;
}
public String getHomeClasses() {
return homeClasses;
}
public String getRecommandBlogs() {
return recommandBlogs;
}
/**
* 更新统计信息
* @param isAddNew boolean 是否为发新贴而不是回贴
*/
public void setStatics(boolean isAddNew) {
// 从数据库中取出今天日期
Calendar todaydb = Calendar.getInstance();
todaydb.setTime(todayDate);
Calendar today = Calendar.getInstance();
// 如果today_date字段中为当前日期,则today_count加1
if (DateUtil.datediff(todaydb, today) == 0) {
setTodayCount(todayCount + 1);
} else { // 如果字段日期与本日不一致,则说明是本日第一贴
setYestodayCount(todayCount);
todayCount = 1;
todayDate = today.getTime();
}
if (isAddNew)
setTopicCount(topicCount + 1);
setPostCount(postCount + 1);
save();
}
// 重新载入
public void reload() {
BlogCache uc = new BlogCache(this);
primaryKey.setValue(new Integer(this.id));
uc.refreshSave(primaryKey);
}
public void setBlogCount(int blogCount) {
this.blogCount = blogCount;
}
public void setNewBlogUserName(String newBlogUserName) {
this.newBlogUserName = newBlogUserName;
}
public void setStar(String star) {
this.star = star;
}
public void setHomeClasses(String homeClasses) {
this.homeClasses = homeClasses;
}
public void setRecommandBlogs(String recommandBlogs) {
this.recommandBlogs = recommandBlogs;
}
public Vector getAllHomeClasses() {
Vector v = new Vector();
if (homeClasses!=null && !homeClasses.equals("")) {
Leaf lf = new Leaf();
homeClasses = homeClasses.replaceAll(",", ",");
String[] ids = homeClasses.split("\\,");
int len = ids.length;
for (int i = 0; i < len; i++) {
String str = ids[i].trim();
// System.out.print("getAllHomeClasses str=" + str);
if (lf==null)
lf = new Leaf();
lf = lf.getLeaf(str);
if (lf != null && lf.isLoaded())
v.addElement(lf);
}
}
return v;
}
/**
* 取得所有的推荐博客
* @return Vector
*/
public Vector getAllRecommandBlogs() {
Vector v = new Vector();
if (recommandBlogs!=null && !recommandBlogs.equals("")) {
UserConfigDb ucd = new UserConfigDb();
recommandBlogs = recommandBlogs.replaceAll(",", ",");
String[] ids = recommandBlogs.split("\\,");
int len = ids.length;
for (int i = 0; i < len; i++) {
String str = ids[i].trim();
// System.out.print("getAllHomeClasses str=" + str);
ucd = ucd.getUserConfigDb(str);
if (ucd != null && ucd.isLoaded())
v.addElement(ucd);
}
}
return v;
}
private Date todayDate;
private int blogCount;
private String star;
private String homeClasses;
private String recommandBlogs;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -