1 16 package dlog4j.tags; 17 18 import java.sql.SQLException ; 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.List ; 22 23 import javax.servlet.jsp.JspException ; 24 25 import net.sf.hibernate.HibernateException; 26 import net.sf.hibernate.Query; 27 import net.sf.hibernate.Session; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import dlog4j.formbean.MessageForm; 33 import dlog4j.formbean.UserForm; 34 35 39 public class MessagesTag extends DlogBaseTag { 40 41 boolean read = false; 42 int status = MessageForm.STATUS_NEW; 43 46 public int doStartTag() throws JspException { 47 UserForm loginUser = getLoginUser(); 48 if(loginUser!=null&&loginUser.isLogin()) { 49 Session ssn = null; 50 try { 51 ssn = getSession(); 52 String hql = "FROM "+MessageForm.class.getName()+" AS msg WHERE msg.toUser.id=? AND msg.status=? ORDER BY msg.sendTime DESC"; 53 Query query = ssn.createQuery(hql); 54 query.setInteger(0, loginUser.getId()); 55 query.setInteger(1,status); 56 List msgs = query.list(); 57 pageContext.setAttribute(id,msgs); 58 if(read && status==MessageForm.STATUS_NEW) { 59 Date d = new Date (); 60 for(int i=0;i<msgs.size();i++) { 61 MessageForm m = (MessageForm)msgs.get(i); 62 m.setStatus(MessageForm.STATUS_READ); 63 m.setReadTime(d); 64 ssn.update(m); 65 } 66 } 67 } catch (SQLException e) { 68 Log log = LogFactory.getLog(MessagesTag.class); 69 log.error("读取发送给"+loginUser.getLoginName()+"的消息失败(SQL)",e); 70 } catch (HibernateException e) { 71 Log log = LogFactory.getLog(MessagesTag.class); 72 log.error("读取发送给"+loginUser.getLoginName()+"的消息失败(Hibernate)",e); 73 } finally { 74 try { 75 commitSession(ssn,true); 76 }catch(Exception e) {} 77 } 78 } 79 else 80 pageContext.setAttribute(id,new ArrayList ()); 81 return super.doStartTag(); 82 } 83 public boolean isRead() { 84 return read; 85 } 86 public void setRead(boolean read) { 87 this.read = read; 88 } 89 public void setRead(Boolean read) { 90 this.read = read.booleanValue(); 91 } 92 public void setRead(String read) { 93 this.read = "true".equalsIgnoreCase(read); 94 } 95 public int getStatus() { 96 return status; 97 } 98 public void setStatus(int status) { 99 this.status = status; 100 } 101 public void setStatus(Integer status) { 102 this.status = status.intValue(); 103 } 104 public void setStatus(String status) { 105 this.status = Integer.parseInt(status); 106 } 107 } 108 | Popular Tags |