Search in sources :

Example 21 with MetadataList

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

the class SeriesEndpoint method updateSeriesMetadata.

@PUT
@Path("{seriesId}/metadata")
@Produces({ "application/json", "application/v1.0.0+json" })
@RestQuery(name = "updateseriesmetadata", description = "Update a series' metadata of the given type. For a metadata catalog there is the flavor such as 'dublincore/series' and this is the unique type.", returnDescription = "", pathParameters = { @RestParameter(name = "seriesId", description = "The series id", isRequired = true, type = STRING) }, restParameters = { @RestParameter(name = "type", isRequired = true, description = "The type of metadata to update", type = STRING), @RestParameter(name = "metadata", description = "Series metadata as Form param", isRequired = true, type = STRING) }, reponses = { @RestResponse(description = "The series' metadata have been updated.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "The request is invalid or inconsistent.", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "The specified series does not exist.", responseCode = HttpServletResponse.SC_NOT_FOUND) })
public Response updateSeriesMetadata(@HeaderParam("Accept") String acceptHeader, @PathParam("seriesId") String id, @QueryParam("type") String type, @FormParam("metadata") String metadataJSON) throws Exception {
    if (StringUtils.trimToNull(metadataJSON) == null) {
        return RestUtil.R.badRequest("Unable to update metadata for series as the metadata provided is empty.");
    }
    Map<String, String> updatedFields;
    try {
        updatedFields = RequestUtils.getKeyValueMap(metadataJSON);
    } catch (ParseException e) {
        logger.debug("Unable to update series '{}' with metadata type '{}' and content '{}' because: {}", id, type, metadataJSON, ExceptionUtils.getStackTrace(e));
        return RestUtil.R.badRequest(String.format("Unable to parse metadata fields as json from '%s' because '%s'", metadataJSON, ExceptionUtils.getStackTrace(e)));
    } catch (IllegalArgumentException e) {
        return RestUtil.R.badRequest(e.getMessage());
    }
    if (updatedFields == null || updatedFields.size() == 0) {
        return RestUtil.R.badRequest(String.format("Unable to parse metadata fields as json from '%s' because there were no fields to update.", metadataJSON));
    }
    Opt<MetadataCollection> optCollection = Opt.none();
    SeriesCatalogUIAdapter adapter = null;
    Opt<Series> optSeries = indexService.getSeries(id, externalIndex);
    if (optSeries.isNone())
        return ApiResponses.notFound("Cannot find a series with id '%s'.", id);
    MetadataList metadataList = new MetadataList();
    // Try the main catalog first as we load it from the index.
    if (typeMatchesSeriesCatalogUIAdapter(type, indexService.getCommonSeriesCatalogUIAdapter())) {
        optCollection = Opt.some(getSeriesMetadata(optSeries.get()));
        adapter = indexService.getCommonSeriesCatalogUIAdapter();
    } else {
        metadataList.add(indexService.getCommonSeriesCatalogUIAdapter(), getSeriesMetadata(optSeries.get()));
    }
    // Try the other catalogs
    List<SeriesCatalogUIAdapter> catalogUIAdapters = indexService.getSeriesCatalogUIAdapters();
    catalogUIAdapters.remove(indexService.getCommonSeriesCatalogUIAdapter());
    if (catalogUIAdapters.size() > 0) {
        for (SeriesCatalogUIAdapter catalogUIAdapter : catalogUIAdapters) {
            if (typeMatchesSeriesCatalogUIAdapter(type, catalogUIAdapter)) {
                optCollection = catalogUIAdapter.getFields(id);
                adapter = catalogUIAdapter;
            } else {
                Opt<MetadataCollection> current = catalogUIAdapter.getFields(id);
                if (current.isSome()) {
                    metadataList.add(catalogUIAdapter, current.get());
                }
            }
        }
    }
    if (optCollection.isNone()) {
        return ApiResponses.notFound("Cannot find a catalog with type '%s' for series with id '%s'.", type, id);
    }
    MetadataCollection collection = optCollection.get();
    for (String key : updatedFields.keySet()) {
        MetadataField<?> field = collection.getOutputFields().get(key);
        if (field == null) {
            return ApiResponses.notFound("Cannot find a metadata field with id '%s' from event with id '%s' and the metadata type '%s'.", key, id, type);
        } else if (field.isRequired() && StringUtils.isBlank(updatedFields.get(key))) {
            return R.badRequest(String.format("The series metadata field with id '%s' and the metadata type '%s' is required and can not be empty!.", key, type));
        }
        collection.removeField(field);
        collection.addField(MetadataField.copyMetadataFieldWithValue(field, updatedFields.get(key)));
    }
    metadataList.add(adapter, collection);
    indexService.updateAllSeriesMetadata(id, metadataList, externalIndex);
    return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, "");
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) Series(org.opencastproject.index.service.impl.index.series.Series) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) ParseException(org.json.simple.parser.ParseException) SeriesCatalogUIAdapter(org.opencastproject.metadata.dublincore.SeriesCatalogUIAdapter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery) PUT(javax.ws.rs.PUT)

Example 22 with MetadataList

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

the class SeriesEndpoint method getAllMetadata.

private Response getAllMetadata(String id) throws SearchIndexException {
    Opt<Series> optSeries = indexService.getSeries(id, externalIndex);
    if (optSeries.isNone())
        return ApiResponses.notFound("Cannot find a series with id '%s'.", id);
    MetadataList metadataList = new MetadataList();
    List<SeriesCatalogUIAdapter> catalogUIAdapters = indexService.getSeriesCatalogUIAdapters();
    catalogUIAdapters.remove(indexService.getCommonSeriesCatalogUIAdapter());
    for (SeriesCatalogUIAdapter adapter : catalogUIAdapters) {
        final Opt<MetadataCollection> optSeriesMetadata = adapter.getFields(id);
        if (optSeriesMetadata.isSome()) {
            metadataList.add(adapter.getFlavor(), adapter.getUITitle(), optSeriesMetadata.get());
        }
    }
    MetadataCollection collection = getSeriesMetadata(optSeries.get());
    ExternalMetadataUtils.changeSubjectToSubjects(collection);
    ExternalMetadataUtils.changeTypeOrderedTextToText(collection);
    metadataList.add(indexService.getCommonSeriesCatalogUIAdapter(), collection);
    return okJson(metadataList.toJSON());
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) Series(org.opencastproject.index.service.impl.index.series.Series) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) SeriesCatalogUIAdapter(org.opencastproject.metadata.dublincore.SeriesCatalogUIAdapter)

