photodb.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 430 行

JAVA
430
字号
package com.redmoon.blog.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 cn.js.fan.web.*;import com.redmoon.forum.*;import com.redmoon.forum.Config;import com.redmoon.forum.util.*;import com.redmoon.kit.util.*;public class PhotoDb extends ObjectDb {    public static final String photoBasePath = "blog/photo";    private long id;    public PhotoDb() {        super();    }    public PhotoDb(long id) {        this.id = id;        init();        load();    }    public PhotoDb getPhotoDb(long id) {        return (PhotoDb)getObjectDb(new Long(id));    }    public ObjectDb getObjectRaw(PrimaryKey pk) {        return new PhotoDb(pk.getLongValue());    }    public String getListPhotoSql() {        return "select id from blog_photo ORDER BY score desc";    }    public void initDB() {        this.tableName = "blog_photo";        primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_LONG);        objectCache = new PhotoCache(this);        this.QUERY_DEL =                "delete FROM " + tableName + " WHERE id=?";        this.QUERY_CREATE =                "INSERT into " + tableName + " (title,image,blog_id,sort,addDate,id,is_remote,dir_code) VALUES (?,?,?,?,?,?,?,?)";        this.QUERY_LOAD =                "SELECT title,image,blog_id,sort,addDate,is_remote,dir_code,score FROM " + tableName + " WHERE id=?";        this.QUERY_SAVE =                "UPDATE " + tableName + " SET title=?,image=?,blog_id=?,sort=?,is_remote=?,dir_code=?,score=? WHERE id=?";        isInitFromConfigDB = false;    }    public boolean save() throws ErrMsgException {                Conn conn = new Conn(connname);        PreparedStatement pstmt = null;        try {            pstmt = conn.prepareStatement(this.QUERY_SAVE);            pstmt.setString(1, title);            pstmt.setString(2, image);            pstmt.setLong(3, blogId);            pstmt.setInt(4, sort);            Config cfg = Config.getInstance();            remote = cfg.getBooleanProperty("forum.ftpUsed");            pstmt.setInt(5, remote?1:0);            pstmt.setString(6, dirCode);            pstmt.setInt(7, score);            pstmt.setLong(8, id);            if (conn.executePreUpdate() == 1) {                PhotoCache mc = new PhotoCache(this);                primaryKey.setValue(new Long(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(ForumFileUpload fu) throws ErrMsgException {                        image = writeImage(fu);        return save();    }    public void load() {                Conn conn = new Conn(connname);        PreparedStatement pstmt = null;        ResultSet rs = null;        try {            pstmt = conn.prepareStatement(this.QUERY_LOAD);            pstmt.setLong(1, id);            rs = conn.executePreQuery();            if (rs.next()) {                title = rs.getString(1);                image = rs.getString(2);                blogId = rs.getLong(3);                sort = rs.getInt(4);                addDate = DateUtil.parse(rs.getString(5));                remote = rs.getInt(6)==1;                dirCode = rs.getString(7);                score = rs.getInt(8);                primaryKey.setValue(new Long(id));                loaded = true;            }        } catch (SQLException e) {            logger.error("load:" + e.getMessage());        } finally {            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.setLong(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("")) {            Config cfg = Config.getInstance();            boolean isFtpUsed = cfg.getBooleanProperty("forum.ftpUsed");            if (isFtpUsed) {                if (remote) {                    ForumFileUtil ffu = new ForumFileUtil();                    ffu.delFtpFile(photoBasePath + "/" + image);                }            } else {                try {                    File file = new File(realPath + "upfile/" + photoBasePath + "/" + image);                    file.delete();                } catch (Exception e) {                    logger.info(e.getMessage());                }            }        }    }    public String writeImage(ForumFileUpload 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 = photoBasePath + "/" + year + "/" + month + "/";                String filepath = Global.getRealPath() + "upfile/" + vpath;                                fu.setSavePath(filepath);                                fu.setRemoteBasePath(vpath);                                fu.writeFile(true);                return year + "/" + month + "/" + fi.getDiskName();            }        }        return image;    }    public boolean create(ForumFileUpload fu) throws ErrMsgException {        Conn conn = null;        sort = getMaxOrders(blogId);        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.setLong(3, blogId);            pstmt.setInt(4, sort+1);            pstmt.setString(5, "" + System.currentTimeMillis());            id = (int)SequenceMgr.nextID(SequenceMgr.PHOTO);            pstmt.setLong(6, id);            Config cfg = Config.getInstance();            remote = cfg.getBooleanProperty("forum.ftpUsed");            pstmt.setInt(7, remote ? 1 : 0);            pstmt.setString(8, dirCode);            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(e.getMessage());        } 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 setSort(int sort) {        this.sort = sort;    }    public long getId() {        return id;    }    public String getTitle() {        return title;    }    public int getSort() {        return sort;    }    public void setImage(String image) {        this.image = image;    }    public void setBlogId(long blogId) {        this.blogId = blogId;    }    public void setRemote(boolean remote) {        this.remote = remote;    }    public void setDirCode(String dirCode) {        this.dirCode = dirCode;    }    public void setScore(int score) {        this.score = score;    }    public String getImage() {        return image;    }    public java.util.Date getAddDate() {        return addDate;    }    public long getBlogId() {        return blogId;    }    public boolean isRemote() {        return remote;    }    public String getDirCode() {        return dirCode;    }    public int getScore() {        return score;    }    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 blog_id=?";;                ResultSet rs = null;                PreparedStatement ps = conn.prepareStatement(sql);                ps.setInt(1, sort-1);                ps.setLong(2, blogId);                rs = conn.executePreQuery();                while (rs.next()) {                    int id = rs.getInt(1);                    PhotoDb ldb = getPhotoDb(id);                    ldb.setSort(ldb.getSort()+1);                    ldb.save();                }                                                                                                PhotoDb ldb = getPhotoDb(id);                ldb.setSort(ldb.getSort()-1);                ldb.save();                re = true;            } else {                int maxorders = getMaxOrders(blogId);                if (sort == maxorders) {                    return true;                } else {                    String sql = "select id from " + tableName + " where sort=? and blog_id=?";                    ResultSet rs = null;                    PreparedStatement ps = conn.prepareStatement(sql);                    ps.setInt(1, sort+1);                    ps.setLong(2, blogId);                    rs = conn.executePreQuery();                    while (rs.next()) {                        int id = rs.getInt(1);                        PhotoDb ldb = getPhotoDb(id);                        ldb.setSort(ldb.getSort()-1);                        ldb.save();                    }                                                                                                                        PhotoDb ldb = getPhotoDb(id);                    ldb.setSort(ldb.getSort()+1);                    ldb.save();                                                                                                                    }                re = true;            }        } catch (Exception e) {            logger.error(e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return re;    }    public int getMaxOrders(long blogId) {        Conn conn = new Conn(connname);        ResultSet rs = null;        int maxorders = -1;        try {            String GETMAXORDERS = "select max(sort) from " + tableName + " where blog_id=?";            PreparedStatement pstmt = conn.prepareStatement(GETMAXORDERS);            pstmt.setLong(1, blogId);            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;    }    private String title;    private String image = "";    private int sort;    private java.util.Date addDate;    private long blogId;    private boolean remote = false;    private String dirCode = DirDb.ROOTCODE;    private int score = 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?