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