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