📄 photodb.java
字号:
package cn.js.fan.module.photo;
import java.io.*;
import java.sql.*;
import java.util.*;
import cn.js.fan.base.*;
import cn.js.fan.db.*;
import cn.js.fan.util.*;
import com.redmoon.kit.util.FileInfo;
import com.redmoon.kit.util.FileUpload;
import com.redmoon.forum.SequenceMgr;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class PhotoDb extends ObjectDb {
private int id;
public static final String KIND_DEFAULT = "default";
public static final String KIND_USER_BLOG_PHOTO = "user_blog_photo";
public static final String USER_SYSTEM = "system";
public PhotoDb() {
super();
}
public PhotoDb(int id) {
this.id = id;
init();
load();
}
private String kind;
public PhotoDb getPhotoDb(int id) {
return (PhotoDb)getObjectDb(new Integer(id));
}
public ObjectDb getObjectRaw(PrimaryKey pk) {
return new PhotoDb(pk.getIntValue());
}
public void initDB() {
this.tableName = "photo";
primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT);
objectCache = new PhotoCache(this);
this.QUERY_DEL =
"delete FROM " + tableName + " WHERE id=?";
this.QUERY_CREATE =
"INSERT into " + tableName + " (title,image,userName,sort,kind,addDate,id) VALUES (?,?,?,?,?,?,?)";
this.QUERY_LOAD =
"SELECT title,image,userName,sort,kind,addDate FROM " + tableName + " WHERE id=?";
this.QUERY_SAVE =
"UPDATE " + tableName + " SET title=?,image=?,userName=?,sort=?,kind=? WHERE id=?";
isInitFromConfigDB = false;
}
public boolean save() throws ErrMsgException {
// Based on the id in the object, get the message data from the database.
Conn conn = new Conn(connname);
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(this.QUERY_SAVE);
pstmt.setString(1, title);
pstmt.setString(2, image);
pstmt.setString(3, userName);
pstmt.setInt(4, sort);
pstmt.setString(5, kind);
pstmt.setInt(6, id);
if (conn.executePreUpdate() == 1) {
PhotoCache mc = new PhotoCache(this);
primaryKey.setValue(new Integer(id));
mc.refreshSave(primaryKey);
return true;
}
else
return false;
} catch (SQLException e) {
logger.error("save:" + e.getMessage());
throw new ErrMsgException("更新时出错!");
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
}
public boolean save(FileUpload fu) throws ErrMsgException {
// 删除图片
delImage(fu.getRealPath());
image = writeImage(fu);
return save();
}
public void load() {
// Based on the id in the object, get the message data from the database.
Conn conn = new Conn(connname);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(this.QUERY_LOAD);
pstmt.setInt(1, id);
//url,title,image,userName,sort,kind
rs = conn.executePreQuery();
if (rs.next()) {
this.title = rs.getString(1);
this.image = rs.getString(2);
this.userName = rs.getString(3);
this.sort = rs.getInt(4);
this.kind = rs.getString(5);
java.util.Date d = null;
try {
DateUtil.parse(rs.getString(6));
}
catch (Exception e) {
}
addDate = DateUtil.format(d, "yyyy-MM-dd HH:mm:ss");
primaryKey.setValue(new Integer(id));
}
} catch (SQLException e) {
logger.error(e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
}
public boolean del() throws ErrMsgException {
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement pstmt = conn.prepareStatement(this.QUERY_DEL);
pstmt.setInt(1, id);
if (conn.executePreUpdate()==1) {
PhotoCache mc = new PhotoCache(this);
mc.refreshDel(primaryKey);
return true;
}
else
return false;
} catch (SQLException e) {
logger.error(e.getMessage());
throw new ErrMsgException("删除出错!");
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
}
public boolean del(String realPath) throws
ErrMsgException {
boolean re = del();
if (re)
delImage(realPath);
return re;
}
public void delImage(String realPath) {
if (image != null && !image.equals("")) {
try {
File file = new File(realPath + image);
file.delete();
} catch (Exception e) {
logger.info(e.getMessage());
}
}
}
public String writeImage(FileUpload fu) {
if (fu.getRet() == fu.RET_SUCCESS) {
Vector v = fu.getFiles();
FileInfo fi = null;
if (v.size() > 0)
fi = (FileInfo) v.get(0);
String vpath = "";
if (fi != null) {
// 置保存路径
Calendar cal = Calendar.getInstance();
String year = "" + (cal.get(cal.YEAR));
String month = "" + (cal.get(cal.MONTH) + 1);
vpath = "upfile/" +
fi.getExt() + "/" + year + "/" + month + "/";
String filepath = fu.getRealPath() + vpath;
fu.setSavePath(filepath);
// 使用随机名称写入磁盘
fu.writeFile(true);
return vpath + fi.getDiskName();
}
}
return "";
}
public boolean create(FileUpload fu) throws ErrMsgException {
Conn conn = null;
sort = getMaxOrders(userName, kind);
boolean re = false;
try {
conn = new Conn(connname);
PreparedStatement pstmt = conn.prepareStatement(this.QUERY_CREATE);
pstmt.setString(1, title);
image = writeImage(fu);
pstmt.setString(2, image);
pstmt.setString(3, userName);
pstmt.setInt(4, sort+1);
pstmt.setString(5, kind);
pstmt.setString(6, "" + System.currentTimeMillis());
id = (int)SequenceMgr.nextID(SequenceMgr.PHOTO);
pstmt.setInt(7, id);
re = conn.executePreUpdate() == 1 ? true : false;
if (re) {
PhotoCache mc = new PhotoCache(this);
mc.refreshCreate();
}
} catch (SQLException e) {
logger.error("create:" + e.getMessage());
throw new ErrMsgException("插入Photo时出错!");
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setSort(int sort) {
this.sort = sort;
}
public void setKind(String kind) {
this.kind = kind;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getUserName() {
return userName;
}
public int getSort() {
return sort;
}
public String getKind() {
return kind;
}
public void setImage(String image) {
this.image = image;
}
public String getImage() {
return image;
}
public String getAddDate() {
return addDate;
}
public boolean move(String direction) {
Conn conn = new Conn(connname);
boolean re = false;
try {
if (direction.equals("up")) {
if (sort == 0)
return true;
String sql = "select id from " + tableName + " where sort=? and userName=? and kind=?";;
ResultSet rs = null;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, sort-1);
ps.setString(2, userName);
ps.setString(3, kind);
rs = conn.executePreQuery();
while (rs.next()) {
int id = rs.getInt(1);
PhotoDb ldb = getPhotoDb(id);
ldb.setSort(ldb.getSort()+1);
ldb.save();
}
//sql = "update link set sort=sort+1 where sort=" +
// (sort - 1) + ";";
//sql += "update link set sort=sort-1 where id=" +
// id;
//conn.executeUpdate(sql);
PhotoDb ldb = getPhotoDb(id);
ldb.setSort(ldb.getSort()-1);
ldb.save();
re = true;
} else {
int maxorders = getMaxOrders(userName, kind);
if (sort == maxorders) {
return true;
} else {
String sql = "select id from " + tableName + " where sort=? and userName=? and kind=?";
ResultSet rs = null;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, sort+1);
ps.setString(2, userName);
ps.setString(3, kind);
rs = conn.executePreQuery();
while (rs.next()) {
int id = rs.getInt(1);
PhotoDb ldb = getPhotoDb(id);
ldb.setSort(ldb.getSort()-1);
ldb.save();
}
//sql = "update link set sort=sort+1 where sort=" +
// (sort - 1) + ";";
//sql += "update link set sort=sort-1 where id=" +
// id;
//conn.executeUpdate(sql);
PhotoDb ldb = getPhotoDb(id);
ldb.setSort(ldb.getSort()+1);
ldb.save();
//String sql = "update link set sort=sort-1 where sort=" +
// (sort + 1) + ";";
//sql += "update link set sort=sort+1 where id=" +
// id;
//conn.executeUpdate(sql);
}
re = true;
}
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return re;
}
public int getMaxOrders(String userName, String kind) {
Conn conn = new Conn(connname);
ResultSet rs = null;
int maxorders = -1;
try {
String GETMAXORDERS = "select max(sort) from " + tableName + " where userName=? and kind=?";
PreparedStatement pstmt = conn.prepareStatement(GETMAXORDERS);
pstmt.setString(1, userName);
pstmt.setString(2, kind);
rs = conn.executePreQuery();
if (rs != null) {
if (rs.next()) {
maxorders = rs.getInt(1);
}
}
} catch (SQLException e) {
logger.error(e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
return maxorders;
}
public ListResult ListPhotos(String listsql, int curPage, int pageSize) throws
ErrMsgException {
int total = 0;
ResultSet rs = null;
Vector result = new Vector();
Conn conn = new Conn(connname);
try {
// 取得总记录条数
String countsql = SQLFilter.getCountSql(listsql);
rs = conn.executeQuery(countsql);
if (rs != null && rs.next()) {
total = rs.getInt(1);
}
if (rs != null) {
rs.close();
rs = null;
}
if (total != 0)
conn.setMaxRows(curPage * pageSize); // 尽量减少内存的使用
rs = conn.executeQuery(listsql);
if (rs == null) {
return null;
} else {
rs.setFetchSize(pageSize);
int absoluteLocation = pageSize * (curPage - 1) + 1;
if (rs.absolute(absoluteLocation) == false) {
return null;
}
do {
PhotoDb ug = getPhotoDb(rs.getInt(1));
result.addElement(ug);
} while (rs.next());
}
} catch (SQLException e) {
logger.error("ListPhotos:" + e.getMessage());
throw new ErrMsgException("数据库出错!");
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
ListResult lr = new ListResult();
lr.setResult(result);
lr.setTotal(total);
return lr;
}
private String title;
private String image = "";
private String userName;
private int sort;
private String addDate;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -