Search in sources :

Example 6 with MetadataCollection

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

the class EventHttpServletRequest method setStartDateAndTimeIfUnset.

/**
 * Set the start date and time to the current date & time if it hasn't been set through the api call.
 *
 * @param metadataList
 *          The metadata list created from the json request to create a new event
 */
private static void setStartDateAndTimeIfUnset(MetadataList metadataList) {
    Opt<MetadataCollection> optCommonEventCollection = metadataList.getMetadataByFlavor(MediaPackageElements.EPISODE.toString());
    if (optCommonEventCollection.isSome()) {
        MetadataCollection commonEventCollection = optCommonEventCollection.get();
        MetadataField<?> startDate = commonEventCollection.getOutputFields().get("startDate");
        if (!startDate.isUpdated()) {
            SimpleDateFormat utcDateFormat = new SimpleDateFormat(startDate.getPattern().get());
            utcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            String currentDate = utcDateFormat.format(new DateTime(DateTimeZone.UTC).toDate());
            commonEventCollection.removeField(startDate);
            commonEventCollection.addField(MetadataField.copyMetadataFieldWithValue(startDate, currentDate));
        }
    }
}
Also used : MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) SimpleDateFormat(java.text.SimpleDateFormat) DateTime(org.joda.time.DateTime)

Example 7 with MetadataCollection

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

the class EventHttpServletRequest method deserializeMetadataList.

/**
 * Change the simplified fields of key values provided to the external api into a {@link MetadataList}.
 *
 * @param json
 *          The json string that contains an array of metadata field lists for the different catalogs.
 * @param startDatePattern
 *          The pattern to use to parse the start date from the json payload.
 * @param startTimePattern
 *          The pattern to use to parse the start time from the json payload.
 * @return A {@link MetadataList} with the fields populated with the values provided.
 * @throws ParseException
 *           Thrown if unable to parse the json string.
 * @throws NotFoundException
 *           Thrown if unable to find the catalog or field that the json refers to.
 */
