I want to read and write to a Rich Text field in my XPages application.
In my Employee class I have defined a field that should contain content of a Rich Text field as followed:
private com.ibm.xsp.http.MimeMultipart comment;
public com.ibm.xsp.http.MimeMultipart getComment() {
return comment;
}
public void setComment(com.ibm.xsp.http.MimeMultipart comment) {
this.comment = comment;
}
Now in my EmployeeDAO class I wonder how I should load the Rich Text content on a Notes document and set the comment field?
I found the following way which I think is not that "elegant":
public void loadRT(Document doc){
MimeMultipart strValue = null;
try {
if (doc.hasItem("Body")){
if (doc.getFirstItem("Body") != null) {
//?? not sure what the type value stands for
if (doc.getFirstItem("Body").getType() != 1) {
strValue = MimeMultipart.fromHTML(doc.getItemValueString("Body"));
} else {
RichTextItem rti = (RichTextItem) doc.getFirstItem("Body");
if (rti != null) {
HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String curURL = rq.getRequestURL().toString();
String docid = doc.getUniversalID();
String notesURL = curURL.substring(0, curURL.indexOf(rq.getContextPath()) + 1) + doc.getParentDatabase().getFilePath() + "/0/" + docid + "/" + "Body"
+ "?OpenField";
URL docURL;
try {
docURL = new URL(notesURL);
URLConnection uc = docURL.openConnection();
uc.setRequestProperty("Cookie", rq.getHeader("Cookie"));
uc.connect();
// do the HTTP request
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
// process the data returned
StringBuffer strBuf = new StringBuffer();
String tmpStr = "";
while ((tmpStr = in.readLine()) != null) {
strBuf.append(tmpStr);
}
strValue = MimeMultipart.fromHTML(strBuf.toString());
employee.setComment(strValue);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In my xpage I can bind this to a xp:inputRichText control as followed:
<xp:inputRichText id="inputRichText1"
value="#{employeeBean.employee.comment}" style="height:400px"
readonly="#{employeeBean.employee.editable eq false}">
</xp:inputRichText>
I have not come so far how I can adjust the rich text control to the size of it's content nor to exclude some of the rich text toolbar options.
I also have not come that far how to save any adjustment.
Is my approach correct? Or is there a neater, cleaner approach?
FYI the field on the Notes document is of type Rich Text. The content however is "only" text. At the moment a Domino (web) form is used and a field of type Rich Text.
Any help is highly appreciated since any explanation / example code seem to be scarce!