this.pnamespace = PredefinedNamespace.get();
}
@Override
public LocalRepository execute(LocalRepository inGraph) {
LocalRepository lr = RepositoryFactory.getServiceRepository(this.getUri());
boolean isNegation = MBBService.NEGATION.equals(this.params.get(1));
boolean isInverse = MBBService.INVERSE.equals(this.params.get(2));
String sFullProperty = this.params.get(3);
String sPropertyNS = this.params.get(4);
String sPropertyName = this.params.get(5);
String sValues = URLDecode.decode(this.params.get(6));
this.params.set(6, sValues);
boolean uriprop = (null == sPropertyNS || "".equals(sPropertyNS));
String[] asFullProperty = null;
if(uriprop) {
if(null != sPropertyName && !"".equals(sPropertyName))
sFullProperty = sPropertyName;
sFullProperty = URLDecode.decode(sFullProperty);
java.net.URI u;
try {
u = new java.net.URI(sFullProperty);
} catch (URISyntaxException e) {
u = null;
}
// if(u == null || !u.isAbsolute())
// sFullProperty = this.pproperty.getURI(sFullProperty);
//
// String prop = this.pproperty.getName(sFullProperty);
String prop;
if(u == null || !u.isAbsolute()){
asFullProperty = this.pproperty.getURI(sFullProperty); // if sFullProperty is a property name - get all URI
prop = sFullProperty;
} else {
asFullProperty = new String[1];
asFullProperty[0] = sFullProperty;
prop = this.pproperty.getName(sFullProperty);
}
this.params.set(3, prop);
}
try {
if(lr.getGraph().getStatements().hasNext())
return lr;//it means that we already invoked this query
} catch (AccessDeniedException e) {
Repository.logger.warning(e.toString());
}
//otherwise we need to execute the query
RDFQuery rdfquery;
String pparam;
String restparam;
String using;
String valueQueryPart;
if(!"*".equals(sFullProperty)) {
//this is the type of browse when you match values
rdfquery = (isInverse)?RDFQuery.RDFQ_BROWSE_LISTVALUES_PROPERTY_INVERSE:RDFQuery.RDFQ_BROWSE_LISTVALUES_PROPERTY;
if(uriprop) {
using = "";
// pparam = "prop";//(isNegation)?"prop":"<"+sFullProperty+">";
restparam = (isNegation)?"NOT (":"(";
if(asFullProperty != null)
for(String s : asFullProperty){
restparam += "prop = <"+s+"> OR ";
}
if(restparam.length() > 4)
restparam = restparam.substring(0, restparam.length()-4);
restparam += ")";
}else {
// String property = this.pproperty.getURI(sPropertyName);
// pparam = "prop";//(sPropertyNS == null)?"<"+property+">":"prop";
restparam = "TRUE";
if(!"*".equals(sPropertyNS))
restparam += ((!isNegation)?" AND (":" AND (NOT ")+"namespace(prop) = property:";
//namespace(prop) = property: AND localName(prop) LIKE "*digitaltype*" //IGNORE CASE
if(!"*".equals(sPropertyName) && !"*".equals(sPropertyNS))
restparam += (!isNegation)?" AND ":" OR ";
if(!"*".equals(sPropertyName))
restparam += ((!isNegation)?"":"NOT")+" localName(prop)=\""+sPropertyName+"\"";
if(!"*".equals(sPropertyNS))
restparam += ") ";
String uprop = this.pnamespace.getURI(sPropertyNS);
using = (sPropertyNS!=null && !"*".equals(sPropertyNS))?" USING NAMESPACE mbb = <http://s3b.corrib.org/mbb#>"+RDFQuery.RDFQ_ADD_NAMESPACE.toString((uprop==null)?"http://dummy.url/":uprop):"";
}
pparam = "prop";
valueQueryPart = rdfquery.toString(restparam);
}else {
//this is the type of browse when you match properties
rdfquery = (isInverse)?RDFQuery.RDFQ_BROWSE_LISTVALUES_VALUE_INVERSE:RDFQuery.RDFQ_BROWSE_LISTVALUES_VALUE;
pparam = "%1$s";
restparam = "";
using = "";
valueQueryPart = rdfquery.toString("%1$s");
}
ValueFilter values = new ValueFilter(sValues);
String valueSelectQuery = values.process(valueQueryPart, this.isIgnoreCase());
valueSelectQuery += using;
//no need for predefined properties
Repository.logger.info(valueSelectQuery);
Value[] avUris = SesameWrapper.performVectorQuery(inGraph, QueryLanguage.SERQL, valueSelectQuery);
Set<Value> ssubjectUris = new HashSet<Value>();
RDFQuery subjectRdfQuery;
if(isInverse)
subjectRdfQuery = (!"*".equals(sFullProperty))?RDFQuery.RDFQ_BROWSE_FILTEROBJECTS_INVERSE:RDFQuery.RDFQ_BROWSE_FILTEROBJECTS_VALUES_INVERSE;
else
subjectRdfQuery = (!"*".equals(sFullProperty))?RDFQuery.RDFQ_BROWSE_FILTEROBJECTS:RDFQuery.RDFQ_BROWSE_FILTEROBJECTS_VALUES;
String subjectQueryPart;
Value[] avResults;
for(Value value : avUris) {
String value_filter;
if(value instanceof BNode)
value_filter = "<_:"+value+">";
else if(value instanceof URI)
value_filter = "<"+value+">";
else
value_filter = "\""+value+"\"";
if(!"*".equals(sFullProperty))
subjectQueryPart = subjectRdfQuery.toString(pparam, ("".equals(restparam))?"TRUE":restparam, value_filter);
else {
String tmpQ = subjectRdfQuery.toString(value_filter, "%1$s");
subjectQueryPart = values.process(tmpQ, this.isIgnoreCase());
}
subjectQueryPart += using;
avResults = SesameWrapper.performVectorQuery(Repository.MAIN_REPOSITORY.getLocalRepository(),
QueryLanguage.SERQL,
subjectQueryPart);
ssubjectUris.addAll(Arrays.asList(avResults));
}
Value[] results = ssubjectUris.toArray(new Value[0]);
try {
SesameWrapper.loadResources(results, Repository.MAIN_REPOSITORY.getLocalRepository(), lr.getGraph());
} catch (AccessDeniedException e) {
e.printStackTrace();
}
this.annotateResults(lr, results);