use of com.entwinemedia.fn.data.json.Field in project opencast by opencast.
the class SeriesEndpoint method getSeries.
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("series.json")
@RestQuery(name = "listSeriesAsJson", description = "Returns the series matching the query parameters", returnDescription = "Returns the series search results as JSON", restParameters = { @RestParameter(name = "sortorganizer", isRequired = false, description = "The sort type to apply to the series organizer or organizers either Ascending or Descending.", type = STRING), @RestParameter(name = "sort", description = "The order instructions used to sort the query result. Must be in the form '<field name>:(ASC|DESC)'", isRequired = false, type = STRING), @RestParameter(name = "filter", isRequired = false, description = "The filter used for the query. They should be formated like that: 'filter1:value1,filter2,value2'", type = STRING), @RestParameter(name = "offset", isRequired = false, description = "The page offset", type = INTEGER, defaultValue = "0"), @RestParameter(name = "optedOut", isRequired = false, description = "Whether this series is opted out", type = BOOLEAN), @RestParameter(name = "limit", isRequired = false, description = "Results per page (max 100)", type = INTEGER, defaultValue = "100") }, reponses = { @RestResponse(responseCode = SC_OK, description = "The access control list."), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "If the current user is not authorized to perform this action") })
public Response getSeries(@QueryParam("filter") String filter, @QueryParam("sort") String sort, @QueryParam("offset") int offset, @QueryParam("limit") int limit, @QueryParam("optedOut") Boolean optedOut) throws UnauthorizedException {
try {
logger.debug("Requested series list");
SeriesSearchQuery query = new SeriesSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
Option<String> optSort = Option.option(trimToNull(sort));
if (offset != 0) {
query.withOffset(offset);
}
// If limit is 0, we set the default limit
query.withLimit(limit == 0 ? DEFAULT_LIMIT : limit);
if (optedOut != null)
query.withOptedOut(optedOut);
Map<String, String> filters = RestUtils.parseFilter(filter);
for (String name : filters.keySet()) {
if (SeriesListQuery.FILTER_ACL_NAME.equals(name)) {
query.withManagedAcl(filters.get(name));
} else if (SeriesListQuery.FILTER_CONTRIBUTORS_NAME.equals(name)) {
query.withContributor(filters.get(name));
} else if (SeriesListQuery.FILTER_CREATIONDATE_NAME.equals(name)) {
try {
Tuple<Date, Date> fromAndToCreationRange = RestUtils.getFromAndToDateRange(filters.get(name));
query.withCreatedFrom(fromAndToCreationRange.getA());
query.withCreatedTo(fromAndToCreationRange.getB());
} catch (IllegalArgumentException e) {
return RestUtil.R.badRequest(e.getMessage());
}
} else if (SeriesListQuery.FILTER_CREATOR_NAME.equals(name)) {
query.withCreator(filters.get(name));
} else if (SeriesListQuery.FILTER_TEXT_NAME.equals(name)) {
query.withText(QueryPreprocessor.sanitize(filters.get(name)));
} else if (SeriesListQuery.FILTER_LANGUAGE_NAME.equals(name)) {
query.withLanguage(filters.get(name));
} else if (SeriesListQuery.FILTER_LICENSE_NAME.equals(name)) {
query.withLicense(filters.get(name));
} else if (SeriesListQuery.FILTER_ORGANIZERS_NAME.equals(name)) {
query.withOrganizer(filters.get(name));
} else if (SeriesListQuery.FILTER_SUBJECT_NAME.equals(name)) {
query.withSubject(filters.get(name));
} else if (SeriesListQuery.FILTER_TITLE_NAME.equals(name)) {
query.withTitle(filters.get(name));
}
}
if (optSort.isSome()) {
Set<SortCriterion> sortCriteria = RestUtils.parseSortQueryParameter(optSort.get());
for (SortCriterion criterion : sortCriteria) {
switch(criterion.getFieldName()) {
case SeriesIndexSchema.TITLE:
query.sortByTitle(criterion.getOrder());
break;
case SeriesIndexSchema.CONTRIBUTORS:
query.sortByContributors(criterion.getOrder());
break;
case SeriesIndexSchema.CREATOR:
query.sortByOrganizers(criterion.getOrder());
break;
case SeriesIndexSchema.CREATED_DATE_TIME:
query.sortByCreatedDateTime(criterion.getOrder());
break;
case SeriesIndexSchema.MANAGED_ACL:
query.sortByManagedAcl(criterion.getOrder());
break;
default:
logger.info("Unknown filter criteria {}", criterion.getFieldName());
return Response.status(SC_BAD_REQUEST).build();
}
}
}
logger.trace("Using Query: " + query.toString());
SearchResult<Series> result = searchIndex.getByQuery(query);
if (logger.isDebugEnabled()) {
logger.debug("Found {} results in {} ms", result.getDocumentCount(), result.getSearchTime());
}
List<JValue> series = new ArrayList<>();
for (SearchResultItem<Series> item : result.getItems()) {
List<Field> fields = new ArrayList<>();
Series s = item.getSource();
String sId = s.getIdentifier();
fields.add(f("id", v(sId)));
fields.add(f("optedOut", v(s.isOptedOut())));
fields.add(f("title", v(s.getTitle(), Jsons.BLANK)));
fields.add(f("organizers", arr($(s.getOrganizers()).map(Functions.stringToJValue))));
fields.add(f("contributors", arr($(s.getContributors()).map(Functions.stringToJValue))));
if (s.getCreator() != null) {
fields.add(f("createdBy", v(s.getCreator())));
}
if (s.getCreatedDateTime() != null) {
fields.add(f("creation_date", v(toUTC(s.getCreatedDateTime().getTime()), Jsons.BLANK)));
}
if (s.getLanguage() != null) {
fields.add(f("language", v(s.getLanguage())));
}
if (s.getLicense() != null) {
fields.add(f("license", v(s.getLicense())));
}
if (s.getRightsHolder() != null) {
fields.add(f("rightsHolder", v(s.getRightsHolder())));
}
if (StringUtils.isNotBlank(s.getManagedAcl())) {
fields.add(f("managedAcl", v(s.getManagedAcl())));
}
series.add(obj(fields));
}
logger.debug("Request done");
return okJsonList(series, offset, limit, result.getHitCount());
} catch (Exception e) {
logger.warn("Could not perform search query: {}", ExceptionUtils.getStackTrace(e));
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
use of com.entwinemedia.fn.data.json.Field in project opencast by opencast.
the class EventsEndpoint method eventToJSON.
/**
* Transform an {@link Event} to Json
*
* @param event
* The event to transform into json
* @param withAcl
* Whether to add the acl information for the event
* @param withMetadata
* Whether to add all the metadata for the event
* @param withPublications
* Whether to add the publications
* @param withSignedUrls
* Whether to sign the urls if they are protected by stream security.
* @return The event in json format.
* @throws IndexServiceException
* Thrown if unable to get the metadata for the event.
* @throws SearchIndexException
* Thrown if unable to get event publications from search service
* @throws NotFoundException
* Thrown if unable to find all of the metadata
*/
protected JValue eventToJSON(Event event, Boolean withAcl, Boolean withMetadata, Boolean withPublications, Boolean withSignedUrls) throws IndexServiceException, SearchIndexException, NotFoundException {
List<Field> fields = new ArrayList<>();
if (event.getArchiveVersion() != null)
fields.add(f("archive_version", v(event.getArchiveVersion())));
fields.add(f("created", v(event.getCreated(), Jsons.BLANK)));
fields.add(f("creator", v(event.getCreator(), Jsons.BLANK)));
fields.add(f("contributor", arr($(event.getContributors()).map(Functions.stringToJValue))));
fields.add(f("description", v(event.getDescription(), Jsons.BLANK)));
fields.add(f("has_previews", v(event.hasPreview())));
fields.add(f("identifier", v(event.getIdentifier(), BLANK)));
fields.add(f("location", v(event.getLocation(), BLANK)));
fields.add(f("presenter", arr($(event.getPresenters()).map(Functions.stringToJValue))));
List<JValue> publicationIds = new ArrayList<>();
if (event.getPublications() != null) {
for (Publication publication : event.getPublications()) {
publicationIds.add(v(publication.getChannel()));
}
}
fields.add(f("publication_status", arr(publicationIds)));
fields.add(f("processing_state", v(event.getWorkflowState(), BLANK)));
fields.add(f("start", v(event.getTechnicalStartTime(), BLANK)));
if (event.getTechnicalEndTime() != null) {
long duration = new DateTime(event.getTechnicalEndTime()).getMillis() - new DateTime(event.getTechnicalStartTime()).getMillis();
fields.add(f("duration", v(duration)));
}
if (StringUtils.trimToNull(event.getSubject()) != null) {
fields.add(f("subjects", arr(splitSubjectIntoArray(event.getSubject()))));
} else {
fields.add(f("subjects", arr()));
}
fields.add(f("title", v(event.getTitle(), BLANK)));
if (withAcl != null && withAcl) {
AccessControlList acl = getAclFromEvent(event);
fields.add(f("acl", arr(AclUtils.serializeAclToJson(acl))));
}
if (withMetadata != null && withMetadata) {
try {
Opt<MetadataList> metadata = getEventMetadata(event);
if (metadata.isSome()) {
fields.add(f("metadata", metadata.get().toJSON()));
}
} catch (Exception e) {
logger.error("Unable to get metadata for event '{}' because: {}", event.getIdentifier(), ExceptionUtils.getStackTrace(e));
throw new IndexServiceException("Unable to add metadata to event", e);
}
}
if (withPublications != null && withPublications) {
List<JValue> publications = getPublications(event, withSignedUrls);
fields.add(f("publications", arr(publications)));
}
return obj(fields);
}
use of com.entwinemedia.fn.data.json.Field in project opencast by opencast.
the class EventCommentReply method toJValue.
public JValue toJValue() {
JValue authorObj = obj(f("name", v(author.getName(), BLANK)), f("username", v(author.getUsername())), f("email", v(author.getEmail(), BLANK)));
JValue idValue = com.entwinemedia.fn.data.json.Jsons.ZERO;
if (id.isSome())
idValue = v(id.get());
List<Field> fields = new ArrayList<>();
fields.add(f("id", idValue));
fields.add(f("text", v(text)));
fields.add(f("author", authorObj));
fields.add(f("creationDate", v(DateTimeSupport.toUTC(creationDate.getTime()))));
fields.add(f("modificationDate", v(DateTimeSupport.toUTC(modificationDate.getTime()))));
return obj(fields);
}
use of com.entwinemedia.fn.data.json.Field 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);
}
Aggregations