1
votes

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!

1
is there a need to display the data in a traditional Notes form? Or is it just to store data?Frank van der Linden
Hello Frank, the document can not be edited via a Notes form (the form is web only)Malin
in a xpages application we store 'RichText' html as plain text in a field, because on the web RichText doesn't exist.Frank van der Linden
I am working on applying an xpages interface and writing the business logic in java for a legacy Domino application so we have numerous documents with Rich text on them I can not simply add some additional fields and ignore the content of the rich text fields. What is IBM's suggestion on this????Malin

1 Answers

0
votes

Speak after me: There is no RichText on the web, there's only MIME.

A Notes RichText Item can store data in "classic" RichText or as Mime entry. It is a property you can set in the field properties on the form. You want to store data as MIME, but might have content still being "classic" RichText. Distinguish the two.

For classic you can use Convert to HTML to obtain a web suitable String.

When writing back you first delete the Body field and recreate it using MimeEntity (see the link for sample code).

Also this gist provides a rough idea how you can code it.

Hope that helps!