KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > action > DlogMessageAction


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.action;
17
18 import java.util.Date JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import net.sf.hibernate.HibernateException;
25 import net.sf.hibernate.Session;
26
27 import org.apache.struts.action.ActionError;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import dlog4j.formbean.MessageForm;
34 import dlog4j.formbean.UserForm;
35
36 /**
37  * 点对点消息传递
38  *
39  * @author Liudong
40  */

41 public class DlogMessageAction extends DlogActionBase {
42
43     public final static String JavaDoc ERROR_KEY = "message";
44
45     /**
46      * 删除消息
47      * @param mapping
48      * @param form
49      * @param request
50      * @param response
51      * @param msgid
52      * @return
53      * @throws Exception
54      */

55     public ActionForward doDelete(ActionMapping mapping, ActionForm form,
56             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc msgid)
57             throws Exception JavaDoc {
58         int mid = -1;
59         Session ssn = null;
60         ActionErrors errors = new ActionErrors();
61         UserForm loginUser = UserForm.getLoginUser(request);
62         if (loginUser != null && loginUser.isLogin()) {
63         try {
64             mid = Integer.parseInt(msgid);
65             if(mid>0) {
66                 ssn = getSession();
67                 MessageForm msg = (MessageForm)ssn.load(MessageForm.class,new Integer JavaDoc(mid));
68                 if(msg.getToUser().getId()!=loginUser.getId())
69                     errors.add(ERROR_KEY,new ActionError("operation_not_allow"));
70                 else {
71                     msg.setStatus(MessageForm.STATUS_DELETE);
72                     if(msg.getReadTime()==null)
73                         msg.setReadTime(new Date JavaDoc());
74                     ssn.update(msg);
75                 }
76             }
77         }catch(NumberFormatException JavaDoc e) {
78         }catch (HibernateException e) {
79             errors.add(ERROR_KEY,
80                     new ActionError("hibernate_exception"));
81         } catch (SQLException JavaDoc e) {
82             errors.add(ERROR_KEY, new ActionError("database_exception"));
83         }finally {
84             if(ssn!=null)
85                 commitSession(ssn,true);
86         }
87         } else
88             errors.add(ERROR_KEY, new ActionError("user_not_login"));
89         if (!errors.isEmpty())
90             saveErrors(request, errors);
91         return mapping.findForward("list");
92     }
93     /**
94      * 发送信息给某人
95      *
96      * @param mapping
97      * @param form
98      * @param request
99      * @param response
100      * @return @throws
101      * Exception
102      */

103     public ActionForward doSend(ActionMapping mapping, ActionForm form,
104             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
105             throws Exception JavaDoc {
106         ActionErrors errors = new ActionErrors();
107         MessageForm msg = (MessageForm) form;
108         UserForm loginUser = UserForm.getLoginUser(request);
109         if (loginUser != null && loginUser.isLogin()) {
110             if (msg.getToUser() == null || msg.getToUser().getId() == -1)
111                 errors.add(ERROR_KEY, new ActionError("operation_not_allow"));
112             else if (msg.getToUser().getId() == loginUser.getId())
113                 errors.add(ERROR_KEY, new ActionError("message_to_self"));
114             else {
115                 Session ssn = null;
116                 try {
117                     msg.setFromUser(loginUser);
118                     msg.setSendTime(new Date JavaDoc());
119                     ssn = getSession();
120                     ssn.save(msg);
121                     errors.add(ERROR_KEY, new ActionError("message_send_ok"));
122                 } catch (HibernateException e) {
123                     errors.add(ERROR_KEY,
124                             new ActionError("hibernate_exception"));
125                 } catch (SQLException JavaDoc e) {
126                     errors.add(ERROR_KEY, new ActionError("database_exception"));
127                 } finally {
128                     commitSession(ssn, true);
129                 }
130             }
131         } else
132             errors.add(ERROR_KEY, new ActionError("user_not_login"));
133         if (!errors.isEmpty())
134             saveErrors(request, errors);
135         return mapping.getInputForward();
136     }
137 }
138
Popular Tags