View Javadoc

1   package org.paneris.messageboard.controller;
2   
3   import java.sql.Connection;
4   import java.util.StringTokenizer;
5   import java.util.Vector;
6   
7   import org.paneris.jal.model.DBConnectionManager;
8   import org.paneris.jal.model.DDField;
9   import org.paneris.jal.model.RecordSet;
10  import org.paneris.messageboard.model.Board;
11  import org.paneris.messageboard.model.Message;
12  import org.paneris.messageboard.model.MessageThread;
13  import org.paneris.user.model.User;
14  import org.webmacro.Template;
15  import org.webmacro.servlet.HandlerException;
16  import org.webmacro.servlet.PanerisPage;
17  import org.webmacro.servlet.WebContext;
18  
19  
20  public class NewMessagePage extends PanerisPage {
21  
22      /**
23     * 
24     */
25    private static final long serialVersionUID = 1L;
26      private DBConnectionManager connMgr;
27      String db;
28  
29  
30      public Template handle(WebContext context) throws HandlerException {
31          connMgr = DBConnectionManager.getInstance();
32          db = (String) context.getForm("db");
33          String messageboard = (String) context.getForm("messageboard");
34          String idString = (String) context.getForm("id");
35          if (idString == null) {
36              idString = "0";
37          }
38          Integer id = new Integer(idString);
39          context.put("db",db);
40          Connection conn = connMgr.getConnection("NewMessagePage",db);
41          if (conn == null) {
42              throw new RuntimeException("can't get connection: " + db);
43          }
44          String sqlString = new String();
45          // get the user (if they exist)
46          String templateName = null;
47          try {
48              templateName = User.checkLoggedIn("controller", this.getClass().getName(), context, " post a new Message.");
49          } catch (Exception e) {
50              throw new HandlerException("Could not get user:" + e.toString());
51          }
52          if (templateName == null) {
53              try {
54                  context.put("types", new RecordSet(db,"messageboards_types", "SELECT id, type FROM messageboards_types ORDER BY display_order",new Integer(0)));
55                  Board board = new Board(db, new Integer(messageboard));
56                  templateName = User.checkLoggedIn("messageboard", board.getFieldValue("name"), context, " post to the Messageboards.");
57                  if (templateName == null) templateName = User.checkLoggedIn("updatemessage", board.getFieldValue("name"), context, " post to the " +  board.getFieldValue("description") + " Messageboard.");
58                  if (templateName == null) {
59                      User user = User.getInstance(context);
60                      if (!user.isLoggedOn()) {
61                          User.setReturnURL(context);
62                          templateName = User.getLoginPage(context," in order to post to a messageboard.");
63                      } else {
64                          templateName = (String) context.getForm("wmtemplate");
65                          if (templateName == null) {
66                              templateName = "messageboard/view/NewMessage.wm";
67                          }
68                          context.put("board",board);
69                          Message m = new Message(db,id);
70                          if (!idString.equals("0")) {
71                              DDField subject = (DDField) m.get("subject");
72                              DDField message = (DDField) m.get("message");
73                              subject.setValue("Re: " + subject.getValue().toString());
74                              Object ms = message.getValue();
75                              if (ms != null) {
76                                  message.setValue(indent(ms.toString()));
77                              }
78                              Message n = m.getTopParent();
79                              MessageThread thread = new MessageThread(db);
80                              Vector rt = new Vector();
81                              while (n != null) {
82                                  rt.addElement(n);
83                                  n = thread.getNext(n);
84                              }
85                              if (rt.size() > 1) {
86                                  context.put("messagecontext",rt);
87                              }
88                          }
89                          context.put("message",m);
90                          context.put("user",user);
91                          if (board.isSubscribed(user)) {
92                              context.put("subscribed","true");
93                          }
94                      }
95                  }
96              } catch (Exception e) {
97                  throw new HandlerException(e.toString() + " sqlString: " + sqlString);
98              }
99          }
100         connMgr.freeConnection(db, conn);
101         // return the appropriate template
102         try {
103             return (Template) context.getBroker().get("template",templateName);
104         } catch (Exception e) {
105             throw new HandlerException("Could not locate template: " + templateName);
106         }
107     }
108 
109     private String indent(String message) {
110         String returnString = "";
111         StringTokenizer st = new StringTokenizer(message,"\n\r");
112         while (st.hasMoreTokens()) {
113             returnString += "> " + st.nextToken() + "\n";
114         }
115         return returnString;
116     }
117 
118 
119 }