use of org.jbei.ice.lib.executor.TaskStatus in project ice by JBEI.
the class WebSearch method run.
/**
* Searches all registries in the web of registries configuration with this
* registry. Without some sort of indexing locally or in some central location,
* this will be slow for large numbers of results
*
* @param query wrapper around search query
* @param includeThisInstance whether to include results from this instance of ICE
* @return list of search results
*/
public WebSearchResults run(SearchQuery query, boolean includeThisInstance) {
List<RemotePartner> partners = this.remotePartnerDAO.getRegistryPartners();
if (partners == null)
return null;
List<SearchTask> searchTasks = new LinkedList<>();
// for each approved partner run the search task
for (RemotePartner partner : partners) {
if (partner.getUrl() == null || partner.getPartnerStatus() != RemotePartnerStatus.APPROVED)
continue;
SearchTask task = runSearchThread(partner, query);
searchTasks.add(task);
}
WebSearchResults searchResults = new WebSearchResults(searchTasks.size() + 1);
// String inWWoR = Utils.getConfigValue(ConfigurationKey.JOIN_WEB_OF_REGISTRIES);
if (includeThisInstance) {
SearchIndexes searchIndexes = new SearchIndexes();
SearchResults results = searchIndexes.runSearch(null, query);
searchResults.setQuery(query);
WebResult thisResult = new WebResult();
thisResult.getResults().addAll(results.getResults());
thisResult.setCount(results.getResultCount());
String url = Utils.getConfigValue(ConfigurationKey.URI_PREFIX);
String projectName = Utils.getConfigValue(ConfigurationKey.PROJECT_NAME);
RegistryPartner thisPartner = new RegistryPartner();
thisPartner.setUrl(url);
thisPartner.setName(projectName);
thisResult.setPartner(thisPartner);
searchResults.getResults().add(thisResult);
searchResults.setTotalCount(thisResult.getCount());
}
// go through tasks and check if completed
while (!searchTasks.isEmpty()) {
Iterator<SearchTask> iterator = searchTasks.iterator();
// todo : set time limit and abandon task (shutdown) if exceeded (or after give it till after this instance is searched)
while (iterator.hasNext()) {
SearchTask task = iterator.next();
TaskStatus status = task.getStatus();
if (status == TaskStatus.COMPLETED || status == TaskStatus.EXCEPTION) {
iterator.remove();
if (status == TaskStatus.COMPLETED) {
WebResult webResult = new WebResult();
webResult.setPartner(task.getPartner().toDataTransferObject());
SearchResults partnerResults = task.getResults();
webResult.setCount(partnerResults.getResultCount());
webResult.getResults().addAll(partnerResults.getResults());
searchResults.getResults().add(webResult);
searchResults.setTotalCount(searchResults.getTotalCount() + partnerResults.getResultCount());
}
}
}
}
return searchResults;
}
Aggregations