GoogleSpeechAttachTranscriptionOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { MediaPackage mediaPackage = workflowInstance.getMediaPackage(); WorkflowOperationInstance operation = workflowInstance.getCurrentOperation(); logger.debug("Attach transcription for mediapackage {} started", mediaPackage); String jobId = StringUtils.trimToNull(operation.getConfiguration(TRANSCRIPTION_JOB_ID)); if (StringUtils.isBlank(jobId)) { throw new WorkflowOperationException(TRANSCRIPTION_JOB_ID + " missing"); } String targetTagOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAG)); String targetFlavorOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR)); if (StringUtils.isBlank(targetFlavorOption)) { throw new WorkflowOperationException(TARGET_FLAVOR + " missing"); } MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption); String captionFormatOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_CAPTION_FORMAT)); String lineSize = StringUtils.trimToNull(operation.getConfiguration(TRANSCRIPTION_LINE_SIZE)); if (StringUtils.isBlank(lineSize)) { lineSize = DEFAULT_LINE_SIZE; } try { MediaPackageElement original = service.getGeneratedTranscription(mediaPackage.getIdentifier().toString(), jobId); MediaPackageElement transcription = original; if (StringUtils.isNotBlank(captionFormatOption)) { Job job = captionService.convert(transcription, "google-speech", captionFormatOption, lineSize); if (!waitForStatus(job).isSuccess()) { throw new WorkflowOperationException("Transcription format conversion job did not complete successfully"); } transcription = MediaPackageElementParser.getFromXml(job.getPayload()); } transcription.setFlavor(flavor); if (StringUtils.isNotBlank(targetTagOption)) { for (String tag : asList(targetTagOption)) { if (StringUtils.isNotBlank(StringUtils.trimToNull(tag))) { transcription.addTag(tag); } } } mediaPackage.add(transcription); String uri = transcription.getURI().toString(); String ext = uri.substring(uri.lastIndexOf(".")); transcription.setURI(workspace.moveTo(transcription.getURI(), mediaPackage.getIdentifier().toString(), transcription.getIdentifier(), "captions." + ext)); } catch (Exception e) { throw new WorkflowOperationException(e); } return createResult(mediaPackage, Action.CONTINUE); }  @Override WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context); void setTranscriptionService(TranscriptionService service); void setWorkspace(Workspace service); void setCaptionService(CaptionService service);  }
@Test public void testStartDfxp() throws Exception { EasyMock.expect(captionService.convert(EasyMock.anyObject(Attachment.class), EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn(job1); EasyMock.replay(captionService); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TRANSCRIPTION_JOB_ID, "transcriptionJob"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_FLAVOR, "captions/timedtext"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_TAG, "tag1,tag2"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_CAPTION_FORMAT, "dfxp"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage updatedMp = result.getMediaPackage(); Catalog[] catalogs = updatedMp.getCatalogs(MediaPackageElementFlavor.parseFlavor("captions/timedtext")); Assert.assertNotNull(catalogs); Assert.assertEquals(1, catalogs.length); Assert.assertNotNull(catalogs[0].getTags()); Assert.assertEquals(2, catalogs[0].getTags().length); Assert.assertEquals("tag1", catalogs[0].getTags()[0]); Assert.assertEquals("tag2", catalogs[0].getTags()[1]); }
@Test public void testStartWebVtt() throws Exception { EasyMock.expect(captionService.convert(EasyMock.anyObject(Attachment.class), EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn(job2); EasyMock.replay(captionService); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TRANSCRIPTION_JOB_ID, "transcriptionJob"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_FLAVOR, "captions/timedtext"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_TAG, "tag1,tag2"); operation.setConfiguration(GoogleSpeechAttachTranscriptionOperationHandler.TARGET_CAPTION_FORMAT, "webvtt"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage updatedMp = result.getMediaPackage(); Attachment[] attachments = updatedMp.getAttachments(MediaPackageElementFlavor.parseFlavor("captions/timedtext")); Assert.assertNotNull(attachments); Assert.assertEquals(1, attachments.length); Assert.assertNotNull(attachments[0].getTags()); Assert.assertEquals(2, attachments[0].getTags().length); Assert.assertEquals("tag1", attachments[0].getTags()[0]); Assert.assertEquals("tag2", attachments[0].getTags()[1]); }
@Test(expected = WorkflowOperationException.class) public void testStartMissingTargetFlavor() throws Exception { operationHandler.start(workflowInstance, null); }