KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > MessagesTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.tags;
17
18 import java.sql.SQLException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
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 /**
36  * 读取点对点消息的标签库
37  * @author Liudong
38  */

39 public class MessagesTag extends DlogBaseTag {
40
41     boolean read = false;
42     int status = MessageForm.STATUS_NEW;
43     /**
44      * 读取发送给当前登录用户所有的未读信息
45      */

46     public int doStartTag() throws JspException JavaDoc {
47         UserForm loginUser = getLoginUser();
48         if(loginUser!=null&&loginUser.isLogin()) {
49             Session ssn = null;
50             try {
51                 ssn = getSession();
52                 String JavaDoc 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 JavaDoc msgs = query.list();
57                 pageContext.setAttribute(id,msgs);
58                 if(read && status==MessageForm.STATUS_NEW) {
59                     Date JavaDoc d = new Date JavaDoc();
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 JavaDoc 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 JavaDoc e) {}
77             }
78         }
79         else
80             pageContext.setAttribute(id,new ArrayList JavaDoc());
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 JavaDoc read) {
90         this.read = read.booleanValue();
91     }
92     public void setRead(String JavaDoc 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 JavaDoc status) {
102         this.status = status.intValue();
103     }
104     public void setStatus(String JavaDoc status) {
105         this.status = Integer.parseInt(status);
106     }
107 }
108
Popular Tags