Search in sources :

Example 16 with Document

use of org.jbpm.document.Document in project jbpm-work-items by kiegroup.

the class MediaDownloadWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Map<String, Object> results = new HashMap<String, Object>();
    String documentPath = (String) workItem.getParameter("DocumentPath");
    if (documentPath != null) {
        try {
            Drive drive = auth.getDriveService(appName, clientSecret);
            Drive.Files.Get request = drive.files().get(documentPath);
            request.getMediaHttpDownloader().setProgressListener(new MediaDownloadProgressListener());
            request.getMediaHttpDownloader().setDirectDownloadEnabled(true);
            InputStream docInputStream = request.executeMediaAsInputStream();
            Document doc = new DocumentImpl();
            String docBaseName = FilenameUtils.getBaseName(documentPath);
            String docExtension = FilenameUtils.getExtension(documentPath);
            doc.setName(docBaseName + "." + docExtension);
            doc.setIdentifier(documentPath);
            doc.setLastModified(new Date());
            doc.setContent(IOUtils.toByteArray(docInputStream));
            results.put(RESULTS_DOCUMENT, doc);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing download document information.");
        throw new IllegalArgumentException("Missing download document information.");
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Document(org.jbpm.document.Document) DocumentImpl(org.jbpm.document.service.impl.DocumentImpl) Date(java.util.Date) Drive(com.google.api.services.drive.Drive)

Example 17 with Document

use of org.jbpm.document.Document in project jbpm-work-items by kiegroup.

the class MediaUploadWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Document docToUpload = (Document) workItem.getParameter("DocToUpload");
    String docMimeType = (String) workItem.getParameter("DocMimeType");
    String uploadPath = (String) workItem.getParameter("UploadPath");
    if (docToUpload != null && docMimeType != null && uploadPath != null) {
        try {
            Drive drive = auth.getDriveService(appName, clientSecret);
            File fileMetadata = new File();
            fileMetadata.setTitle(docToUpload.getName());
            fileMetadata.setAlternateLink(docToUpload.getLink());
            if (docToUpload.getLastModified() != null) {
                fileMetadata.setModifiedDate(new DateTime(docToUpload.getLastModified()));
            }
            java.io.File tempDocFile = java.io.File.createTempFile(FilenameUtils.getBaseName(docToUpload.getName()), "." + FilenameUtils.getExtension(docToUpload.getName()));
            FileOutputStream fos = new FileOutputStream(tempDocFile);
            fos.write(docToUpload.getContent());
            fos.close();
            FileContent mediaContent = new FileContent(docMimeType, tempDocFile);
            Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
            MediaHttpUploader uploader = insert.getMediaHttpUploader();
            uploader.setDirectUploadEnabled(true);
            uploader.setProgressListener(new MediaUploadProgressListener());
            insert.execute();
            workItemManager.completeWorkItem(workItem.getId(), null);
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing upload document information.");
        throw new IllegalArgumentException("Missing upload document information.");
    }
}
Also used : MediaHttpUploader(com.google.api.client.googleapis.media.MediaHttpUploader) Document(org.jbpm.document.Document) DateTime(com.google.api.client.util.DateTime) FileContent(com.google.api.client.http.FileContent) FileOutputStream(java.io.FileOutputStream) Drive(com.google.api.services.drive.Drive) File(com.google.api.services.drive.model.File)

Example 18 with Document

use of org.jbpm.document.Document in project jbpm-work-items by kiegroup.

the class DownloadFileWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Map<String, Object> results = new HashMap<String, Object>();
    try {
        if (auth == null) {
            auth = new DropboxAuth();
        }
        client = auth.authorize(clientIdentifier, accessToken);
        String dropboxDocumentPath = (String) workItem.getParameter("DocumentPath");
        InputStream inStream = client.files().downloadBuilder(dropboxDocumentPath).start().getInputStream();
        Path path = Paths.get(dropboxDocumentPath);
        Document doc = new DocumentImpl();
        doc.setName(path.getFileName().toString());
        doc.setIdentifier(dropboxDocumentPath);
        doc.setLastModified(new Date());
        doc.setContent(IOUtils.toByteArray(inStream));
        results.put(RESULTS_DOCUMENT, doc);
        workItemManager.completeWorkItem(workItem.getId(), results);
    } catch (Exception e) {
        logger.error("Unable to download file: " + e.getMessage());
        handleException(e);
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Document(org.jbpm.document.Document) DocumentImpl(org.jbpm.document.service.impl.DocumentImpl) Date(java.util.Date)

Example 19 with Document

use of org.jbpm.document.Document in project jbpm-work-items by kiegroup.

the class DropboxWorkitemHandlerTest method testDownloadFile.

@Test
public void testDownloadFile() throws Exception {
    TestWorkItemManager manager = new TestWorkItemManager();
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setParameter("DocumentPath", "/testpath/testDocName.txt");
    DownloadFileWorkitemHandler handler = new DownloadFileWorkitemHandler("testClientID", "{}");
    handler.setAuth(auth);
    handler.executeWorkItem(workItem, manager);
    assertNotNull(manager.getResults());
    assertEquals(1, manager.getResults().size());
    assertTrue(manager.getResults().containsKey(workItem.getId()));
    assertTrue((manager.getResults().get(workItem.getId())).get("Document") instanceof Document);
    Document downloadedDoc = (Document) manager.getResults().get(workItem.getId()).get("Document");
    assertNotNull(downloadedDoc);
    assertEquals("testDocName.txt", downloadedDoc.getName());
    assertEquals("test doc content", new String(downloadedDoc.getContent()));
}
Also used : TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) Matchers.anyString(org.mockito.Matchers.anyString) Document(org.jbpm.document.Document) AbstractBaseTest(org.jbpm.test.AbstractBaseTest) Test(org.junit.Test)

Example 20 with Document

use of org.jbpm.document.Document in project jbpm-work-items by kiegroup.

the class ClassifyImageWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Document classificationImage = (Document) workItem.getParameter("ImageToClassify");
    Map<String, Object> widResults = new HashMap<String, Object>();
    if (classificationImage != null) {
        try {
            VisualRecognition service = auth.getService(apiKey);
            ByteArrayInputStream imageStream = new ByteArrayInputStream(classificationImage.getContent());
            ClassifyOptions classifyOptions = new ClassifyOptions.Builder().imagesFile(imageStream).imagesFilename(classificationImage.getName()).parameters("{\"owners\": [\"me\"]}").build();
            ClassifiedImage result = service.classify(classifyOptions).execute().getImages().get(0);
            if (result.getError() != null) {
                ErrorInfo errorInfo = result.getError();
                logger.error("Error classifying image: " + errorInfo.getDescription());
                workItemManager.abortWorkItem(workItem.getId());
            } else {
                List<ImageClassificationResult> resultList = new ArrayList<>();
                for (ClassifierResult classification : result.getClassifiers()) {
                    for (ClassResult classResult : classification.getClasses()) {
                        resultList.add(new ImageClassificationResult(classification, classResult));
                    }
                    widResults.put(RESULT_VALUE, resultList);
                }
                workItemManager.completeWorkItem(workItem.getId(), widResults);
            }
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing image for classification.");
        throw new IllegalArgumentException("Missing image for classification.");
    }
}
Also used : HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ErrorInfo(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo) ArrayList(java.util.ArrayList) Document(org.jbpm.document.Document) ImageClassificationResult(org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)

Aggregations

Document (org.jbpm.document.Document)38 Test (org.junit.Test)21 Date (java.util.Date)14 DocumentImpl (org.jbpm.document.service.impl.DocumentImpl)9 HashMap (java.util.HashMap)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DocumentData (org.kie.workbench.common.forms.dynamic.model.document.DocumentData)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Documents (org.jbpm.document.Documents)3 Drive (com.google.api.services.drive.Drive)2 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)2 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)2 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)2 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)2