4.4.3.4. The edit.jsp page

Following an example of the edit.jsp page. This implementation shows the content of the node in an HTML textarea field. Furthermore, a "Save" button is shown below the input field:
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
<%@page contentType="text/html" pageEncoding="UTF-8"
session="true"
import="org.docma.plugin.*,org.docma.plugin.web.*"
%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="0">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<title>Edit File</title>
<%
request.setCharacterEncoding("UTF-8");
String sessId = request.getParameter("docsess");
String nodeId = request.getParameter("nodeid");
String store = request.getParameter("store");
String ver = request.getParameter("version");
String lang = request.getParameter("lang");
String save = request.getParameter("edit");
WebUserSession sess =
WebPluginUtil.getUserSession(application, sessId);
StoreConnection conn = (sess != null) ? sess.getOpenedStore()
: null;
boolean open = (conn != null) &&
store.equals(conn.getStoreId()) &&
ver.equals(conn.getVersionId().toString()) &&
lang.equals(conn.getCurrentLanguage().getCode());
String editTxt = null;
if (open) {
Node node = conn.getNodeById(nodeId);
if (node instanceof Content) {
Content cont = (Content) node;
editTxt = cont.getContentString();
if ((save != null) && !save.equals(editTxt)) {
// Save changed content
if (cont instanceof PubContent) {
cont.makeRevision();
}
cont.setContentString(save);
editTxt = save; // show saved content in textarea
}
}
}
%>
</head>
<body style="background-color:E0E0E0; overflow:hidden;">
<form name="editform" action="edit.jsp" method="post" >
<table border="0" style="width:100%; height:100%;">
<tr>
<td>
<textarea name="edit" style="width:100%; height:100%;"><%=
(editTxt != null) ?
editTxt.replace("<", "&lt;").replace(">", "&gt;") : ""
%></textarea>
</td>
</tr>
<tr>
<td height="40">
<input type="submit" value="Save" />
<input type="hidden" name="docsess" value="<%= sessId %>" />
<input type="hidden" name="nodeid" value="<%= nodeId %>" />
<input type="hidden" name="store" value="<%= store %>" />
<input type="hidden" name="version" value="<%= ver %>" />
<input type="hidden" name="lang" value="<%= lang %>" />
</td>
</tr>
</table>
</form>
</body>
</html>

Listing 4.4.10. edit.jsp

First of all, the URL parameters are retrieved via the getParameter method of the JSP request object. Then the WebUserSession instance and the currently opened store is retrieved the same way as already described in Section 4.4.2, “Viewer Plug-ins” for the view.jsp page.
The editor is a simple HTML form with a textarea input field and a submit button. An HTML table is used to create a layout with two rows. The first row contains the textarea field and the second row the submit button (Save). Clicking the submit button posts the form to the page itself (edit.jsp). The content of the textarea field is posted as form field with name "edit". Therefore, if the request contains a parameter with name "edit", this indicates that the "Save" button has been clicked. The content to be saved is the value of the "edit" parameter.
The open variable contains the value true only if the user is still connected to the same store and has still selected the same content language as at the time when the editor was opened. For this, the URL parameters store, version and lang need to be supplied in the URL of the edit.jsp page, when the editor is opened. Consider the situation that the user opens the editor, and then logs out from Docmenta, while the editor is still opened. If the user then clicks the "Save" button in the editor, then the store connection of the user session is already closed. Another situation to be handled is, that the user switches to another store or to another content language, while the content is still opened in the editor. In all these cases the open variable evaluates to false. In our example, if the open variable is false, the "Save" request is just ignored. Of course, instead of ignoring the "Save" request an error message like "Store connection has been closed" should be presented to the user. Another possibility is, to allow saving of the content even if the store connection
has been closed or changed meanwhile. To achieve this, the editor has to open a new connection to the original store from which the content was retrieved. This can be achieved by invoking the createTempStoreConnection method on the
user session instance. Here is code fragment that shows the principle of opening a temporary store connection:
    if (! open) {  // original connection is no longer opened
        StoreConnection conn = webSess.createTempStoreConnection(store, ver);
        try {
            // ... Use conn to save the content.
        } finally {
            conn.close(); // always close temporary connection
        }
    }
However, in our example we only allow saving as long as the connection in the main window is still the same as when the editor was opened.
After having retrieved a store connection, the submitted content can be assigned to the content-node. Though, before the new content is assigned, we assure that the submitted content actually differs from the currently stored content. This is not required, but it avoids unnecessary write operations on the store. The currently stored content is retrieved by the statement:
    editTxt = cont.getContentString();
Therefore, the content is only saved if the expression save.equals(editTxt) evaluates to false. Furthermore, in case of an XHTML content-node (nodes of type org.docma.plugin.PubContent), also a new revision is created by invoking the makeRevision method. Although the makeRevision method is available for all content-nodes, it has currently only effect for PubContent nodes. That means, invocations of makeRevision on file-nodes (org.docma.plugin.FileContent) are ignored by the current store implementation.
Finally, the content is saved by invoking the setContentString method on the node, passing the modified content as argument:
    cont.setContentString(save);