use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class IndexServiceImpl method updateEventMetadata.
@Override
public MetadataList updateEventMetadata(String id, MetadataList metadataList, AbstractSearchIndex index) throws IndexServiceException, SearchIndexException, NotFoundException, UnauthorizedException {
Opt<Event> optEvent = getEvent(id, index);
if (optEvent.isNone())
throw new NotFoundException("Cannot find an event with id " + id);
Event event = optEvent.get();
MediaPackage mediaPackage = getEventMediapackage(event);
Opt<Set<String>> presenters = Opt.none();
Opt<MetadataCollection> eventCatalog = metadataList.getMetadataByAdapter(getCommonEventCatalogUIAdapter());
if (eventCatalog.isSome()) {
presenters = updatePresenters(eventCatalog.get());
}
updateMediaPackageMetadata(mediaPackage, metadataList);
switch(getEventSource(event)) {
case WORKFLOW:
Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
if (workflowInstance.isNone()) {
logger.error("No workflow instance for event {} found!", event.getIdentifier());
throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
}
try {
WorkflowInstance instance = workflowInstance.get();
instance.setMediaPackage(mediaPackage);
updateWorkflowInstance(instance);
} catch (WorkflowException e) {
logger.error("Unable to update workflow event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
throw new IndexServiceException("Unable to update workflow event " + id);
}
break;
case ARCHIVE:
assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
break;
case SCHEDULE:
try {
schedulerService.updateEvent(id, Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), presenters, Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
} catch (SchedulerException e) {
logger.error("Unable to update scheduled event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
throw new IndexServiceException("Unable to update scheduled event " + id);
}
break;
default:
logger.error("Unkown event source!");
}
return metadataList;
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class IndexServiceImpl method removeCatalogByFlavor.
@Override
public void removeCatalogByFlavor(Event event, MediaPackageElementFlavor flavor) throws IndexServiceException, NotFoundException, UnauthorizedException {
MediaPackage mediaPackage = getEventMediapackage(event);
Catalog[] catalogs = mediaPackage.getCatalogs(flavor);
if (catalogs.length == 0) {
throw new NotFoundException(String.format("Cannot find a catalog with flavor '%s' for event with id '%s'.", flavor.toString(), event.getIdentifier()));
}
for (Catalog catalog : catalogs) {
mediaPackage.remove(catalog);
}
switch(getEventSource(event)) {
case WORKFLOW:
Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
if (workflowInstance.isNone()) {
logger.error("No workflow instance for event {} found!", event.getIdentifier());
throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
}
try {
WorkflowInstance instance = workflowInstance.get();
instance.setMediaPackage(mediaPackage);
updateWorkflowInstance(instance);
} catch (WorkflowException e) {
logger.error("Unable to remove catalog with flavor {} by updating workflow event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException("Unable to update workflow event " + event.getIdentifier());
}
break;
case ARCHIVE:
assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
break;
case SCHEDULE:
try {
schedulerService.updateEvent(event.getIdentifier(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
} catch (SchedulerException e) {
logger.error("Unable to remove catalog with flavor {} by updating scheduled event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException("Unable to update scheduled event " + event.getIdentifier());
}
break;
default:
throw new IndexServiceException(String.format("Unable to handle event source type '%s'", getEventSource(event)));
}
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class IndexServiceImpl method getEventMediapackage.
@Override
public MediaPackage getEventMediapackage(Event event) throws IndexServiceException {
switch(getEventSource(event)) {
case WORKFLOW:
Opt<WorkflowInstance> currentWorkflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
if (currentWorkflowInstance.isNone()) {
logger.error("No workflow instance for event {} found!", event.getIdentifier());
throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
}
return currentWorkflowInstance.get().getMediaPackage();
case ARCHIVE:
final AQueryBuilder q = assetManager.createQuery();
final AResult r = q.select(q.snapshot()).where(q.mediaPackageId(event.getIdentifier()).and(q.version().isLatest())).run();
if (r.getSize() > 0) {
logger.debug("Found event in archive with id {}", event.getIdentifier());
return enrich(r).getSnapshots().head2().getMediaPackage();
}
logger.error("No event with id {} found from archive!", event.getIdentifier());
throw new IndexServiceException("No archived event found with id " + event.getIdentifier());
case SCHEDULE:
try {
MediaPackage mediaPackage = schedulerService.getMediaPackage(event.getIdentifier());
logger.debug("Found event in scheduler with id {}", event.getIdentifier());
return mediaPackage;
} catch (NotFoundException e) {
logger.error("No scheduled event with id {} found!", event.getIdentifier());
throw new IndexServiceException(e.getMessage(), e);
} catch (UnauthorizedException e) {
logger.error("Unauthorized to get event with id {} from scheduler because {}", event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException(e.getMessage(), e);
} catch (SchedulerException e) {
logger.error("Unable to get event with id {} from scheduler because {}", event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException(e.getMessage(), e);
}
default:
throw new IllegalStateException("Unknown event type!");
}
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class EventsLoader method addWorkflowEntry.
private void addWorkflowEntry(MediaPackage mediaPackage) throws Exception {
WorkflowDefinition def = workflowService.getWorkflowDefinitionById("full");
WorkflowInstance workflowInstance = new WorkflowInstanceImpl(def, mediaPackage, null, securityService.getUser(), securityService.getOrganization(), new HashMap<String, String>());
workflowInstance.setState(WorkflowState.SUCCEEDED);
String xml = WorkflowParser.toXml(workflowInstance);
// create job
Job job = serviceRegistry.createJob(WorkflowService.JOB_TYPE, "START_WORKFLOW", null, null, false);
job.setStatus(Status.FINISHED);
job.setPayload(xml);
job = serviceRegistry.updateJob(job);
workflowInstance.setId(job.getId());
workflowService.update(workflowInstance);
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class AbstractEventEndpoint method workflowAction.
@PUT
@Path("{eventId}/workflows/{workflowId}/action/{action}")
@RestQuery(name = "workflowAction", description = "Performs the given action for the given workflow.", returnDescription = "", pathParameters = { @RestParameter(name = "eventId", description = "The id of the media package", isRequired = true, type = RestParameter.Type.STRING), @RestParameter(name = "workflowId", description = "The id of the workflow", isRequired = true, type = RestParameter.Type.STRING), @RestParameter(name = "action", description = "The action to take: STOP, RETRY or NONE (abort processing)", isRequired = true, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "Workflow resumed."), @RestResponse(responseCode = SC_NOT_FOUND, description = "Event or workflow instance not found."), @RestResponse(responseCode = SC_BAD_REQUEST, description = "Invalid action entered."), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to perform the action. Maybe you need to authenticate."), @RestResponse(responseCode = SC_INTERNAL_SERVER_ERROR, description = "An exception occurred.") })
public Response workflowAction(@PathParam("eventId") String id, @PathParam("workflowId") long wfId, @PathParam("action") String action) {
if (StringUtils.isEmpty(id) || StringUtils.isEmpty(action)) {
return badRequest();
}
try {
final Opt<Event> optEvent = getIndexService().getEvent(id, getIndex());
if (optEvent.isNone()) {
return notFound("Cannot find an event with id '%s'.", id);
}
final WorkflowInstance wfInstance = getWorkflowService().getWorkflowById(wfId);
if (!wfInstance.getMediaPackage().getIdentifier().toString().equals(id)) {
return badRequest(String.format("Workflow %s is not associated to event %s", wfId, id));
}
if (RetryStrategy.NONE.toString().equalsIgnoreCase(action) || RetryStrategy.RETRY.toString().equalsIgnoreCase(action)) {
getWorkflowService().resume(wfId, Collections.singletonMap("retryStrategy", action));
return ok();
}
if (WORKFLOW_ACTION_STOP.equalsIgnoreCase(action)) {
getWorkflowService().stop(wfId);
return ok();
}
return badRequest("Action not supported: " + action);
} catch (NotFoundException e) {
return notFound("Workflow not found: '%d'.", wfId);
} catch (IllegalStateException e) {
return badRequest(String.format("Action %s not allowed for current workflow state. EventId: %s", action, id));
} catch (UnauthorizedException e) {
return forbidden();
} catch (Exception e) {
return serverError();
}
}
Aggregations