use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.
the class DefaultsWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Applying default values to {}", workflowInstance.getId());
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
Long id = workflowInstance.getId();
Organization organization = workflowInstance.getOrganization();
String seriesID = workflowInstance.getMediaPackage().getSeries();
// Iterate over all configuration keys
Map<String, String> properties = new HashMap<>();
logger.debug("Getting properties for " + id + " " + organization + " " + seriesID);
for (String key : operation.getConfigurationKeys()) {
String value = workflowInstance.getConfiguration(key);
if (StringUtils.isBlank(value)) {
// Check to see if the default value was set as a preset at the series or organization level
String preset = getPreset(organization, seriesID, key);
if (StringUtils.isNotBlank(preset)) {
properties.put(key, preset);
logger.debug("Configuration key '{}' of workflow {} is set to preset value '{}'", key, id, preset);
} else {
String defaultValue = operation.getConfiguration(key);
properties.put(key, defaultValue);
logger.debug("Configuration key '{}' of workflow {} is set to default value '{}' specified in workflow", key, id, defaultValue);
}
} else {
properties.put(key, value);
logger.debug("Configuration key '{}' of workflow {} is set to '{}' specified in event.", key, id, value);
}
}
return createResult(workflowInstance.getMediaPackage(), properties, Action.CONTINUE, 0);
}
use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.
the class WorkflowRestService method getWorkflowsAsXml.
@GET
@Produces(MediaType.TEXT_XML)
@Path("instances.xml")
@RestQuery(name = "workflowsasxml", description = "List all workflow instances matching the query parameters", returnDescription = "An XML representation of the set of workflows matching these query parameters", restParameters = { @RestParameter(name = "state", isRequired = false, description = "Filter results by workflows' current state", type = STRING), @RestParameter(name = "q", isRequired = false, description = "Filter results by free text query", type = STRING), @RestParameter(name = "seriesId", isRequired = false, description = "Filter results by series identifier", type = STRING), @RestParameter(name = "seriesTitle", isRequired = false, description = "Filter results by series title", type = STRING), @RestParameter(name = "creator", isRequired = false, description = "Filter results by the mediapackage's creator", type = STRING), @RestParameter(name = "contributor", isRequired = false, description = "Filter results by the mediapackage's contributor", type = STRING), @RestParameter(name = "fromdate", isRequired = false, description = "Filter results by workflow start date.", type = STRING), @RestParameter(name = "todate", isRequired = false, description = "Filter results by workflow start date.", type = STRING), @RestParameter(name = "language", isRequired = false, description = "Filter results by mediapackage's language.", type = STRING), @RestParameter(name = "license", isRequired = false, description = "Filter results by mediapackage's license.", type = STRING), @RestParameter(name = "title", isRequired = false, description = "Filter results by mediapackage's title.", type = STRING), @RestParameter(name = "subject", isRequired = false, description = "Filter results by mediapackage's subject.", type = STRING), @RestParameter(name = "workflowdefinition", isRequired = false, description = "Filter results by workflow definition.", type = STRING), @RestParameter(name = "mp", isRequired = false, description = "Filter results by mediapackage identifier.", type = STRING), @RestParameter(name = "op", isRequired = false, description = "Filter results by workflows' current operation.", type = STRING), @RestParameter(name = "sort", isRequired = false, description = "The sort order. May include any " + "of the following: DATE_CREATED, TITLE, SERIES_TITLE, SERIES_ID, MEDIA_PACKAGE_ID, WORKFLOW_DEFINITION_ID, CREATOR, " + "CONTRIBUTOR, LANGUAGE, LICENSE, SUBJECT. Add '_DESC' to reverse the sort order (e.g. TITLE_DESC).", type = STRING), @RestParameter(name = "startPage", isRequired = false, description = "The paging offset", type = INTEGER), @RestParameter(name = "count", isRequired = false, description = "The number of results to return.", type = INTEGER), @RestParameter(name = "compact", isRequired = false, description = "Whether to return a compact version of " + "the workflow instance, with mediapackage elements, workflow and workflow operation configurations and " + "non-current operations removed.", type = STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the workflow set.") })
public // So for now, we disable checkstyle here.
Response getWorkflowsAsXml(@QueryParam("state") List<String> states, @QueryParam("q") String text, @QueryParam("seriesId") String seriesId, @QueryParam("seriesTitle") String seriesTitle, @QueryParam("creator") String creator, @QueryParam("contributor") String contributor, @QueryParam("fromdate") String fromDate, @QueryParam("todate") String toDate, @QueryParam("language") String language, @QueryParam("license") String license, @QueryParam("title") String title, @QueryParam("subject") String subject, @QueryParam("workflowdefinition") String workflowDefinitionId, @QueryParam("mp") String mediapackageId, @QueryParam("op") List<String> currentOperations, @QueryParam("sort") String sort, @QueryParam("startPage") int startPage, @QueryParam("count") int count, @QueryParam("compact") boolean compact) throws Exception {
// CHECKSTYLE:ON
if (count < 1)
count = DEFAULT_LIMIT;
WorkflowQuery q = new WorkflowQuery();
q.withCount(count);
q.withStartPage(startPage);
if (states != null && states.size() > 0) {
try {
for (String state : states) {
if (StringUtils.isBlank(state)) {
continue;
}
if (state.startsWith(NEGATE_PREFIX)) {
q.withoutState(WorkflowState.valueOf(state.substring(1).toUpperCase()));
} else {
q.withState(WorkflowState.valueOf(state.toUpperCase()));
}
}
} catch (IllegalArgumentException e) {
logger.debug("Unknown workflow state.", e);
}
}
q.withText(text);
q.withSeriesId(seriesId);
q.withSeriesTitle(seriesTitle);
q.withSubject(subject);
q.withMediaPackage(mediapackageId);
q.withCreator(creator);
q.withContributor(contributor);
q.withDateAfter(SolrUtils.parseDate(fromDate));
q.withDateBefore(SolrUtils.parseDate(toDate));
q.withLanguage(language);
q.withLicense(license);
q.withTitle(title);
q.withWorkflowDefintion(workflowDefinitionId);
if (currentOperations != null && currentOperations.size() > 0) {
for (String op : currentOperations) {
if (StringUtils.isBlank(op)) {
continue;
}
if (op.startsWith(NEGATE_PREFIX)) {
q.withoutCurrentOperation(op.substring(1));
} else {
q.withCurrentOperation(op);
}
}
}
if (StringUtils.isNotBlank(sort)) {
// Parse the sort field and direction
Sort sortField = null;
if (sort.endsWith(DESCENDING_SUFFIX)) {
String enumKey = sort.substring(0, sort.length() - DESCENDING_SUFFIX.length()).toUpperCase();
try {
sortField = Sort.valueOf(enumKey);
q.withSort(sortField, false);
} catch (IllegalArgumentException e) {
logger.debug("No sort enum matches '{}'", enumKey);
}
} else {
try {
sortField = Sort.valueOf(sort);
q.withSort(sortField);
} catch (IllegalArgumentException e) {
logger.debug("No sort enum matches '{}'", sort);
}
}
}
WorkflowSet set = service.getWorkflowInstances(q);
// Marshalling of a full workflow takes a long time. Therefore, we strip everything that's not needed.
if (compact) {
for (WorkflowInstance instance : set.getItems()) {
// Remove all operations but the current one
WorkflowOperationInstance currentOperation = instance.getCurrentOperation();
List<WorkflowOperationInstance> operations = instance.getOperations();
// instance.getOperations() is a copy
operations.clear();
if (currentOperation != null) {
for (String key : currentOperation.getConfigurationKeys()) {
currentOperation.removeConfiguration(key);
}
operations.add(currentOperation);
}
instance.setOperations(operations);
// Remove all mediapackage elements (but keep the duration)
MediaPackage mediaPackage = instance.getMediaPackage();
Long duration = instance.getMediaPackage().getDuration();
for (MediaPackageElement element : mediaPackage.elements()) {
mediaPackage.remove(element);
}
mediaPackage.setDuration(duration);
}
}
return Response.ok(set).build();
}
use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.
the class WorkflowRestService method getOperationsAsJson.
@SuppressWarnings("unchecked")
protected JSONArray getOperationsAsJson(List<WorkflowOperationInstance> operations) {
JSONArray jsonArray = new JSONArray();
for (WorkflowOperationInstance op : operations) {
JSONObject jsOp = new JSONObject();
jsOp.put("name", op.getTemplate());
jsOp.put("description", op.getDescription());
jsOp.put("state", op.getState().name().toLowerCase());
jsOp.put("configurations", getConfigsAsJson(op));
jsonArray.add(jsOp);
}
return jsonArray;
}
use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.
the class WorkflowOperationWorker method start.
/**
* Starts executing the workflow operation.
*
* @return the workflow operation result
* @throws WorkflowOperationException
* if executing the workflow operation handler fails
* @throws WorkflowException
* if there is a problem processing the workflow
*/
public WorkflowOperationResult start() throws WorkflowOperationException, WorkflowException, UnauthorizedException {
final WorkflowOperationInstance operation = workflow.getCurrentOperation();
// Do we need to execute the operation?
// if
final String executionCondition = operation.getExecutionCondition();
final boolean execute;
if (executionCondition == null) {
execute = true;
} else {
final Result<Boolean> parsed = booleanExpressionEvaluator.eval(executionCondition);
if (parsed.isDefined() && parsed.getRest().isEmpty()) {
execute = parsed.getResult();
} else {
operation.setState(OperationState.FAILED);
throw new WorkflowOperationException(format("Unable to parse execution condition '%s'. Result is '%s'", executionCondition, parsed.toString()));
}
}
operation.setState(OperationState.RUNNING);
service.update(workflow);
try {
WorkflowOperationResult result = null;
if (execute) {
if (handler == null) {
// If there is no handler for the operation, yet we are supposed to run it, we must fail
logger.warn("No handler available to execute operation '{}'", operation.getTemplate());
throw new IllegalStateException("Unable to find a workflow handler for '" + operation.getTemplate() + "'");
}
result = handler.start(workflow, null);
} else {
// Allow for null handlers when we are skipping an operation
if (handler != null) {
result = handler.skip(workflow, null);
result.setAction(Action.SKIP);
}
}
return result;
} catch (Exception e) {
operation.setState(OperationState.FAILED);
if (e instanceof WorkflowOperationException)
throw (WorkflowOperationException) e;
throw new WorkflowOperationException(e);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.
the class WorkflowOperationWorker method resume.
/**
* Resumes a previously suspended workflow operation. Note that only workflow operation handlers that implement
* {@link ResumableWorkflowOperationHandler} can be resumed.
*
* @return the workflow operation result
* @throws WorkflowOperationException
* if executing the workflow operation handler fails
* @throws WorkflowException
* if there is a problem processing the workflow
* @throws IllegalStateException
* if the workflow operation cannot be resumed
*/
public WorkflowOperationResult resume() throws WorkflowOperationException, WorkflowException, IllegalStateException, UnauthorizedException {
WorkflowOperationInstance operation = workflow.getCurrentOperation();
// Make sure we have a (suitable) handler
if (handler == null) {
// If there is no handler for the operation, yet we are supposed to run it, we must fail
logger.warn("No handler available to resume operation '{}'", operation.getTemplate());
throw new IllegalStateException("Unable to find a workflow handler for '" + operation.getTemplate() + "'");
} else if (!(handler instanceof ResumableWorkflowOperationHandler)) {
throw new IllegalStateException("An attempt was made to resume a non-resumable operation");
}
ResumableWorkflowOperationHandler resumableHandler = (ResumableWorkflowOperationHandler) handler;
operation.setState(OperationState.RUNNING);
service.update(workflow);
try {
WorkflowOperationResult result = resumableHandler.resume(workflow, null, properties);
return result;
} catch (Exception e) {
operation.setState(OperationState.FAILED);
if (e instanceof WorkflowOperationException)
throw (WorkflowOperationException) e;
throw new WorkflowOperationException(e);
}
}
Aggregations