Search in sources :

Example 1 with SearchResult

use of org.opencastproject.matterhorn.search.SearchResult 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);
}
Also used : AdminUISearchIndex(org.opencastproject.adminui.impl.index.AdminUISearchIndex) SeriesSearchQuery(org.opencastproject.index.service.impl.index.series.SeriesSearchQuery) EventSearchQuery(org.opencastproject.index.service.impl.index.event.EventSearchQuery) SearchResultItem(org.opencastproject.matterhorn.search.SearchResultItem) SearchResult(org.opencastproject.matterhorn.search.SearchResult) Series(org.opencastproject.index.service.impl.index.series.Series) IAnswer(org.easymock.IAnswer) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) Event(org.opencastproject.index.service.impl.index.event.Event) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Example 2 with SearchResult

use of org.opencastproject.matterhorn.search.SearchResult in project opencast by opencast.

the class TestThemesEndpoint method setupServices.

private void setupServices() throws Exception {
    user = new JaxbUser("test", null, "Test User", "test@test.com", "test", new DefaultOrganization(), new HashSet<JaxbRole>());
    UserDirectoryService userDirectoryService = EasyMock.createNiceMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(user).anyTimes();
    EasyMock.replay(userDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.replay(securityService);
    SeriesService seriesService = EasyMock.createNiceMock(SeriesService.class);
    EasyMock.replay(seriesService);
    MessageSender messageSender = EasyMock.createNiceMock(MessageSender.class);
    messageSender.sendObjectMessage(EasyMock.anyObject(String.class), EasyMock.anyObject(MessageSender.DestinationType.class), EasyMock.anyObject(Serializable.class));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(messageSender);
    // Create AdminUI Search Index
    AdminUISearchIndex adminUISearchIndex = EasyMock.createMock(AdminUISearchIndex.class);
    final Capture<ThemeSearchQuery> themeQueryCapture = new Capture<ThemeSearchQuery>();
    EasyMock.expect(adminUISearchIndex.getByQuery(EasyMock.capture(themeQueryCapture))).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 {
            return createThemeCaptureResult(themeQueryCapture);
        }
    });
    final Capture<SeriesSearchQuery> seriesQueryCapture = new Capture<SeriesSearchQuery>();
    EasyMock.expect(adminUISearchIndex.getByQuery(EasyMock.capture(seriesQueryCapture))).andAnswer(new IAnswer<SearchResult<Series>>() {

        @Override
        public SearchResult<Series> answer() throws Throwable {
            return createSeriesCaptureResult(seriesQueryCapture);
        }
    });
    EasyMock.replay(adminUISearchIndex);
    themesServiceDatabaseImpl = new ThemesServiceDatabaseImpl();
    themesServiceDatabaseImpl.setEntityManagerFactory(newTestEntityManagerFactory(ThemesServiceDatabaseImpl.PERSISTENCE_UNIT));
    themesServiceDatabaseImpl.setUserDirectoryService(userDirectoryService);
    themesServiceDatabaseImpl.setSecurityService(securityService);
    themesServiceDatabaseImpl.setMessageSender(messageSender);
    themesServiceDatabaseImpl.activate(null);
    StaticFileService staticFileService = EasyMock.createNiceMock(StaticFileService.class);
    EasyMock.expect(staticFileService.getFile(EasyMock.anyString())).andReturn(new ByteArrayInputStream("test".getBytes("utf-8"))).anyTimes();
    EasyMock.expect(staticFileService.getFileName(EasyMock.anyString())).andStubReturn("test.mp4");
    EasyMock.replay(staticFileService);
    BundleContext bundleContext = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bundleContext.getProperty("org.opencastproject.server.url")).andReturn("http://localhost:8080").anyTimes();
    EasyMock.replay(bundleContext);
    ComponentContext componentContext = EasyMock.createNiceMock(ComponentContext.class);
    EasyMock.expect(componentContext.getBundleContext()).andReturn(bundleContext).anyTimes();
    EasyMock.expect(componentContext.getProperties()).andReturn(new Hashtable<String, Object>()).anyTimes();
    EasyMock.replay(componentContext);
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.setStaticFileService(staticFileService);
    staticFileRestService.activate(componentContext);
    this.setThemesServiceDatabase(themesServiceDatabaseImpl);
    this.setSecurityService(securityService);
    this.setSeriesService(seriesService);
    this.setStaticFileService(staticFileService);
    this.setStaticFileRestService(staticFileRestService);
    this.setIndex(adminUISearchIndex);
}
Also used : Serializable(java.io.Serializable) MessageSender(org.opencastproject.message.broker.api.MessageSender) AdminUISearchIndex(org.opencastproject.adminui.impl.index.AdminUISearchIndex) JaxbUser(org.opencastproject.security.api.JaxbUser) StaticFileRestService(org.opencastproject.staticfiles.endpoint.StaticFileRestService) Capture(org.easymock.Capture) ThemesServiceDatabaseImpl(org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl) SecurityService(org.opencastproject.security.api.SecurityService) HashSet(java.util.HashSet) ComponentContext(org.osgi.service.component.ComponentContext) SeriesSearchQuery(org.opencastproject.index.service.impl.index.series.SeriesSearchQuery) Hashtable(java.util.Hashtable) SearchResult(org.opencastproject.matterhorn.search.SearchResult) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) StaticFileService(org.opencastproject.staticfiles.api.StaticFileService) SeriesService(org.opencastproject.series.api.SeriesService) ByteArrayInputStream(java.io.ByteArrayInputStream) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) BundleContext(org.osgi.framework.BundleContext)

