1 package org.paneris.messageboard.controller;
2
3 import java.sql.Connection;
4
5 import org.paneris.jal.model.DDField;
6 import org.paneris.jal.model.RecordSet;
7 import org.paneris.messageboard.model.Board;
8 import org.paneris.messageboard.model.Message;
9 import org.paneris.user.model.User;
10 import org.webmacro.Template;
11 import org.webmacro.servlet.HandlerException;
12 import org.webmacro.servlet.PanerisPage;
13 import org.webmacro.servlet.WebContext;
14
15 public class UpdateMessage extends PanerisPage {
16
17 private static final long serialVersionUID = 1L;
18 Board board;
19 Connection conn;
20
21 public Template handle(WebContext context) throws HandlerException {
22 String templateName = null;
23 try {
24 templateName = User.checkLoggedIn("controller", this.getClass().getName(), context, " update messages.");
25 } catch (Exception e) {
26 throw new HandlerException("Could not get user:" + e.toString());
27 }
28 if (templateName == null) {
29 try {
30 String db = (String) context.getForm("db");
31 String messageboard = (String) context.getForm("messageboard");
32 context.put("types", new RecordSet(db,"messageboards_types", "SELECT id, type FROM messageboards_types ORDER BY display_order",new Integer(0)));
33 board = new Board(db, new Integer(messageboard));
34 templateName = User.checkLoggedIn("messageboard", board.getFieldValue("name"), context, " post to the Messageboards.");
35 if (templateName == null) templateName = User.checkLoggedIn("updatemessage", board.getFieldValue("name"), context, " post to the " + board.getFieldValue("description") + " Messageboard.");
36 if (templateName == null) {
37 templateName = (String) context.getForm("wmtemplate");
38 if (templateName == null) {
39 templateName = "messageboard/view/UpdateMessage.wm";
40 }
41 String subscribe = (String) context.getForm("subscribe");
42 String userId = (String) context.getForm("user");
43 String subject = (String) context.getForm("subject");
44 String status = (String) context.getForm("status");
45 String message = (String) context.getForm("message");
46 String idString = (String) context.getForm("id");
47 String parent = (String) context.getForm("parent");
48 context.put("returnURL",(String) context.getForm("returnURL"));
49 if (idString == null) {
50 idString = "0";
51 }
52 if (userId == null) {
53 userId = "0";
54 }
55 if (status == null) {
56 status = "0";
57 }
58 if (parent == null || parent.equals("")) {
59 parent = "0";
60 }
61 Integer id = new Integer(idString);
62 context.put("db",db);
63 context.put("board",board);
64 if (subscribe != null) {
65 User user = User.getInstance(context);
66 if (user.isLoggedOn()) {
67 if (!board.isSubscribed(user)) {
68 board.subscribe(user);
69 }
70 }
71 }
72 Message m;
73 if (idString.equals("0")) {
74 m = new Message(db);
75 DDField field = (DDField) m.get("id");
76 field.setValue(new Integer(0));
77 field = (DDField) m.get("author");
78 field.setValue(new Integer(userId));
79 field = (DDField) m.get("subject");
80 field.setValue(subject);
81 field = (DDField) m.get("message");
82 field.setValue(message);
83 m.setFieldValue("status",status);
84 field = (DDField) m.get("parent");
85 field.setValue(new Integer(parent));
86 field = (DDField) m.get("board");
87 field.setValue(messageboard);
88 context.put("message",m);
89 m.write();
90 m.distribute();
91 } else {
92 m = new Message(db,id);
93 m.setFieldValue("status",status);
94 context.put("message",m);
95 m.write();
96 }
97 }
98 } catch (Exception e) {
99 throw new HandlerException(e.toString());
100 }
101 }
102
103 try {
104 return (Template) context.getBroker().get("template",templateName);
105 } catch (Exception e) {
106 throw new HandlerException("Could not locate template: " + templateName);
107 }
108 }
109 }