response.add(new Document(summary));
if (getTo() > 0)
{
StatelessQueryService sqs = getQueryService(srvContext);
InformationFragment fragshtml[] = null;
if (_html) {
ExplicitRecordFormatSpecification htmlrfs = new ExplicitRecordFormatSpecification("html", null, "f");
StatelessSearchResultsPageDTO res = sqs.getResultsPageFor(this.zinfo.getSetname(),this.zinfo.getQueryModel(),this.zinfo.getLandscape(),getFrom(),getTo(),def_html_request_spec,htmlrfs,null);
fragshtml = res.records;
}
ExplicitRecordFormatSpecification rfs = new ExplicitRecordFormatSpecification("xml", null, "f");
StatelessSearchResultsPageDTO res = sqs.getResultsPageFor(this.zinfo.getSetname(),this.zinfo.getQueryModel(),this.zinfo.getLandscape(),getFrom(),getTo(),def_request_spec,rfs,null);
InformationFragment frags[] = res.records;
// FIXME: we may not get all the records we want back sometimes!
int fragsLength = 0;
if (frags != null) fragsLength = frags.length;
int theLimit = Math.min(getPageSize(),fragsLength);
for (int i = 0; i < theLimit; i++) {
InformationFragment fraghtml = null;
if (_html) {
fraghtml = fragshtml[i];
}
InformationFragment frag = frags[i];
try {
DOMBuilder builder = new DOMBuilder();
org.w3c.dom.Document doc = (org.w3c.dom.Document)frag.getOriginalObject();
Document jDoc = builder.build(doc);
Element md = jDoc.getRootElement();
String elementFileName = "none";
String htmlError = "";
if (_html) {
Object docObj = fraghtml.getOriginalObject();
if (docObj instanceof org.w3c.dom.Document) {
org.w3c.dom.Document dochtml = (org.w3c.dom.Document)fraghtml.getOriginalObject();
String fileid = UUID.randomUUID().toString();
String filename = srvContext.getAppPath()+_searchMan.getHtmlCacheDir()+File.separator+fileid+".html";
elementFileName = srvContext.getBaseUrl()+"/"+_searchMan.getHtmlCacheDir()+"/"+fileid+".html";
File outHtmlFile = new File(filename);
try {
Transformer xformer = TransformerFactoryFactory.getTransformerFactory().newTransformer();
xformer.setOutputProperty(OutputKeys.METHOD, "text");
Source source = new DOMSource(dochtml);
Result result = new StreamResult(outHtmlFile.toURI().getPath());
xformer.transform(source,result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
} else {
htmlError = "HTML result not available. Error message: "+docObj.toString();
}
}
Element info = new Element(Edit.RootChild.INFO, Edit.NAMESPACE);
md.removeChild(Edit.RootChild.INFO, Edit.NAMESPACE);
addElement(info, Edit.Info.Elem.ID, (getFrom() + i)+"");
addElement(info, Edit.Info.Elem.SERVER, frag.getSourceRepositoryID());
addElement(info, Edit.Info.Elem.COLLECTION, frag.getSourceCollectionName());
addElement(info, Edit.Info.Elem.SCHEMA, _schemaMan.autodetectSchema(md));
if (_html) {
Element html = new Element(Edit.Info.Elem.HTML).setText(elementFileName);
if (!htmlError.equals("")) html.setAttribute("error", htmlError);
info.addContent(html);
}
md.addContent(info);
if(Log.isDebugEnabled(Geonet.SEARCH_ENGINE))
Log.debug(Geonet.SEARCH_ENGINE, "Add info element of "+Xml.getString(info));
response.add(jDoc);
} catch (Exception ex) {
ex.printStackTrace();
Element error = new Element("error");
error.setAttribute("server", frag.getSourceRepositoryID());
error.setAttribute("collection", frag.getSourceCollectionName());
error.setAttribute("id", (getFrom() + i)+"");
error.setAttribute("message", ex.getClass().getName() + ": " + ex.getMessage());
Log.error(Geonet.SEARCH_ENGINE, "Exception raised during Z3950 search and retrieval "+" Server: "+error.getAttributeValue("server")+" id: "+error.getAttributeValue("id"));
response.add(new Document(error));
}