Search in sources :

Example 1 with BatchIWantAttachment

use of edu.cornell.kfs.module.purap.businessobject.BatchIWantAttachment in project cu-kfs by CU-CommunityApps.

the class IWantDocumentFeedServiceImpl method loadDocumentAttachments.

/**
 * Adds attachments to the I Want document.
 * @param document
 * @param attachments
 */
private void loadDocumentAttachments(IWantDocument document, List<BatchIWantAttachment> attachments, String incomingFileName) {
    String attachmentsPath = new File(iWantDocumentInputFileType.getDirectoryPath()).toString() + "/attachment/";
    for (BatchIWantAttachment attachment : attachments) {
        Note note = new Note();
        note.setNoteText(attachment.getAttachmentType());
        note.setRemoteObjectIdentifier(document.getObjectId());
        note.setAuthorUniversalIdentifier(document.getDocumentHeader().getWorkflowDocument().getPrincipalId());
        note.setNoteTypeCode(KFSConstants.NoteTypeEnum.DOCUMENT_HEADER_NOTE_TYPE.getCode());
        note.setNotePostedTimestampToCurrent();
        // attempt to load file
        String fileName = attachmentsPath + "/" + attachment.getAttachmentFileName();
        File attachmentFile = new File(fileName);
        if (!attachmentFile.exists()) {
            continue;
        }
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(fileName);
            Integer fileSize = Integer.parseInt(Long.toString(attachmentFile.length()));
            String mimeTypeCode = attachment.getAttachmentMimeTypeCode();
            String fileExtension = "." + StringUtils.substringAfterLast(fileName, ".");
            if (StringUtils.isNotBlank(fileExtension) && mimeTypeProperties.containsKey(fileExtension)) {
                if (StringUtils.isBlank(mimeTypeCode)) {
                    mimeTypeCode = mimeTypeProperties.getProperty(fileExtension);
                }
            } else {
                LOG.error("Mime type error" + fileName + " " + mimeTypeCode);
            }
            LOG.info("Mime type " + fileName + " " + mimeTypeCode);
            String attachType = KFSConstants.EMPTY_STRING;
            Attachment noteAttachment = attachmentService.createAttachment(document.getDocumentHeader(), attachment.getAttachmentFileName(), mimeTypeCode, fileSize, fileInputStream, attachType);
            note.addAttachment(noteAttachment);
            document.addNote(note);
        } catch (FileNotFoundException e) {
            continue;
        } catch (IOException e1) {
            throw new RuntimeException("Unable to create attachment for File: " + fileName, e1);
        } finally {
            IOUtils.closeQuietly(fileInputStream);
        }
    }
}
Also used : Note(org.kuali.kfs.krad.bo.Note) FileNotFoundException(java.io.FileNotFoundException) BatchIWantAttachment(edu.cornell.kfs.module.purap.businessobject.BatchIWantAttachment) BatchIWantAttachment(edu.cornell.kfs.module.purap.businessobject.BatchIWantAttachment) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

BatchIWantAttachment (edu.cornell.kfs.module.purap.businessobject.BatchIWantAttachment)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Attachment (org.kuali.kfs.krad.bo.Attachment)1 Note (org.kuali.kfs.krad.bo.Note)1