Example usage for com.liferay.portal.kernel.search SearchContextFactory getInstance

List of usage examples for com.liferay.portal.kernel.search SearchContextFactory getInstance

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.search SearchContextFactory getInstance.

Prototype

public static SearchContext getInstance(HttpServletRequest httpServletRequest) 

Source Link

Usage

From source file:com.liferay.alloy.mvc.BaseAlloyControllerImpl.java

License:Open Source License

protected AlloySearchResult search(Indexer indexer, AlloyServiceInvoker alloyServiceInvoker,
        HttpServletRequest request, PortletRequest portletRequest,
        SearchContainer<? extends BaseModel<?>> searchContainer, Map<String, Serializable> attributes,
        String keywords, Sort[] sorts) throws Exception {

    if (indexer == null) {
        throw new Exception("No indexer found for " + controllerPath);
    }/*from   w ww. j  a v  a 2s . c  o m*/

    AlloySearchResult alloySearchResult = new AlloySearchResult();

    alloySearchResult.setAlloyServiceInvoker(alloyServiceInvoker);

    if (searchContainer == null) {
        searchContainer = new SearchContainer<BaseModel<?>>(portletRequest, portletURL, null, null);
    }

    SearchContext searchContext = SearchContextFactory.getInstance(request);

    boolean andOperator = ParamUtil.getBoolean(request, "andOperator");

    searchContext.setAndSearch(andOperator);

    if ((attributes != null) && !attributes.isEmpty()) {
        searchContext.setAttributes(attributes);
    }

    searchContext.setEnd(searchContainer.getEnd());

    Class<?> indexerClass = Class.forName(indexer.getClassNames()[0]);

    if (!GroupedModel.class.isAssignableFrom(indexerClass)) {
        searchContext.setGroupIds(null);
    } else if (searchContext.getAttribute(Field.GROUP_ID) != null) {
        long groupId = GetterUtil.getLong(searchContext.getAttribute(Field.GROUP_ID));

        searchContext.setGroupIds(new long[] { groupId });
    }

    if (Validator.isNotNull(keywords)) {
        searchContext.setKeywords(keywords);
    }

    if (ArrayUtil.isNotEmpty(sorts)) {
        searchContext.setSorts(sorts);
    }

    searchContext.setStart(searchContainer.getStart());

    Hits hits = indexer.search(searchContext);

    alloySearchResult.setHits(hits);

    alloySearchResult.setPortletURL(portletURL, searchContext.getAttributes());

    alloySearchResult.afterPropertiesSet();

    return alloySearchResult;
}

From source file:com.liferay.asset.internal.util.AssetHelperImpl.java

License:Open Source License

@Override
public Hits search(HttpServletRequest request, AssetEntryQuery assetEntryQuery, int start, int end)
        throws Exception {

    SearchContext searchContext = SearchContextFactory.getInstance(request);

    return search(searchContext, assetEntryQuery, start, end);
}

From source file:com.liferay.asset.internal.util.AssetHelperImpl.java

License:Open Source License

@Override
public BaseModelSearchResult<AssetEntry> searchAssetEntries(HttpServletRequest request,
        AssetEntryQuery assetEntryQuery, int start, int end) throws Exception {

    SearchContext searchContext = SearchContextFactory.getInstance(request);

    return searchAssetEntries(searchContext, assetEntryQuery, start, end);
}

From source file:com.liferay.dynamic.data.lists.form.web.internal.display.context.DDLFormViewRecordsDisplayContext.java

License:Open Source License

