initEntityMetadata(entity,true);
}
return new QueryResultListImpl<Entity>(query, results, Entity.class);
} catch (YardException e) {
if(siteConfiguration.getEntitySearcherType()==null || isOfflineMode()){
throw new ReferencedSiteException("Unable to execute query on Cache "+siteConfiguration.getCacheId(),e);
} else {
log.warn(String.format("Error while performing query on Cache %s! Try to use remote site %s as fallback!",
siteConfiguration.getCacheId(),siteConfiguration.getQueryUri()),e);
}
}
} else {
if(siteConfiguration.getEntitySearcherType()==null || isOfflineMode()){
throw new ReferencedSiteException(String.format("Unable to execute query on Cache %s because it is currently not active",
siteConfiguration.getCacheId()));
} else {
log.warn(String.format("Cache %s currently not active will query remote Site %s as fallback",
siteConfiguration.getCacheId(),siteConfiguration.getQueryUri()));
}
}
}
QueryResultList<String> entityIds;
if(entitySearcher == null) {
throw new ReferencedSiteException(
String.format("EntitySearcher %s not available for remote site %s!",siteConfiguration.getEntitySearcherType(),
siteConfiguration.getQueryUri()));
}
ensureOnline(siteConfiguration.getQueryUri(),entitySearcher.getClass());
try {
entityIds = entitySearcher.findEntities(query);
} catch (IOException e) {
throw new ReferencedSiteException(String.format("Unable to execute query on remote site %s with entitySearcher %s!",
siteConfiguration.getQueryUri(),siteConfiguration.getEntitySearcherType()), e);
}
int numResults = entityIds.size();
List<Entity> entities = new ArrayList<Entity>(numResults);
int errors = 0;
ReferencedSiteException lastError = null;
for(String id : entityIds){
Entity entity;
try {
entity = getEntity(id);
if(entity == null){
log.warn("Unable to create Entity for ID that was selected by an FieldQuery (id="+id+")");
}
entities.add(entity);
//use the position in the list as resultSocre
entity.getRepresentation().set(RdfResourceEnum.resultScore.getUri(), Float.valueOf((float)numResults));
} catch (ReferencedSiteException e) {
lastError = e;
errors++;
log.warn(String.format("Unable to get Representation for Entity %s. -> %d Error%s for %d Entities in QueryResult (Reason:%s)",
id,errors,errors>1?"s":"",entityIds.size(),e.getMessage()));
}
//decrease numResults because it is used as resultScore for entities
numResults--;
}
if(lastError != null){
if(entities.isEmpty()){
throw new ReferencedSiteException("Unable to get anly Representations for Entities selected by the parsed Query (Root-Cause is the last Exception trown)",lastError);
} else {
log.warn(String.format("Unable to get %d/%d Represetnations for selected Entities.",errors,entityIds.size()));
log.warn("Stack trace of the last Exception:",lastError);
}
}