Search in sources :

Example 1 with Sort

use of org.opencastproject.workflow.api.WorkflowQuery.Sort 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();
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Sort(org.opencastproject.workflow.api.WorkflowQuery.Sort) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 2 with Sort

use of org.opencastproject.workflow.api.WorkflowQuery.Sort in project opencast by opencast.

the class JobEndpoint method getTasks.

@GET
@Path("tasks.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of tasks", name = "tasks", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "status", 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 = "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 = "workflow", isRequired = false, description = "Filter results by workflow definition.", type = STRING), @RestParameter(name = "operation", 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.  The suffix must be :ASC for ascending or :DESC for descending sort order (e.g. TITLE:DESC).", type = STRING) }, reponses = { @RestResponse(description = "Returns the list of tasks from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of tasks as JSON")
public Response getTasks(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("status") 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("title") String title, @QueryParam("subject") String subject, @QueryParam("workflowdefinition") String workflowDefinitionId, @QueryParam("mp") String mediapackageId, @QueryParam("operation") List<String> currentOperations, @QueryParam("sort") String sort, @Context HttpHeaders headers) throws JobEndpointException {
    WorkflowQuery query = new WorkflowQuery();
    query.withStartPage(offset);
    query.withCount(limit);
    // Add filters
    query.withText(text);
    query.withSeriesId(seriesId);
    query.withSeriesTitle(seriesTitle);
    query.withSubject(subject);
    query.withMediaPackage(mediapackageId);
    query.withCreator(creator);
    query.withContributor(contributor);
    try {
        query.withDateAfter(SolrUtils.parseDate(fromDate));
    } catch (ParseException e) {
        logger.error("Not able to parse the date {}: {}", fromDate, e.getMessage());
    }
    try {
        query.withDateBefore(SolrUtils.parseDate(toDate));
    } catch (ParseException e) {
        logger.error("Not able to parse the date {}: {}", fromDate, e.getMessage());
    }
    query.withLanguage(language);
    query.withTitle(title);
    query.withWorkflowDefintion(workflowDefinitionId);
    if (states != null && states.size() > 0) {
        try {
            for (String state : states) {
                if (StringUtils.isBlank(state)) {
                    continue;
                } else if (state.startsWith(NEGATE_PREFIX)) {
                    query.withoutState(WorkflowState.valueOf(state.substring(1).toUpperCase()));
                } else {
                    query.withState(WorkflowState.valueOf(state.toUpperCase()));
                }
            }
        } catch (IllegalArgumentException e) {
            logger.debug("Unknown workflow state.", e);
        }
    }
    if (currentOperations != null && currentOperations.size() > 0) {
        for (String op : currentOperations) {
            if (StringUtils.isBlank(op)) {
                continue;
            }
            if (op.startsWith(NEGATE_PREFIX)) {
                query.withoutCurrentOperation(op.substring(1));
            } else {
                query.withCurrentOperation(op);
            }
        }
    }
    // Sorting
    if (StringUtils.isNotBlank(sort)) {
        try {
            SortCriterion sortCriterion = RestUtils.parseSortQueryParameter(sort).iterator().next();
            Sort sortKey = Sort.valueOf(sortCriterion.getFieldName().toUpperCase());
            boolean ascending = SearchQuery.Order.Ascending == sortCriterion.getOrder() || SearchQuery.Order.None == sortCriterion.getOrder();
            query.withSort(sortKey, ascending);
        } catch (WebApplicationException ex) {
            logger.warn("Failed to parse sort criterion \"{}\", invalid format.", sort);
        } catch (IllegalArgumentException ex) {
            logger.warn("Can not apply sort criterion \"{}\", no field with this name.", sort);
        }
    }
    JObject json;
    try {
        json = getTasksAsJSON(query);
    } catch (NotFoundException e) {
        return NOT_FOUND;
    }
    return Response.ok(stream(serializer.fn.toJson(json)), MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) WebApplicationException(javax.ws.rs.WebApplicationException) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) Sort(org.opencastproject.workflow.api.WorkflowQuery.Sort) NotFoundException(org.opencastproject.util.NotFoundException) ParseException(java.text.ParseException) JObject(com.entwinemedia.fn.data.json.JObject) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)2 Sort (org.opencastproject.workflow.api.WorkflowQuery.Sort)2 JObject (com.entwinemedia.fn.data.json.JObject)1 ParseException (java.text.ParseException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 SortCriterion (org.opencastproject.matterhorn.search.SortCriterion)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 NotFoundException (org.opencastproject.util.NotFoundException)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)1 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)1