Search in sources :

Example 41 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.

the class StartTranscriptionOperationHandlerTest method testStartSelectByTag.

@Test
public void testStartSelectByTag() throws Exception {
    operation.setConfiguration(StartTranscriptionOperationHandler.SOURCE_TAG, "transcript");
    WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals("audioTrack1", capturedTrack.getValue().getIdentifier());
}
Also used : WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Example 42 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.

the class StartTranscriptionOperationHandlerTest method testStartDontSkipFlavor.

@Test
public void testStartDontSkipFlavor() throws Exception {
    operation.setConfiguration(StartTranscriptionOperationHandler.SOURCE_TAG, "transcript");
    // Make sure operation will NOT be skipped if media package does NOT contain the flavor passed
    operation.setConfiguration(StartTranscriptionOperationHandler.SKIP_IF_FLAVOR_EXISTS, "captions/timedtext");
    WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals("audioTrack1", capturedTrack.getValue().getIdentifier());
}
Also used : WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Example 43 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.

the class CommentWorkflowOperationHandler method handleCommentOperation.

/**
 * Determine the type of operation to do on a comment and execute it.
 *
 * @param workflowInstance
 *          The {@link WorkflowInstance} to be handled.
 * @return The result of handling the {@link WorkflowInstance}
 * @throws EventCommentException
 *           Thrown if there is an issue creating, resolving or deleting a comment
 * @throws NotFoundException
 *           Thrown if the comment cannot be found to delete.
 */
private WorkflowOperationResult handleCommentOperation(WorkflowInstance workflowInstance) throws EventCommentException, NotFoundException {
    Date date = new Date();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    Operation action;
    String inputAction = StringUtils.trimToNull(operation.getConfiguration(ACTION));
    if (inputAction == null) {
        action = Operation.create;
    } else {
        action = Operation.valueOf(inputAction.toLowerCase());
    }
    String reason = StringUtils.trimToNull(operation.getConfiguration(REASON));
    String description = StringUtils.trimToNull(operation.getConfiguration(DESCRIPTION));
    switch(action) {
        case create:
            createComment(workflowInstance, reason, description);
            break;
        case resolve:
            resolveComment(workflowInstance, reason, description);
            break;
        case delete:
            deleteComment(workflowInstance, reason, description);
            break;
        default:
            logger.warn("Unknown action '{}' for comment with description '{}' and reason '{}'. It should be one of the following: ", inputAction, description, reason, StringUtils.join(Operation.values(), ","));
    }
    WorkflowOperationResult result = createResult(workflowInstance.getMediaPackage(), Action.CONTINUE, (new Date().getTime()) - date.getTime());
    return result;
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) Date(java.util.Date) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult)

Example 44 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.

