View Javadoc

1   package org.paneris.messageboard.controller;
2   
3   import java.io.File;
4   import java.sql.Connection;
5   import java.util.Vector;
6   
7   import org.paneris.jal.model.DBConnectionManager;
8   import org.paneris.jal.model.DDRecord;
9   import org.paneris.jal.model.RecordSet;
10  import org.paneris.user.model.User;
11  import org.paneris.util.ValidationProblem;
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 MessageboardUpdater extends PanerisPage {
18  
19    private static final long serialVersionUID = 1L;
20      private DBConnectionManager connMgr = DBConnectionManager.getInstance();
21      private Connection conn;
22  
23      /**
24       * Handle this request to administer the database
25       *
26       * @param context   the WebContext
27       * @return          a Template to be expanded
28       * @throws Eception if any problem occurs
29       */
30      public Template handle(WebContext context) throws HandlerException {
31  
32          String db             = null;
33          String table          = null;
34          String id             = null;
35          Integer idInteger;
36          String templateName   = null;
37          String returnURL      = null;
38  
39          // --------------------------------
40          // Read in parameters from the form
41          // --------------------------------
42  
43          if (db == null) db = (String) context.getForm("db");
44          id = (String) context.getForm("id");
45          if (db == null) throw new HandlerException("No datasource specified.");
46          else {
47              conn = connMgr.getConnection("MessageboardUpdater",db);
48              if (conn == null) throw new HandlerException("Can't get connection: "+db);
49          }
50          table = "messageboards";
51          if (returnURL==null)      returnURL      = (String) context.getForm("returnURL");
52          try {
53              //------------
54              // List the TABLES in a database
55              //------------
56              // do a check so that we can restrict access using one resource
57              templateName = User.checkLoggedIn("controller","org.paneris.jal.controller.Admin", context, " access the Database Administration pages.");
58              if (templateName == null) {
59                  templateName = (String) context.getForm("wmtemplate");
60                  if (templateName == null) templateName = "jal/view/AdministrationUpdate1.wm";
61                  if (id!=null) idInteger = new Integer(id);
62                  else throw new HandlerException("No id specified.");
63                  DDRecord board = new DDRecord(db, "messageboards", idInteger);
64  
65                      // Delete the record
66                  if (context.getForm("delete_button") != null) {
67                      Vector links = board.getLinksToThis(true);
68                      if (links.isEmpty()) {
69                          board.delete();
70                      } else {
71                          context.put("notDeleted",links);
72                          context.put("error","Update Failed");
73                      }
74                  // Update it
75                  } else {
76                      board.setFromForm(context);
77                      if (context.getForm("duplicate_button") != null) board.setFieldValue("id","0");
78                      Vector problems = board.getProblems();
79                      if (problems.isEmpty()) {
80                          // check that the attachments directory exists
81                          File dir = new File(board.getFieldValue("attachmentpath"));
82                          if (!dir.exists()) {
83                              if (!dir.mkdirs()) {
84                                  problems.addElement(new ValidationProblem("Failed to create directory: " + dir.toString()));
85                              }
86                          }
87                      }
88                      if (problems.isEmpty()) {
89                          board.write();
90                          // new record?
91                          if (!board.getFieldValue("id").equals(id)) {
92                              RecordSet set = RecordSet.getInstance(context, "Admin" + "." + table);
93                              if (set != null) {
94                                  set.addElement(new Integer(board.getFieldValue("id")));
95                              }
96                          }
97                      } else {
98                          context.put("problems",problems);
99                          context.put("error","Update Failed");
100                     }
101                 }
102                 context.put("ddtable",board);
103             }
104         } catch (Exception e) {
105             throw new HandlerException(e.toString());
106         } finally {
107             connMgr.freeConnection(db, conn);
108         }
109 
110 
111         // ----------------------------------------------------------------
112         // Put all the variables into the context (maybe to go into a form)
113         // ----------------------------------------------------------------
114 
115         context.put("db",db);
116         context.put("table",table);
117         context.put("id",id);
118         context.put("wmtemplate",templateName);
119         context.put("returnURL",returnURL);
120 
121         // return the appropriate template
122         try {
123             return (Template) context.getBroker().get("template",templateName);
124         } catch (Exception e) {
125             throw new HandlerException("Could not locate template: " + templateName);
126         }
127     }
128 
129 
130 
131 }