use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.
the class AbstractMetadataCollectionTest method testOrderOfFieldsInputFieldOrderZeroExpectsAtFront.
@Test
public void testOrderOfFieldsInputFieldOrderZeroExpectsAtFront() {
MetadataCollection collection = getAbstractMetadataCollection();
collection.addField(unorderedOne);
collection.addField(unorderedTwo);
collection.addField(unorderedThree);
collection.addField(first);
assertEquals(4, 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));
collection = getAbstractMetadataCollection();
collection.addField(first);
collection.addField(unorderedOne);
collection.addField(unorderedTwo);
collection.addField(unorderedThree);
assertEquals(4, 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));
collection = getAbstractMetadataCollection();
collection.addField(unorderedOne);
collection.addField(first);
collection.addField(unorderedTwo);
collection.addField(unorderedThree);
assertEquals(4, 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));
}
use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.
the class AbstractMetadataCollectionTest method testAddExistingFieldInputAlreadyExistingFieldExpectsOnlyOneFieldFromGetFields.
@Test
public void testAddExistingFieldInputAlreadyExistingFieldExpectsOnlyOneFieldFromGetFields() {
MetadataCollection collection = getAbstractMetadataCollection();
collection.addField(unorderedOne);
collection.addField(unorderedTwo);
collection.addField(unorderedThree);
collection.addField(first);
collection.addField(third);
collection.addField(seventh);
MetadataField<String> newFirst = MetadataField.createTextMetadataField("first", Opt.<String>none(), "first", false, false, Opt.<Boolean>none(), Opt.<Map<String, String>>none(), Opt.<String>none(), Opt.some(0), Opt.<String>none());
String value = "Hello";
newFirst.setValue(value);
collection.addField(newFirst);
int numberOfFirsts = 0;
Opt<String> valueFound = Opt.none();
for (MetadataField<?> field : collection.getFields()) {
if (field.getInputID().equals(FIRST_ID)) {
numberOfFirsts++;
if (field.getValue().isSome() && field.getValue().get() instanceof String) {
valueFound = Opt.some((String) field.getValue().get());
}
}
}
assertEquals("There should only be one field called first in the collection.", 1, numberOfFirsts);
assertTrue("The value has been set so it should be in the collection.", valueFound.isSome());
assertEquals("There should only be one field called first in the collection.", value, valueFound.get());
}
use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.
the class EventsEndpoint method updateEventMetadataByType.
@PUT
@Path("{eventId}/metadata")
@Produces({ "application/json", "application/v1.0.0+json" })
@RestQuery(name = "updateeventmetadata", description = "Update the metadata with the matching type of the specified event. 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 = true, description = "The type of metadata to update", type = STRING), @RestParameter(name = "metadata", description = "Metadata catalog in JSON format", isRequired = true, type = STRING) }, reponses = { @RestResponse(description = "The metadata of the given namespace has been updated.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "The request is invalid or inconsistent.", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "The specified event does not exist.", responseCode = HttpServletResponse.SC_NOT_FOUND) })
public Response updateEventMetadataByType(@HeaderParam("Accept") String acceptHeader, @PathParam("eventId") String id, @QueryParam("type") String type, @FormParam("metadata") String metadataJSON) throws Exception {
Map<String, String> updatedFields;
JSONParser parser = new JSONParser();
try {
updatedFields = RequestUtils.getKeyValueMap(metadataJSON);
} catch (ParseException e) {
logger.debug("Unable to update event '{}' 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) {
logger.debug("Unable to update event '{}' with metadata type '{}' and content '{}' because: {}", id, type, metadataJSON, ExceptionUtils.getStackTrace(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<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));
}
MetadataCollection collection = null;
EventCatalogUIAdapter adapter = null;
for (final Event event : indexService.getEvent(id, externalIndex)) {
MetadataList metadataList = new MetadataList();
// Try the main catalog first as we load it from the index.
if (flavor.get().equals(eventCatalogUIAdapter.getFlavor())) {
collection = EventUtils.getEventMetadata(event, eventCatalogUIAdapter);
adapter = eventCatalogUIAdapter;
} else {
metadataList.add(eventCatalogUIAdapter, EventUtils.getEventMetadata(event, eventCatalogUIAdapter));
}
// 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())) {
collection = catalogUIAdapter.getFields(mediaPackage);
adapter = eventCatalogUIAdapter;
} else {
metadataList.add(catalogUIAdapter, catalogUIAdapter.getFields(mediaPackage));
}
}
}
if (collection == null) {
return ApiResponses.notFound("Cannot find a catalog with type '%s' for event with id '%s'.", type, id);
}
for (String key : updatedFields.keySet()) {
if ("subjects".equals(key)) {
MetadataField<?> field = collection.getOutputFields().get(DublinCore.PROPERTY_SUBJECT.getLocalName());
Opt<Response> error = validateField(field, key, id, type, updatedFields);
if (error.isSome()) {
return error.get();
}
collection.removeField(field);
JSONArray subjectArray = (JSONArray) parser.parse(updatedFields.get(key));
collection.addField(MetadataField.copyMetadataFieldWithValue(field, StringUtils.join(subjectArray.iterator(), ",")));
} else if ("startDate".equals(key)) {
// Special handling for start date since in API v1 we expect start date and start time to be separate fields.
MetadataField<String> field = (MetadataField<String>) collection.getOutputFields().get(key);
Opt<Response> error = validateField(field, key, id, type, updatedFields);
if (error.isSome()) {
return error.get();
}
String apiPattern = field.getPattern().get();
if (configuredMetadataFields.containsKey("startDate")) {
apiPattern = configuredMetadataFields.get("startDate").getPattern().getOr(apiPattern);
}
SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(apiPattern);
SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
DateTime newStartDate = new DateTime(apiSdf.parse(updatedFields.get(key)), DateTimeZone.UTC);
DateTime updatedStartDate = oldStartDate.withDate(newStartDate.year().get(), newStartDate.monthOfYear().get(), newStartDate.dayOfMonth().get());
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(updatedStartDate.toDate())));
} else if ("startTime".equals(key)) {
// Special handling for start time since in API v1 we expect start date and start time to be separate fields.
MetadataField<String> field = (MetadataField<String>) collection.getOutputFields().get("startDate");
Opt<Response> error = validateField(field, "startDate", id, type, updatedFields);
if (error.isSome()) {
return error.get();
}
String apiPattern = "HH:mm";
if (configuredMetadataFields.containsKey("startTime")) {
apiPattern = configuredMetadataFields.get("startTime").getPattern().getOr(apiPattern);
}
SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(apiPattern);
SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
DateTime newStartDate = new DateTime(apiSdf.parse(updatedFields.get(key)), DateTimeZone.UTC);
DateTime updatedStartDate = oldStartDate.withTime(newStartDate.hourOfDay().get(), newStartDate.minuteOfHour().get(), newStartDate.secondOfMinute().get(), newStartDate.millisOfSecond().get());
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(updatedStartDate.toDate())));
} else {
MetadataField<?> field = collection.getOutputFields().get(key);
Opt<Response> error = validateField(field, key, id, type, updatedFields);
if (error.isSome()) {
return error.get();
}
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, updatedFields.get(key)));
}
}
metadataList.add(adapter, collection);
indexService.updateEventMetadata(id, metadataList, externalIndex);
return ApiResponses.Json.noContent(ApiVersion.VERSION_1_0_0);
}
return ApiResponses.notFound("Cannot find an event with id '%s'.", id);
}
use of org.opencastproject.metadata.dublincore.MetadataCollection 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);
}
}
use of org.opencastproject.metadata.dublincore.MetadataCollection 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);
}
Aggregations