if (serverResults.locallyRemoved > 0) {
dataMan.flush();
}
// --- Search remote node
MetaSearcher s = searchMan.newSearcher(SearchManager.Z3950, Geonet.File.SEARCH_Z3950_CLIENT);
ServiceConfig config = new ServiceConfig();
Element request = new Element("request");
// --- Z39.50 servers from harvest params
for (String id : params.getRepositories()) {
request.addContent(new Element(Geonet.SearchResult.SERVERS).setText(id));
}
// --- Z39.50 query from harvest params
request.addContent(new Element(Geonet.SearchResult.ZQUERY)
.setText(params.query));
// --- don't get html presentations (get them later)
request.addContent(new Element(Geonet.SearchResult.SERVERHTML)
.setText("off"));
// --- set timeout to be 100 seconds
request.addContent(new Element(Geonet.SearchResult.TIMEOUT).setText("100"));
// --- set hitsPerPage
request.addContent(new Element(Geonet.SearchResult.HITS_PER_PAGE).setText(groupSize+""));
// --- do the search
s.search(context, request, config);
if (s.getSize() == 0) {
log.error("Search failed or returned 0 results, trying again");
s.search(context, request, config);
if (s.getSize() == 0) {
throw new Exception("Bad luck, Search failed or returned 0 results");
}
}
if(log.isDebugEnabled()) log.debug("Search returned "+s.getSize()+" hits");
// -- process the hits in groups of groupSize
int numberOfHits = Math.min(Integer.parseInt(params.maximumHits),s.getSize());
// -- add from and to placeholders to request
request.addContent(new Element("from"));
request.addContent(new Element("to"));
Element repositories = new Info().getZRepositories(context, settingMan);
if(log.isDebugEnabled()) {
log.debug("repos "+Xml.getString(repositories));
}
// -- build a map of collection code versus repository name for
// -- assigning the categories
Map <String,String> codes = new HashMap<String,String>();
Map <String,String> catCodes = new HashMap<String,String>();
final MetadataCategoryRepository categoryRepository = this.context.getBean(MetadataCategoryRepository.class);
// -- add new category for each repository
boolean addcateg = false;
for (String repo : params.getRepositories()) {
Element repoElem = Xml.selectElement(repositories, "record[id='"+repo+"']");
if (repoElem != null) {
Element repoId = repoElem.getChild("id");
String repoName = repoElem.getChildText("name");
codes.put(repoId.getAttributeValue("serverCode")+":"+repoId.getAttributeValue("code"), repoName);
// create a result holder for this repository
serverResults.getServerResult(repoName);
// sanitize the name of the category
String categName = repoName.replaceAll("[^\\w]","");
categName = categName.toLowerCase();
catCodes.put(repoId.getAttributeValue("serverCode")+":"+repoId.getAttributeValue("code"), categName);
if (categoryRepository.findOneByNameIgnoreCase(categName) == null) {
MetadataCategory category = new MetadataCategory();
category.setName(categName);
categoryRepository.save(category);
addcateg = true;
}
}
}
if (addcateg) {
categoryRepository.flush();
}
// --- return only maximum hits as directed by the harvest params
int nrGroups = (numberOfHits / groupSize) + 1;
for (int i = 1; i <= nrGroups; i++) {
int lower = ((i-1)*groupSize)+1;
int upper = Math.min((i*groupSize),numberOfHits);
request.getChild("from").setText(""+lower);
request.getChild("to").setText(""+upper);
// --- Loading results
List<Document> list = s.presentDocuments(context, request, config);
// --- Loading categories and groups
localCateg = new CategoryMapper(context);
localGroups = new GroupMapper(context);