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());
}
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());
}
}
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);
}
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());
}
}
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);
}
}
}
Aggregations