Skip to content

Commit

Permalink
Merge pull request #77 from srdc/refactoring-query-builder
Browse files Browse the repository at this point in the history
Refactoring query builder
  • Loading branch information
tnamli authored Oct 10, 2024
2 parents 4fc375c + bbb0a62 commit 4a7f325
Show file tree
Hide file tree
Showing 21 changed files with 3,237 additions and 2,053 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ case class OnFhirNetworkClient(serverBaseUrl:String, interceptors:Seq[IHttpReque
nextPageParams.find {
case (pn, pv) =>
// Check if the parameter is either "_page" or "_skip"
(pn.contentEquals("_page") || pn.contentEquals("_skip") || pn.contentEquals("_searchafter")) &&
(pn.contentEquals("_page") || pn.contentEquals("_skip") || pn.contentEquals("_searchafter")) ||
// Ensure that either the parameter does not exist in the previous request,
// or it has a different value compared to the "next" link's parameter
(!previousPageParams.contains(pn) || previousPageParams(pn).toSet != pv.toSet)
Expand Down
1 change: 1 addition & 0 deletions onfhir-common/src/main/scala/io/onfhir/api/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ package object api {
val ABOVE = ":above"
val BELOW = ":below"
val TEXT = ":text"
val CODE_TEXT = ":code-text"
val NOT = ":not"
val TYPE = ":type"
val IDENTIFIER = ":identifier"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ class FHIRSearchParameterValueParser(fhirConfig: FhirServerConfig) {
.getOrElse(Set.empty)
possibleTargets.size match {
case 1 => possibleTargets.head
case _ => throw new InvalidParameterException(s"Invalid usage of chained search '$nameExpr', need type discriminator for parameter '$pname'! Syntax is erroneous, please see https://build.fhir.org/search.html#chaining !")
case _ =>
throw new InvalidParameterException(s"Invalid usage of chained search '$nameExpr', need type discriminator for parameter '$pname'! Syntax is erroneous, please see https://build.fhir.org/search.html#chaining !")
}
case 2 => chains.last.last
case 2 => c.last
case _ => throw new InvalidParameterException(s"Invalid usage of chained search '$nameExpr'! Syntax is erroneous, please see https://build.fhir.org/search.html#chaining !")
}
(lastTempRType, pname)
Expand Down
26 changes: 13 additions & 13 deletions onfhir-common/src/main/scala/io/onfhir/config/ResourceConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import io.onfhir.api.model.InternalEntity
* @param referencePolicies How this resource type uses FHIR references i.e. literal | logical | resolves | enforced | local
*/
case class ResourceConf(resource:String,
profile:Option[String],
supportedProfiles:Set[String],
interactions:Set[String],
searchParams:Set[String],
versioning:String,
readHistory:Boolean,
updateCreate:Boolean,
conditionalCreate:Boolean,
conditionalRead:String,
conditionalUpdate:Boolean,
conditionalDelete:String,
searchInclude:Set[String],
searchRevInclude:Set[String],
profile:Option[String] = None,
supportedProfiles:Set[String] = Set.empty,
interactions:Set[String] =Set.empty,
searchParams:Set[String] = Set.empty,
versioning:String = "no-version",
readHistory:Boolean = false,
updateCreate:Boolean = false,
conditionalCreate:Boolean = false,
conditionalRead:String = "not-supported",
conditionalUpdate:Boolean = false,
conditionalDelete:String = "not-supported",
searchInclude:Set[String] = Set.empty,
searchRevInclude:Set[String] = Set.empty,
referencePolicies:Set[String] = Set.empty[String]
) extends InternalEntity
2 changes: 1 addition & 1 deletion onfhir-core/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fhir {
search-total = "accurate"
# Default pagination mechanism
# 'page' --> Page based pagination e.g. _count=50&_page=4
# 'offset' --> Offset based pagination e.g. _count=500&_searchafter=65156168498
# 'offset' --> Offset/Cursor based pagination (cursor is MongoDB _id of resource) e.g. _count=500&_searchafter=65156168498
pagination = "page"
# Default value for [CapabilityStatement|Conformance].rest.resource.readHistory when not present in CapabilityStatenent for the resource type
# Indicates whether server can return past versions for FHIR vRead interaction. See https://www.hl7.org/fhir/capabilitystatement-definitions.html#CapabilityStatement.rest.resource.readHistory
Expand Down
71 changes: 71 additions & 0 deletions onfhir-core/src/main/scala/io/onfhir/db/AggregationUtil.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.onfhir.db

import org.mongodb.scala.bson.collection.immutable.Document
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.bson.{BsonArray, BsonDocument, BsonInt32, BsonString, BsonValue}

object AggregationUtil {
Expand Down Expand Up @@ -124,4 +126,73 @@ object AggregationUtil {
)
)
}

/**
* Construct Mongodb $lookup phase expression with both fields and pipeline
* @param col Collection name to join
* @param localFieldPath Path to the local field
* @param foreignFieldPath Path to the foreign field
* @param pipeline Pipeline to execute (at least one should be provided)
* @param as The results name
* @return
*/
def constructLookupPhaseExpression(col:String, localFieldPath:String, foreignFieldPath:String, pipeline:Seq[BsonDocument], as:String):BsonDocument = {
BsonDocument(
"$lookup" -> BsonDocument.apply(
"from" -> BsonString(col),
"localField" -> BsonString(localFieldPath),
"foreignField" -> BsonString(foreignFieldPath),
"pipeline" -> BsonArray.fromIterable(pipeline),
"as" -> BsonString(as)
)
)
}

/**
* Construct Mongodb $lookup phase expression with both fields and pipeline and let
*
* @param col Collection name to join
* @param localFieldPath Path to the local field
* @param foreignFieldPath Path to the foreign field
* @param letVariablePathMap Variable name -> path e.g. rid --> id, rtype --> resourceType
* @param pipeline Pipeline to execute (at least one should be provided)
* @param as The results name
* @return
*/
def constructLookupPhaseExpression(col: String, localFieldPath: String, foreignFieldPath: String, letVariablePathMap:Map[String, String], pipeline: Seq[BsonDocument], as: String): BsonDocument = {
BsonDocument(
"$lookup" -> BsonDocument.apply(
"from" -> BsonString(col),
"localField" -> BsonString(localFieldPath),
"foreignField" -> BsonString(foreignFieldPath),
"let" -> BsonDocument(letVariablePathMap.map(v => v._1 -> BsonString("$"+v._2))),
"pipeline" -> BsonArray.fromIterable(pipeline),
"as" -> BsonString(as)
)
)
}

/**
* Construct the Mongodb expression for checking if given array field size is larger than given value
* @param field Field name
* @param size Size
* @return
*/
def constructGreaterThanSizeExpression(field:String, size:Int):BsonDocument = {
BsonDocument(field -> BsonDocument("$gt" -> BsonDocument("$size" -> BsonInt32(size))))
}

/**
*
* @param field1
* @param field2
* @return
*/
def constructAggEqual(field1:BsonValue, field2:BsonValue):BsonDocument = {
BsonDocument("$eq" -> BsonArray(field1,field2))
}

def constructAggNotEqual(field1:BsonValue, field2:BsonValue): BsonDocument = {
BsonDocument("$ne" -> BsonArray(field1,field2))
}
}
Loading

0 comments on commit 4a7f325

Please sign in to comment.