View Javadoc

1   package org.paneris.messageboard.controller;
2   
3   import java.sql.Connection;
4   import java.util.Enumeration;
5   
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  /*
16    This handler sends the contents of the whole board to the user via email.
17  */
18  public class SendBoard extends PanerisPage {
19  
20    private static final long serialVersionUID = 1L;
21      Board board;
22      Connection conn;
23  
24      public Template handle(WebContext context) throws HandlerException {
25          String templateName = null;
26          try {
27              templateName = User.checkLoggedIn("controller", this.getClass().getName(), context, " update messages.");
28          } catch (Exception e) {
29              throw new HandlerException("Could not get user:" + e.toString());
30          }
31          if (templateName == null) {
32              try {
33                  User user = User.getInstance(context);
34                  if (!user.isLoggedOn()) {
35                      User.setReturnURL(context);
36                      templateName = User.getLoginPage(context,"  recieve all board messages.");
37                  } else {
38                      String db = (String) context.getForm("db");
39                      String messageboard = (String) context.getForm("messageboard");
40                      try {
41                          board = new Board(db, new Integer(messageboard));
42                      } catch (Exception e) {
43                          throw new HandlerException("Could not create messageboard: " + e.toString());
44                      }
45                      templateName = User.checkLoggedIn("messageboard", board.getFieldValue("name"), context, " recieve all board messages.");
46                      if (templateName == null) {
47                          templateName = (String) context.getForm("wmtemplate");
48                          if (templateName == null) {
49                              templateName = "messageboard/view/SendBoard.wm";
50                          }
51                          context.put("returnURL",(String) context.getForm("returnURL"));
52                          context.put("db",db);
53                          context.put("board",board);
54                          String sqlString = new String("SELECT messages.id FROM messages " + 
55                                                     " WHERE messages.board = " + board.getFieldValue("id") + "");
56                          sqlString += new String(" ORDER BY messagedate ASC");
57                          RecordSet set = RecordSet.getInstance(context, db, "messages", sqlString, new Integer(0));
58                          String addressee = user.getFieldValue("email").trim();
59                          for (Enumeration en = set.getNext(); en.hasMoreElements();) {
60                             Integer id = (Integer) en.nextElement();
61  
62                             Message m = new Message(db, id);
63                             m.send(addressee);
64                          }
65                     }
66                  }
67              } catch (Exception e) {
68                  throw new HandlerException(e.toString());
69              }
70          }
71          // return the appropriate template
72          try {
73              return (Template) context.getBroker().get("template",templateName);
74          } catch (Exception e) {
75              throw new HandlerException("Could not locate template: " + templateName);
76          }
77      }
78  }
79