the class ExecuteManyWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.debug("Running execute workflow operation with ID {}", operation.getId());
    // Get operation parameters
    String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
    String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
    float load = 1.0f;
    String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
    if (StringUtils.isNotBlank(loadPropertyStr)) {
        try {
            load = Float.parseFloat(loadPropertyStr);
        } catch (NumberFormatException e) {
            String description = StringUtils.trimToEmpty(operation.getDescription());
            logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
        }
    }
    String sourceFlavor = StringUtils.trimToNull(operation.getConfiguration(SOURCE_FLAVOR_PROPERTY));
    String sourceTags = StringUtils.trimToNull(operation.getConfiguration(SOURCE_TAGS_PROPERTY));
    String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
    String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
    MediaPackageElementFlavor matchingFlavor = null;
    if (sourceFlavor != null)
        matchingFlavor = MediaPackageElementFlavor.parseFlavor(sourceFlavor);
    // Unmarshall target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (targetFlavorStr != null)
        targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
    // Unmarshall expected mediapackage element type
    MediaPackageElement.Type expectedType = null;
    if (expectedTypeStr != null) {
        for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
            expectedType = type;
            break;
        }
        if (expectedType == null)
            throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
    }
    List<String> sourceTagList = asList(sourceTags);
    // Select the tracks based on source flavors and tags
    Set<MediaPackageElement> inputSet = new HashSet<>();
    for (MediaPackageElement element : mediaPackage.getElementsByTags(sourceTagList)) {
        MediaPackageElementFlavor elementFlavor = element.getFlavor();
        if (sourceFlavor == null || (elementFlavor != null && elementFlavor.matches(matchingFlavor))) {
            inputSet.add(element);
        }
    }
    if (inputSet.size() == 0) {
        logger.warn("Mediapackage {} has no suitable elements to execute the command {} based on tags {} and flavor {}", mediaPackage, exec, sourceTags, sourceFlavor);
        return createResult(mediaPackage, Action.CONTINUE);
    }
    MediaPackageElement[] inputElements = inputSet.toArray(new MediaPackageElement[inputSet.size()]);
    try {
        Job[] jobs = new Job[inputElements.length];
        MediaPackageElement[] resultElements = new MediaPackageElement[inputElements.length];
        long totalTimeInQueue = 0;
        for (int i = 0; i < inputElements.length; i++) jobs[i] = executeService.execute(exec, params, inputElements[i], outputFilename, expectedType, load);
        // Wait for all jobs to be finished
        if (!waitForStatus(jobs).isSuccess())
            throw new WorkflowOperationException("Execute operation failed");
        // Find which output elements are tracks and inspect them
        HashMap<Integer, Job> jobMap = new HashMap<>();
        for (int i = 0; i < jobs.length; i++) {
            // Add this job's queue time to the total
            totalTimeInQueue += jobs[i].getQueueTime();
            if (StringUtils.trimToNull(jobs[i].getPayload()) != null) {
                resultElements[i] = MediaPackageElementParser.getFromXml(jobs[i].getPayload());
                if (resultElements[i].getElementType() == MediaPackageElement.Type.Track) {
                    jobMap.put(i, inspectionService.inspect(resultElements[i].getURI()));
                }
            } else
                resultElements[i] = inputElements[i];
        }
        if (jobMap.size() > 0) {
            if (!waitForStatus(jobMap.values().toArray(new Job[jobMap.size()])).isSuccess())
                throw new WorkflowOperationException("Execute operation failed in track inspection");
            for (Entry<Integer, Job> entry : jobMap.entrySet()) {
                // Add this job's queue time to the total
                totalTimeInQueue += entry.getValue().getQueueTime();
                resultElements[entry.getKey()] = MediaPackageElementParser.getFromXml(entry.getValue().getPayload());
            }
        }
        for (int i = 0; i < resultElements.length; i++) {
            if (resultElements[i] != inputElements[i]) {
                // Store new element to mediaPackage
                mediaPackage.addDerived(resultElements[i], inputElements[i]);
                // Store new element to mediaPackage
                URI uri = workspace.moveTo(resultElements[i].getURI(), mediaPackage.getIdentifier().toString(), resultElements[i].getIdentifier(), outputFilename);
                resultElements[i].setURI(uri);
                // Set new flavor
                if (targetFlavor != null)
                    resultElements[i].setFlavor(targetFlavor);
            }
            // Set new tags
            if (targetTags != null) {
                // Assume the tags starting with "-" means we want to eliminate such tags form the result element
                for (String tag : asList(targetTags)) {
                    if (tag.startsWith("-"))
                        // We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
                        resultElements[i].removeTag(tag.replaceAll("^-+", ""));
                    else
                        resultElements[i].addTag(tag);
                }
            }
        }
        WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
        logger.debug("Execute operation {} completed", operation.getId());
        return result;
    } catch (ExecuteException e) {
        throw new WorkflowOperationException(e);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException("Some result element couldn't be serialized", e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException("Could not find mediapackage", e);
    } catch (IOException e) {
        throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
    } catch (MediaInspectionException e) {
        throw new WorkflowOperationException("Error inspecting one of the created tracks", e);
    }
}
Also used : HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Example 45 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.

