1 package org.paneris.messageboard.controller;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.Statement;
6 import java.util.Enumeration;
7 import java.util.Vector;
8
9 import org.paneris.jal.model.DBConnectionManager;
10 import org.paneris.jal.model.RecordSet;
11 import org.paneris.messageboard.model.Board;
12 import org.paneris.messageboard.model.Message;
13 import org.paneris.messageboard.model.MessageThread;
14 import org.paneris.user.model.User;
15 import org.webmacro.Template;
16 import org.webmacro.servlet.HandlerException;
17 import org.webmacro.servlet.PanerisPage;
18 import org.webmacro.servlet.WebContext;
19
20 public class MessageBoard extends PanerisPage {
21
22 private static final long serialVersionUID = 1L;
23 private DBConnectionManager connMgr;
24 private String db;
25 private Board board;
26 private User user;
27 private String messageboardParameter;
28 private String statusParameter;
29
30 public Template handle(WebContext context) throws HandlerException {
31 connMgr = DBConnectionManager.getInstance();
32 db = (String) context.getForm("db");
33 if (db == null) db = "paneris";
34 messageboardParameter = (String) context.getForm("messageboard");
35
36 statusParameter = (String) context.getForm("status");
37 if (statusParameter == null) {
38 statusParameter = "";
39 }
40
41
42 context.put("db",db);
43 Integer boardid = null;
44 try {
45 boardid = new Integer(messageboardParameter);
46 } catch (Exception e) {
47
48 try {
49 String sqlString = "SELECT id FROM messageboards WHERE name = '" + messageboardParameter + "'";
50 Connection conn = connMgr.getConnection("MessageBoard",db);
51 Statement s = conn.createStatement();
52 ResultSet rs = s.executeQuery(sqlString);
53 if (rs.next()) {
54 boardid = new Integer(rs.getInt(1));
55 }
56 connMgr.freeConnection(db, conn);
57 } catch (Exception f) {
58 throw new HandlerException("Could not find messageboard: " + f.toString());
59 }
60 }
61 try {
62 board = new Board(db, boardid);
63 } catch (Exception e) {
64 throw new HandlerException("Could not create messageboard: " + e.toString());
65 }
66 String templateName = null;
67 try {
68 templateName = User.checkLoggedIn("controller", this.getClass().getName(), context, " access the Messageboards.");
69 if (templateName == null) {
70 templateName = User.checkLoggedIn("messageboard", board.getFieldValue("name"), context, " access this Messageboard.");
71 }
72 } catch (Exception e) {
73 throw new HandlerException("Could not get user:" + e.toString());
74 }
75 if (templateName == null) {
76 templateName = (String) context.getForm("wmtemplate");
77 if (templateName == null) {
78 templateName = "messageboard/view/MessageBoard.wm";
79 }
80
81
82 try {
83 user = User.getInstance(context);
84 } catch (Exception e) {
85 throw new HandlerException("Could not get user: " + e.toString());
86 }
87 context.put("user",user);
88 buildPage(context,statusParameter);
89 }
90
91 try {
92 return (Template) context.getBroker().get("template",templateName);
93 } catch (Exception e) {
94 throw new HandlerException("Could not locate template: " + templateName);
95 }
96 }
97
98 private void buildPage(WebContext context, String status) throws HandlerException {
99 String sqlString = new String();
100 try {
101 context.put("types", new RecordSet(db,"messageboards_types", "SELECT id, type FROM messageboards_types ORDER BY display_order",new Integer(0)));
102 context.put("board",board);
103 Connection conn = connMgr.getConnection("MessageBoard",db);
104 if (conn == null) {
105 throw new RuntimeException("can't get connection: " + db);
106 }
107 Vector rt = new Vector();
108 sqlString = new String("SELECT messages.id FROM messages, messageboards WHERE (parent = 0) AND (messages.board = messageboards.id) AND (messageboards.id = " + board.getFieldValue("id") + ")");
109 if (!status.equals("")) {
110 sqlString += new String(" and status = " + status);
111 }
112 sqlString += " ORDER BY messagedate DESC LIMIT 50";
113 RecordSet set;
114 try {
115 set = RecordSet.getInstance(context, db, "messages", sqlString, null);
116 } catch (Exception e) {
117 throw e;
118 }
119 context.put("navigation", set);
120 for (Enumeration en = set.getNext(); en.hasMoreElements();) {
121 Integer id = (Integer) en.nextElement();
122 Message m = new Message(db, id);
123 rt.addElement(m);
124 MessageThread thread = new MessageThread(db);
125 while ((m = thread.getNext(m)) != null) {
126 rt.addElement(m);
127 }
128 }
129 context.put("results", rt);
130 context.put("loggedon",user.isLoggedOn());
131 context.put("subscribed",board.isSubscribed(user));
132 sqlString = new String("SELECT usersboards.username FROM usersboards, users WHERE board = " + board.getFieldValue("id") + " AND usersboards.username = users.id ORDER BY users.username");
133 RecordSet users = RecordSet.getInstance(context, db, "users", sqlString, new Integer(0));
134 context.put("users", users);
135 connMgr.freeConnection(db, conn);
136 } catch (Exception e) {
137 throw (HandlerException)new HandlerException("Failed to build page: " + e.toString() + " sqlString: " + sqlString).initCause(e);
138 }
139 }
140 }