Example 23 with MetadataList

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

the class EventsEndpointTest method testUpdateEventMetadataByType.

@Test
public void testUpdateEventMetadataByType() throws IOException {
    String jsonString = IOUtils.toString(getClass().getResource("/event-metadata-update.json"));
    String expectedJson = IOUtils.toString(getClass().getResource("/event-metadata-update-expected.json"));
    String eventId = TestEventsEndpoint.METADATA_UPDATE_EVENT;
    given().formParam("metadata", jsonString).pathParam("event_id", eventId).queryParam("type", "dublincore/episode").expect().statusCode(SC_NO_CONTENT).when().put(env.host("{event_id}/metadata"));
    MetadataList actualMetadataList = TestEventsEndpoint.getCapturedMetadataList2().getValue();
    assertThat(actualMetadataList.getMetadataByFlavor("dublincore/episode").get().toJSON().toString(), SameJSONAs.sameJSONAs(expectedJson).allowingAnyArrayOrdering());
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) Test(org.junit.Test)

Example 24 with MetadataList

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

the class MetadataListTest method testFromJson.

@Test
public void testFromJson() throws WebApplicationException, Exception {
    InputStream stream = SeriesEndpointTest.class.getResourceAsStream("/metadata-list-input.json");
    InputStreamReader reader = new InputStreamReader(stream);
    JSONArray inputJson = (JSONArray) new JSONParser().parse(reader);
    MetadataCollection abstractMetadataCollection = episodeDublinCoreCatalogUIAdapter.getRawFields();
    MetadataList metadataList = new MetadataList();
    metadataList.add(episodeDublinCoreCatalogUIAdapter, abstractMetadataCollection);
    metadataList.fromJSON(inputJson.toJSONString());
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Test(org.junit.Test)

Example 25 with MetadataList

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

the class MetadataListTest method testLocked.

@Test
public void testLocked() throws WebApplicationException, Exception {
    InputStream stream = SeriesEndpointTest.class.getResourceAsStream("/metadata-list-input-locked.json");
    InputStreamReader reader = new InputStreamReader(stream);
    JSONArray inputJson = (JSONArray) new JSONParser().parse(reader);
    MetadataList metadataList = new MetadataList();
    metadataList.add(episodeDublinCoreCatalogUIAdapter, episodeDublinCoreCatalogUIAdapter.getRawFields());
    metadataList.setLocked(Locked.WORKFLOW_RUNNING);
    assertThat(inputJson.toJSONString(), SameJSONAs.sameJSONAs(new SimpleSerializer().toJson(metadataList.toJSON())).allowingAnyArrayOrdering());
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) SimpleSerializer(com.entwinemedia.fn.data.json.SimpleSerializer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

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