Search in sources :

Example 11 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class EventsEndpoint method getAllEventMetadata.

@GET
@Path("{eventId}/metadata")
@Produces({ "application/json", "application/v1.0.0+json" })
@RestQuery(name = "geteventmetadata", description = "Returns the event's metadata of the specified type. For a metadata catalog there is the flavor such as 'dublincore/episode' and this is the unique type.", returnDescription = "", pathParameters = { @RestParameter(name = "eventId", description = "The event id", isRequired = true, type = STRING) }, restParameters = { @RestParameter(name = "type", isRequired = false, description = "The type of metadata to get", type = STRING) }, reponses = { @RestResponse(description = "The metadata collection is returned.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "The specified event does not exist.", responseCode = HttpServletResponse.SC_NOT_FOUND) })
public Response getAllEventMetadata(@HeaderParam("Accept") String acceptHeader, @PathParam("eventId") String id, @QueryParam("type") String type) throws Exception {
    if (StringUtils.trimToNull(type) == null) {
        Opt<MetadataList> metadataList = getEventMetadataById(id);
        if (metadataList.isSome()) {
            MetadataList actualList = metadataList.get();
            // API v1 should return a two separate fields for start date and start time. Since those fields were merged in index service, we have to split them up.
            Opt<MetadataCollection> collection = actualList.getMetadataByFlavor("dublincore/episode");
            if (collection.isSome()) {
                convertStartDateTimeToApiV1(collection.get());
                ExternalMetadataUtils.changeTypeOrderedTextToText(collection.get());
            }
            return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, actualList.toJSON());
        } else
            return ApiResponses.notFound("Cannot find an event with id '%s'.", id);
    } else {
        return getEventMetadataByType(id, type);
    }
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 12 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class EventsEndpoint method getEventMetadata.

protected Opt<MetadataList> getEventMetadata(Event event) throws IndexServiceException, Exception {
    MetadataList metadataList = new MetadataList();
    List<EventCatalogUIAdapter> catalogUIAdapters = getEventCatalogUIAdapters();
    catalogUIAdapters.remove(this.eventCatalogUIAdapter);
    MediaPackage mediaPackage = indexService.getEventMediapackage(event);
    if (catalogUIAdapters.size() > 0) {
        for (EventCatalogUIAdapter catalogUIAdapter : catalogUIAdapters) {
            // TODO: This is very slow:
            MetadataCollection fields = catalogUIAdapter.getFields(mediaPackage);
            if (fields != null)
                metadataList.add(catalogUIAdapter, fields);
        }
    }
    // TODO: This is slow:
    MetadataCollection collection = EventUtils.getEventMetadata(event, eventCatalogUIAdapter);
    ExternalMetadataUtils.changeSubjectToSubjects(collection);
    ExternalMetadataUtils.removeCollectionList(collection);
    metadataList.add(eventCatalogUIAdapter, collection);
    if (WorkflowInstance.WorkflowState.RUNNING.toString().equals(event.getWorkflowState())) {
        metadataList.setLocked(Locked.WORKFLOW_RUNNING);
    }
    return Opt.some(metadataList);
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) MediaPackage(org.opencastproject.mediapackage.MediaPackage) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection)

Example 13 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class EventsEndpoint method deserializeMetadataList.

/**
 * Change the simplified fields of key values provided to the external api into a {@link MetadataList}.
 *
 * @param json
 *          The json string that contains an array of metadata field lists for the different catalogs.
 * @return A {@link MetadataList} with the fields populated with the values provided.
 * @throws ParseException
 *           Thrown if unable to parse the json string.
 * @throws NotFoundException
 *           Thrown if unable to find the catalog or field that the json refers to.
 */
protected MetadataList deserializeMetadataList(String json) throws ParseException, NotFoundException {
    MetadataList metadataList = new MetadataList();
    JSONParser parser = new JSONParser();
    JSONArray jsonCatalogs = (JSONArray) parser.parse(json);
    for (int i = 0; i < jsonCatalogs.size(); i++) {
        JSONObject catalog = (JSONObject) jsonCatalogs.get(i);
        String flavorString = catalog.get("flavor").toString();
        if (StringUtils.trimToNull(flavorString) == null) {
            throw new IllegalArgumentException("Unable to create new event as no flavor was given for one of the metadata collections");
        }
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
        MetadataCollection collection = null;
        EventCatalogUIAdapter adapter = null;
        for (EventCatalogUIAdapter eventCatalogUIAdapter : getEventCatalogUIAdapters()) {
            if (eventCatalogUIAdapter.getFlavor().equals(flavor)) {
                adapter = eventCatalogUIAdapter;
                collection = eventCatalogUIAdapter.getRawFields();
            }
        }
        if (collection == null) {
            throw new IllegalArgumentException(String.format("Unable to find an EventCatalogUIAdapter with Flavor '%s'", flavorString));
        }
        String fieldsJson = catalog.get("fields").toString();
        if (StringUtils.trimToNull(fieldsJson) != null) {
            Map<String, String> fields = RequestUtils.getKeyValueMap(fieldsJson);
            for (String key : fields.keySet()) {
                MetadataField<?> field = collection.getOutputFields().get(key);
                if (field == null) {
                    throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                }
                collection.removeField(field);
                collection.addField(MetadataField.copyMetadataFieldWithValue(field, fields.get(key)));
            }
        }
        metadataList.add(adapter, collection);
    }
    return metadataList;
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) JSONObject(org.json.simple.JSONObject) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection)

Example 14 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class EventsEndpointTest method testUpdateEventMetadata.

@Test
public void testUpdateEventMetadata() throws IOException {
    String jsonString = IOUtils.toString(getClass().getResource("/event-update.json"));
    String expectedJson = IOUtils.toString(getClass().getResource("/event-update-expected.json"));
    String eventId = TestEventsEndpoint.UPDATE_EVENT;
    given().multiPart("metadata", jsonString).pathParam("event_id", eventId).expect().statusCode(SC_NO_CONTENT).when().post(env.host("{event_id}"));
    MetadataList actualMetadataList = TestEventsEndpoint.getCapturedMetadataList1().getValue();
    assertThat(actualMetadataList.toJSON().toString(), SameJSONAs.sameJSONAs(expectedJson).allowingAnyArrayOrdering());
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) Test(org.junit.Test)

Example 15 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class SeriesEndpoint method createNewSeries.

@POST
@Path("")
@Produces({ "application/json", "application/v1.0.0+json" })
@RestQuery(name = "createseries", description = "Creates a series.", returnDescription = "", restParameters = { @RestParameter(name = "metadata", isRequired = true, description = "Series metadata", type = STRING), @RestParameter(name = "acl", description = "A collection of roles with their possible action", isRequired = false, type = STRING), @RestParameter(name = "theme", description = "The theme ID to be applied to the series", isRequired = false, type = STRING) }, reponses = { @RestResponse(description = "A new series is created and its identifier is returned in the Location header.", responseCode = HttpServletResponse.SC_CREATED), @RestResponse(description = "The request is invalid or inconsistent..", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "The user doesn't have the rights to create the series.", responseCode = HttpServletResponse.SC_UNAUTHORIZED) })
public Response createNewSeries(@HeaderParam("Accept") String acceptHeader, @FormParam("metadata") String metadataParam, @FormParam("acl") String aclParam, @FormParam("theme") String themeIdParam) throws UnauthorizedException, NotFoundException {
    if (isBlank(metadataParam))
        return R.badRequest("Required parameter 'metadata' is missing or invalid");
    MetadataList metadataList;
    try {
        metadataList = deserializeMetadataList(metadataParam);
    } catch (ParseException e) {
        logger.debug("Unable to parse series metadata '{}' because: {}", metadataParam, ExceptionUtils.getStackTrace(e));
        return R.badRequest(String.format("Unable to parse metadata because '%s'", e.toString()));
    } catch (NotFoundException e) {
        // One of the metadata fields could not be found in the catalogs or one of the catalogs cannot be found.
        return R.badRequest(e.getMessage());
    } catch (IllegalArgumentException e) {
        logger.debug("Unable to create series with metadata '{}' because: {}", metadataParam, ExceptionUtils.getStackTrace(e));
        return R.badRequest(e.getMessage());
    }
    Map<String, String> options = new TreeMap<>();
    Opt<Long> optThemeId = Opt.none();
    if (StringUtils.trimToNull(themeIdParam) != null) {
        try {
            Long themeId = Long.parseLong(themeIdParam);
            optThemeId = Opt.some(themeId);
        } catch (NumberFormatException e) {
            return R.badRequest(String.format("Unable to parse the theme id '%s' into a number", themeIdParam));
        }
    }
    AccessControlList acl;
    try {
        acl = AclUtils.deserializeJsonToAcl(aclParam, false);
    } catch (ParseException e) {
        logger.debug("Unable to parse acl '{}' because: '{}'", aclParam, ExceptionUtils.getStackTrace(e));
        return R.badRequest(String.format("Unable to parse acl '%s' because '%s'", aclParam, e.getMessage()));
    } catch (IllegalArgumentException e) {
        logger.debug("Unable to create new series with acl '{}' because: '{}'", aclParam, ExceptionUtils.getStackTrace(e));
        return R.badRequest(e.getMessage());
    }
    try {
        String seriesId = indexService.createSeries(metadataList, options, Opt.some(acl), optThemeId);
        return ApiResponses.Json.created(VERSION_1_0_0, URI.create(getSeriesUrl(seriesId)), obj(f("identifier", v(seriesId, BLANK))));
    } catch (IndexServiceException e) {
        logger.error("Unable to create series with metadata '{}', acl '{}', theme '{}' because: ", metadataParam, aclParam, themeIdParam, ExceptionUtils.getStackTrace(e));
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) WebApplicationException(javax.ws.rs.WebApplicationException) NotFoundException(org.opencastproject.util.NotFoundException) TreeMap(java.util.TreeMap) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) ParseException(org.json.simple.parser.ParseException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

MetadataList (org.opencastproject.index.service.catalog.adapter.MetadataList)27 MetadataCollection (org.opencastproject.metadata.dublincore.MetadataCollection)15 NotFoundException (org.opencastproject.util.NotFoundException)10 Path (javax.ws.rs.Path)8 JSONArray (org.json.simple.JSONArray)8 JSONParser (org.json.simple.parser.JSONParser)8 RestQuery (org.opencastproject.util.doc.rest.RestQuery)8 ParseException (org.json.simple.parser.ParseException)7 IOException (java.io.IOException)6 Produces (javax.ws.rs.Produces)6 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)6 EventCatalogUIAdapter (org.opencastproject.metadata.dublincore.EventCatalogUIAdapter)6 GET (javax.ws.rs.GET)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 JSONObject (org.json.simple.JSONObject)5 CommonEventCatalogUIAdapter (org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter)5 IngestException (org.opencastproject.ingest.api.IngestException)5 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 AccessControlList (org.opencastproject.security.api.AccessControlList)5 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)5