Search in sources :

Example 11 with Attachment

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

the class CuAttachmentDaoOjb method getAttachmentByAttachmentId.

@Override
public Attachment getAttachmentByAttachmentId(String attachmentIdentifier) {
    Criteria crit = new Criteria();
    crit.addEqualTo(CUKRADPropertyConstants.ATTACHMENT_IDENTIFIER, attachmentIdentifier);
    return (Attachment) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(Attachment.class, crit));
}
Also used : QueryByCriteria(org.apache.ojb.broker.query.QueryByCriteria) Attachment(org.kuali.kfs.krad.bo.Attachment) QueryByCriteria(org.apache.ojb.broker.query.QueryByCriteria) Criteria(org.apache.ojb.broker.query.Criteria)

Example 12 with Attachment

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

the class VendorBatchServiceImpl method loadDocumentAttachments.

/*
	 * add 'attachments' to vendor document.  attachment file names is in input csv file.
	 * Attachment files are upload to "staging.directory"/vendorBatch/attachment folder
	 */
private void loadDocumentAttachments(Document document, List<String> attachments) {
    String attachmentsPath = new File(batchInputFileTypes.get(0).getDirectoryPath()).toString() + "/attachment";
    for (String attachment : attachments) {
        Note note = new Note();
        note.setNoteText("Procurement vendor batch process - add attachment");
        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;
        File attachmentFile = new File(fileName);
        if (!attachmentFile.exists()) {
            continue;
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            Integer fileSize = Integer.parseInt(Long.toString(attachmentFile.length()));
            // TODO : Files.probeContentType is supported by java 7.  Not sure if this will be an issue
            String mimeTypeCode = getMimeTypeFromAttachmentFile(attachmentFile);
            // TODO : urlconnection is working for java 7 and under, but it return null for 'docx/pptx/xslx'
            // String type = URLConnection.guessContentTypeFromName(attachmentFile.getAbsolutePath());
            LOG.info("Mime type " + fileName + " " + mimeTypeCode);
            String attachType = KFSConstants.EMPTY_STRING;
            Attachment noteAttachment = attachmentService.createAttachment(document.getDocumentHeader(), attachment, 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);
        }
    }
}
Also used : Note(org.kuali.kfs.krad.bo.Note) VendorBatchAdditionalNote(edu.cornell.kfs.vnd.businessobject.VendorBatchAdditionalNote) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 13 with Attachment

use of org.kuali.kfs.krad.bo.Attachment 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)

Example 14 with Attachment

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

the class CuElectronicInvoiceHelperServiceImpl method attachInvoiceXMLWithRejectDoc.

protected void attachInvoiceXMLWithRejectDoc(ElectronicInvoiceRejectDocument eInvoiceRejectDocument, File attachmentFile, String noteText) {
    Note note = null;
    try {
        note = SpringContext.getBean(DocumentService.class).createNoteFromDocument(eInvoiceRejectDocument, noteText);
        // KFSCNTRB-1369: Can't add note without remoteObjectIdentifier
        note.setRemoteObjectIdentifier(eInvoiceRejectDocument.getNoteTarget().getObjectId());
    } catch (Exception e1) {
        throw new RuntimeException("Unable to create note from document: ", e1);
    }
    String attachmentType = null;
    BufferedInputStream fileStream = null;
    try {
        fileStream = new BufferedInputStream(new FileInputStream(attachmentFile));
    } catch (FileNotFoundException e) {
        LOG.error("Exception opening attachment file", e);
    }
    Attachment attachment = null;
    try {
        attachment = SpringContext.getBean(AttachmentService.class).createAttachment(eInvoiceRejectDocument.getNoteTarget(), attachmentFile.getName(), INVOICE_FILE_MIME_TYPE, (int) attachmentFile.length(), fileStream, attachmentType);
    } catch (Exception e) {
        // it may have more than one kind of Exception
        // if attachment is not created for any reason, then don't include in note and proceed.
        // otherwise it will throw runtimeexception and cause job to stop
        LOG.error("Unable to create attachment", e);
    // throw new RuntimeException("Unable to create attachment", e);
    } finally {
        if (fileStream != null) {
            try {
                fileStream.close();
            } catch (IOException e) {
                LOG.error("Exception closing file", e);
            }
        }
    }
    if (attachment != null) {
        // if attachment is not created for any reason, then don't ininclude in note and proceed.
        // otherwise it will throw runtimeexception and cause job to stop
        note.setAttachment(attachment);
        attachment.setNote(note);
    }
    SpringContext.getBean(NoteService.class).save(note);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Note(org.kuali.kfs.krad.bo.Note) FileNotFoundException(java.io.FileNotFoundException) NoteService(org.kuali.kfs.krad.service.NoteService) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) ValidationException(org.kuali.kfs.krad.exception.ValidationException) IOException(java.io.IOException) WorkflowException(org.kuali.rice.kew.api.exception.WorkflowException) FileNotFoundException(java.io.FileNotFoundException) RemoteException(java.rmi.RemoteException) CxmlParseException(org.kuali.kfs.module.purap.exception.CxmlParseException) FileInputStream(java.io.FileInputStream)

Example 15 with Attachment

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

the class CreateAccountingDocumentServiceImplTest method buildSimpleAttachment.

private Attachment buildSimpleAttachment(String fileName) {
    Attachment attachment = new Attachment();
    attachment.setAttachmentFileName(fileName);
    return attachment;
}
Also used : Attachment(org.kuali.kfs.krad.bo.Attachment)

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