linkdb.java

来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 402 行

JAVA
402
字号
package cn.js.fan.module.nav;import cn.js.fan.base.ObjectDb;import java.sql.ResultSet;import java.sql.SQLException;import cn.js.fan.db.PrimaryKey;import cn.js.fan.util.ErrMsgException;import cn.js.fan.db.Conn;import java.sql.PreparedStatement;import com.redmoon.kit.util.FileUpload;import java.util.Vector;import com.redmoon.kit.util.FileInfo;import java.util.Calendar;import java.io.File;public class LinkDb extends ObjectDb {    private int id;    public static final String KIND_DEFAULT = "default";    public static final String KIND_SHOP = "shop";    public static final String USER_SYSTEM = "system";    public static final String KIND_USER_BLOG = "user_blog";    public LinkDb() {        super();    }    public LinkDb(int id) {        this.id = id;        init();        load();    }    private String kind;    public LinkDb getLinkDb(int id) {        return (LinkDb)getObjectDb(new Integer(id));    }    public ObjectDb getObjectRaw(PrimaryKey pk) {        return new LinkDb(pk.getIntValue());    }    public void initDB() {        this.tableName = "link";        primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT);        objectCache = new LinkCache(this);        this.QUERY_DEL =                "delete FROM " + tableName + " WHERE id=?";        this.QUERY_CREATE =                "INSERT " + tableName + " (url,title,image,userName,sort,kind) VALUES (?,?,?,?,?,?)";        this.QUERY_LOAD =                "SELECT url,title,image,userName,sort,kind FROM " + tableName + " WHERE id=?";        this.QUERY_SAVE =                "UPDATE " + tableName + " SET url=?,title=?,image=?,userName=?,sort=?,kind=? 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, url);            pstmt.setString(2, title);            pstmt.setString(3, image);            pstmt.setString(4, userName);            pstmt.setInt(5, sort);            pstmt.setString(6, kind);            pstmt.setInt(7, id);            if (conn.executePreUpdate() == 1) {                LinkCache mc = new LinkCache(this);                primaryKey.setValue(new Integer(id));                mc.refreshSave(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 save(FileUpload fu) throws ErrMsgException {                delImage(fu.getRealPath());        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.setInt(1, id);                        rs = conn.executePreQuery();            if (rs.next()) {                this.url = rs.getString(1);                this.title = rs.getString(2);                this.image = rs.getString(3);                this.userName = rs.getString(4);                this.sort = rs.getInt(5);                this.kind = rs.getString(6);                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) {                LinkCache mc = new LinkCache(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, url);            pstmt.setString(2, title);            image = writeImage(fu);            pstmt.setString(3, image);            pstmt.setString(4, userName);            pstmt.setInt(5, sort+1);            pstmt.setString(6, kind);            re = conn.executePreUpdate() == 1 ? true : false;            if (re) {                LinkCache mc = new LinkCache(this);                mc.refreshCreate();            }        } catch (SQLException e) {            logger.error("create:" + e.getMessage());            throw new ErrMsgException("插入Link时出错!");        } 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 void setUrl(String url) {        this.url = url;    }    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 String getUrl() {        return url;    }    public void setImage(String image) {        this.image = image;    }    public String getImage() {        return image;    }    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);                    LinkDb ldb = getLinkDb(id);                    ldb.setSort(ldb.getSort()+1);                    ldb.save();                }                                                                                                LinkDb ldb = getLinkDb(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);                        LinkDb ldb = getLinkDb(id);                        ldb.setSort(ldb.getSort()-1);                        ldb.save();                    }                                                                                                                        LinkDb ldb = getLinkDb(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(String userName, String kind) {        Conn conn = new Conn(connname);        ResultSet rs = null;        int maxorders = -1;        try {            String GETMAXORDERS = "select max(sort) from link 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;    }    private String title;    private String image = "";    private String userName;    private int sort;    private String url;}

⌨️ 快捷键说明

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