Java Code Examples for org.alfresco.service.cmr.search.ResultSet#getNodeRef()
The following examples show how to use
org.alfresco.service.cmr.search.ResultSet#getNodeRef() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmailServiceImpl.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
/** * Authenticate in Alfresco repository by sender's e-mail address. * * @param from Sender's email address * @return User name or null if the user does not exist. * @throws EmailMessageException Exception will be thrown if authentication is failed. */ private String getUsername(String from) { String userName = null; if(from == null || from.length()==0) { return null; } if(logger.isDebugEnabled()) { logger.debug("getUsername from: " + from); } StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"); String query = "TYPE:cm\\:person AND =@cm\\:email:\"" + from + "\""; ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_FTS_ALFRESCO, query); try { if (resultSet == null || resultSet.length() == 0) { return null; } if (resultSet.length() > 1) { if(logger.isWarnEnabled()) { logger.warn("found more as one result for email '" + from + "'. The first will be used"); } } NodeRef userNode = resultSet.getNodeRef(0); if (nodeService.exists(userNode)) { userName = DefaultTypeConverter.INSTANCE.convert( String.class, nodeService.getProperty(userNode, ContentModel.PROP_USERNAME)); if(logger.isDebugEnabled()) { logger.debug("found username: " + userName); } } else { // The Lucene index returned a dead result throw new EmailMessageException(ERR_UNKNOWN_SOURCE_ADDRESS, from); } } finally { if(resultSet != null) { resultSet.close(); } } return userName; }
Example 2
Source File: KeywordSearch.java From alfresco-remote-api with GNU Lesser General Public License v3.0 | 4 votes |
/** * Execute the search */ private SearchResult search(String searchTerms, int startPage, int itemsPerPage, Locale locale, WebScriptRequest req) { SearchResult searchResult = null; ResultSet results = null; try { // construct search statement String[] terms = searchTerms.split(" "); searchTerms = searchTerms.replaceAll("\"", """); // Escape special characters in the terms, so that they can't confuse the parser for (int i=0; i<terms.length; i++) { terms[i] = SearchLanguageConversion.escapeLuceneQuery(terms[i]); } Map<String, Object> statementModel = new HashMap<String, Object>(7, 1.0f); statementModel.put("args", createArgs(req)); statementModel.put("terms", terms); Writer queryWriter = new StringWriter(1024); renderFormatTemplate(QUERY_FORMAT, statementModel, queryWriter); String query = queryWriter.toString(); // execute query if (logger.isDebugEnabled()) { logger.debug("Search parameters: searchTerms=" + searchTerms + ", startPage=" + startPage + ", itemsPerPage=" + itemsPerPage + ", search locale=" + locale.toString()); logger.debug("Issuing lucene search: " + query); } SearchParameters parameters = new SearchParameters(); parameters.addStore(SEARCH_STORE); parameters.setLanguage(SearchService.LANGUAGE_LUCENE); parameters.setQuery(query); if (locale != null) { parameters.addLocale(locale); } results = searchService.query(parameters); int totalResults = results.length(); if (logger.isDebugEnabled()) logger.debug("Results: " + totalResults + " rows (limited: " + results.getResultSetMetaData().getLimitedBy() + ")"); // are we out-of-range int totalPages = (totalResults / itemsPerPage); totalPages += (totalResults % itemsPerPage != 0) ? 1 : 0; if (totalPages != 0 && (startPage < 1 || startPage > totalPages)) { throw new WebScriptException("Start page " + startPage + " is outside boundary of " + totalPages + " pages"); } // construct search result searchResult = new SearchResult(); searchResult.setSearchTerms(searchTerms); searchResult.setLocale(locale); searchResult.setItemsPerPage(itemsPerPage); searchResult.setStartPage(startPage); searchResult.setTotalResults(totalResults); if (totalResults == 0) { searchResult.setTotalPages(0); searchResult.setStartIndex(0); searchResult.setTotalPageItems(0); } else { searchResult.setTotalPages(totalPages); searchResult.setStartIndex(((startPage -1) * itemsPerPage) + 1); searchResult.setTotalPageItems(Math.min(itemsPerPage, totalResults - searchResult.getStartIndex() + 1)); } SearchTemplateNode[] nodes = new SearchTemplateNode[searchResult.getTotalPageItems()]; for (int i = 0; i < searchResult.getTotalPageItems(); i++) { NodeRef node = results.getNodeRef(i + searchResult.getStartIndex() - 1); // Make the search resilient to invalid nodes if (!nodeService.exists(node)) { continue; } float score = results.getScore(i + searchResult.getStartIndex() - 1); nodes[i] = new SearchTemplateNode(node, score); } searchResult.setResults(nodes); return searchResult; } finally { if (results != null) { results.close(); } } }