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));
}
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, "");
}
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());
}
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);
}
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);
}
Aggregations