Search in sources :

Example 41 with JValue

use of com.entwinemedia.fn.data.json.JValue in project opencast by opencast.

the class MetadataField method createTemporalMetadata.

private static MetadataField<String> createTemporalMetadata(String inputID, Opt<String> outputID, String label, boolean readOnly, boolean required, final String pattern, final Type type, final JsonType jsonType, Opt<Integer> order, Opt<String> namespace) {
    if (StringUtils.isBlank(pattern)) {
        throw new IllegalArgumentException("For temporal metadata field " + inputID + " of type " + type + " there needs to be a pattern.");
    }
    final SimpleDateFormat dateFormat = getSimpleDateFormatter(pattern);
    Fn<Object, String> jsonToDateString = new Fn<Object, String>() {

        @Override
        public String apply(Object value) {
            String date = (String) value;
            if (StringUtils.isBlank(date))
                return "";
            try {
                dateFormat.parse(date);
            } catch (java.text.ParseException e) {
                logger.error("Not able to parse date string {}: {}", value, getMessage(e));
                return null;
            }
            return date;
        }
    };
    Fn<Opt<String>, JValue> dateToJSON = new Fn<Opt<String>, JValue>() {

        @Override
        public JValue apply(Opt<String> periodEncodedString) {
            if (periodEncodedString.isNone() || StringUtils.isBlank(periodEncodedString.get())) {
                return Jsons.BLANK;
            }
            // Try to parse the metadata as DCIM metadata.
            DCMIPeriod p = EncodingSchemeUtils.decodePeriod(periodEncodedString.get());
            if (p != null) {
                return v(dateFormat.format(p.getStart()), Jsons.BLANK);
            }
            // Not DCIM metadata so it might already be formatted (given from the front and is being returned there
            try {
                dateFormat.parse(periodEncodedString.get());
                return v(periodEncodedString.get(), Jsons.BLANK);
            } catch (Exception e) {
                logger.error("Unable to parse temporal metadata '{}' as either DCIM data or a formatted date using pattern {} because: {}", periodEncodedString.get(), pattern, getStackTrace(e));
                throw new IllegalArgumentException(e);
            }
        }
    };
    MetadataField<String> temporalStart = new MetadataField<>(inputID, outputID, label, readOnly, required, null, Opt.none(), type, jsonType, Opt.<Map<String, String>>none(), Opt.<String>none(), dateToJSON, jsonToDateString, order, namespace);
    temporalStart.setPattern(Opt.some(pattern));
    return temporalStart;
}
Also used : Fn(com.entwinemedia.fn.Fn) ParseException(org.json.simple.parser.ParseException) Opt(com.entwinemedia.fn.data.Opt) JValue(com.entwinemedia.fn.data.json.JValue) JObject(com.entwinemedia.fn.data.json.JObject) SimpleDateFormat(java.text.SimpleDateFormat)

Example 42 with JValue

use of com.entwinemedia.fn.data.json.JValue in project opencast by opencast.

the class MetadataList method toJSON.

public JValue toJSON() {
    List<JValue> catalogs = new ArrayList<>();
    for (Entry<String, Tuple<String, MetadataCollection>> metadata : metadataList.entrySet()) {
        List<Field> fields = new ArrayList<>();
        MetadataCollection metadataCollection = metadata.getValue().getB();
        if (!Locked.NONE.equals(locked)) {
            fields.add(f(KEY_METADATA_LOCKED, v(locked.getValue())));
            makeMetadataCollectionReadOnly(metadataCollection);
        }
        fields.add(f(KEY_METADATA_FLAVOR, v(metadata.getKey())));
        fields.add(f(KEY_METADATA_TITLE, v(metadata.getValue().getA())));
        fields.add(f(KEY_METADATA_FIELDS, metadataCollection.toJSON()));
        catalogs.add(obj(fields));
    }
    return arr(catalogs);
}
Also used : MetadataField(org.opencastproject.metadata.dublincore.MetadataField) Field(com.entwinemedia.fn.data.json.Field) JValue(com.entwinemedia.fn.data.json.JValue) ArrayList(java.util.ArrayList) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Tuple(org.opencastproject.util.data.Tuple)

Aggregations

JValue (com.entwinemedia.fn.data.json.JValue)42 ArrayList (java.util.ArrayList)31 Path (javax.ws.rs.Path)25 GET (javax.ws.rs.GET)24 RestQuery (org.opencastproject.util.doc.rest.RestQuery)24 Produces (javax.ws.rs.Produces)22 Field (com.entwinemedia.fn.data.json.Field)10 SortCriterion (org.opencastproject.matterhorn.search.SortCriterion)10 Date (java.util.Date)8 WebApplicationException (javax.ws.rs.WebApplicationException)8 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)8 JObject (com.entwinemedia.fn.data.json.JObject)6 Fn (com.entwinemedia.fn.Fn)5 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)5 Event (org.opencastproject.index.service.impl.index.event.Event)5 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)5 NotFoundException (org.opencastproject.util.NotFoundException)5 Opt (com.entwinemedia.fn.data.Opt)4 JobEndpointException (org.opencastproject.adminui.exception.JobEndpointException)4 Series (org.opencastproject.index.service.impl.index.series.Series)4