use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.
the class CreateAccountingDocumentServiceImplTest method assertNoteIsCorrect.
private void assertNoteIsCorrect(Note expectedNote, Note actualNote) {
assertEquals("Wrong note type", expectedNote.getNoteTypeCode(), actualNote.getNoteTypeCode());
assertEquals("Wrong note text", expectedNote.getNoteText(), actualNote.getNoteText());
if (expectedNote.getAttachment() == null) {
assertNull("Note should not have had an attachment", actualNote.getAttachment());
} else {
Attachment expectedAttachment = expectedNote.getAttachment();
Attachment actualAttachment = actualNote.getAttachment();
assertNotNull("Note should have had an attachment", actualAttachment);
assertEquals("Wrong attachment file name", expectedAttachment.getAttachmentFileName(), actualAttachment.getAttachmentFileName());
}
}
use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.
the class IWantDocumentServiceImpl method copyIWantdDocAttachmentsToDV.
private void copyIWantdDocAttachmentsToDV(DisbursementVoucherDocument dvDocument, DisbursementVoucherForm disbursementVoucherForm, IWantDocument iWantDocument) {
purapService.saveDocumentNoValidation(dvDocument);
if (iWantDocument.getNotes() != null && iWantDocument.getNotes().size() > 0) {
for (Iterator iterator = iWantDocument.getNotes().iterator(); iterator.hasNext(); ) {
Note note = (Note) iterator.next();
Note copyNote;
try {
copyNote = noteService.createNote(new Note(), dvDocument.getDocumentHeader(), GlobalVariables.getUserSession().getPrincipalId());
copyNote.setNoteText(note.getNoteText());
copyNote.setAuthorUniversalIdentifier(note.getAuthorUniversalIdentifier());
copyNote.setRemoteObjectIdentifier(dvDocument.getObjectId());
copyNote.setNotePostedTimestamp(note.getNotePostedTimestamp());
String attachmentType = StringUtils.EMPTY;
Attachment attachment = note.getAttachment();
if (attachment != null) {
Note newNote = disbursementVoucherForm.getNewNote();
Attachment copyAttachment = attachmentService.createAttachment(iWantDocument.getDocumentHeader(), attachment.getAttachmentFileName(), attachment.getAttachmentMimeTypeCode(), attachment.getAttachmentFileSize().intValue(), attachment.getAttachmentContents(), attachment.getAttachmentTypeCode());
if (copyAttachment != null) {
copyNote.addAttachment(copyAttachment);
noteService.save(copyNote);
dvDocument.addNote(copyNote);
purapService.saveDocumentNoValidation(dvDocument);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.
the class IWantDocumentServiceImpl method copyIWantDocAttachments.
/**
* Copies all attachments from the I Want document to the accounting document
*
* NOTE: A true extCopyNoteIndicator value will cause the copied notes to also
* include an extended attribute with copyNoteIndicator set to true.
*
* @param document
* @param iWantDocument
* @param copyConfidentialAttachments
* @param extCopyNoteIndicator
* @throws Exception
*/
private void copyIWantDocAttachments(AccountingDocument document, IWantDocument iWantDocument, boolean copyConfidentialAttachments, boolean extCopyNoteIndicator) throws Exception {
purapService.saveDocumentNoValidation(document);
if (iWantDocument.getNotes() != null && iWantDocument.getNotes().size() > 0) {
for (Iterator iterator = iWantDocument.getNotes().iterator(); iterator.hasNext(); ) {
Note note = (Note) iterator.next();
try {
Note copyingNote = documentService.createNoteFromDocument(document, note.getNoteText());
purapService.saveDocumentNoValidation(document);
copyingNote.setNotePostedTimestamp(note.getNotePostedTimestamp());
copyingNote.setAuthorUniversalIdentifier(note.getAuthorUniversalIdentifier());
copyingNote.setNoteTopicText(note.getNoteTopicText());
Attachment originalAttachment = attachmentService.getAttachmentByNoteId(note.getNoteIdentifier());
if (originalAttachment != null && (copyConfidentialAttachments || !ConfidentialAttachmentTypeCodes.CONFIDENTIAL_ATTACHMENT_TYPE.equals(originalAttachment.getAttachmentTypeCode()))) {
// new Attachment();
Attachment newAttachment = attachmentService.createAttachment((PersistableBusinessObject) copyingNote, originalAttachment.getAttachmentFileName(), originalAttachment.getAttachmentMimeTypeCode(), originalAttachment.getAttachmentFileSize().intValue(), originalAttachment.getAttachmentContents(), originalAttachment.getAttachmentTypeCode());
if (ObjectUtils.isNotNull(originalAttachment) && ObjectUtils.isNotNull(newAttachment)) {
copyingNote.addAttachment(newAttachment);
}
document.addNote(copyingNote);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
purapService.saveDocumentNoValidation(document);
// If specified, auto-construct extended attributes on the document notes at this point. (Doing so before initial persistence can cause save problems.)
if (extCopyNoteIndicator && CollectionUtils.isNotEmpty(document.getNotes())) {
for (Note copiedNote : document.getNotes()) {
NoteExtendedAttribute noteExtension = (NoteExtendedAttribute) copiedNote.getExtension();
noteExtension.setNoteIdentifier(copiedNote.getNoteIdentifier());
noteExtension.setCopyNoteIndicator(true);
}
purapService.saveDocumentNoValidation(document);
}
}
use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.
the class ReceiptProcessingServiceImpl method matchAndAttach.
/**
* Performs match and attach. It will search for a match PCDO document and attempt to attach the receipt.
*
* @param receipts
* @param attachmentsPath
* @param mimeTypeCode
*/
protected void matchAndAttach(List<ReceiptProcessing> receipts, String attachmentsPath, String mimeTypeCode) {
StringBuilder processResults = new StringBuilder();
processResults.append(RESULT_FILE_HEADER_LINE);
for (ReceiptProcessing receipt : receipts) {
Note note = new Note();
java.util.Date pdate = null;
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
pdate = (java.util.Date) df.parse(receipt.getPurchasedate());
} catch (ParseException e) {
processResults.append(receipt.badData(false));
LOG.error("Bad date field on incoming csv");
continue;
} catch (java.text.ParseException e) {
processResults.append(receipt.badData(false));
LOG.error("Bad date field on incoming csv");
continue;
}
Date pdateSQL = null;
if (pdate != null) {
pdateSQL = new Date(pdate.getTime());
}
List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByCarhdHolderAmountDateVendor(receipt.getCardHolder(), receipt.getAmount(), pdateSQL);
ProcurementCardDocument pcdo = null;
if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
processResults.append(receipt.noMatch(false));
continue;
}
if (pcdoList.size() > 1) {
processResults.append(receipt.multipleMatch(false));
continue;
}
if (pcdoList.size() == 1) {
pcdo = pcdoList.get(0);
}
String pdfFileName = attachmentsPath + "/" + receipt.getFilename();
LOG.info("Start creating note and attaching pdf file " + pdfFileName + " to PCDO document #" + pcdo.getDocumentNumber());
File f = null;
FileInputStream fileInputStream = null;
try {
f = new File(pdfFileName);
fileInputStream = new FileInputStream(pdfFileName);
} catch (FileNotFoundException e) {
LOG.error("File " + pdfFileName + " not found for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.badData(false));
continue;
} catch (IOException e) {
LOG.error("generic Io exception for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.badData(false));
continue;
}
long fileSizeLong = f.length();
Integer fileSize = Integer.parseInt(Long.toString(fileSizeLong));
String attachType = "";
Attachment noteAttachment = null;
try {
noteAttachment = attachmentService.createAttachment(pcdo.getDocumentHeader(), pdfFileName, mimeTypeCode, fileSize, fileInputStream, attachType);
} catch (IOException e) {
LOG.error("Failed to attach file for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.noMatch(false));
e.printStackTrace();
continue;
} catch (IllegalArgumentException e) {
/*
* Our custom attachment service will throw an IllegalArgumentException if the virus scan fails.
* The virus scan could also end up failing if the file size is too large. In such cases,
* return an error code indicating such a problem (or a problem with invalid parameters).
*/
LOG.error("Failed to create attachment for Document " + pcdo.getDocumentNumber(), e);
processResults.append(receipt.attachmentCreationError(false));
continue;
}
if (noteAttachment != null) {
note.setNoteText("Receipt Attached");
note.addAttachment(noteAttachment);
note.setRemoteObjectIdentifier(pcdo.getDocumentHeader().getObjectId());
note.setAuthorUniversalIdentifier(getSystemUser().getPrincipalId());
note.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
note.setNotePostedTimestampToCurrent();
try {
noteService.save(note);
} catch (Exception e) {
LOG.error("Failed to save note for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.noMatch(false));
e.printStackTrace();
continue;
}
LOG.info("Attached pdf " + pdfFileName + " for document " + pcdo.getDocumentNumber());
processResults.append(receipt.match(pcdo.getDocumentNumber(), false));
}
}
String outputCsv = processResults.toString();
// this is CALS output folder and it has to stay unchanged
String reportDropFolder = pdfDirectory + "/CIT-csv-archive/";
getcsvWriter(outputCsv, reportDropFolder);
}
use of org.kuali.kfs.krad.bo.Attachment in project cu-kfs by CU-CommunityApps.
the class ReceiptProcessingServiceImpl method matchOrAttachOnly.
/**
* Performs match or attach on each incoming record. The method determines
* for each incoming record if a match only or attach only should be
* performed. If first card holder, amount and purchase date are not blank
* then a match only is performed. If the source unique id and edoc number
* are not blank then an attach only is performed.
*
* @param fileName
* @param receipts
* @param attachmentsPath
* @param mimeTypeCode
*/
protected void matchOrAttachOnly(String fileName, BatchInputFileType batchInputFileType, List<ReceiptProcessing> receipts, String attachmentsPath, String mimeTypeCode) {
StringBuilder processResults = new StringBuilder();
processResults.append(RESULT_FILE_HEADER_LINE_WITH_EXTRA_FIELDS);
String customerName = getCustomerNameFromFileName(fileName, batchInputFileType);
for (ReceiptProcessing receipt : receipts) {
boolean matchOnly = StringUtils.isNotBlank(receipt.getCardHolder()) && StringUtils.isNotBlank(receipt.getAmount()) && StringUtils.isNotBlank(receipt.getPurchasedate()) && StringUtils.isBlank(receipt.getFilePath()) && StringUtils.isBlank(receipt.getFilename());
boolean attachOnly = StringUtils.isNotBlank(receipt.getSourceUniqueID()) && StringUtils.isNotBlank(receipt.getFilePath()) && StringUtils.isNotBlank(receipt.getFilename());
if (matchOnly) {
java.util.Date pdate = null;
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
pdate = (java.util.Date) df.parse(receipt.getPurchasedate());
} catch (ParseException e) {
processResults.append(receipt.badData(true));
LOG.error("Bad date field on incoming csv");
continue;
} catch (java.text.ParseException e) {
processResults.append(receipt.badData(true));
LOG.error("Bad date field on incoming csv");
continue;
}
Date pdateSQL = null;
if (pdate != null) {
pdateSQL = new Date(pdate.getTime());
}
List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByCarhdHolderNameAmountDateCardHolderNetID(receipt.getAmount(), pdateSQL, receipt.getCardHolderNetID());
ProcurementCardDocument pcdo = null;
if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
processResults.append(receipt.noMatch(true));
continue;
}
if (pcdoList.size() > 1) {
processResults.append(receipt.multipleMatch(true));
continue;
}
if (pcdoList.size() == 1) {
pcdo = pcdoList.get(0);
String eDocNumber = pcdo.getDocumentNumber();
receipt.seteDocNumber(eDocNumber);
processResults.append(receipt.match(eDocNumber, true));
}
}
if (attachOnly) {
Note note = new Note();
List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByEdocNumber(receipt.geteDocNumber());
ProcurementCardDocument pcdo = null;
if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
processResults.append(receipt.attachOnlyError());
continue;
}
if (pcdoList.size() > 1) {
processResults.append(receipt.attachOnlyError());
continue;
}
if (pcdoList.size() == 1) {
pcdo = pcdoList.get(0);
}
if (StringUtils.isNotBlank(customerName)) {
attachmentsPath = pdfDirectory + "/" + StringUtils.upperCase(customerName) + CUSTOMER_PDF_SUBFOLDER_SUFFIX;
}
String pdfFileName = attachmentsPath + "/" + receipt.getFilename();
LOG.info("Start creating note and attaching pdf file " + pdfFileName + " to PCDO document #" + pcdo.getDocumentNumber());
File f = null;
FileInputStream fileInputStream = null;
try {
f = new File(pdfFileName);
fileInputStream = new FileInputStream(pdfFileName);
} catch (FileNotFoundException e) {
LOG.error("File " + pdfFileName + " not found for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.attachOnlyError());
continue;
} catch (IOException e) {
LOG.error("generic Io exception for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.attachOnlyError());
continue;
}
long fileSizeLong = f.length();
Integer fileSize = Integer.parseInt(Long.toString(fileSizeLong));
String attachType = "";
Attachment noteAttachment = null;
try {
noteAttachment = attachmentService.createAttachment(pcdo.getDocumentHeader(), pdfFileName, mimeTypeCode, fileSize, fileInputStream, attachType);
} catch (IOException e) {
LOG.error("Failed to attach file for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.attachOnlyError());
e.printStackTrace();
continue;
} catch (IllegalArgumentException e) {
/*
* Our custom attachment service will throw an IllegalArgumentException if the virus scan fails.
* The virus scan could also end up failing if the file size is too large. In such cases,
* return an error code indicating such a problem (or a problem with invalid parameters).
*/
LOG.error("Failed to create attachment for Document " + pcdo.getDocumentNumber(), e);
processResults.append(receipt.attachmentCreationError(true));
continue;
}
if (noteAttachment != null) {
note.setNoteText("Receipt Attached");
note.addAttachment(noteAttachment);
note.setRemoteObjectIdentifier(pcdo.getDocumentHeader().getObjectId());
note.setAuthorUniversalIdentifier(getSystemUser().getPrincipalId());
note.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
note.setNotePostedTimestampToCurrent();
try {
noteService.save(note);
} catch (Exception e) {
LOG.error("Failed to save note for Document " + pcdo.getDocumentNumber());
processResults.append(receipt.attachOnlyError());
e.printStackTrace();
continue;
}
LOG.info("Attached pdf " + pdfFileName + " for document " + pcdo.getDocumentNumber());
processResults.append(receipt.match("8", true));
}
}
if (!matchOnly && !attachOnly) {
LOG.info("Invalid input data does not meet either match only nor attach only conditions: " + receipt.returnBoLine(true));
}
}
String outputCsv = processResults.toString();
// each customer will have a separate output folder for easier processing of the results files
String reportDropFolder = pdfDirectory + "/CIT-" + customerName + "-csv-archive/";
try {
/**
* Create, if not there
*/
FileUtils.forceMkdir(new File(reportDropFolder));
} catch (IOException e) {
throw new RuntimeException(e);
}
getcsvWriter(outputCsv, reportDropFolder);
}
Aggregations