Search in sources :

Example 6 with Attachment

use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.

the class CuAttachmentServiceImplTest method createAttachment.

@Test
public void createAttachment() throws Exception {
    attachmentService.setAntiVirusService(new DummyAntiVirusServiceImpl());
    PersistableBusinessObject pbo = setupPersistableBusinessObject();
    InputStream inputStream = setupInputStream(ATTACHMENT_TEST_FILE_PATH + File.separator + GOOD_FILE_NAME);
    Attachment createdAttachment = attachmentService.createAttachment(pbo, GOOD_FILE_NAME, "txt", 10, inputStream, "txt");
    InputStream createdInputStream = new BufferedInputStream(new FileInputStream(buildDocumentDirectory(pbo.getObjectId()) + File.separator + createdAttachment.getAttachmentIdentifier()));
    String fileContents = IOUtils.toString(createdInputStream, "UTF-8");
    Assert.assertEquals(GOOD_FILE_CONTENTS, fileContents);
    Assert.assertEquals(GOOD_FILE_NAME, createdAttachment.getAttachmentFileName());
    Assert.assertEquals(10L, createdAttachment.getAttachmentFileSize().longValue());
    Assert.assertEquals("txt", createdAttachment.getAttachmentMimeTypeCode());
    Assert.assertEquals("txt", createdAttachment.getAttachmentTypeCode());
}
Also used : PersistableBusinessObject(org.kuali.kfs.krad.bo.PersistableBusinessObject) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Attachment(org.kuali.kfs.krad.bo.Attachment) DummyAntiVirusServiceImpl(edu.cornell.cynergy.antivirus.service.DummyAntiVirusServiceImpl) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 7 with Attachment

use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.

the class AdvanceDepositServiceImpl method createNotes.

private void createNotes(AchIncomeTransaction transaction, AdvanceDepositDocument document) {
    String fileName = CuFPConstants.ADVANCE_DEPOSIT_NOTE_FILE_PREFIX + document.getDocumentNumber() + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(dateTimeService.getCurrentDate()) + ".txt";
    StringBuilder notes = new StringBuilder();
    for (AchIncomeNote achIncomeNote : transaction.getNotes()) {
        notes.append(achIncomeNote.getNoteText());
        notes.append("\n");
    }
    byte[] notesAttachmentBytes = notes.toString().getBytes();
    String attachmentType = null;
    try {
        Attachment attachment = attachmentService.createAttachment(document.getDocumentHeader(), fileName, "text", notesAttachmentBytes.length, new ByteArrayInputStream(notesAttachmentBytes), attachmentType);
        Note note = documentService.createNoteFromDocument(document, "Attachment with transaction notes created by ach/incoming wire batch job.");
        note.setAttachment(attachment);
        attachment.setNote(note);
        noteService.save(note);
    } catch (IOException e) {
        LOG.error("Error while adding notes to advance deposit documents: " + e.getMessage(), e);
        throw new RuntimeException("Error while adding notes to the document " + document.getDocumentNumber());
    }
}
Also used : AchIncomeNote(edu.cornell.kfs.fp.businessobject.AchIncomeNote) ByteArrayInputStream(java.io.ByteArrayInputStream) Note(org.kuali.kfs.krad.bo.Note) AchIncomeFileTransactionNote(edu.cornell.kfs.fp.businessobject.AchIncomeFileTransactionNote) AchIncomeNote(edu.cornell.kfs.fp.businessobject.AchIncomeNote) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with Attachment

use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.

the class AccountingDocumentGeneratorBase method addAttachmentToNote.

protected void addAttachmentToNote(T document, AccountingXmlDocumentBackupLink backupLink, Note note) {
    Attachment attachment = accountingXmlDocumentDownloadAttachmentService.createAttachmentFromBackupLink(document, backupLink);
    note.setAttachment(attachment);
}
Also used : Attachment(org.kuali.kfs.krad.bo.Attachment)

Example 9 with Attachment

use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.

the class AccountingXmlDocumentDownloadAttachmentServiceImpl method createAttachmentFromBackupLink.

