use of org.opencastproject.index.service.impl.index.series.SeriesSearchQuery in project opencast by opencast.
the class EventIndexUtils method updateSeriesName.
/**
* A function to update the series title within an event.
*
* @param event
* The event to update the series name in
* @param organization
* The organization for this event and series
* @param user
* The user
* @param searchIndex
* The index to search for the series
* @param tries
* The number of attempts to try to get the series title
* @param sleep
* The amount of time in ms to sleep between attempts to get the series title.
*/
public static void updateSeriesName(Event event, String organization, User user, AbstractSearchIndex searchIndex, int tries, long sleep) throws SearchIndexException {
if (event.getSeriesId() != null) {
for (int i = 1; i <= tries; i++) {
SearchResult<Series> result = searchIndex.getByQuery(new SeriesSearchQuery(organization, user).withoutActions().withIdentifier(event.getSeriesId()));
if (result.getHitCount() > 0) {
event.setSeriesName(result.getItems()[0].getSource().getTitle());
break;
} else {
Integer triesLeft = new Integer(tries - i);
logger.debug("Not able to find the series {} in the search index for the event {}. Will try {} more times.", event.getSeriesId(), event.getIdentifier(), triesLeft);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
logger.warn("Interupted while sleeping before checking for the series being added to the index {}", ExceptionUtils.getStackTrace(e));
}
}
}
}
}
use of org.opencastproject.index.service.impl.index.series.SeriesSearchQuery in project opencast by opencast.
the class ThemesEndpoint method deleteThemeOnSeries.
/**
* Deletes all related series theme entries
*
* @param themeId
* the theme id
*/
private void deleteThemeOnSeries(long themeId) throws UnauthorizedException {
SeriesSearchQuery query = new SeriesSearchQuery(securityService.getOrganization().getId(), securityService.getUser()).withTheme(themeId);
SearchResult<Series> results = null;
try {
results = searchIndex.getByQuery(query);
} catch (SearchIndexException e) {
logger.error("The admin UI Search Index was not able to get the series with theme '{}': {}", themeId, ExceptionUtils.getStackTrace(e));
throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
}
for (SearchResultItem<Series> item : results.getItems()) {
String seriesId = item.getSource().getIdentifier();
try {
seriesService.deleteSeriesProperty(seriesId, SeriesEndpoint.THEME_KEY);
} catch (NotFoundException e) {
logger.warn("Theme {} already deleted on series {}", themeId, seriesId);
} catch (SeriesException e) {
logger.error("Unable to remove theme from series {}: {}", seriesId, ExceptionUtils.getStackTrace(e));
throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
}
}
}
use of org.opencastproject.index.service.impl.index.series.SeriesSearchQuery in project opencast by opencast.
the class ThemesEndpoint method getThemeUsage.
@GET
@Path("{themeId}/usage.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "getThemeUsage", description = "Returns the theme usage by the given id as JSON", returnDescription = "The theme usage as JSON", pathParameters = { @RestParameter(name = "themeId", description = "The theme id", isRequired = true, type = RestParameter.Type.INTEGER) }, reponses = { @RestResponse(description = "Returns the theme usage as JSON", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "Theme with the given id does not exist", responseCode = HttpServletResponse.SC_NOT_FOUND) })
public Response getThemeUsage(@PathParam("themeId") long themeId) throws Exception {
Opt<org.opencastproject.index.service.impl.index.theme.Theme> theme = getTheme(themeId);
if (theme.isNone())
return notFound("Cannot find a theme with id {}", themeId);
SeriesSearchQuery query = new SeriesSearchQuery(securityService.getOrganization().getId(), securityService.getUser()).withTheme(themeId);
SearchResult<Series> results = null;
try {
results = searchIndex.getByQuery(query);
} catch (SearchIndexException e) {
logger.error("The admin UI Search Index was not able to get the series with theme '{}': {}", themeId, ExceptionUtils.getStackTrace(e));
return RestUtil.R.serverError();
}
List<JValue> seriesValues = new ArrayList<JValue>();
for (SearchResultItem<Series> item : results.getItems()) {
Series series = item.getSource();
seriesValues.add(obj(f("id", v(series.getIdentifier())), f("title", v(series.getTitle()))));
}
return okJson(obj(f("series", arr(seriesValues))));
}
use of org.opencastproject.index.service.impl.index.series.SeriesSearchQuery in project opencast by opencast.
the class TestSeriesEndpoint method setupIndex.
@SuppressWarnings({ "unchecked" })
private void setupIndex() throws SearchIndexException, IOException, IllegalStateException, ParseException {
long time = DateTimeSupport.fromUTC("2014-04-27T14:35:50Z");
Series series1 = createSeries("1", "title 1", "contributor 1", "organizer 1", time, 1L);
time = DateTimeSupport.fromUTC("2014-04-28T14:35:50Z");
Series series2 = createSeries("2", "title 2", "contributor 2", "organizer 2", time, null);
time = DateTimeSupport.fromUTC("2014-04-29T14:35:50Z");
Series series3 = createSeries("3", "title 3", "contributor 3", "organizer 3", time, null);
org.opencastproject.index.service.impl.index.theme.Theme theme1 = new org.opencastproject.index.service.impl.index.theme.Theme(1L, new DefaultOrganization().getId());
theme1.setName("theme-1-name");
theme1.setDescription("theme-1-description");
SearchResultItem<Series> item1 = EasyMock.createMock(SearchResultItem.class);
EasyMock.expect(item1.getSource()).andReturn(series1).anyTimes();
SearchResultItem<Series> item2 = EasyMock.createMock(SearchResultItem.class);
EasyMock.expect(item2.getSource()).andReturn(series2).anyTimes();
SearchResultItem<Series> item3 = EasyMock.createMock(SearchResultItem.class);
EasyMock.expect(item3.getSource()).andReturn(series3).anyTimes();
SearchResultItem<Series>[] ascSeriesItems = new SearchResultItem[3];
ascSeriesItems[0] = item1;
ascSeriesItems[1] = item2;
ascSeriesItems[2] = item3;
SearchResultItem<Series>[] descSeriesItems = new SearchResultItem[3];
descSeriesItems[0] = item3;
descSeriesItems[1] = item2;
descSeriesItems[2] = item1;
// final SearchResultItem<Event>[] eventItems1 = new SearchResultItem[0];
final SearchResultItem<Event>[] eventItems1 = createEvents(1, 1, 1);
// Setup the events for series 2
final SearchResultItem<Event>[] eventItems2 = new SearchResultItem[0];
// Setup the events for series 3
final SearchResultItem<Event>[] eventItems3 = createEvents(0, 1, 2);
final SearchResultItem<org.opencastproject.index.service.impl.index.theme.Theme> themeItem1 = EasyMock.createMock(SearchResultItem.class);
EasyMock.expect(themeItem1.getSource()).andReturn(theme1);
// Setup series search results
final SearchResult<Series> ascSeriesSearchResult = EasyMock.createMock(SearchResult.class);
EasyMock.expect(ascSeriesSearchResult.getItems()).andReturn(ascSeriesItems);
EasyMock.expect(ascSeriesSearchResult.getHitCount()).andReturn((long) ascSeriesItems.length);
final SearchResult<Series> descSeriesSearchResult = EasyMock.createMock(SearchResult.class);
EasyMock.expect(descSeriesSearchResult.getItems()).andReturn(descSeriesItems);
EasyMock.expect(descSeriesSearchResult.getHitCount()).andReturn((long) descSeriesItems.length);
// Create an empty search result.
final SearchResult<Series> emptySearchResult = EasyMock.createMock(SearchResult.class);
EasyMock.expect(emptySearchResult.getPageSize()).andReturn(0L).anyTimes();
// Create a single search result for series 1.
final SearchResult<Series> oneSearchResult = EasyMock.createMock(SearchResult.class);
EasyMock.expect(oneSearchResult.getPageSize()).andReturn(1L).anyTimes();
EasyMock.expect(oneSearchResult.getItems()).andReturn(new SearchResultItem[] { item1 }).anyTimes();
// Create a single search result for series 2.
final SearchResult<Series> twoSearchResult = EasyMock.createMock(SearchResult.class);
EasyMock.expect(twoSearchResult.getPageSize()).andReturn(1L).anyTimes();
EasyMock.expect(twoSearchResult.getItems()).andReturn(new SearchResultItem[] { item2 }).anyTimes();
adminuiSearchIndex = EasyMock.createMock(AdminUISearchIndex.class);
final Capture<SeriesSearchQuery> captureSeriesSearchQuery = EasyMock.newCapture();
final Capture<EventSearchQuery> captureEventSearchQuery = EasyMock.newCapture();
final Capture<ThemeSearchQuery> captureThemeSearchQuery = EasyMock.newCapture();
EasyMock.expect(adminuiSearchIndex.getByQuery(EasyMock.capture(captureSeriesSearchQuery))).andAnswer(new IAnswer<SearchResult<Series>>() {
@Override
public SearchResult<Series> answer() throws Throwable {
if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getIdentifier().length == 1) {
if ("1".equals(captureSeriesSearchQuery.getValue().getIdentifier()[0])) {
return oneSearchResult;
} else if ("2".equals(captureSeriesSearchQuery.getValue().getIdentifier()[0])) {
return twoSearchResult;
} else {
return emptySearchResult;
}
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesContributorsSortOrder() == Order.Ascending) {
return ascSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesContributorsSortOrder() == Order.Descending) {
return descSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesDateSortOrder() == Order.Ascending) {
return ascSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesDateSortOrder() == Order.Descending) {
return descSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesOrganizersSortOrder() == Order.Ascending) {
return ascSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesOrganizersSortOrder() == Order.Descending) {
return descSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesTitleSortOrder() == Order.Ascending) {
return ascSeriesSearchResult;
} else if (captureSeriesSearchQuery.hasCaptured() && captureSeriesSearchQuery.getValue().getSeriesTitleSortOrder() == Order.Descending) {
return descSeriesSearchResult;
} else {
return ascSeriesSearchResult;
}
}
});
EasyMock.expect(adminuiSearchIndex.getByQuery(EasyMock.capture(captureEventSearchQuery))).andAnswer(new IAnswer<SearchResult<Event>>() {
@Override
public SearchResult<Event> answer() throws Throwable {
SearchResult<Event> eventsSearchResult = EasyMock.createMock(SearchResult.class);
if (captureEventSearchQuery.hasCaptured() && "1".equals(captureEventSearchQuery.getValue().getSeriesId()) && !("RUNNING".equals(captureEventSearchQuery.getValue().getWorkflowState())) && !("INSTANTIATED".equals(captureEventSearchQuery.getValue().getWorkflowState()))) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems1).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems1.length).anyTimes();
} else if (captureEventSearchQuery.hasCaptured() && "1".equals(captureEventSearchQuery.getValue().getSeriesId()) && "INSTANTIATED".equals(captureEventSearchQuery.getValue().getWorkflowState())) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems2).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems2.length).anyTimes();
} else if (captureEventSearchQuery.hasCaptured() && "1".equals(captureEventSearchQuery.getValue().getSeriesId()) && "RUNNING".equals(captureEventSearchQuery.getValue().getWorkflowState())) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems2).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems2.length).anyTimes();
} else if (captureEventSearchQuery.hasCaptured() && "2".equals(captureEventSearchQuery.getValue().getSeriesId()) && !("RUNNING".equals(captureEventSearchQuery.getValue().getWorkflowState()))) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems2).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems2.length).anyTimes();
} else if (captureEventSearchQuery.hasCaptured() && "3".equals(captureEventSearchQuery.getValue().getSeriesId())) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems3).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems3.length).anyTimes();
} else if (captureEventSearchQuery.hasCaptured() && "2".equals(captureEventSearchQuery.getValue().getSeriesId()) && "RUNNING".equals(captureEventSearchQuery.getValue().getWorkflowState())) {
// Setup events search results
EasyMock.expect(eventsSearchResult.getItems()).andReturn(eventItems3).anyTimes();
EasyMock.expect(eventsSearchResult.getHitCount()).andReturn((long) eventItems3.length).anyTimes();
} else {
if (!captureEventSearchQuery.hasCaptured()) {
Assert.fail("Haven't captured an event search query yet.");
} else {
logger.info("IDs for search query" + captureEventSearchQuery.getValue().getSeriesId());
Assert.fail("Tried to get an event collection that doesn't exist.");
}
}
EasyMock.replay(eventsSearchResult);
return eventsSearchResult;
}
}).anyTimes();
EasyMock.expect(adminuiSearchIndex.getByQuery(EasyMock.capture(captureThemeSearchQuery))).andAnswer(new IAnswer<SearchResult<org.opencastproject.index.service.impl.index.theme.Theme>>() {
@Override
public SearchResult<org.opencastproject.index.service.impl.index.theme.Theme> answer() throws Throwable {
SearchResult<org.opencastproject.index.service.impl.index.theme.Theme> themeSearchResult = EasyMock.createMock(SearchResult.class);
// Setup theme search results
EasyMock.expect(themeSearchResult.getPageSize()).andReturn(1L).anyTimes();
EasyMock.expect(themeSearchResult.getItems()).andReturn(new SearchResultItem[] { themeItem1 }).anyTimes();
EasyMock.replay(themeSearchResult);
return themeSearchResult;
}
}).anyTimes();
EasyMock.replay(adminuiSearchIndex, item1, item2, item3, themeItem1, ascSeriesSearchResult, descSeriesSearchResult, emptySearchResult, oneSearchResult, twoSearchResult);
}
use of org.opencastproject.index.service.impl.index.series.SeriesSearchQuery 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);
}
}
Aggregations