Problem with getting list of names of attached files in NotesDocument

272 views Asked by At

I am getting sometimes problems with creating a list of names of the attached files in a NotesDocument. The custom message looks as followed:

AttachmentDominoDAO - General problem during reading attachment from entry 39E411CEC4AD22F3C1258821003399EF in database mail.nsf. fileObject.getName() returns null. Files found in document [contract.pdf]

Here is the method that I am calling:

private Attachment loadFromEntry(ViewEntry entry) {
        utils.printToConsole(this.getClass().getSimpleName().toString() + " - loadFromEntry(...) unid=" + entry.getUniversalID());
        Attachment attachment = new Attachment();
        try{
            
            attachment.setUnid(entry.getUniversalID()); 
            Document doc = entry.getDocument();
            if (null != doc){               
                attachment.setCreated(doc.getCreated().toJavaDate());               
                if(doc.hasItem("$FILE")){                   
                    List<String> files = doc.getAttachmentNames();
                    for (int j = 0; j < files.size(); ++j) {                         
                        EmbeddedObject fileObject = doc.getAttachment(files.get(j));                        
                        if(null != fileObject.getName()) {
                            if(null != fileObject.getName()) {
                                attachment.setFile(fileObject.getName());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.getName() returns " + fileObject.getName(), Level.SEVERE, null);
                            }

                            if(null != fileObject.getName() && !utils.Right(fileObject.getName(),".").isEmpty()) {
                                attachment.setExtension(utils.Right(fileObject.getName(),".")); 
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", extension is empty for file " + fileObject.getName(), Level.SEVERE, null);
                            }
                            
                            attachment.setSizeHuman(FileUtils.byteCountToDisplaySize(fileObject.getFileSize()));                            
                            
                            if(fileObject.getFileSize() > 0) {
                                attachment.setSize(fileObject.getFileSize());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.size() returns " + fileObject.getFileSize(), Level.SEVERE, null);
                            }

                            if(null != doc.getAuthors() && null != doc.getAuthors().firstElement()) {
                                attachment.setCreator(doc.getAuthors().firstElement());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", doc.getAuthors().firstElement() returns " + doc.getAuthors().firstElement(), Level.SEVERE, null);
                            }

                            String fieldName = "type";
                            if (doc.hasItem(fieldName)) {
                                attachment.setType(fieldName);
                            }
                            
                        }else {
                            XspOpenLogUtil.logEvent(null, "AttachmentDominoDAO - General problem during reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName() + ". fileObject.getName() returns null. Files found in document " + doc.getAttachmentNames().toString(), Level.SEVERE, null);
                        }
                    }                   
                }           
            }
        }catch (Exception e) {
            XspOpenLogUtil.logEvent(e, "General problem with reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName(), Level.SEVERE, null);
        }
        return attachment;
    }   

One document can only contain one file. When I check the document there is only one attachment and the attachment mentioned in the error message.

Anyone has a suggestion how to fix this issue?

Note: in 99% of the cases the error does not occur.

2

There are 2 answers

4
Richard Schwartz On

NotesDocument.getAttachment does not find attachments that were created in rich text fields. It only finds attachments that were created directly in the document itself.

We used to call an attachment that is directly acceessed through the document a "V2 attachment" because that's the way things worked in Notes V2 -- over 30 years ago. Objects created through OLE launch properties on the form can also attach objects that are directly in the document instead of inside rich text. Since OLE and V2 are both relics of the deep, dark past, almost all attachments are created inside rich text fields these days.

Attachments that are inside rich text fields are accessed through the getEmbeddedObject method of the RichTextItem class.

1
Scott Leis On

If getName() returns null for an attachment, try using getSource() instead. As long as it's an actual attachment (as opposed to an OLE embedded object), then getSource() always returns the original file name.