use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.
the class SiteManagerImpl method findIds.
@Override
public QueryResultList<String> findIds(FieldQuery query) {
log.debug("findIds for query{}", query);
// We need to search all referenced Sites
Set<String> entityIds = new HashSet<String>();
//TODO: The QueryResultList expects that the query as executed is added
//to the response. However when executing queries on multiple site they
//might support a different set of features and therefore execute
//different variants. For now I return simple the query as executed by
//the first Site that contributes results
FieldQuery processedQuery = null;
FieldQuery queryWithResults = null;
for (Site site : referencedSites) {
if (site.supportsSearch()) {
log.debug(" > query site {}", site.getId());
try {
QueryResultList<String> results = site.findReferences(query);
if (processedQuery == null) {
processedQuery = results.getQuery();
}
if (!results.isEmpty() && queryWithResults == null) {
processedQuery = results.getQuery();
}
for (String entityId : results) {
entityIds.add(entityId);
}
} catch (SiteException e) {
log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
}
} else {
log.debug(" > Site {} does not support queries", site.getId());
}
}
return new QueryResultListImpl<String>(//use the query with results
queryWithResults != null ? //use the query with results
queryWithResults : //if not a processed
processedQuery != null ? //if not a processed
processedQuery : //else the parsed one
query, entityIds.iterator(), String.class);
}
use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.
the class SiteManagerImpl method findEntities.
@Override
public QueryResultList<Entity> findEntities(FieldQuery query) {
log.debug("findEntities for query{}", query);
//TODO: The QueryResultList expects that the query as executed is added
//to the response. However when executing queries on multiple site they
//might support a different set of features and therefore execute
//different variants. For now I return simple the query as executed by
//the first Site that contributes results
FieldQuery processedQuery = null;
FieldQuery queryWithResults = null;
Set<Entity> entities = new HashSet<Entity>();
for (Site site : referencedSites) {
if (site.supportsSearch()) {
//do not search on sites that do not support it
log.debug(" > query site {}", site.getId());
try {
QueryResultList<Entity> results = site.findEntities(query);
if (processedQuery == null) {
processedQuery = results.getQuery();
}
if (!results.isEmpty() && queryWithResults == null) {
processedQuery = results.getQuery();
}
for (Entity rep : results) {
if (!entities.contains(rep)) {
//do not override
entities.add(rep);
} else {
//TODO: find a solution for this problem
// e.g. allow to add the site for entities
log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
}
}
} catch (SiteException e) {
log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
}
} else {
log.debug(" > Site {} does not support queries", site.getId());
}
}
return new QueryResultListImpl<Entity>(//use the query with results
queryWithResults != null ? //use the query with results
queryWithResults : //if not a processed
processedQuery != null ? //if not a processed
processedQuery : //else the parsed one
query, entities, Entity.class);
}
use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.
the class VirtuosoSearcher method findEntities.
@Override
public final QueryResultList<String> findEntities(FieldQuery parsedQuery) throws IOException {
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
query.setSparqlEndpointType(SparqlEndpointTypeEnum.Virtuoso);
String sparqlQuery = query.toSparqlSelect(false);
log.trace("Sending Sparql request [{}].", sparqlQuery);
InputStream in = sendSparqlRequest(getQueryUri(), sparqlQuery, SparqlSearcher.DEFAULT_SPARQL_RESULT_CONTENT_TYPE);
// Move to util class!
final List<String> entities = extractEntitiesFromJsonResult(in, query.getRootVariableName());
return new QueryResultListImpl<String>(query, entities.iterator(), String.class);
}
use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.
the class EntityhubRootResource method getSymbolMappings.
@GET
@Path("mapping/symbol")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbolMappings(@QueryParam("id") String symbol, @Context HttpHeaders headers) throws WebApplicationException {
log.debug("getSymbolMappings() POST Request > symbol: {} > accept: {}", symbol, headers.getAcceptableMediaTypes());
Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
supported.add(TEXT_HTML);
MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
if (symbol == null || symbol.isEmpty()) {
//if HTML -> print the docu of the restfull service
if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
ResponseBuilder rb = Response.ok(new Viewable("mapping_symbol", this));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
} else {
return Response.status(Status.BAD_REQUEST).entity("No symbol given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
//Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
Collection<Entity> mappings;
try {
mappings = entityhub.getMappingsByTarget(symbol);
} catch (EntityhubException e) {
throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
}
if (mappings == null || mappings.isEmpty()) {
return Response.status(Status.NOT_FOUND).entity("No mapping found for symbol '" + symbol + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
} else {
// TODO: Implement Support for list of Signs, Representations and Strings
// For now use a pseudo QueryResultList
QueryResultList<Entity> mappingResultList = new QueryResultListImpl<Entity>(null, mappings, Entity.class);
ResponseBuilder rb = Response.ok(mappingResultList);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.
the class MockEntityhub method findEntities.
@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws EntityhubException {
log.info("Performing Query: {}", query);
QueryResultList<Representation> results = yard.findRepresentation(query);
log.info(" ... {} results", results.size());
Collection<Entity> entities = new ArrayList<Entity>(results.size());
for (Representation r : results) {
log.info(" > {}", r.getId());
entities.add(new EntityImpl("dbpedia", r, null));
}
return new QueryResultListImpl<Entity>(results.getQuery(), entities, Entity.class);
}
Aggregations