the class AnalyzeTracksWorkflowOperationHandlerTest method testStart.

@Test
public void testStart() throws MediaPackageException, WorkflowOperationException {
    MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    VideoStreamImpl videoStream = new VideoStreamImpl("234");
    videoStream.setFrameWidth(1280);
    videoStream.setFrameHeight(720);
    videoStream.setFrameRate(30.0f);
    TrackImpl track = new TrackImpl();
    track.setFlavor(MediaPackageElementFlavor.parseFlavor("presenter/source"));
    track.addStream(videoStream);
    JobContext jobContext = EasyMock.createMock(JobContext.class);
    EasyMock.replay(jobContext);
    WorkflowOperationInstance operationInstance = EasyMock.createMock(WorkflowOperationInstance.class);
    String[][] config = { { AnalyzeTracksWorkflowOperationHandler.OPT_SOURCE_FLAVOR, "*/source" }, { AnalyzeTracksWorkflowOperationHandler.OPT_VIDEO_ASPECT, "4/3,16/9" } };
    for (String[] cfg : config) {
        EasyMock.expect(operationInstance.getConfiguration(cfg[0])).andReturn(cfg[1]).anyTimes();
    }
    EasyMock.expect(operationInstance.getConfiguration(AnalyzeTracksWorkflowOperationHandler.OPT_FAIL_NO_TRACK)).andReturn("true");
    EasyMock.expect(operationInstance.getConfiguration(AnalyzeTracksWorkflowOperationHandler.OPT_FAIL_NO_TRACK)).andReturn("false").anyTimes();
    EasyMock.replay(operationInstance);
    WorkflowInstance workflowInstance = EasyMock.createMock(WorkflowInstance.class);
    EasyMock.expect(workflowInstance.getMediaPackage()).andReturn(mediaPackage).anyTimes();
    EasyMock.expect(workflowInstance.getId()).andReturn(0L).anyTimes();
    EasyMock.expect(workflowInstance.getCurrentOperation()).andReturn(operationInstance).anyTimes();
    EasyMock.replay(workflowInstance);
    // With no matching track (should fail)
    try {
        operationHandler.start(workflowInstance, jobContext);
        fail();
    } catch (WorkflowOperationException e) {
        logger.info("Fail on no tracks works");
    }
    WorkflowOperationResult workflowOperationResult = operationHandler.start(workflowInstance, jobContext);
    Map<String, String> properties = workflowOperationResult.getProperties();
    assertTrue(properties.isEmpty());
    // With matching track
    mediaPackage.add(track);
    workflowOperationResult = operationHandler.start(workflowInstance, jobContext);
    properties = workflowOperationResult.getProperties();
    String[][] props = { { "presenter_source_media", "true" }, { "presenter_source_audio", "false" }, { "presenter_source_aspect", "16/9" }, { "presenter_source_resolution_y", "720" }, { "presenter_source_resolution_x", "1280" }, { "presenter_source_aspect_snap", "16/9" }, { "presenter_source_video", "true" }, { "presenter_source_framerate", "30.0" } };
    for (String[] prop : props) {
        assertEquals(prop[1], properties.get(prop[0]));
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) JobContext(org.opencastproject.job.api.JobContext) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Aggregations

WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)101 Test (org.junit.Test)85 HashMap (java.util.HashMap)46 MediaPackage (org.opencastproject.mediapackage.MediaPackage)35 Track (org.opencastproject.mediapackage.Track)34 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)28 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)26 ArrayList (java.util.ArrayList)19 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)19 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)17 Job (org.opencastproject.job.api.Job)13 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)11 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)11 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)11 Catalog (org.opencastproject.mediapackage.Catalog)10 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)9 URI (java.net.URI)7 File (java.io.File)6 Attachment (org.opencastproject.mediapackage.Attachment)6 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)6