001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.action;
017:
018: import java.util.Date;
019: import java.sql.SQLException;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import net.sf.hibernate.HibernateException;
025: import net.sf.hibernate.Session;
026:
027: import org.apache.struts.action.ActionError;
028: import org.apache.struts.action.ActionErrors;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionForward;
031: import org.apache.struts.action.ActionMapping;
032:
033: import dlog4j.formbean.MessageForm;
034: import dlog4j.formbean.UserForm;
035:
036: /**
037: * 点对点消息传递
038: *
039: * @author Liudong
040: */
041: public class DlogMessageAction extends DlogActionBase {
042:
043: public final static String ERROR_KEY = "message";
044:
045: /**
046: * 删除消息
047: * @param mapping
048: * @param form
049: * @param request
050: * @param response
051: * @param msgid
052: * @return
053: * @throws Exception
054: */
055: public ActionForward doDelete(ActionMapping mapping,
056: ActionForm form, HttpServletRequest request,
057: HttpServletResponse response, String msgid)
058: throws Exception {
059: int mid = -1;
060: Session ssn = null;
061: ActionErrors errors = new ActionErrors();
062: UserForm loginUser = UserForm.getLoginUser(request);
063: if (loginUser != null && loginUser.isLogin()) {
064: try {
065: mid = Integer.parseInt(msgid);
066: if (mid > 0) {
067: ssn = getSession();
068: MessageForm msg = (MessageForm) ssn.load(
069: MessageForm.class, new Integer(mid));
070: if (msg.getToUser().getId() != loginUser.getId())
071: errors.add(ERROR_KEY, new ActionError(
072: "operation_not_allow"));
073: else {
074: msg.setStatus(MessageForm.STATUS_DELETE);
075: if (msg.getReadTime() == null)
076: msg.setReadTime(new Date());
077: ssn.update(msg);
078: }
079: }
080: } catch (NumberFormatException e) {
081: } catch (HibernateException e) {
082: errors.add(ERROR_KEY, new ActionError(
083: "hibernate_exception"));
084: } catch (SQLException e) {
085: errors.add(ERROR_KEY, new ActionError(
086: "database_exception"));
087: } finally {
088: if (ssn != null)
089: commitSession(ssn, true);
090: }
091: } else
092: errors.add(ERROR_KEY, new ActionError("user_not_login"));
093: if (!errors.isEmpty())
094: saveErrors(request, errors);
095: return mapping.findForward("list");
096: }
097:
098: /**
099: * 发送信息给某人
100: *
101: * @param mapping
102: * @param form
103: * @param request
104: * @param response
105: * @return @throws
106: * Exception
107: */
108: public ActionForward doSend(ActionMapping mapping, ActionForm form,
109: HttpServletRequest request, HttpServletResponse response)
110: throws Exception {
111: ActionErrors errors = new ActionErrors();
112: MessageForm msg = (MessageForm) form;
113: UserForm loginUser = UserForm.getLoginUser(request);
114: if (loginUser != null && loginUser.isLogin()) {
115: if (msg.getToUser() == null
116: || msg.getToUser().getId() == -1)
117: errors.add(ERROR_KEY, new ActionError(
118: "operation_not_allow"));
119: else if (msg.getToUser().getId() == loginUser.getId())
120: errors.add(ERROR_KEY,
121: new ActionError("message_to_self"));
122: else {
123: Session ssn = null;
124: try {
125: msg.setFromUser(loginUser);
126: msg.setSendTime(new Date());
127: ssn = getSession();
128: ssn.save(msg);
129: errors.add(ERROR_KEY, new ActionError(
130: "message_send_ok"));
131: } catch (HibernateException e) {
132: errors.add(ERROR_KEY, new ActionError(
133: "hibernate_exception"));
134: } catch (SQLException e) {
135: errors.add(ERROR_KEY, new ActionError(
136: "database_exception"));
137: } finally {
138: commitSession(ssn, true);
139: }
140: }
141: } else
142: errors.add(ERROR_KEY, new ActionError("user_not_login"));
143: if (!errors.isEmpty())
144: saveErrors(request, errors);
145: return mapping.getInputForward();
146: }
147: }
|