KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > actions > message > AddMessageAction


1 /*
2  * $$Id: AddMessageAction.java,v 1.5 2005/06/07 12:31:54 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Sep 20, 2003
27  *
28  */

29 package org.jresearch.gossip.actions.message;
30
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43
44 import org.apache.struts.action.ActionForm;
45 import org.apache.struts.action.ActionForward;
46 import org.apache.struts.action.ActionMapping;
47 import org.apache.struts.action.ActionMessage;
48 import org.apache.struts.action.ActionMessages;
49 import org.apache.struts.upload.FormFile;
50 import org.apache.struts.util.MessageResources;
51 import org.jresearch.gossip.IConst;
52 import org.jresearch.gossip.actions.BaseAction;
53 import org.jresearch.gossip.am.ban.BanGuard;
54 import org.jresearch.gossip.beans.forum.Forum;
55 import org.jresearch.gossip.beans.forum.attachment.FileData;
56 import org.jresearch.gossip.beans.forum.attachment.FileDataInfo;
57 import org.jresearch.gossip.beans.subscription.Subscriber;
58 import org.jresearch.gossip.beans.user.User;
59 import org.jresearch.gossip.configuration.Configurator;
60 import org.jresearch.gossip.constants.BanType;
61 import org.jresearch.gossip.constants.UserStatus;
62 import org.jresearch.gossip.dao.ForumDAO;
63 import org.jresearch.gossip.dao.UserDAO;
64 import org.jresearch.gossip.exception.SystemException;
65 import org.jresearch.gossip.forms.FileUploadForm;
66 import org.jresearch.gossip.forms.MessageForm;
67 import org.jresearch.gossip.mail.MailMessage;
68 import org.jresearch.gossip.mail.MailQueue;
69 import org.jresearch.gossip.util.HtmlCodec;
70 import org.jresearch.gossip.util.MessageProcessor;
71
72 /**
73  * DOCUMENT ME!
74  *
75  * @author Bel
76  */

