Commit a5a9cefd authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

1323: Implement metasearch for SRU endpoints

Task-Url: https://pm.winseda.de/issues/1323
parent 420eb23a
package eu.dariah.de.search.query.meta;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import de.unibamberg.minf.dme.model.base.Element;
import de.unibamberg.minf.dme.model.base.Nonterminal;
import de.unibamberg.minf.dme.model.datamodel.base.Datamodel;
import de.unibamberg.minf.dme.model.mapping.base.Mapping;
import de.unibamberg.minf.processing.consumption.CollectingResourceConsumptionServiceImpl;
import de.unibamberg.minf.processing.service.base.BaseResourceProcessingServiceImpl;
import de.unibamberg.minf.processing.service.xml.XmlProcessingService;
import eu.dariah.de.search.model.Collection;
import eu.dariah.de.search.model.Dataset;
import eu.dariah.de.search.model.Endpoint;
import eu.dariah.de.search.model.ExtendedDatamodelContainer;
import eu.dariah.de.search.query.Query;
import eu.dariah.de.search.query.SimpleQuery;
import eu.dariah.de.search.query.execution.QueryExecutionService;
import eu.dariah.de.search.query.results.QueryResult;
import eu.dariah.de.search.service.CollectionService;
import eu.dariah.de.search.service.DatamodelService;
import eu.dariah.de.search.service.MappingService;
@Component
public class SruQueryExecutionServiceImpl implements QueryExecutionService {
protected final Logger logger = LoggerFactory.getLogger(SruQueryExecutionServiceImpl.class);
@Autowired private ApplicationContext appContext;
@Autowired private CollectionService collectionService;
@Autowired protected RestTemplate restTemplate;
@Autowired private DatamodelService datamodelService;
@Autowired private MappingService mappingService;
@Override
public QueryResult executeQuery(Query q, Locale locale) {
List<Collection> coll;
......@@ -55,9 +85,62 @@ public class SruQueryExecutionServiceImpl implements QueryExecutionService {
// Actually execute federated searches
for (Entry<String, Endpoint> entry : collectionIdEndpointMap.entrySet()) {
String result = restTemplate.getForObject(entry.getValue().getUrl(), String.class);
String entityId = null;
for (Dataset ds : entry.getValue().getDatasets()) {
entityId = ds.getId();
break;
}
ExtendedDatamodelContainer s = datamodelService.findById(entityId);
/*if (s==null) {
Mapping m = mappingService.g(entityId);
s = datamodelService.findSchemaById(m.getSourceId());
}*/
Element r = s.getOrRenderElementHierarchy();
XmlProcessingService processingSvc = appContext.getBean(XmlProcessingService.class);
CollectingResourceConsumptionServiceImpl consumptionService = new CollectingResourceConsumptionServiceImpl();
processingSvc.setSchema(s.getModel());
processingSvc.addConsumptionService(consumptionService);
try {
//processingSvc.setExecutionContext(new SessionExecutionContext(sessionsPath, session.getId()));
processingSvc.setRoot((Nonterminal)r);
processingSvc.init();
URIBuilder b = new URIBuilder(entry.getValue().getUrl());
b.addParameter("query", ((SimpleQuery)q).getQueryString());
b.addParameter("recordSchema", "dc");
b.addParameter("maximumRecords", "10");
b.addParameter("startRecord", "1");
b.addParameter("recordPacking", "xml");
String sss = restTemplate.getForObject(b.build(), String.class);
processingSvc.setInputStream(IOUtils.toInputStream(sss, Charset.defaultCharset()));
processingSvc.run();
} catch (Exception e) {
logger.error("Error", e);
}
System.out.print(false);
}
return null;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment