use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method filter.
private List<Base> filter(Resource context, Property prop, String fieldName, List<Argument> arguments, List<Base> values, boolean extensionMode) throws FHIRException, EGraphQLException {
List<Base> result = new ArrayList<Base>();
if (values.size() > 0) {
int count = Integer.MAX_VALUE;
int offset = 0;
StringBuilder fp = new StringBuilder();
for (Argument arg : arguments) {
List<Value> vl = resolveValues(arg);
if ((vl.size() != 1))
throw new EGraphQLException("Incorrect number of arguments");
if (values.get(0).isPrimitive())
throw new EGraphQLException("Attempt to use a filter (" + arg.getName() + ") on a primtive type (" + prop.getTypeCode() + ")");
if ((arg.getName().equals("fhirpath")))
fp.append(" and " + vl.get(0).toString());
else if ((arg.getName().equals("_count")))
count = Integer.valueOf(vl.get(0).toString());
else if ((arg.getName().equals("_offset")))
offset = Integer.valueOf(vl.get(0).toString());
else {
Property p = values.get(0).getNamedProperty(arg.getName());
if (p == null)
throw new EGraphQLException("Attempt to use an unknown filter (" + arg.getName() + ") on a type (" + prop.getTypeCode() + ")");
fp.append(" and " + arg.getName() + " = '" + vl.get(0).toString() + "'");
}
}
// Account for situations where the GraphQL expression selected e.g.
// effectiveDateTime but the field contains effectivePeriod
String propName = prop.getName();
List<Base> newValues = new ArrayList<>(values.size());
for (Base value : values) {
if (propName.endsWith("[x]")) {
String propNameShortened = propName.substring(0, propName.length() - 3);
if (fieldName.startsWith(propNameShortened) && fieldName.length() > propNameShortened.length()) {
if (!value.fhirType().equalsIgnoreCase(fieldName.substring(propNameShortened.length()))) {
continue;
}
}
}
newValues.add(value);
}
int i = 0;
int t = 0;
if (fp.length() == 0)
for (Base v : newValues) {
if ((i >= offset) && passesExtensionMode(v, extensionMode)) {
result.add(v);
t++;
if (t >= count)
break;
}
i++;
}
else {
ExpressionNode node = fpe.parse(fp.substring(5));
for (Base v : newValues) {
if ((i >= offset) && passesExtensionMode(v, extensionMode) && fpe.evaluateToBoolean(null, context, v, node)) {
result.add(v);
t++;
if (t >= count)
break;
}
i++;
}
}
}
return result;
}
use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processSearchFull.
private void processSearchFull(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
List<Argument> params = new ArrayList<Argument>();
Argument carg = null;
for (Argument arg : field.getArguments()) if (arg.getName().equals("cursor"))
carg = arg;
else
params.add(arg);
if ((carg != null)) {
params.clear();
;
String[] parts = getSingleValue(carg).split(":");
params.add(new Argument("search-id", new StringValue(parts[0])));
params.add(new Argument("search-offset", new StringValue(parts[1])));
}
Bundle bnd = (Bundle) services.search(appInfo, field.getName().substring(0, field.getName().length() - 10), params);
SearchWrapper bndWrapper = new SearchWrapper(field.getName(), bnd);
Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
ObjectValue obj = new ObjectValue();
arg.addValue(obj);
processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix);
}
use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processReverseReferenceSearch.
private void processReverseReferenceSearch(Resource source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
List<Argument> params = new ArrayList<Argument>();
Argument parg = null;
for (Argument a : field.getArguments()) if (!(a.getName().equals("_reference")))
params.add(a);
else if ((parg == null))
parg = a;
else
throw new EGraphQLException("Duplicate parameter _reference");
if (parg == null)
throw new EGraphQLException("Missing parameter _reference");
Argument arg = new Argument();
params.add(arg);
arg.setName(getSingleValue(parg));
arg.addValue(new StringValue(source.fhirType() + "/" + source.getId()));
Bundle bnd = (Bundle) services.search(appInfo, field.getName().substring(0, field.getName().length() - 10), params);
Base bndWrapper = new SearchWrapper(field.getName(), bnd);
arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
ObjectValue obj = new ObjectValue();
arg.addValue(obj);
processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix);
}
use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method filterResources.
private List<Resource> filterResources(Argument fhirpath, Bundle bnd) throws EGraphQLException, FHIRException {
List<Resource> result = new ArrayList<Resource>();
if (bnd.getEntry().size() > 0) {
if ((fhirpath == null))
for (BundleEntryComponent be : bnd.getEntry()) result.add(be.getResource());
else {
FHIRPathEngine fpe = new FHIRPathEngine(context);
ExpressionNode node = fpe.parse(getSingleValue(fhirpath));
for (BundleEntryComponent be : bnd.getEntry()) if (fpe.evaluateToBoolean(null, be.getResource(), be.getResource(), node))
result.add(be.getResource());
}
}
return result;
}
use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processSearchSingle.
private void processSearchSingle(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
if (services == null)
throw new EGraphQLException("Resource Referencing services not provided");
String id = "";
for (Argument arg : field.getArguments()) if ((arg.getName().equals("id")))
id = getSingleValue(arg);
else
throw new EGraphQLException("Unknown/invalid parameter " + arg.getName());
if (Utilities.noString(id))
throw new EGraphQLException("No id found");
Resource res = (Resource) services.lookup(appInfo, field.getName(), id);
if (res == null)
throw new EGraphQLException("Resource " + field.getName() + "/" + id + " not found");
Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
ObjectValue obj = new ObjectValue();
arg.addValue(obj);
processObject(res, res, obj, field.getSelectionSet(), inheritedList, suffix);
}
Aggregations