protected void updateSearchContainerResults() {
    List<DDLRecord> results = null;
    int total = 0;

    DisplayTerms displayTerms = _recordSearchContainer.getDisplayTerms();

    int status = WorkflowConstants.STATUS_ANY;

    if (Validator.isNull(displayTerms.getKeywords())) {
        results = _ddlRecordLocalService.getRecords(_ddlRecordSet.getRecordSetId(), status,
                _recordSearchContainer.getStart(), _recordSearchContainer.getEnd(),
                _recordSearchContainer.getOrderByComparator());
        total = _ddlRecordLocalService.getRecordsCount(_ddlRecordSet.getRecordSetId(), status);
    } else {/*from w  w  w . j av a2 s . co  m*/
        SearchContext searchContext = SearchContextFactory
                .getInstance(PortalUtil.getHttpServletRequest(_renderRequest));

        searchContext.setAttribute(Field.STATUS, status);
        searchContext.setAttribute("recordSetId", _ddlRecordSet.getRecordSetId());
        searchContext.setAttribute("recordSetScope", _ddlRecordSet.getScope());
        searchContext.setEnd(_recordSearchContainer.getEnd());
        searchContext.setKeywords(displayTerms.getKeywords());
        searchContext.setStart(_recordSearchContainer.getStart());

        BaseModelSearchResult<DDLRecord> baseModelSearchResult = _ddlRecordLocalService
                .searchDDLRecords(searchContext);

        results = baseModelSearchResult.getBaseModels();
        total = baseModelSearchResult.getLength();
    }

    _recordSearchContainer.setResults(results);
    _recordSearchContainer.setTotal(total);
}

From source file:com.liferay.dynamic.data.mapping.form.web.internal.display.context.DDMFormViewFormInstanceRecordsDisplayContext.java

License:Open Source License

protected void updateSearchContainerResults() {
    List<DDMFormInstanceRecord> results = null;
    int total = 0;

    DisplayTerms displayTerms = _ddmFormInstanceRecordSearchContainer.getDisplayTerms();

    int status = WorkflowConstants.STATUS_ANY;

    if (Validator.isNull(displayTerms.getKeywords())) {
        results = _ddmFormInstanceRecordLocalService.getFormInstanceRecords(
                _ddmFormInstance.getFormInstanceId(), status, _ddmFormInstanceRecordSearchContainer.getStart(),
                _ddmFormInstanceRecordSearchContainer.getEnd(),
                _ddmFormInstanceRecordSearchContainer.getOrderByComparator());
        total = _ddmFormInstanceRecordLocalService
                .getFormInstanceRecordsCount(_ddmFormInstance.getFormInstanceId(), status);
    } else {//  w  w w  . j  a va2s . co  m
        SearchContext searchContext = SearchContextFactory
                .getInstance(PortalUtil.getHttpServletRequest(_renderRequest));

        searchContext.setAttribute(Field.STATUS, status);
        searchContext.setAttribute("formInstanceId", _ddmFormInstance.getFormInstanceId());
        searchContext.setEnd(_ddmFormInstanceRecordSearchContainer.getEnd());
        searchContext.setKeywords(displayTerms.getKeywords());
        searchContext.setStart(_ddmFormInstanceRecordSearchContainer.getStart());

        BaseModelSearchResult<DDMFormInstanceRecord> baseModelSearchResult = _ddmFormInstanceRecordLocalService
                .searchFormInstanceRecords(searchContext);

        results = baseModelSearchResult.getBaseModels();
        total = baseModelSearchResult.getLength();
    }

    _ddmFormInstanceRecordSearchContainer.setResults(results);
    _ddmFormInstanceRecordSearchContainer.setTotal(total);
}

From source file:com.liferay.journal.content.search.web.internal.display.context.JournalContentSearchDisplayContext.java

License:Open Source License

public Hits getHits() throws Exception {
    if (_hits != null) {
        return _hits;
    }//from  ww w.  j  av  a 2  s .com

    Indexer<JournalArticle> indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);

    SearchContext searchContext = SearchContextFactory.getInstance(_request);

    searchContext.setGroupIds(null);
    searchContext.setKeywords(getKeywords());

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(_journalContentSearchPortletInstanceConfiguration.enableHighlighting());

    _hits = indexer.search(searchContext);

    return _hits;
}

From source file:com.liferay.journal.web.internal.display.context.JournalDisplayContext.java

License:Open Source License