Example 3 with SearchResult

use of org.opencastproject.matterhorn.search.SearchResult in project opencast by opencast.

the class ThemesListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    Map<String, String> list = new HashMap<String, String>();
    if (NAME.equals(listName)) {
        ThemeSearchQuery themeQuery = new ThemeSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
        themeQuery.withOffset(query.getOffset().getOrElse(0));
        int limit = query.getLimit().getOrElse(Integer.MAX_VALUE - themeQuery.getOffset());
        themeQuery.withLimit(limit);
        themeQuery.sortByName(SearchQuery.Order.Ascending);
        SearchResult<Theme> results = null;
        try {
            results = searchIndex.getByQuery(themeQuery);
        } catch (SearchIndexException e) {
            logger.error("The admin UI Search Index was not able to get the themes: {}", ExceptionUtils.getStackTrace(e));
            throw new ListProviderException("No themes list for list name " + listName + " found!");
        }
        for (SearchResultItem<Theme> item : results.getItems()) {
            Theme theme = item.getSource();
            list.put(Long.toString(theme.getIdentifier()), theme.getName());
        }
    } else if (DESCRIPTION.equals(listName)) {
        ThemeSearchQuery themeQuery = new ThemeSearchQuery(securityService.getOrganization().getId(), securityService.getUser());
        themeQuery.withOffset(query.getOffset().getOrElse(0));
        int limit = query.getLimit().getOrElse(Integer.MAX_VALUE - themeQuery.getOffset());
        themeQuery.withLimit(limit);
        themeQuery.sortByName(SearchQuery.Order.Ascending);
        SearchResult<Theme> results = null;
        try {
            results = searchIndex.getByQuery(themeQuery);
        } catch (SearchIndexException e) {
            logger.error("The admin UI Search Index was not able to get the themes: {}", ExceptionUtils.getStackTrace(e));
            throw new ListProviderException("No themes list for list name " + listName + " found!");
        }
        for (SearchResultItem<Theme> item : results.getItems()) {
            Theme theme = item.getSource();
            if (theme.getDescription() == null) {
                theme.setDescription("");
            } else {
                theme.getDescription();
            }
            list.put(Long.toString(theme.getIdentifier()), theme.getDescription());
        }
    }
    return list;
}
Also used : SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) HashMap(java.util.HashMap) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) SearchResultItem(org.opencastproject.matterhorn.search.SearchResultItem) Theme(org.opencastproject.index.service.impl.index.theme.Theme) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) SearchResult(org.opencastproject.matterhorn.search.SearchResult)

Aggregations

ThemeSearchQuery (org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery)3 SearchResult (org.opencastproject.matterhorn.search.SearchResult)3 AdminUISearchIndex (org.opencastproject.adminui.impl.index.AdminUISearchIndex)2 SeriesSearchQuery (org.opencastproject.index.service.impl.index.series.SeriesSearchQuery)2 SearchResultItem (org.opencastproject.matterhorn.search.SearchResultItem)2 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 Capture (org.easymock.Capture)1 IAnswer (org.easymock.IAnswer)1 ListProviderException (org.opencastproject.index.service.exception.ListProviderException)1 Event (org.opencastproject.index.service.impl.index.event.Event)1 EventSearchQuery (org.opencastproject.index.service.impl.index.event.EventSearchQuery)1 Series (org.opencastproject.index.service.impl.index.series.Series)1 Theme (org.opencastproject.index.service.impl.index.theme.Theme)1 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)1 MessageSender (org.opencastproject.message.broker.api.MessageSender)1