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.Vector;
7
8 import org.paneris.jal.model.DBConnectionManager;
9 import org.paneris.jal.model.RecordSet;
10 import org.paneris.messageboard.model.Board;
11 import org.paneris.user.model.User;
12 import org.webmacro.Template;
13 import org.webmacro.servlet.HandlerException;
14 import org.webmacro.servlet.PanerisPage;
15 import org.webmacro.servlet.WebContext;
16
17 public class ListBoards extends PanerisPage {
18
19 private static final long serialVersionUID = 1L;
20 private DBConnectionManager connMgr;
21 private Connection conn;
22 private static final boolean debug = false;
23 String db;
24 private String typeParameter;
25
26 public Template handle(WebContext context) throws HandlerException {
27
28 connMgr = DBConnectionManager.getInstance();
29
30 db = (String) context.getForm("db");
31 context.put("db",db);
32
33 typeParameter = (String) context.getForm("type");
34 if (typeParameter == null)
35 typeParameter = "";
36
37 conn = connMgr.getConnection("ListBoards",db);
38 if (conn == null)
39 throw new RuntimeException("can't get connection: " + db);
40
41 String sqlString = "";
42 String templateName = null;
43 try {
44 templateName = User.checkLoggedIn("controller",
45 this.getClass().getName(),
46 context,
47 " access the Messageboards.");
48 }
49 catch (Exception e) {
50 throw new HandlerException("Could not get user:" + e.toString());
51 }
52
53
54 if (templateName == null) {
55
56 templateName = (String) context.getForm("wmtemplate");
57 if (templateName == null)
58 templateName = "messageboard/view/ListBoards.wm";
59
60 try {
61 context.put("types",
62 new RecordSet(db,"messageboards_types",
63 "SELECT id, type FROM messageboards_types ORDER BY display_order",
64 new Integer(0)));
65
66 if (!typeParameter.equals("")) {
67 Vector rtsub = new Vector();
68 Vector rtun = new Vector();
69
70 sqlString = "SELECT messageboards.* FROM messageboards, "
71 + "messageboards_types WHERE messageboards.deleted="
72 + connMgr.sqlFalse(db)
73 + (
74 (!typeParameter.equals(""))
75 ? " AND messageboards.messageboard_type_key = "
76 +typeParameter
77 : ""
78 )
79 + " AND messageboards.messageboard_type_key ="
80 + " messageboards_types.id"
81 + " ORDER BY messageboards_types.display_order,"
82 + " messageboards.name";
83
84 Statement s = conn.createStatement();
85
86 if (debug)
87 System.err.println("sqlString: " + sqlString);
88
89 ResultSet rs = s.executeQuery(sqlString);
90 User user = User.getInstance(context);
91
92 while (rs.next()) {
93 Board b = new Board(db, rs);
94
95 if (user.isLoggedOn() && b.isSubscribed(user))
96 rtsub.addElement(b);
97 else
98 rtun.addElement(b);
99 }
100 context.put("subscribed",rtsub);
101 context.put("unsubscribed",rtun);
102 context.put("user",user);
103 }
104 }
105 catch (Exception e) {
106 throw new HandlerException(e.toString() +
107 " sqlString: " + sqlString);
108 }
109 }
110
111 connMgr.freeConnection(db, conn);
112
113 try {
114 return (Template) context.getBroker().get("template",templateName);
115 } catch (Exception e) {
116 throw new HandlerException(
117 "Could not locate template: " + templateName);
118 }
119 }
120 }