userconfigdb.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 877 行 · 第 1/2 页
JAVA
877 行
package com.redmoon.blog;import java.io.*;import java.sql.*;import java.sql.Date;import java.util.*;import javax.servlet.http.*;import cn.js.fan.base.*;import cn.js.fan.db.*;import cn.js.fan.module.cms.robot.*;import cn.js.fan.util.*;import cn.js.fan.util.file.*;import cn.js.fan.web.*;import com.cloudwebsoft.framework.util.*;import com.redmoon.blog.ui.*;import com.redmoon.forum.*;import com.redmoon.forum.util.*;import com.redmoon.kit.util.*;public class UserConfigDb extends ObjectDb { public static int TYPE_PERSON = 0; public static int TYPE_GROUP = 1; public static int NO_BLOG = -1; public static final String iconBasePath = "blog/icon"; public static final String cssBasePath = "blog/css"; public String oldDomain; public UserConfigDb() { } public UserConfigDb(long id) { this.id = id; init(); load(); } public String writeIcon(ForumFileUpload fu) throws ErrMsgException { if (fu.getRet() == fu.RET_SUCCESS) { Vector v = fu.getFiles(); FileInfo fi = null; String vpath = ""; Iterator ir = v.iterator(); if (ir.hasNext()) { fi = (FileInfo) ir.next(); Calendar cal = Calendar.getInstance(); String year = "" + (cal.get(cal.YEAR)); vpath = "upfile/" + iconBasePath + "/" + year + "/"; String filepath = Global.getRealPath() + vpath; File f = new File(vpath); if (!f.isDirectory()) f.mkdirs(); fu.setRemoteBasePath(iconBasePath + "/" + year); fu.setSavePath(filepath); fu.writeFile(true); return year + "/" + fi.getDiskName(); } } return ""; } public String getIconUrl(HttpServletRequest request) { com.redmoon.forum.Config cfg = com.redmoon.forum.Config.getInstance(); String basePath = request.getContextPath() + "/upfile/" + iconBasePath + "/"; boolean isFtpUsed = cfg.getBooleanProperty("forum.ftpUsed"); if (isFtpUsed) { basePath = cfg.getRemoteBaseUrl(); basePath += iconBasePath + "/"; } return basePath + icon; } public boolean create(ForumFileUpload fu) throws ErrMsgException { icon = writeIcon(fu); Conn conn = null; try { id = SequenceMgr.nextID(SequenceMgr.BLOG_ID); conn = new Conn(connname); PreparedStatement ps = conn.prepareStatement(QUERY_CREATE); ps.setString(1, userName); ps.setString(2, title); ps.setString(3, subtitle); ps.setString(4, penName); ps.setString(5, skin); ps.setString(6, notice); ps.setString(7, kind); ps.setString(8, "" + System.currentTimeMillis()); ps.setLong(9, id); ps.setInt(10, type); ps.setString(11, icon); ps.setInt(12, footprint ? 1 : 0); ps.setString(13, domain); if (!(conn.executePreUpdate() == 1 ? true : false)) return false; UserConfigCache mc = new UserConfigCache(this); mc.refreshCreate(); mc.refreshUserBlogId(userName); } catch (SQLException e) { logger.error("create: " + e.getMessage()); return false; } finally { if (conn != null) { conn.close(); conn = null; } } return true; } public void delIcon() throws ResKeyException { if (!icon.equals("")) { com.redmoon.forum.Config cfg = com.redmoon.forum.Config.getInstance(); boolean isFtpUsed = cfg.getBooleanProperty("forum.ftpUsed"); if (isFtpUsed) { ForumFileUtil ffu = new ForumFileUtil(); ffu.delFtpFile(iconBasePath + "/" + icon); } else { File f = new File(Global.realPath + "upfile/" + iconBasePath + "/" + icon); f.delete(); } icon = ""; save(); } } public boolean del() throws ErrMsgException, ResKeyException { boolean re = false; MsgDb md = new MsgDb(); md.delMesssagesOfBlog(id); UserDirDb udd = new UserDirDb(); udd.delDirsOfBlog(id); Conn conn = new Conn(connname); try { PreparedStatement ps = conn.prepareStatement(QUERY_DEL); ps.setLong(1, id); re = conn.executePreUpdate() == 1 ? true : false; } catch (Exception e) { logger.error("del:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } if (re) { if (!icon.equals("")) { File f = new File(Global.realPath + icon); f.delete(); } UserConfigCache uc = new UserConfigCache(this); primaryKey.setValue(new Long(id)); uc.refreshDel(primaryKey); uc.refreshList(); if (type == TYPE_GROUP) { BlogGroupUserDb bgu = new BlogGroupUserDb(); bgu.delUserOfBlog(id); } } return re; } public ListResult ListBlog(String listsql, int curPage, int pageSize) throws ErrMsgException { int total = 0; ResultSet rs = null; Vector result = new Vector(); ListResult lr = new ListResult(); lr.setTotal(total); lr.setResult(result); 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 lr; } else { rs.setFetchSize(pageSize); int absoluteLocation = pageSize * (curPage - 1) + 1; if (rs.absolute(absoluteLocation) == false) { return lr; } do { UserConfigDb ug = getUserConfigDb(rs.getLong(1)); result.addElement(ug); } while (rs.next()); } } catch (SQLException e) { logger.error(e.getMessage()); throw new ErrMsgException("数据库出错!"); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } lr.setResult(result); lr.setTotal(total); return lr; } public ObjectDb getObjectRaw(PrimaryKey pk) { return new UserConfigDb(pk.getLongValue()); } public UserConfigDb getUserConfigDb(long id) { return (UserConfigDb) getObjectDb(new Long(id)); } public long getBlogIdByUserName(String userName) { UserConfigCache ucc = new UserConfigCache(); return ucc.getBlogIdByUserName(userName); } public long getBlogIdByDomain(String domain) { UserConfigCache ucc = new UserConfigCache(); return ucc.getBlogIdByDomain(domain); } public long getBlogIdByUserNameFromDb(String userName) { String sql = "select id from " + tableName + " where userName=? and blog_type=" + TYPE_PERSON; PreparedStatement pstmt = null; ResultSet rs = null; Conn conn = new Conn(connname); try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, userName); rs = conn.executePreQuery(); if (rs.next()) { return rs.getLong(1); } } catch (SQLException e) { logger.error("getBlogIdByUserNameFromDb:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return NO_BLOG; } public long getBlogIdByDomainFromDb(String domain) { String sql = "select id from " + tableName + " where domain=?"; PreparedStatement pstmt = null; ResultSet rs = null; Conn conn = new Conn(connname); try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, domain); rs = conn.executePreQuery(); if (rs.next()) { return rs.getLong(1); } } catch (SQLException e) { logger.error("getBlogIdByDomain:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return NO_BLOG; } public UserConfigDb getUserConfigDbByUserName(String userName) { long blogId = getBlogIdByUserName(userName); if (blogId != NO_BLOG) { return getUserConfigDb(blogId); } else return null; } public UserConfigDb getUserConfigDbByDomain(String domain) { long blogId = getBlogIdByDomain(domain); if (blogId != NO_BLOG) { return getUserConfigDb(blogId); } else return null; } public ObjectBlockIterator getGroupBlogsOwnedByUser(String userName) { String sql = "select id from " + tableName + " where userName=" + StrUtil.sqlstr(userName) + " and blog_type=" + TYPE_GROUP; int count = getObjectCount(sql); return getObjects(sql, 0, count); } 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); subtitle = rs.getString(2); penName = rs.getString(3); skin = rs.getString(4); notice = rs.getString(5); addDate = DateUtil.parse(rs.getString(6)); valid = rs.getInt(7) == 1 ? true : false; viewCount = rs.getInt(8); msgCount = rs.getInt(9); replyCount = rs.getInt(10); kind = rs.getString(11); type = rs.getInt(12); userName = rs.getString(13); updateDate = DateUtil.parse(rs.getString(14)); icon = StrUtil.getNullStr(rs.getString(15)); friends = StrUtil.getNullStr(rs.getString(16)); footprint = rs.getInt(17) == 1; domain = StrUtil.getNullStr(rs.getString(18)); bkMusic = rs.getInt(19) == 1; userCss = rs.getInt(20) == 1; cssPath = StrUtil.getNullStr(rs.getString(21)); photoDig = rs.getInt(22)==1; oldDomain = domain; loaded = true; primaryKey.setValue(new Long(id)); } } catch (SQLException e) { logger.error("load:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } } public boolean save(ForumFileUpload fu) throws ErrMsgException, ResKeyException { String tmpicon = writeIcon(fu); if (!tmpicon.equals("")) { delIcon(); icon = tmpicon; } return save(); } public boolean save() throws ResKeyException { boolean re = false; Conn conn = new Conn(connname); try { PreparedStatement ps = conn.prepareStatement(QUERY_SAVE); ps.setString(1, title); ps.setString(2, subtitle); ps.setString(3, penName); ps.setString(4, skin); ps.setString(5, notice); ps.setInt(6, valid ? 1 : 0); ps.setInt(7, viewCount); ps.setInt(8, msgCount); ps.setInt(9, replyCount); ps.setString(10, kind); ps.setInt(11, type); ps.setString(12, DateUtil.toLongString(updateDate)); ps.setString(13, icon); ps.setString(14, friends); ps.setInt(15, footprint ? 1 : 0); ps.setString(16, domain); ps.setInt(17, bkMusic ? 1 : 0); ps.setInt(18, userCss ? 1 : 0); ps.setString(19, cssPath); ps.setInt(20, photoDig?1:0); ps.setLong(21, id); re = conn.executePreUpdate() == 1 ? true : false; } catch (Exception e) { logger.error("save:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } if (re) { UserConfigCache uc = new UserConfigCache(this); primaryKey.setValue(userName); uc.refreshSave(primaryKey); if (!oldDomain.equals(domain)) uc.refreshDomain(oldDomain); } return re; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?