77 public class AddMessageAction extends BaseAction {
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @param mapping
83      * DOCUMENT ME!
84      * @param form
85      * DOCUMENT ME!
86      * @param request
87      * DOCUMENT ME!
88      * @param response
89      * DOCUMENT ME!
90      *
91      * @return DOCUMENT ME!
92      */

93     public ActionForward process(ActionMapping mapping, ActionForm form,
94             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
95             throws SystemException {
96         HttpSession JavaDoc session = request.getSession();
97         MessageResources messages = getResources(request);
98         MessageForm messageForm = (MessageForm) form;
99         User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
100         ForumDAO dao = ForumDAO.getInstance();
101         UserDAO userdao = UserDAO.getInstance();
102         ActionMessages errors = new ActionMessages();
103         StringBuffer JavaDoc forward = new StringBuffer JavaDoc();
104         Configurator config = Configurator.getInstance();
105
106         if ((user.getStatus() > 0)) {
107             messageForm.setEmail(user.getInfo().getEmail());
108             messageForm.setName(user.getName());
109         } else { // validation if user is not registered yet
110

111             if (messageForm.getName().trim().equals("")) {
112
113                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
114                         "errors.ERR20"));
115             }
116
117             if (messageForm.getEmail().trim().equals("")) {
118                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
119                         "errors.ERR19"));
120             }
121         }
122
123         // Report any errors we have discovered back to the original form
124
if (!errors.isEmpty()) {
125             saveErrors(request, errors);
126
127             return (mapping.getInputForward());
128         }
129
130         try {
131
132             boolean announce = false;
133             boolean isUserMod = dao.checkMod(Integer.parseInt(messageForm
134                     .getFid()), user);
135             Forum currForum = dao.getForumInfo(Integer.parseInt(messageForm
136                     .getFid()));
137
138             // check access rights if forum invisible
139
if ((currForum.getLocked() == IConst.Forum.STATUS_INVISIBLE)
140                     && (user.getStatus() < Integer.parseInt(Configurator
141                             .getInstance().get(IConst.CONFIG.INVADER1)))) {
142                 return (mapping.findForward(IConst.TOKEN.DENIED));
143             }
144
145             // check access rights if forum topics are locked
146
if (((currForum.getLocked() == IConst.Forum.STATUS_TOPICS_LOCKED) && messageForm
147                     .getTid().equals(""))
148                     && (!isUserMod)) {
149                 return (mapping.findForward(IConst.TOKEN.DENIED));
150             }
151
152             // check user access rights if completely forum is
153
// locked
154
if ((currForum.getLocked() == IConst.Forum.STATUS_COMPLETELY_LOCKED)
155                     && (!isUserMod)) {
156                 return (mapping.findForward(IConst.TOKEN.DENIED));
157             }
158             // insert new thread if it is necessary
159
if (messageForm.getTid().equals("")) {
160                 messageForm.setTid(dao.insertNewThread(messageForm.getFid()));
161
162                 announce = ((dao.isUserMod(user.getName()) || (user.getStatus() > 7)) && IConst.VALUES.TRUE
163                         .equals(messageForm.getAnnounce()));
164             } else {
165
166                 // check user access rights if current topic is
167
// locked
168
if ((dao.getThreadInfo(Integer.parseInt(messageForm.getTid()))
169                         .getLocked() == IConst.Topic.STATUS_LOCKED)
170                         && (!isUserMod)) {
171                     return (mapping.findForward(IConst.TOKEN.DENIED));
172                 }
173             }
174             // mark username for not registered users
175
if (user.getStatus() == 0) {
176                 messageForm.setName("<" + messageForm.getName() + ">");
177             }
178
179             int mid = dao.addMessage(messageForm, request.getRemoteAddr(),
180                     announce);
181             // save attachment
182
if (Configurator.getInstance().getBoolean(
183                     IConst.CONFIG.ENABLE_FILE_UPLOAD)) {
184                 saveAttach(request, mid, ((FileUploadForm) messageForm)
185                         .getFileData());
186             }
187             forward.append("/ShowMessage.do?fid=");
188             forward.append(messageForm.getFid());
189             forward.append("&tid=");
190             forward.append(messageForm.getTid());
191             forward.append("&mid=");
192             forward.append(mid);
193
194             log(request, "logs.LOG5", messageForm.getTid() + " fid="
195                     + messageForm.getFid());
196
197             // e-mail all the subscribors of this thread...
198
ArrayList JavaDoc subscrbe = dao.getSubscribersList(messageForm.getTid(),
199                     messageForm.getName());
200
201             if (subscrbe.size() > 0) {
202                 StringBuffer JavaDoc siteUrl = new StringBuffer JavaDoc();
203                 siteUrl.append(request.getServerName());
204                 siteUrl.append(":");
205                 siteUrl.append(request.getServerPort());
206                 siteUrl.append(request.getContextPath());
207                 siteUrl.append(config.get(IConst.CONFIG.MODULE_PREFIX));
208
209                 String JavaDoc tSubj = dao.getThreadSubject(messageForm.getTid());
210                 MessageProcessor mp = MessageProcessor.getInstance();
211                 String JavaDoc mess = mp.prepareMessage(HtmlCodec.encode(messageForm
212                         .getText()), 0, messages);
213                 mess = mp.cleanup(mess);
214
215                 /*
216                  * {0} - subsriber's login {1} - site url {2} - show message url
217                  * {3} - topic title {4} - new message text {5} - new message
218                  * sender {6} - site name
219                  */

220                 Object JavaDoc[] messArgs = new Object JavaDoc[] { "", siteUrl.toString(),
221                         forward.toString(), tSubj, mess,
222                         HtmlCodec.encode(messageForm.getName()),
223                         config.get(IConst.CONFIG.SITE_NAME) };
224                 MailQueue queue = (MailQueue) session.getServletContext()
225                         .getAttribute(IConst.CONTEXT.MAIL_QUEUE);
226
227                 Iterator JavaDoc it = subscrbe.iterator();
228                 BanGuard guard = BanGuard.getInstance();
229                 while (it.hasNext()) {
230                     Subscriber s = (Subscriber) it.next();
231                     if (!guard.checkBan(s.getEmail(), BanType.EMAIL)) {
232                         messArgs[0] = HtmlCodec.encode(s.getName());
233
234                         queue
235                                 .push(new MailMessage(
236                                         messages
237                                                 .getMessage(
238                                                         config
239                                                                 .getLocale(IConst.CONFIG.DEFAULT_LOCALE),
240                                                         "mails.NEW_MESSAGE",
241                                                         messArgs),
242                                         messages
243                                                 .getMessage(
244                                                         config
245                                                                 .getLocale(IConst.CONFIG.DEFAULT_LOCALE),
246                                                         "mails.NEW_MESSAGE_SUBJ"),
247                                         config.get(IConst.CONFIG.ADMINMAIL),
248                                         messages
249                                                 .getMessage(
250                                                         config
251                                                                 .getLocale(IConst.CONFIG.DEFAULT_LOCALE),
252                                                         "mails.FORUM_ADMIN"), s
253                                                 .getEmail(), s.getName()));
254                     }
255                 }
256             }
257
258             // subscribe user to e-mail from this thread...
259
if (IConst.VALUES.TRUE.equals(messageForm.getSubscribe())) {
260                 dao.subscribe(messageForm.getTid(), messageForm.getEmail(),
261                         messageForm.getName());
262             }
263
264             session.removeAttribute(IConst.REQUEST.CURR_THREAD);
265
266             if (user.getStatus() != UserStatus.GUEST) {
267
268                 // set(update) last visit date for this thread
269
HashMap JavaDoc last_intime = (HashMap JavaDoc) session
270                         .getAttribute(IConst.SESSION.LAST_INTIME);
271
272                 if (last_intime.containsKey(messageForm.getTid())) {
273                     last_intime.remove(messageForm.getTid());
274                 }
275
276                 last_intime.put(messageForm.getTid(), new Date JavaDoc());
277             }
278         } catch (SQLException JavaDoc sqle) {
279             getServlet().log("Connection.process", sqle);
280             throw new SystemException(sqle);
281         } catch (FileNotFoundException JavaDoc e) {
282             throw new SystemException(e);
283         } catch (IOException JavaDoc e) {
284             throw new SystemException(e);
285         }
286
287         return (new ActionForward(forward.toString(), true));
288     }
289
290     private void saveAttach(HttpServletRequest JavaDoc request, int mid, List JavaDoc[] files)
291             throws FileNotFoundException JavaDoc, IOException JavaDoc, SystemException,
292             SQLException JavaDoc {
293         ForumDAO dao = ForumDAO.getInstance();
294
295         FileData[] filedata = new FileData[files[0].size()];
296         // populate filedata
297
for (int i = 0; i < filedata.length; i++) {
298             FileData fdata = new FileData();
299             FileDataInfo finfo = new FileDataInfo();
300             FormFile file = (FormFile) files[0].get(i);
301
302             finfo.setContentType(file.getContentType());
303             fdata.setData(file.getFileData());
304
305             finfo.setDescription((String JavaDoc) files[1].get(i));
306             finfo.setMessageId(mid);
307             finfo.setName(file.getFileName());
308             finfo.setSize(file.getFileSize());
309
310             fdata.setInfo(finfo);
311
312             filedata[i] = fdata;
313         }
314         // save filedata
315
dao.saveAttachments(filedata);
316
317     }
318 }
Popular Tags