worklogdb.java
来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 428 行
JAVA
428 行
package com.redmoon.oa.worklog;import java.sql.*;import java.util.*;import cn.js.fan.base.*;import cn.js.fan.db.*;import cn.js.fan.util.*;import org.htmlparser.Parser;import org.htmlparser.util.*;import org.htmlparser.*;import org.htmlparser.filters.*;import org.htmlparser.nodes.*;import org.htmlparser.filters.*;import org.htmlparser.tags.LinkTag;import org.htmlparser.tags.ImageTag;import cn.js.fan.web.SkinUtil;import com.cloudwebsoft.framework.util.LogUtil;import javax.servlet.http.HttpServletRequest;public class WorkLogDb extends ObjectDb { private int id; public static final int TYPE_SYSTEM = 1; public static final int TYPE_USER = 0; public WorkLogDb() { init(); } public WorkLogDb(int id) { this.id = id; init(); load(); } public int getId() { return id; } public void initDB() { tableName = "work_log"; primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT); objectCache = new WorkLogCache(this); isInitFromConfigDB = false; QUERY_CREATE = "insert into " + tableName + " (userName,content,myDate) values (?,?,NOW())"; QUERY_SAVE = "update " + tableName + " set userName=?,content=? where id=?"; QUERY_LIST = "select id from " + tableName + " order by mydate desc"; QUERY_DEL = "delete from " + tableName + " where id=?"; QUERY_LOAD = "select userName,content,myDate from " + tableName + " where id=?"; } public WorkLogDb getWorkLogDb(int id) { return (WorkLogDb)getObjectDb(new Integer(id)); } public boolean create() throws ErrMsgException { Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement ps = conn.prepareStatement(QUERY_CREATE); ps.setString(1, userName); ps.setString(2, content); re = conn.executePreUpdate()==1?true:false; if (re) { WorkLogCache rc = new WorkLogCache(this); rc.refreshCreate(); } } catch (SQLException e) { logger.error("create:" + e.getMessage()); throw new ErrMsgException("数据库操作失败!"); } finally { if (conn!=null) { conn.close(); conn = null; } } return re; } public boolean del() throws ErrMsgException { Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement ps = conn.prepareStatement(QUERY_DEL); ps.setInt(1, id); re = conn.executePreUpdate() == 1 ? true : false; if (re) { WorkLogCache rc = new WorkLogCache(this); primaryKey.setValue(new Integer(id)); rc.refreshDel(primaryKey); } } catch (SQLException e) { logger.error("del: " + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public ObjectDb getObjectRaw(PrimaryKey pk) { return new WorkLogDb(pk.getIntValue()); } public void load() { ResultSet rs = null; Conn conn = new Conn(connname); try { PreparedStatement ps = conn.prepareStatement(QUERY_LOAD); ps.setInt(1, id); rs = conn.executePreQuery(); if (rs != null && rs.next()) { userName = rs.getString(1); content = rs.getString(2); myDate = rs.getTimestamp(3); loaded = true; primaryKey.setValue(new Integer(id)); } } catch (SQLException e) { logger.error("load: " + e.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} rs = null; } if (conn!=null) { conn.close(); conn = null; } } } public boolean isWorkLogTodayWritten(String userName) { ResultSet rs = null; Conn conn = new Conn(connname); try { String sql = "select id from " + tableName + " where TO_DAYS(myDate)=TO_DAYS(NOW()) and userName=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, userName); rs = conn.executePreQuery(); if (rs != null && rs.next()) { return true; } } catch (SQLException e) { logger.error("isWorkLogTodayWritten: " + e.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} rs = null; } if (conn!=null) { conn.close(); conn = null; } } return false; } public boolean save() throws ErrMsgException { Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement ps = conn.prepareStatement(QUERY_SAVE); ps.setString(1, userName); ps.setString(2, content); ps.setInt(3, id); re = conn.executePreUpdate()==1?true:false; if (re) { WorkLogCache rc = new WorkLogCache(this); primaryKey.setValue(new Integer(id)); rc.refreshSave(primaryKey); } } catch (SQLException e) { logger.error("save: " + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public Vector list(String sql) { ResultSet rs = null; Conn conn = new Conn(connname); Vector result = new Vector(); try { rs = conn.executeQuery(sql); if (rs == null) { return null; } else { while (rs.next()) { result.addElement(getWorkLogDb(rs.getInt(1))); } } } catch (SQLException e) { logger.error("list:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return result; } public ListResult listResult(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 { WorkLogDb ug = getWorkLogDb(rs.getInt(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 java.util.Date getMyDate() { return myDate; } public String getContent() { return content; } public String getUserName() { return userName; } public void setMyDate(java.util.Date myDate) { this.myDate = myDate; } public void setContent(String content) { this.content = content; } public void setUserName(String userName) { this.userName = userName; } public static boolean isBlank(String astr) { if ((null == astr) || (astr.length() == 0)) { return true; } else { return false; } } public static boolean isTrimEmpty(String astr) { if ((null == astr) || (astr.length() == 0)) { return true; } if (isBlank(astr.trim())) { return true; } return false; } public static String getAbstract(HttpServletRequest request, WorkLogDb wld, int len) { String content = StrUtil.getLeft(wld.getContent(), len); int idx1 = content.lastIndexOf('<'); int idx2 = content.lastIndexOf('>'); if ((idx2 == -1 && idx1 >= 0) || (idx1 > idx2)) { String ct3 = wld.getContent(); int idx3 = ct3.indexOf('>', idx1); if (idx3!=-1) { if (idx3 < MAX_LEN2) { content = ct3.substring(0, idx3 + 1); } } } idx2 = content.toLowerCase().lastIndexOf("</object>"); idx1 = content.toLowerCase().lastIndexOf("<object"); if ((idx2 == -1 && idx1 >= 0) || idx1 > idx2) { String ct2 = wld.getContent().toLowerCase(); int idx3 = ct2.indexOf("</object>"); if (idx3 != -1) content += wld.getContent().substring(content.length(), content.length() + idx3 + 9); else content = wld.getContent().substring(0, idx1); } String str = ""; try { Parser myParser; NodeList nodeList = null; myParser = Parser.createParser(content, "utf-8"); NodeFilter textFilter = new NodeClassFilter(TextNode.class); NodeFilter linkFilter = new NodeClassFilter(LinkTag.class); NodeFilter imgFilter = new NodeClassFilter(ImageTag.class); OrFilter lastFilter = new OrFilter(); lastFilter.setPredicates(new NodeFilter[] {textFilter, linkFilter, imgFilter}); nodeList = myParser.parse(lastFilter); Node[] nodes = nodeList.toNodeArray(); for (int i = 0; i < nodes.length; i++) { Node anode = (Node) nodes[i]; String line = ""; if (anode instanceof TextNode) { TextNode textnode = (TextNode) anode; line = textnode.getText(); } else if (anode instanceof ImageTag) { ImageTag imagenode = (ImageTag) anode; String url = imagenode.getImageURL(); String ext = StrUtil.getFileExt(url).toLowerCase(); if (ext.equals("gif") || ext.equals("png") || ext.equals("jpg") || ext.equals("jpeg") || ext.equals("bmp")) { if (imagenode.getImageURL().startsWith("http")) ; else if (imagenode.getImageURL().startsWith("/")) { ; } else { url = request.getContextPath() + "/" + imagenode.getImageURL(); } line = "<div align=center><a onfocus=this.blur() href=\"" + url + "\" target=_blank><IMG SRC=\"" + url + "\" border=0 alt=" + SkinUtil.LoadString(request, "res.cn.js.fan.util.StrUtil", "click_open_win") + " onload=\"javascript:if(this.width>screen.width*0.4) this.width=screen.width*0.4\"></a></div><BR>"; } } if (isTrimEmpty(line)) continue; str += line + " "; } } catch (ParserException e) { LogUtil.getLog(WorkLogDb.class.getName()).error("getAbstract:" + e.getMessage()); } return str; } private java.util.Date myDate; private String content; private String userName; public static final int MAX_LEN2 = 3000;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?