public SearchContainer<MBMessage> getCommentsSearchContainer() throws PortalException {

    SearchContainer<MBMessage> searchContainer = new SearchContainer(_liferayPortletRequest,
            _liferayPortletResponse.createRenderURL(), null, null);

    SearchContext searchContext = SearchContextFactory
            .getInstance(_liferayPortletRequest.getHttpServletRequest());

    searchContext.setAttribute(Field.CLASS_NAME_ID, PortalUtil.getClassNameId(JournalArticle.class));

    searchContext.setAttribute("discussion", Boolean.TRUE);

    List<MBMessage> mbMessages = new ArrayList<>();

    Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);

    Hits hits = indexer.search(searchContext);

    for (Document document : hits.getDocs()) {
        long entryClassPK = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

        MBMessage mbMessage = MBMessageLocalServiceUtil.fetchMBMessage(entryClassPK);

        mbMessages.add(mbMessage);//www. j  a v  a2 s  . com
    }

    searchContainer.setResults(mbMessages);

    searchContainer.setTotal(hits.getLength());

    return searchContainer;
}

From source file:com.liferay.message.boards.web.internal.display.context.DefaultMBListDisplayContext.java

License:Open Source License

@Override
public void populateResultsAndTotal(SearchContainer searchContainer) throws PortalException {

    ThemeDisplay themeDisplay = (ThemeDisplay) _request.getAttribute(WebKeys.THEME_DISPLAY);

    if (isShowSearch()) {
        long searchCategoryId = ParamUtil.getLong(_request, "searchCategoryId");

        long[] categoryIdsArray = null;

        List categoryIds = new ArrayList();

        categoryIds.add(Long.valueOf(searchCategoryId));

        MBCategoryServiceUtil.getSubcategoryIds(categoryIds, themeDisplay.getScopeGroupId(), searchCategoryId);

        categoryIdsArray = StringUtil.split(StringUtil.merge(categoryIds), 0L);

        Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);

        SearchContext searchContext = SearchContextFactory.getInstance(_request);

        searchContext.setAttribute("paginationType", "more");
        searchContext.setCategoryIds(categoryIdsArray);
        searchContext.setEnd(searchContainer.getEnd());
        searchContext.setIncludeAttachments(true);

        String keywords = ParamUtil.getString(_request, "keywords");

        searchContext.setKeywords(keywords);

        searchContext.setStart(searchContainer.getStart());

        Hits hits = indexer.search(searchContext);

        searchContainer.setResults(SearchResultUtil.getSearchResults(hits, _request.getLocale()));

        searchContainer.setSearch(true);
        searchContainer.setTotal(hits.getLength());
    } else if (isShowRecentPosts()) {
        searchContainer.setEmptyResultsMessage("there-are-no-recent-posts");

        long groupThreadsUserId = ParamUtil.getLong(_request, "groupThreadsUserId");

        Calendar calendar = Calendar.getInstance();

        MBGroupServiceSettings mbGroupServiceSettings = MBGroupServiceSettings
                .getInstance(themeDisplay.getSiteGroupId());

        int offset = GetterUtil.getInteger(mbGroupServiceSettings.getRecentPostsDateOffset());

        calendar.add(Calendar.DATE, -offset);

        searchContainer.setTotal(MBThreadServiceUtil.getGroupThreadsCount(themeDisplay.getScopeGroupId(),
                groupThreadsUserId, calendar.getTime(), WorkflowConstants.STATUS_APPROVED));
        searchContainer.setResults(MBThreadServiceUtil.getGroupThreads(themeDisplay.getScopeGroupId(),
                groupThreadsUserId, calendar.getTime(), WorkflowConstants.STATUS_APPROVED,
                searchContainer.getStart(), searchContainer.getEnd()));
    } else if (isShowMyPosts()) {
        long groupThreadsUserId = ParamUtil.getLong(_request, "groupThreadsUserId");

        if (themeDisplay.isSignedIn()) {
            groupThreadsUserId = themeDisplay.getUserId();
        }/*from w w w.  j a  va  2 s  .c  o m*/

        int status = WorkflowConstants.STATUS_ANY;

        searchContainer.setTotal(MBThreadServiceUtil.getGroupThreadsCount(themeDisplay.getScopeGroupId(),
                groupThreadsUserId, status));
        searchContainer.setResults(MBThreadServiceUtil.getGroupThreads(themeDisplay.getScopeGroupId(),
                groupThreadsUserId, status, searchContainer.getStart(), searchContainer.getEnd()));
        searchContainer.setEmptyResultsMessage("you-do-not-have-any-posts");
    } else {
        int status = WorkflowConstants.STATUS_APPROVED;

        PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

        if (permissionChecker.isContentReviewer(themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId())) {

            status = WorkflowConstants.STATUS_ANY;
        }

        searchContainer.setTotal(MBCategoryLocalServiceUtil
                .getCategoriesAndThreadsCount(themeDisplay.getScopeGroupId(), _categoryId, status));
        searchContainer.setResults(MBCategoryServiceUtil.getCategoriesAndThreads(themeDisplay.getScopeGroupId(),
                _categoryId, status, searchContainer.getStart(), searchContainer.getEnd()));
    }
}

