Search in sources :

Example 26 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class AbstractMetadataCollectionTest method testOrderOfFieldsInputMultipleOrderedFieldsExpectsInCorrectPositions.

@Test
public void testOrderOfFieldsInputMultipleOrderedFieldsExpectsInCorrectPositions() {
    MetadataCollection collection = getAbstractMetadataCollection();
    collection.addField(unorderedOne);
    collection.addField(unorderedTwo);
    collection.addField(unorderedThree);
    collection.addField(first);
    collection.addField(third);
    collection.addField(seventh);
    assertEquals(6, collection.getFields().size());
    assertEquals("A field with an order value of 0 should be first in the list of fields", first, collection.getFields().get(0));
    assertEquals("A field with an order value of 2 should be in that position in the list of fields", third, collection.getFields().get(2));
    assertEquals("A field with an order value of 7 should be in the last position in the list of fields", seventh, collection.getFields().get(5));
    collection = getAbstractMetadataCollection();
    collection.addField(first);
    collection.addField(third);
    collection.addField(seventh);
    collection.addField(unorderedOne);
    collection.addField(unorderedTwo);
    collection.addField(unorderedThree);
    assertEquals(6, collection.getFields().size());
    assertEquals("A field with an order value of 0 should be first in the list of fields", first, collection.getFields().get(0));
    assertEquals("A field with an order value of 2 should be in that position in the list of fields", third, collection.getFields().get(2));
    assertEquals("A field with an order value of 7 should be in the last position in the list of fields", seventh, collection.getFields().get(5));
    collection = getAbstractMetadataCollection();
    collection.addField(third);
    collection.addField(unorderedOne);
    collection.addField(unorderedTwo);
    collection.addField(first);
    collection.addField(seventh);
    collection.addField(unorderedThree);
    assertEquals(6, collection.getFields().size());
    assertEquals("A field with an order value of 0 should be first in the list of fields", first, collection.getFields().get(0));
    assertEquals("A field with an order value of 2 should be in that position in the list of fields", third, collection.getFields().get(2));
    assertEquals("A field with an order value of 7 should be in the last position in the list of fields", seventh, collection.getFields().get(5));
}
Also used : AbstractMetadataCollection(org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Test(org.junit.Test)

Example 27 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection 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 28 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection 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 29 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class SeriesEndpoint method getMetadataByType.

private Response getMetadataByType(String id, String type) throws SearchIndexException {
    Opt<Series> optSeries = indexService.getSeries(id, externalIndex);
    if (optSeries.isNone())
        return ApiResponses.notFound("Cannot find a series with id '%s'.", id);
    // Try the main catalog first as we load it from the index.
    if (typeMatchesSeriesCatalogUIAdapter(type, indexService.getCommonSeriesCatalogUIAdapter())) {
        MetadataCollection collection = getSeriesMetadata(optSeries.get());
        ExternalMetadataUtils.changeSubjectToSubjects(collection);
        ExternalMetadataUtils.changeTypeOrderedTextToText(collection);
        return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, collection.toJSON());
    }
    // Try the other catalogs
    List<SeriesCatalogUIAdapter> catalogUIAdapters = indexService.getSeriesCatalogUIAdapters();
    catalogUIAdapters.remove(indexService.getCommonSeriesCatalogUIAdapter());
    for (SeriesCatalogUIAdapter adapter : catalogUIAdapters) {
        if (typeMatchesSeriesCatalogUIAdapter(type, adapter)) {
            final Opt<MetadataCollection> optSeriesMetadata = adapter.getFields(id);
            if (optSeriesMetadata.isSome()) {
                return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, optSeriesMetadata.get().toJSON());
            }
        }
    }
    return ApiResponses.notFound("Cannot find a catalog with type '%s' for series with id '%s'.", type, id);
}
Also used : Series(org.opencastproject.index.service.impl.index.series.Series) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) SeriesCatalogUIAdapter(org.opencastproject.metadata.dublincore.SeriesCatalogUIAdapter)

Example 30 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class EventsEndpoint method getEventMetadataByType.

private Response getEventMetadataByType(String id, String type) throws IndexServiceException, Exception {
    for (final Event event : indexService.getEvent(id, externalIndex)) {
        Opt<MediaPackageElementFlavor> flavor = getFlavor(type);
        if (flavor.isNone()) {
            return R.badRequest(String.format("Unable to parse type '%s' as a flavor so unable to find the matching catalog.", type));
        }
        // Try the main catalog first as we load it from the index.
        if (flavor.get().equals(eventCatalogUIAdapter.getFlavor())) {
            MetadataCollection collection = EventUtils.getEventMetadata(event, eventCatalogUIAdapter);
            ExternalMetadataUtils.changeSubjectToSubjects(collection);
            ExternalMetadataUtils.removeCollectionList(collection);
            convertStartDateTimeToApiV1(collection);
            ExternalMetadataUtils.changeTypeOrderedTextToText(collection);
            return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, collection.toJSON());
        }
        // Try the other catalogs
        List<EventCatalogUIAdapter> catalogUIAdapters = getEventCatalogUIAdapters();
        catalogUIAdapters.remove(eventCatalogUIAdapter);
        MediaPackage mediaPackage = indexService.getEventMediapackage(event);
        if (catalogUIAdapters.size() > 0) {
            for (EventCatalogUIAdapter catalogUIAdapter : catalogUIAdapters) {
                if (flavor.get().equals(catalogUIAdapter.getFlavor())) {
                    MetadataCollection fields = catalogUIAdapter.getFields(mediaPackage);
                    ExternalMetadataUtils.removeCollectionList(fields);
                    convertStartDateTimeToApiV1(fields);
                    ExternalMetadataUtils.changeTypeOrderedTextToText(fields);
                    return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, fields.toJSON());
                }
            }
        }
        return ApiResponses.notFound("Cannot find a catalog with type '%s' for event with id '%s'.", type, id);
    }
    return ApiResponses.notFound("Cannot find an event with id '%s'.", id);
}
Also used : CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Aggregations

MetadataCollection (org.opencastproject.metadata.dublincore.MetadataCollection)36 MetadataList (org.opencastproject.index.service.catalog.adapter.MetadataList)16 Test (org.junit.Test)10 CommonEventCatalogUIAdapter (org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter)9 JSONArray (org.json.simple.JSONArray)7 JSONParser (org.json.simple.parser.JSONParser)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)7 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)7 EventCatalogUIAdapter (org.opencastproject.metadata.dublincore.EventCatalogUIAdapter)7 NotFoundException (org.opencastproject.util.NotFoundException)7 Path (javax.ws.rs.Path)6 AbstractMetadataCollection (org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection)6 RestQuery (org.opencastproject.util.doc.rest.RestQuery)6 IOException (java.io.IOException)5 JSONObject (org.json.simple.JSONObject)5 ParseException (org.json.simple.parser.ParseException)5 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)5 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 GET (javax.ws.rs.GET)4