Search in sources :

Example 76 with Note

use of org.kuali.kfs.krad.bo.Note 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);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Attachment(org.kuali.kfs.krad.bo.Attachment) ProcurementCardDocument(org.kuali.kfs.fp.document.ProcurementCardDocument) ReceiptProcessing(edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing) IOException(java.io.IOException) Date(java.sql.Date) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.kuali.kfs.sys.exception.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Note(org.kuali.kfs.krad.bo.Note) ParseException(org.kuali.kfs.sys.exception.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Aggregations

Note (org.kuali.kfs.krad.bo.Note)76 Attachment (org.kuali.kfs.krad.bo.Attachment)14 WorkflowException (org.kuali.rice.kew.api.exception.WorkflowException)12 IOException (java.io.IOException)9 FileNotFoundException (java.io.FileNotFoundException)7 ArrayList (java.util.ArrayList)7 NoteService (org.kuali.kfs.krad.service.NoteService)7 FileInputStream (java.io.FileInputStream)6 KualiDocumentFormBase (org.kuali.kfs.kns.web.struts.form.KualiDocumentFormBase)6 NonTransactional (org.kuali.kfs.sys.service.NonTransactional)6 File (java.io.File)5 Iterator (java.util.Iterator)5 DocumentService (org.kuali.kfs.krad.service.DocumentService)5 Person (org.kuali.rice.kim.api.identity.Person)5 NoteExtendedAttribute (edu.cornell.kfs.sys.businessobject.NoteExtendedAttribute)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Document (org.kuali.kfs.krad.document.Document)4 PurchaseOrderDocument (org.kuali.kfs.module.purap.document.PurchaseOrderDocument)4 HashMap (java.util.HashMap)3 List (java.util.List)3