From source file:com.liferay.wiki.web.internal.display.context.DefaultWikiListPagesDisplayContext.java

License:Open Source License

@Override
public void populateResultsAndTotal(SearchContainer searchContainer) throws PortalException {

    WikiPage page = (WikiPage) _request.getAttribute(WikiWebKeys.WIKI_PAGE);

    ThemeDisplay themeDisplay = (ThemeDisplay) _request.getAttribute(WebKeys.THEME_DISPLAY);

    String navigation = ParamUtil.getString(_request, "navigation", "all-pages");

    String keywords = ParamUtil.getString(_request, "keywords");

    int total = 0;
    List<WikiPage> results = new ArrayList<>();

    if (Validator.isNotNull(keywords)) {
        Indexer<WikiPage> indexer = IndexerRegistryUtil.getIndexer(WikiPage.class);

        SearchContext searchContext = SearchContextFactory.getInstance(_request);

        searchContext.setAttribute("paginationType", "more");
        searchContext.setEnd(searchContainer.getEnd());
        searchContext.setIncludeAttachments(true);
        searchContext.setIncludeDiscussions(true);
        searchContext.setKeywords(keywords);
        searchContext.setNodeIds(new long[] { _wikiNode.getNodeId() });
        searchContext.setStart(searchContainer.getStart());

        Hits hits = indexer.search(searchContext);

        searchContainer.setTotal(hits.getLength());

        List<SearchResult> searchResults = SearchResultUtil.getSearchResults(hits, themeDisplay.getLocale());

        for (SearchResult searchResult : searchResults) {
            WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(searchResult.getClassPK());

            results.add(wikiPage);/*from  w w w .j  a  v  a  2  s . c om*/
        }
    } else if (navigation.equals("all-pages")) {
        total = WikiPageServiceUtil.getPagesCount(themeDisplay.getScopeGroupId(), _wikiNode.getNodeId(), true,
                themeDisplay.getUserId(), true, WorkflowConstants.STATUS_APPROVED);

        searchContainer.setTotal(total);

        OrderByComparator<WikiPage> obc = WikiPortletUtil
                .getPageOrderByComparator(searchContainer.getOrderByCol(), searchContainer.getOrderByType());

        results = WikiPageServiceUtil.getPages(themeDisplay.getScopeGroupId(), _wikiNode.getNodeId(), true,
                themeDisplay.getUserId(), true, WorkflowConstants.STATUS_APPROVED, searchContainer.getStart(),
                searchContainer.getEnd(), obc);
    } else if (navigation.equals("categorized-pages") || navigation.equals("tagged-pages")) {

        AssetEntryQuery assetEntryQuery = new AssetEntryQuery(WikiPage.class.getName(), searchContainer);

        assetEntryQuery.setEnablePermissions(true);

        total = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);

        searchContainer.setTotal(total);

        assetEntryQuery.setEnd(searchContainer.getEnd());
        assetEntryQuery.setStart(searchContainer.getStart());

        List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(assetEntryQuery);

        for (AssetEntry assetEntry : assetEntries) {
            WikiPageResource pageResource = WikiPageResourceLocalServiceUtil
                    .getPageResource(assetEntry.getClassPK());

            WikiPage assetPage = WikiPageLocalServiceUtil.getPage(pageResource.getNodeId(),
                    pageResource.getTitle());

            results.add(assetPage);
        }
    } else if (navigation.equals("draft-pages") || navigation.equals("pending-pages")) {

        long draftUserId = themeDisplay.getUserId();

        PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

        if (permissionChecker.isContentReviewer(themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId())) {

            draftUserId = 0;
        }

        int status = WorkflowConstants.STATUS_DRAFT;

        if (navigation.equals("pending-pages")) {
            status = WorkflowConstants.STATUS_PENDING;
        }

        total = WikiPageServiceUtil.getPagesCount(themeDisplay.getScopeGroupId(), draftUserId,
                _wikiNode.getNodeId(), status);

        searchContainer.setTotal(total);

        results = WikiPageServiceUtil.getPages(themeDisplay.getScopeGroupId(), draftUserId,
                _wikiNode.getNodeId(), status, searchContainer.getStart(), searchContainer.getEnd());
    } else if (navigation.equals("frontpage")) {
        WikiWebComponentProvider wikiWebComponentProvider = WikiWebComponentProvider
                .getWikiWebComponentProvider();

        WikiGroupServiceConfiguration wikiGroupServiceConfiguration = wikiWebComponentProvider
                .getWikiGroupServiceConfiguration();

        WikiPage wikiPage = WikiPageServiceUtil.getPage(themeDisplay.getScopeGroupId(), _wikiNode.getNodeId(),
                wikiGroupServiceConfiguration.frontPageName());

        searchContainer.setTotal(1);

        results.add(wikiPage);
    } else if (navigation.equals("history")) {
        total = WikiPageLocalServiceUtil.getPagesCount(page.getNodeId(), page.getTitle());

        searchContainer.setTotal(total);

        results = WikiPageLocalServiceUtil.getPages(page.getNodeId(), page.getTitle(), QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, new PageVersionComparator());
    } else if (navigation.equals("incoming-links")) {
        List<WikiPage> links = WikiPageLocalServiceUtil.getIncomingLinks(page.getNodeId(), page.getTitle());

        total = links.size();

        searchContainer.setTotal(total);

        results = ListUtil.subList(links, searchContainer.getStart(), searchContainer.getEnd());
    } else if (navigation.equals("orphan-pages")) {
        List<WikiPage> orphans = WikiPageServiceUtil.getOrphans(themeDisplay.getScopeGroupId(),
                _wikiNode.getNodeId());

        total = orphans.size();

        searchContainer.setTotal(total);

        results = ListUtil.subList(orphans, searchContainer.getStart(), searchContainer.getEnd());
    } else if (navigation.equals("outgoing-links")) {
        List<WikiPage> links = WikiPageLocalServiceUtil.getOutgoingLinks(page.getNodeId(), page.getTitle());

        total = links.size();

        searchContainer.setTotal(total);

        results = ListUtil.subList(links, searchContainer.getStart(), searchContainer.getEnd());
    } else if (navigation.equals("recent-changes")) {
        total = WikiPageServiceUtil.getRecentChangesCount(themeDisplay.getScopeGroupId(),
                _wikiNode.getNodeId());

        searchContainer.setTotal(total);

        results = WikiPageServiceUtil.getRecentChanges(themeDisplay.getScopeGroupId(), _wikiNode.getNodeId(),
                searchContainer.getStart(), searchContainer.getEnd());
    }

    searchContainer.setResults(results);
}

From source file:com.liferay.wiki.web.internal.portlet.action.AutocompletePageTitleMVCResourceCommand.java

License:Open Source License

protected JSONArray getJSONArray(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws PortalException {

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);

    SearchContext searchContext = SearchContextFactory.getInstance(request);

    searchContext.setEnd(20);//from w ww  . j av  a 2s . co  m

    String query = ParamUtil.getString(request, "query");

    searchContext.setKeywords(StringUtil.toLowerCase(query));

    long nodeId = ParamUtil.getLong(resourceRequest, "nodeId");

    searchContext.setNodeIds(new long[] { nodeId });

    searchContext.setStart(0);

    Hits hits = _wikiPageTitleSearcher.search(searchContext);

    for (Document document : hits.getDocs()) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

        jsonObject.put("title", document.get(Field.TITLE));

        jsonArray.put(jsonObject);
    }

    return jsonArray;
}