@Override
public Attachment createAttachmentFromBackupLink(Document document, AccountingXmlDocumentBackupLink accountingXmlDocumentBackupLink) {
    if (StringUtils.isBlank(accountingXmlDocumentBackupLink.getCredentialGroupCode())) {
        LOG.error("createAttachmentFromBackupLink, the Credential Group Code is blank");
        throw new ValidationException("Unable to download attachment with blank Credential Group Code: " + accountingXmlDocumentBackupLink.getLinkUrl());
    }
    try {
        byte[] formFile = downloadByteArray(accountingXmlDocumentBackupLink);
        if (formFile.length > 0) {
            String uploadFileName = accountingXmlDocumentBackupLink.getFileName();
            String mimeType = URLConnection.guessContentTypeFromName(uploadFileName);
            int fileSize = (int) formFile.length;
            String attachmentType = null;
            if (LOG.isDebugEnabled()) {
                LOG.debug("createAttachmentFromBackupLink, uploadFileName: " + uploadFileName + " mimeType: " + mimeType + " fileSize: " + fileSize);
            }
            InputStream inputStream = new ByteArrayInputStream(formFile);
            Attachment attachment = attachmentService.createAttachment(document, uploadFileName, mimeType, fileSize, inputStream, attachmentType);
            return attachment;
        } else {
            LOG.error("createAttachmentFromBackupLink, the form file is NULL");
            throw new ValidationException("Unable to download attachment: " + accountingXmlDocumentBackupLink.getLinkUrl());
        }
    } catch (IOException e) {
        LOG.error("createAttachmentFromBackupLink, Unable to download attachment: " + accountingXmlDocumentBackupLink.getLinkUrl(), e);
        throw new ValidationException("Unable to download attachment: " + accountingXmlDocumentBackupLink.getLinkUrl());
    }
}
Also used : ValidationException(org.kuali.kfs.krad.exception.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException)

Example 10 with Attachment

use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.

the class BatchFeedHelperServiceImpl method loadDocumentAttachments.

/**
 * @see com.rsmart.kuali.kfs.sys.batch.service.BatchFeedHelperService#loadDocumentAttachments(org.kuali.kfs.kns.document.Document,
 *      java.util.List, java.lang.String, java.lang.String, org.kuali.kfs.kns.util.ErrorMap)
 */
public void loadDocumentAttachments(Document document, List<Attachment> attachments, String attachmentsPath, String attachmentType, MessageMap errorMap) {
    for (Attachment attachment : attachments) {
        Note note = new Note();
        note.setNoteText(kualiConfigurationService.getPropertyValueAsString(KFSKeyConstants.IMAGE_ATTACHMENT_NOTE_TEXT));
        note.setRemoteObjectIdentifier(document.getObjectId());
        note.setAuthorUniversalIdentifier(getSystemUser().getPrincipalId());
        note.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
        note.setNotePostedTimestampToCurrent();
        // attempt to load file
        String fileName = attachmentsPath + "/" + attachment.getAttachmentFileName();
        File attachmentFile = new File(fileName);
        if (!attachmentFile.exists()) {
            errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_FEED_ATTACHMENT, new String[] { attachment.getAttachmentFileName(), attachmentsPath });
            continue;
        }
        try {
            FileInputStream 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 {
                errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_FEED_ATTACHMENT_TYPE, new String[] { fileName, fileExtension });
            }
            Attachment noteAttachment = attachmentService.createAttachment(document.getDocumentHeader(), fileName, mimeTypeCode, fileSize, fileInputStream, attachmentType);
            note.addAttachment(noteAttachment);
            document.addNote(note);
        } catch (FileNotFoundException e) {
            errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_FEED_ATTACHMENT, new String[] { attachment.getAttachmentFileName(), attachmentsPath });
            continue;
        } catch (IOException e1) {
            throw new RuntimeException("Unable to create attachment for image: " + fileName, e1);
        }
    }
}
Also used : Note(org.kuali.kfs.krad.bo.Note) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Attachment (org.kuali.kfs.krad.bo.Attachment)20 Note (org.kuali.kfs.krad.bo.Note)14 IOException (java.io.IOException)9 FileInputStream (java.io.FileInputStream)7 FileNotFoundException (java.io.FileNotFoundException)6 File (java.io.File)5 SimpleDateFormat (java.text.SimpleDateFormat)4 InputStream (java.io.InputStream)3 Iterator (java.util.Iterator)3 ReceiptProcessing (edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing)2 NoteExtendedAttribute (edu.cornell.kfs.sys.businessobject.NoteExtendedAttribute)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Date (java.sql.Date)2 DateFormat (java.text.DateFormat)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MessagingException (javax.mail.MessagingException)2 ProcurementCardDocument (org.kuali.kfs.fp.document.ProcurementCardDocument)2 MaintenanceDocument (org.kuali.kfs.kns.document.MaintenanceDocument)2