KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > Context


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums;
12
13 import org.jboss.portal.core.modules.ModuleException;
14 import org.jboss.portlet.JBossActionRequest;
15 import org.jboss.portlet.JBossActionResponse;
16 import org.jboss.portlet.forums.action.*;
17 import org.jboss.portlet.forums.commands.post.*;
18 import org.jboss.portlet.forums.impl.ForumsModuleImpl;
19 import org.jboss.portlet.forums.impl.MessageImpl;
20 import org.jboss.portlet.forums.impl.PollImpl;
21 import org.jboss.portlet.forums.impl.PosterImpl;
22 import org.jboss.portlet.forums.model.Message;
23 import org.jboss.portlet.forums.model.Poll;
24 import org.jboss.portlet.forums.model.Poster;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.LinkedList JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
32  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute</a>
33  * @version $Revision: 1.7 $
34  */

35 public class Context
36 {
37    // Request parameters
38

39    /**
40     * DOCUMENT_ME
41     */

42    public boolean p_submit;
43
44    /**
45     * DOCUMENT_ME
46     */

47    public boolean p_confirm;
48
49    /**
50     * DOCUMENT_ME
51     */

52    public boolean p_preview;
53
54    /**
55     * Scope : NewTopic/EditPost/Reply
56     * When it's true that means the button add attachment is pressed.
57     */

58    public boolean p_attach;
59
60    /**
61     * Scope : NewTopic/EditPost/Reply
62     * When it's true the user choosed to see the add attachment panel.
63     */

64    public boolean p_attach_box;
65
66    /**
67     * Scope : editing a post.
68     * When it's true the user choosed to see the posted attachment.
69     */

70    public boolean p_posted_attach;
71
72    /**
73     * DOCUMENT_ME
74     */

75    public int p_mode;
76
77    /**
78     * DOCUMENT_ME
79     */

80    public int p_forum_id;
81
82    /**
83     * DOCUMENT_ME
84     */

85    public int p_topic_id;
86
87    /**
88     * DOCUMENT_ME
89     */

90    public int p_post_id;
91
92    /**
93     * DOCUMENT_ME
94     */

95    public int p_topic_type;
96
97    /**
98     * DOCUMENT_ME
99     */

100    public boolean p_cancel;
101
102    /**
103     * DOCUMENT_ME
104     */

105    public boolean p_notify;
106
107    /**
108     * DOCUMENT_ME
109     */

110    public int p_vote_option_id;
111
112    /**
113     * DOCUMENT_ME
114     */

115    public boolean p_poll_delete;
116
117    /**
118     * DOCUMENT_ME
119     */

120    public Poll p_poll;
121
122    /**
123     * DOCUMENT_ME
124     */

125    public Message p_message;
126
127    /**
128     * DOCUMENT_ME
129     */

130    public Poster p_poster;
131
132    /**
133     * DOCUMENT_ME
134     */

135    public ArrayList JavaDoc p_deleted_attachments = new ArrayList JavaDoc();
136
137    /**
138     * DOCUMENT_ME
139     */

140    public ArrayList JavaDoc p_session_attachments = new ArrayList JavaDoc();
141
142    /**
143     * DOCUMENT_ME
144     */

145    boolean c_refresh;
146
147    /**
148     * DOCUMENT_ME
149     */

150    ForumsModule forumsModule;
151
152    /**
153     * Creates a new {@link Context} object.
154     */

155    public Context()
156    {
157       forumsModule = new ForumsModuleImpl();
158    }
159
160    /**
161     * DOCUMENT_ME
162     *
163     * @param req DOCUMENT_ME
164     * @param resp DOCUMENT_ME
165     */

166    public void update(JBossActionRequest req,
167                       JBossActionResponse resp)
168    {
169       p_mode = ForumsConstants.ModeDecoder.decodePosting(req.getParameter("mode"));
170
171       p_forum_id = req.getParameters().getInt(ForumsConstants.POST_FORUM_URL, -1);
172       p_topic_id = req.getParameters().getInt(ForumsConstants.POST_TOPIC_URL, -1);
173       p_post_id = req.getParameters().getInt(ForumsConstants.POST_POST_URL, -1);
174       p_topic_type = req.getParameters().getInt("topictype", ForumsConstants.POST_NORMAL);
175       p_notify = req.getParameters().getParameterExists("notify");
176       p_vote_option_id = req.getParameters().getInt("vote_id", -1);
177
178       //
179
p_message = new MessageImpl();
180       p_message.setSubject(req.getParameters().get("subject", ""));
181       p_message.setText(req.getParameters().get("message", ""));
182       p_message.setHTMLEnabled(!req.getParameters().getParameterExists("disable_html"));
183       p_message.setBBCodeEnabled(!req.getParameters().getParameterExists("disable_bbcode"));
184       p_message.setSmiliesEnabled(!req.getParameters().getParameterExists("disable_smilies"));
185       p_message.setSignatureEnabled(req.getParameters().getParameterExists("attach_sig"));
186
187       //
188
Integer JavaDoc userID = req.getUser().getID();
189       try
190       {
191          p_poster = forumsModule.findPosterByUserID(userID);
192       }
193       catch(ModuleException e)
194       {
195          // TODO Auto-generated catch block
196
e.printStackTrace();
197       }
198
199       if(p_poster == null)
200       {
201          p_poster = new PosterImpl(userID);
202       }
203
204       //
205
p_poll = new PollImpl();
206       p_poll.setTitle(req.getParameters().get("poll_title", ""));
207       p_poll.setLength(Math.max(req.getParameters().getInt("poll_length", 0),
208                                 0));
209       p_poll.setOptions(new LinkedList JavaDoc());
210
211       String JavaDoc p_add_poll_option_text = req.getParameter("add_poll_option_text");
212       boolean p_poll_add = req.getParameters().getParameterExists("add_poll_option");
213       boolean p_poll_edit = req.getParameters().getParameterExists("edit_poll_option");
214
215       boolean option_deleted = false;
216       for(int index = 0; true; index++)
217       {
218          String JavaDoc param = "poll_option_text[" + index + "]";
219          String JavaDoc value = req.getParameter(param);
220          boolean deleted = req.getParameter("del_poll_option[" + index + "]") != null;
221          option_deleted |= deleted;
222          if(value != null)
223          {
224             p_poll.getOptions().add(deleted ? null : value);
225          }
226          else
227          {
228             break;
229          }
230       }
231
232       if(p_poll_add)
233       {
234          p_poll.getOptions().add(p_add_poll_option_text);
235       }
236
237       // if (p_poll_add && p_add_poll_option_text != null)
238
// {
239
// // htmlspecialchars(trim(stripslashes(
240
// p_poll_option_text.add(p_add_poll_option_text);
241
// }
242
boolean toggle_attach_box = req.getParameters().getParameterExists("add_attachment_box");
243       boolean toggle_posted_attachments = req.getParameters().getParameterExists("posted_attachments");
244
245       p_poll_delete = req.getParameters().getParameterExists("poll_delete");
246       p_cancel = req.getParameters().getParameterExists("cancel");
247       p_submit = req.getParameters().getParameterExists("post");
248       p_confirm = req.getParameters().getParameterExists("confirm");
249       p_preview = req.getParameters().getParameterExists("preview");
250       p_attach_box = toggle_attach_box | req.getParameters().getParameterExists("add_attachment_box_flag");
251       p_posted_attach = toggle_posted_attachments ^ req.getParameters().getParameterExists("posted_attachments_flag");
252       p_attach = req.getParameters().getParameterExists("add_attachment");
253
254       /*
255          // Delete attachment
256          boolean session_attachment_deleted = false;
257          for (Iterator i = req.getParameterNames().iterator();i.hasNext();)
258          {
259             String parameter = (String)i.next();
260             if (parameter.startsWith("del_attachment["))
261             {
262                boolean isSession = parameter.startsWith("del_attachment[session");
263                int from = (isSession ? "del_attachment[session_" : "del_attachment[").length();
264                int to = parameter.length() - 1;
265                String s = parameter.substring(from, to);
266                int index = Integer.parseInt(s);
267                if (isSession)
268                {
269                   // Remove it from there
270                   p_session_attachments.remove(index);
271                }
272                else
273                {
274                   // Save it to create the delete attachment command
275                   // p_deleted_attachment = index;
276                   p_deleted_attachments.add(new Integer(index));
277                }
278                session_attachment_deleted = true;
279                break;
280             }
281          }
282          // Handle a new file
283          if (p_attach)
284          {
285             Attachment attachment = new Attachment();
286             UploadedFile webFile = page.getParameterAsFile("fileupload");
287             if (webFile != null)
288             {
289                attachment.setFile(webFile);
290                attachment.setComment(req.getParameters().get("filecomment"));
291                p_session_attachments.add(attachment);
292             }
293          }
294        */

295
296       // Are we refreshing the page ?
297
boolean c_refresh = p_preview;
298       c_refresh |= p_poll_delete;
299       c_refresh |= p_poll_add;
300       c_refresh |= p_poll_edit;
301       c_refresh |= p_attach;
302       c_refresh |= option_deleted;
303
304       // c_refresh |= session_attachment_deleted;
305
c_refresh |= toggle_attach_box;
306       c_refresh |= toggle_posted_attachments;
307
308       this.c_refresh = c_refresh;
309
310       // Clean up the attachments if necessary
311
if(((p_mode == ForumsConstants.PMODE_EDIT_POST) || (p_mode == ForumsConstants.PMODE_NEW_TOPIC)
312           || (p_mode == ForumsConstants.PMODE_REPLY))
313          && (c_refresh || p_submit))
314       {
315          //
316
}
317       else
318       {
319          p_deleted_attachments.clear();
320          p_session_attachments.clear();
321       }
322    }
323
324    /**
325     * DOCUMENT_ME
326     *
327     * @param req DOCUMENT_ME
328     * @param resp DOCUMENT_ME
329     * @return DOCUMENT_ME
330     */

331    public Action action(JBossActionRequest req,
332                         JBossActionResponse resp)
333    {
334       Action action = null;
335       switch(p_mode)
336       {
337          /*
338             case ForumsConstants.PMODE_VOTE:
339                {
340                   VoteTopicCommand vtc = new VoteTopicCommand();
341                   vtc.setPoster(p_poster);
342                   vtc.topicId = p_topic_id;
343                   vtc.voteOptionId = p_vote_option_id;
344                   action = new ProcessCommandAction(vtc);
345                   break;
346                }*/

347          case ForumsConstants.PMODE_REPLY:
348             {
349                if(c_refresh)
350                {
351                   ReplyAction rra = new ReplyAction(p_post_id, p_message, p_poster, p_session_attachments);
352                   rra.setModule(forumsModule);
353                   action = rra;
354                }
355                else if(p_submit)
356                {
357                   ReplyCommand rc = new ReplyCommand(req, resp);
358                   rc.setPoster(p_poster);
359                   rc.postId = p_post_id;
360                   rc.message = p_message;
361                   rc.current_time = new Date JavaDoc();
362                   rc.setModule(forumsModule);
363                   // rc.attachments = (Attachment[])p_session_attachments.toArray(new Attachment[p_session_attachments.size()]);
364
action = new ProcessCommandAction(rc);
365                }
366                else
367                {
368                   ReplyAction rra = new ReplyAction(false, p_post_id, p_message, p_poster);
369                   rra.setModule(forumsModule);
370                   action = rra;
371                }
372
373                break;
374             }
375
376          case ForumsConstants.PMODE_QUOTE:
377             {
378                ReplyAction rra = new ReplyAction(true, p_post_id, p_message, p_poster);
379                rra.setModule(forumsModule);
380                action = rra;
381                break;
382             }
383
384          case ForumsConstants.PMODE_EDIT_POST:
385             {
386                if(c_refresh)
387                {
388                   EditPostAction epa =
389                         new EditPostAction(p_message, p_poster, p_post_id, p_session_attachments, p_poll);
390                   epa.setModule(forumsModule);
391                   action = epa;
392                }
393                else if(p_submit)
394                {
395                   EditPostCommand epc = new EditPostCommand(req, resp);
396                   epc.setPoster(p_poster);
397                   epc.postId = p_post_id;
398                   epc.message = p_message;
399                   epc.poll = p_poll;
400
401                   // epc.attachments = (Attachment[])p_session_attachments.toArray(new Attachment[p_session_attachments.size()]);
402
epc.topic_type = p_topic_type;
403                   epc.current_time = new Date JavaDoc();
404                   epc.poll_delete = p_poll_delete;
405                   epc.notify = p_notify;
406                   epc.deletedAttachments =
407                   (Integer JavaDoc[]) p_deleted_attachments.toArray(new Integer JavaDoc[p_deleted_attachments.size()]);
408                   action = new ProcessCommandAction(epc);
409                }
410                else
411                {
412                   EditPostAction epa =
413                         new EditPostAction(p_message, p_poster, p_post_id, p_session_attachments, p_poll);
414                   epa.setModule(forumsModule);
415                   action = epa;
416                }
417
418                break;
419             }
420
421          case ForumsConstants.PMODE_DELETE:
422             {
423                DeletePostCommand dpc = new DeletePostCommand(req, resp);
424                dpc.setPoster(p_poster);
425                dpc.postId = p_post_id;
426                dpc.setModule(forumsModule);
427                action = new ProcessCommandAction(dpc);
428                break;
429             }
430
431          case ForumsConstants.PMODE_POLL_DELETE:
432             {
433                DeletePollCommand dpc = new DeletePollCommand(req, resp);
434                dpc.setPoster(p_poster);
435                dpc.postId = p_post_id;
436                action = new ProcessCommandAction(dpc);
437                break;
438             }
439
440          case ForumsConstants.PMODE_NEW_TOPIC:
441             {
442                if(c_refresh)
443                {
444                   NewTopicAction nta =
445                         new NewTopicAction(p_poster, p_message, p_forum_id, p_session_attachments, p_poll);
446                   nta.setModule(forumsModule);
447                   action = nta;
448                }
449                else if(p_submit)
450                {
451                   NewTopicCommand ntc = new NewTopicCommand(req, resp);
452                   ntc.setPoster(p_poster);
453                   ntc.forumId = p_forum_id;
454                   ntc.message = p_message;
455                   ntc.poll = p_poll;
456
457                   // ntc.attachments = (Attachment[])p_session_attachments.toArray(new Attachment[p_session_attachments.size()]);
458
ntc.topic_type = p_topic_type;
459                   ntc.current_time = new Date JavaDoc();
460                   ntc.notify = p_notify;
461                   ntc.setModule(forumsModule);
462                   action = new ProcessCommandAction(ntc);
463                }
464                else
465                {
466                   NewTopicAction nta = new NewTopicAction(p_forum_id);
467                   nta.setModule(forumsModule);
468                   action = nta;
469                }
470
471                break;
472             }
473
474          case ForumsConstants.PMODE_REPOST:
475             {
476                RepostCommand rc = new RepostCommand(req, resp);
477                rc.setPoster(p_poster);
478                rc.postId = p_post_id;
479                action = new ProcessCommandAction(rc);
480                break;
481             }
482       }
483
484       return action;
485    }
486 }
Popular Tags