private void processSearchSimple(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); List<Resource> list = new ArrayList<Resource>(); services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), field.getArguments(), list); Argument arg = null; ObjectValue obj = null; List<Resource> vl = filterResources(field.argument("fhirpath"), list); if (!vl.isEmpty()) { arg = target.addField(field.getAlias()+suffix, listStatus(field, true)); for (Resource v : vl) { obj = new ObjectValue(); arg.addValue(obj); processObject(v, v, obj, field.getSelectionSet(), inheritedList, suffix); } } }
private Field parseField() throws EGraphQLException, IOException { Field result = new Field(); result.setName(consumeName()); result.setAlias(result.getName()); if (hasPunctuation(":")) { consumePunctuation(":"); result.setName(consumeName()); } if (hasPunctuation("(")) { consumePunctuation("("); while (!hasPunctuation(")")) result.getArguments().add(parseArgument()); consumePunctuation(")"); } while (hasPunctuation("@")) result.getDirectives().add(parseDirective()); if (hasPunctuation("{")) { consumePunctuation("{"); do { result.getSelectionSet().add(parseSelection()); } while (!hasPunctuation("}")); consumePunctuation("}"); } return result; }
Argument arg = null; ExpressionNode expression = null; if (sel.getField().hasDirective("slice")) { Directive dir = sel.getField().directive("slice"); String s = ((StringValue) dir.getArguments().get(0).getValues().get(0)).getValue(); if (s.equals("$index")) if (sel.getField().hasDirective("flatten")) // special: instruction to drop this node... il = prop.isList() && !sel.getField().hasDirective("first"); else if (sel.getField().hasDirective("first")) { if (expression != null) throw new FHIRException("You cannot mix @slice and @first"); arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), inheritedList)); } else if (expression == null) arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), prop.isList() || inheritedList)); else ss = suffix+'.'+fpe.evaluateToString(null, null, value, expression); if (!sel.getField().hasDirective("flatten")) arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), prop.isList() || inheritedList)); if (!sel.getField().getSelectionSet().isEmpty()) throw new EGraphQLException("Encountered a selection set on a scalar field type"); processPrimitive(arg, value); } else { if (sel.getField().getSelectionSet().isEmpty()) throw new EGraphQLException("No Fields selected on a complex object"); if (arg == null) processObject(context, value, target, sel.getField().getSelectionSet(), il, ss);
private void processReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (!(source instanceof Reference)) throw new EGraphQLException("Not done yet"); if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); Reference ref = (Reference) source; ReferenceResolution res = services.lookup(appInfo, context, ref); if (res != null) { if (targetTypeOk(field.getArguments(), res.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
private void processObject(Resource context, Base source, ObjectValue target, List<Selection> selection, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { for (Selection sel : selection) { if (sel.getField() != null) { if (checkDirectives(sel.getField().getDirectives())) { Property prop = source.getNamedProperty(sel.getField().getName()); if ((prop == null) && sel.getField().getName().startsWith("_")) prop = source.getNamedProperty(sel.getField().getName().substring(1)); if (prop == null) { if ((sel.getField().getName().equals("resourceType") && source instanceof Resource)) target.addField("resourceType", listStatus(sel.getField(), false)).addValue(new StringValue(source.fhirType())); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("Reference"))) processReference(context, source, sel.getField(), target, inheritedList, suffix); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("canonical"))) processCanonicalReference(context, source, sel.getField(), target, inheritedList, suffix); else if (isResourceName(sel.getField().getName(), "List") && (source instanceof Resource)) processReverseReferenceList((Resource) source, sel.getField(), target, inheritedList, suffix); else if (isResourceName(sel.getField().getName(), "Connection") && (source instanceof Resource)) processReverseReferenceSearch((Resource) source, sel.getField(), target, inheritedList, suffix); else throw new EGraphQLException("Unknown property "+sel.getField().getName()+" on "+source.fhirType()); } else { if (!isPrimitive(prop.getTypeCode()) && sel.getField().getName().startsWith("_")) throw new EGraphQLException("Unknown property "+sel.getField().getName()+" on "+source.fhirType()); List<Base> vl = filter(context, prop, sel.getField().getArguments(), prop.getValues(), sel.getField().getName().startsWith("_")); if (!vl.isEmpty()) processValues(context, sel, prop, target, vl, sel.getField().getName().startsWith("_"), inheritedList, suffix);
private void processSearch(ObjectValue target, List<Selection> selection, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { for (Selection sel : selection) { if ((sel.getField() == null)) throw new EGraphQLException("Only field selections are allowed in this context"); checkNoDirectives(sel.getField().getDirectives()); if ((isResourceName(sel.getField().getName(), ""))) processSearchSingle(target, sel.getField(), inheritedList, suffix); else if ((isResourceName(sel.getField().getName(), "List"))) processSearchSimple(target, sel.getField(), inheritedList, suffix); else if ((isResourceName(sel.getField().getName(), "Connection"))) processSearchFull(target, sel.getField(), inheritedList, suffix); } }
private ArgumentListStatus listStatus(Field field, boolean isList) { if (field.hasDirective("singleton")) return ArgumentListStatus.SINGLETON; else if (isList) return ArgumentListStatus.REPEATING; else return ArgumentListStatus.NOT_SPECIFIED; }
private void processCanonicalReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (!(source instanceof CanonicalType)) throw new EGraphQLException("Not done yet"); if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); Reference ref = new Reference(source.primitiveValue()); ReferenceResolution res = services.lookup(appInfo, context, ref); if (res != null) { if (targetTypeOk(field.getArguments(), res.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
private void processObject(Resource context, Base source, ObjectValue target, List<Selection> selection, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { for (Selection sel : selection) { if (sel.getField() != null) { if (checkDirectives(sel.getField().getDirectives())) { Property prop = source.getNamedProperty(sel.getField().getName()); if ((prop == null) && sel.getField().getName().startsWith("_")) prop = source.getNamedProperty(sel.getField().getName().substring(1)); if (prop == null) { if ((sel.getField().getName().equals("resourceType") && source instanceof Resource)) target.addField("resourceType", listStatus(sel.getField(), false)).addValue(new StringValue(source.fhirType())); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("Reference"))) processReference(context, source, sel.getField(), target, inheritedList, suffix); else if ((sel.getField().getName().equals("resource") && source.fhirType().equals("canonical"))) processCanonicalReference(context, source, sel.getField(), target, inheritedList, suffix); else if (isResourceName(sel.getField().getName(), "List") && (source instanceof Resource)) processReverseReferenceList((Resource) source, sel.getField(), target, inheritedList, suffix); else if (isResourceName(sel.getField().getName(), "Connection") && (source instanceof Resource)) processReverseReferenceSearch((Resource) source, sel.getField(), target, inheritedList, suffix); else throw new EGraphQLException("Unknown property "+sel.getField().getName()+" on "+source.fhirType()); } else { if (!isPrimitive(prop.getTypeCode()) && sel.getField().getName().startsWith("_")) throw new EGraphQLException("Unknown property "+sel.getField().getName()+" on "+source.fhirType()); List<Base> vl = filter(context, prop, sel.getField().getArguments(), prop.getValues(), sel.getField().getName().startsWith("_")); if (!vl.isEmpty()) processValues(context, sel, prop, target, vl, sel.getField().getName().startsWith("_"), inheritedList, suffix);
private void processSearch(ObjectValue target, List<Selection> selection, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { for (Selection sel : selection) { if ((sel.getField() == null)) throw new EGraphQLException("Only field selections are allowed in this context"); checkNoDirectives(sel.getField().getDirectives()); if ((isResourceName(sel.getField().getName(), ""))) processSearchSingle(target, sel.getField(), inheritedList, suffix); else if ((isResourceName(sel.getField().getName(), "List"))) processSearchSimple(target, sel.getField(), inheritedList, suffix); else if ((isResourceName(sel.getField().getName(), "Connection"))) processSearchFull(target, sel.getField(), inheritedList, suffix); } }
private ArgumentListStatus listStatus(Field field, boolean isList) { if (field.hasDirective("singleton")) return ArgumentListStatus.SINGLETON; else if (isList) return ArgumentListStatus.REPEATING; else return ArgumentListStatus.NOT_SPECIFIED; }
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 = 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); }
private Field parseField() throws EGraphQLException, IOException { Field result = new Field(); result.setName(consumeName()); result.setAlias(result.getName()); if (hasPunctuation(":")) { consumePunctuation(":"); result.setName(consumeName()); } if (hasPunctuation("(")) { consumePunctuation("("); while (!hasPunctuation(")")) result.getArguments().add(parseArgument()); consumePunctuation(")"); } while (hasPunctuation("@")) result.getDirectives().add(parseDirective()); if (hasPunctuation("{")) { consumePunctuation("{"); do { result.getSelectionSet().add(parseSelection()); } while (!hasPunctuation("}")); consumePunctuation("}"); } return result; }
private void processReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (!(source instanceof Reference)) throw new EGraphQLException("Not done yet"); if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); Reference ref = (Reference) source; ReferenceResolution res = services.lookup(appInfo, context, ref); if (res != null) { if (targetTypeOk(field.getArguments(), res.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
Argument arg = null; ExpressionNode expression = null; if (sel.getField().hasDirective("slice")) { Directive dir = sel.getField().directive("slice"); String s = ((StringValue) dir.getArguments().get(0).getValues().get(0)).getValue(); if (s.equals("$index")) if (sel.getField().hasDirective("flatten")) // special: instruction to drop this node... il = prop.isList() && !sel.getField().hasDirective("first"); else if (sel.getField().hasDirective("first")) { if (expression != null) throw new FHIRException("You cannot mix @slice and @first"); arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), inheritedList)); } else if (expression == null) arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), prop.isList() || inheritedList)); else ss = suffix+'.'+fpe.evaluateToString(null, null, value, expression); if (!sel.getField().hasDirective("flatten")) arg = target.addField(sel.getField().getAlias()+suffix, listStatus(sel.getField(), prop.isList() || inheritedList)); if (!sel.getField().getSelectionSet().isEmpty()) throw new EGraphQLException("Encountered a selection set on a scalar field type"); processPrimitive(arg, value); } else { if (sel.getField().getSelectionSet().isEmpty()) throw new EGraphQLException("No Fields selected on a complex object"); if (arg == null) processObject(context, value, target, sel.getField().getSelectionSet(), il, ss);
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 = 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); }
private void processCanonicalReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (!(source instanceof CanonicalType)) throw new EGraphQLException("Not done yet"); if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); Reference ref = new Reference(source.primitiveValue()); ReferenceResolution res = services.lookup(appInfo, context, ref); if (res != null) { if (targetTypeOk(field.getArguments(), res.target)) { Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList)); ObjectValue obj = new ObjectValue(); arg.addValue(obj); processObject(res.targetContext, res.target, obj, field.getSelectionSet(), inheritedList, suffix); } } else if (!hasArgument(field.getArguments(), "optional", "true")) throw new EGraphQLException("Unable to resolve reference to "+ref.getReference()); }
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 = 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); }
List<Argument> params = new ArrayList<Argument>(); Argument parg = null; for (Argument a : field.getArguments()) if (!(a.getName().equals("_reference"))) params.add(a); arg.setName(getSingleValue(parg)); arg.addValue(new StringValue(source.fhirType()+"/"+source.getId())); services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), params, list); arg = null; ObjectValue obj = null; List<Resource> vl = filterResources(field.argument("fhirpath"), list); if (!vl.isEmpty()) { arg = target.addField(field.getAlias()+suffix, listStatus(field, true)); for (Resource v : vl) { obj = new ObjectValue(); arg.addValue(obj); processObject(v, v, obj, field.getSelectionSet(), inheritedList, suffix);
private void processSearchSimple(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException { if (services == null) throw new EGraphQLException("Resource Referencing services not provided"); List<Resource> list = new ArrayList<Resource>(); services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), field.getArguments(), list); Argument arg = null; ObjectValue obj = null; List<Resource> vl = filterResources(field.argument("fhirpath"), list); if (!vl.isEmpty()) { arg = target.addField(field.getAlias()+suffix, listStatus(field, true)); for (Resource v : vl) { obj = new ObjectValue(); arg.addValue(obj); processObject(v, v, obj, field.getSelectionSet(), inheritedList, suffix); } } }