View Javadoc

1   package org.paneris.messageboard.controller;
2   
3   import java.sql.Connection;
4   import java.util.Enumeration;
5   import java.util.StringTokenizer;
6   import java.util.Vector;
7   
8   import org.paneris.jal.model.DBConnectionManager;
9   import org.paneris.jal.model.DDField;
10  import org.paneris.jal.model.HTMLUtil;
11  import org.paneris.jal.model.RecordSet;
12  import org.paneris.messageboard.model.Attachment;
13  import org.paneris.messageboard.model.Board;
14  import org.paneris.messageboard.model.Message;
15  import org.paneris.messageboard.model.MessageThread;
16  import org.paneris.user.model.User;
17  import org.webmacro.Template;
18  import org.webmacro.servlet.HandlerException;
19  import org.webmacro.servlet.PanerisPage;
20  import org.webmacro.servlet.WebContext;
21  
22  public class MessagePage extends PanerisPage {
23  
24    private static final long serialVersionUID = 1L;
25      private DBConnectionManager connMgr;
26      String db;
27  
28      public Template handle(WebContext context) throws HandlerException {
29          db = (String) context.getForm("db");
30          connMgr = DBConnectionManager.getInstance();
31          Connection conn = connMgr.getConnection("MessagePage",db);
32          String sqlString = "";
33          String templateName = null;
34          try {
35              templateName = User.checkLoggedIn("controller", this.getClass().getName(), context, " read messages.");
36          } catch (Exception e) {
37              throw new HandlerException("Could not get user:" + e.toString());
38          }
39          if (templateName == null) {
40              try {
41                  context.put("types", new RecordSet(db,"messageboards_types", "SELECT id, type FROM messageboards_types ORDER BY display_order",new Integer(0)));
42                  String idString = (String) context.getForm("id");
43                  Integer id = new Integer(idString);
44                  Message m = new Message(db, id);
45                  Board board = new Board(db, new Integer(m.getFieldValue("board")));
46                  try {
47                      templateName = User.checkLoggedIn("messageboard",  board.getFieldValue("name"), context, " access this Messageboard.");
48                  } catch (Exception e) {
49                      throw new HandlerException("Could not get user:" + e.toString());
50                  }
51                  if (templateName == null) {
52                      templateName = (String) context.getForm("wmtemplate");
53                      if (templateName == null) {
54                          templateName = "messageboard/view/Message.wm";
55                      }
56                      context.put("db",db);
57                      if (conn == null) {
58                          throw new RuntimeException("can't get connection: " + db);
59                      }
60                      context.put("board",board);
61                      // specila code to escape messages
62                      DDField mf = (DDField) m.get("message");
63                      String message = (String) mf.getValue();
64                      String returnString = "";
65                      if (message != null) {
66                          StringTokenizer st = new StringTokenizer(HTMLUtil.escapeHTML(message),"\n\r");
67                          while (st.hasMoreTokens()) {
68                              returnString += st.nextToken() + "<br>";
69                          }
70                      }
71                      context.put("messagetext",returnString);
72                      context.put("message",m);
73                      Message n = m.getTopParent();
74                      MessageThread thread = new MessageThread(db);
75                      Vector rt = new Vector();
76                      while (n != null) {
77                          rt.addElement(n);
78                          n = thread.getNext(n);
79                      }
80                      if (rt.size() > 1) {
81                          context.put("messagecontext",rt);
82                      }
83                      sqlString = "SELECT * FROM attachments WHERE message = " + id;
84                      RecordSet set = RecordSet.getInstance(context, db, "attachments", sqlString, null);
85                      Vector att = new Vector();
86                      for (Enumeration en = set.getNext(); en.hasMoreElements();) {
87                          Integer id2 = (Integer) en.nextElement();
88                          att.addElement(new Attachment(db,id2));
89                      }
90                      context.put("attachments",att);
91                  }
92              } catch (Exception e) {
93                  throw new HandlerException(e.toString() + " sqlString: " + sqlString);
94              }
95          }
96  
97          connMgr.freeConnection(db, conn);
98          // return the appropriate template
99          try {
100             return (Template) context.getBroker().get("template",templateName);
101         } catch (Exception e) {
102             throw new HandlerException("Could not locate template: " + templateName);
103         }
104     }
105 }