protected static MetadataList deserializeMetadataList(String json, List<EventCatalogUIAdapter> catalogAdapters, Opt<String> startDatePattern, Opt<String> startTimePattern) throws ParseException, NotFoundException, java.text.ParseException {
    MetadataList metadataList = new MetadataList();
    JSONParser parser = new JSONParser();
    JSONArray jsonCatalogs = (JSONArray) parser.parse(json);
    for (int i = 0; i < jsonCatalogs.size(); i++) {
        JSONObject catalog = (JSONObject) jsonCatalogs.get(i);
        if (catalog.get("flavor") == null || StringUtils.isBlank(catalog.get("flavor").toString())) {
            throw new IllegalArgumentException("Unable to create new event as no flavor was given for one of the metadata collections");
        }
        String flavorString = catalog.get("flavor").toString();
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
        MetadataCollection collection = null;
        EventCatalogUIAdapter adapter = null;
        for (EventCatalogUIAdapter eventCatalogUIAdapter : catalogAdapters) {
            if (eventCatalogUIAdapter.getFlavor().equals(flavor)) {
                adapter = eventCatalogUIAdapter;
                collection = eventCatalogUIAdapter.getRawFields();
            }
        }
        if (collection == null) {
            throw new IllegalArgumentException(String.format("Unable to find an EventCatalogUIAdapter with Flavor '%s'", flavorString));
        }
        String fieldsJson = catalog.get("fields").toString();
        if (StringUtils.trimToNull(fieldsJson) != null) {
            Map<String, String> fields = RequestUtils.getKeyValueMap(fieldsJson);
            for (String key : fields.keySet()) {
                if ("subjects".equals(key)) {
                    // Handle the special case of allowing subjects to be an array.
                    MetadataField<?> field = collection.getOutputFields().get(DublinCore.PROPERTY_SUBJECT.getLocalName());
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id 'subject' from Catalog with Flavor '%s'.", flavorString));
                    }
                    collection.removeField(field);
                    try {
                        JSONArray subjects = (JSONArray) parser.parse(fields.get(key));
                        collection.addField(MetadataField.copyMetadataFieldWithValue(field, StringUtils.join(subjects.iterator(), ",")));
                    } catch (ParseException e) {
                        throw new IllegalArgumentException(String.format("Unable to parse the 'subjects' metadata array field because: %s", e.toString()));
                    }
                } 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);
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                    }
                    SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(startDatePattern.getOr(field.getPattern().get()));
                    SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
                    DateTime newStartDate = new DateTime(apiSdf.parse(fields.get(key)), DateTimeZone.UTC);
                    if (field.getValue().isSome()) {
                        DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
                        newStartDate = oldStartDate.withDate(newStartDate.year().get(), newStartDate.monthOfYear().get(), newStartDate.dayOfMonth().get());
                    }
                    collection.removeField(field);
                    collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(newStartDate.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");
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", "startDate", flavorString));
                    }
                    SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(startTimePattern.getOr("HH:mm"));
                    SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
                    DateTime newStartDate = new DateTime(apiSdf.parse(fields.get(key)), DateTimeZone.UTC);
                    if (field.getValue().isSome()) {
                        DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
                        newStartDate = oldStartDate.withTime(newStartDate.hourOfDay().get(), newStartDate.minuteOfHour().get(), newStartDate.secondOfMinute().get(), newStartDate.millisOfSecond().get());
                    }
                    collection.removeField(field);
                    collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(newStartDate.toDate())));
                } else {
                    MetadataField<?> field = collection.getOutputFields().get(key);
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                    }
                    collection.removeField(field);
                    collection.addField(MetadataField.copyMetadataFieldWithValue(field, fields.get(key)));
                }
            }
        }
        metadataList.add(adapter, collection);
    }
    setStartDateAndTimeIfUnset(metadataList);
    return metadataList;
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MetadataField(org.opencastproject.metadata.dublincore.MetadataField) DateTime(org.joda.time.DateTime) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) JSONObject(org.json.simple.JSONObject) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) ParseException(org.json.simple.parser.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with MetadataCollection

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

the class SeriesEndpoint method getSeriesMetadata.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{seriesId}/metadata.json")
@RestQuery(name = "getseriesmetadata", description = "Returns the series metadata as JSON", returnDescription = "Returns the series metadata as JSON", pathParameters = { @RestParameter(name = "seriesId", isRequired = true, description = "The series identifier", type = STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "The series metadata as JSON."), @RestResponse(responseCode = SC_NOT_FOUND, description = "The series has not been found"), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "If the current user is not authorized to perform this action") })
public Response getSeriesMetadata(@PathParam("seriesId") String series) throws UnauthorizedException, NotFoundException, SearchIndexException {
    Opt<Series> optSeries = indexService.getSeries(series, searchIndex);
    if (optSeries.isNone())
        return notFound("Cannot find a series with id '%s'.", series);
    MetadataList metadataList = new MetadataList();
    List<SeriesCatalogUIAdapter> catalogUIAdapters = indexService.getSeriesCatalogUIAdapters();
    catalogUIAdapters.remove(indexService.getCommonSeriesCatalogUIAdapter());
    for (SeriesCatalogUIAdapter adapter : catalogUIAdapters) {
        final Opt<MetadataCollection> optSeriesMetadata = adapter.getFields(series);
        if (optSeriesMetadata.isSome()) {
            metadataList.add(adapter.getFlavor(), adapter.getUITitle(), optSeriesMetadata.get());
        }
    }
    metadataList.add(indexService.getCommonSeriesCatalogUIAdapter(), getSeriesMetadata(optSeries.get()));
    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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 9 with MetadataCollection

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

the class AbstractMetadataCollectionTest method testOrderOfFieldsInputDuplicateOrderValueExpectsBothInserted.

@Test
public void testOrderOfFieldsInputDuplicateOrderValueExpectsBothInserted() {
    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("New first", Opt.<String>none(), "New first", false, false, Opt.<Boolean>none(), Opt.<Map<String, String>>none(), Opt.<String>none(), Opt.some(0), Opt.<String>none());
    collection.addField(newFirst);
    assertEquals(7, collection.getFields().size());
    assertTrue("A field with an order value of 0 should be first in the list of fields", first == collection.getFields().get(0) || newFirst == collection.getFields().get(0));
    assertTrue("A field with an order value of 0 should be second in the list of fields", first == collection.getFields().get(1) || newFirst == collection.getFields().get(1));
    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(6));
}
Also used : AbstractMetadataCollection(org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Test(org.junit.Test)

Example 10 with MetadataCollection

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

the class AbstractMetadataCollectionTest method testOrderOfFields.

@Test
public void testOrderOfFields() {
    MetadataCollection collection = getAbstractMetadataCollection();
    // Add a single field that has an index greater than 0.
    collection.addField(third);
    assertEquals(1, collection.getFields().size());
    assertEquals(third, collection.getFields().get(0));
}
Also used : AbstractMetadataCollection(org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Test(org.junit.Test)

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