2
votes

I have an old Notes Client application. On the form are two RichText fields that hold attachments. JPG's, PDF's, whatever. The document also contains a unique key and other meta-data.

What I want to do is migrate from having multiple attachments on a document to a new document for each attachment. I've never done much with embedded objects and even less with MIME.

I'm currently working in XPages Java but could go to LotusScript if need be.

I was working with this snippet:

List<EmbeddedObject> docPicture = this.getFileAttachments(doc, "picture");
List<EmbeddedObject> docPDF = this.getFileAttachments(doc, "pdf");

for (EmbeddedObject eoPic : docPicture) {
picCount++;
 Document newDoc = currentDatabase.createDocument();
newDoc.replaceItemValue("form", "fm_file");
newDoc.replaceItemValue("uploadToken", doc.getItemValueString("barCodeHuman"));
newDoc.replaceItemValue("fileName", eoPic.getName());
newDoc.replaceItemValue("size", eoPic.getFileSize());
fileName = eoPic.getName();
fileType = fileName.substring(fileName.length() - 3);

newDoc.replaceItemValue("type", this.getMIMEType(fileType));


// Extract Attachment and Add To Attachment Document
InputStream attachInputStream = eoPic.getInputStream();
Stream attachStream = session.createStream();
attachStream.setContents(attachInputStream);

MIMEEntity attachField = newDoc.createMIMEEntity("attachment");
MIMEHeader attachHeader = attachField.createHeader("content-disposition");
attachHeader.setHeaderVal("attachment;filename=\"" + eoPic.getName() + "\"");
attachField.setContentFromBytes(attachStream, this.getMIMEType(fileType), MIMEEntity.ENC_IDENTITY_BINARY);
  • Note I'm using the OpenNTF API but could go back to the lotus objects if need be.

Anyway - this almost worked. I got my documents - 1 per attachment. But when going into the field "attachment" in the document propertied it's not a RichTextField it's a MIME something. that's causing me probably with the next phase of my project. The RichTextDocuments work fine but not the MIME ones.

this is a 1 time migration need so any thoughts on how I can end up with RichTextFields would be appreciated. Thanks!!

1
Hmm ever thought of using a file download control attached to the RTF item plus displaying the RTF item as editor in your Xpage? Notice: all updated / new created data via Xpage is stored as MIME, so you may not have backward compability when using Notes client with a form. You can also convert the RTF by changing the property of the RTF on your form to store data as MIME and re-save all documents in the client - not elegant but it works. I know this does not answer the question of extracting each attachment to a unique document, but maybe this might be much easier.Oliver Busse

1 Answers

0
votes

try to not involve mime entities at all. as Oliver said, check your target richText field on the form does not have the 'store contents as mime' checked. you could even use a richText lite field and restrict it to attachments.

I think you might be using the MIMEEntity method setContentsFromStream because you want to directly move the attachment from doc to doc? if you want to move using just RichText embedded objects (no mime entity involvement) you need to extract the embeddedObject using .extractFile to the file system first. Then using the RichTextItem that you create on the new doc (instead of create mime entity) you can use rti.embedObject to attach the file you extracted. (probably best to delete the temporary extract file after successful migration), see the designer help for an example of the parameters required for embedding attachments.

when extracting the file to file system you could extract it to the JVM's temporary directory, the file on the file system needs to have the same file name that you want it to have when attached to the new document. for this reason you can't really use File.createTemporaryFile() because your temp file name will have random characters in it. instead you you can get the temp directory with

System.getProperty("java.io.tmpdir")`

and the use that in your extract filepath.

another thing to check before starting processing, is the current notesSession's isConvertMIME setting, if to source field is mime, session.isConvertMIME == true will convert the field to richText when loading the doc. I think in xpages it is false by default, though I don't think it will affect you because I think your source attachments are already richText but for someone reading this and using mime source field it would be important to note. also if you change this using setConvertMIME, be sure to change it back to what it was when you finish your processing.