diff options
author | vlad <vlad@driver.xyz> | 2017-06-13 16:12:20 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-06-13 16:12:20 -0700 |
commit | cd1b635b2ae90d9ac2d8b1779183a1fbd8c5fd5c (patch) | |
tree | 062e8dad1a1513e26b0fd08b1742d6ff2ee874f7 /src/main/scala/xyz/driver/pdsuidomain/storage | |
parent | 0000a65ab4479a2a40e2d6468036438e9705b4aa (diff) | |
download | rest-query-cd1b635b2ae90d9ac2d8b1779183a1fbd8c5fd5c.tar.gz rest-query-cd1b635b2ae90d9ac2d8b1779183a1fbd8c5fd5c.tar.bz2 rest-query-cd1b635b2ae90d9ac2d8b1779183a1fbd8c5fd5c.zip |
Adding domain entitiesv0.1.0
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/storage')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala | 27 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala | 52 |
2 files changed, 79 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala b/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala new file mode 100644 index 0000000..d50be2c --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala @@ -0,0 +1,27 @@ +package xyz.driver.pdsuidomain.storage + +import com.google.cloud.storage.StorageOptions +import com.typesafe.scalalogging.StrictLogging +import xyz.driver.pdsuidomain.entities.MedicalRecord.PdfSource + +import scala.concurrent.{ExecutionContext, Future, blocking} + +object MedicalRecordDocumentStorage extends StrictLogging { + private val storage = StorageOptions.getDefaultInstance.getService + + def fetchPdf(bucket: String, path: String) + (implicit ec: ExecutionContext): Future[PdfSource] = { + logger.trace(s"fetchPdf(bucket=$bucket, path=$path)") + Future { + blocking { + Option(storage.get(bucket, path)) match { + case Some(blob) => + PdfSource.Channel(() => blob.reader()) + case None => + logger.error(s"Failed to find the pdf file $path in bucket: $bucket") + PdfSource.Empty + } + } + } + } +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala b/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala new file mode 100644 index 0000000..d9651ca --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala @@ -0,0 +1,52 @@ +package xyz.driver.pdsuidomain.storage + +import xyz.driver.pdsuicommon.domain.{LongId, UuidId} +import xyz.driver.pdsuicommon.logging._ +import xyz.driver.pdsuidomain.entities.{Arm, Patient} + +import scala.collection.concurrent.TrieMap + +object RequestStorage { + type Key = (UuidId[Patient], String) + type Value = Set[LongId[Arm]] +} + +class RequestStorage extends PhiLogging { + import RequestStorage._ + + private val storage = new TrieMap[Key, Value]() + + def put(patientId: UuidId[Patient], + disease: String, + ineligibleArms: Set[LongId[Arm]]): Unit = { + logger.debug(phi"put($patientId, ${Unsafe(disease)}, $ineligibleArms") + val key = (patientId, disease.toLowerCase) + get(patientId, disease.toLowerCase) match { + case Some(oldValue) => + logger.trace(phi"Requested ineligible arms=$oldValue, replace it") + storage.replace(key, oldValue, ineligibleArms) + case None => + logger.trace(phi"Put request into storage") + storage.put(key, ineligibleArms) + } + } + + def get(patientId: UuidId[Patient], disease: String): Option[Value] = { + logger.debug(phi"get($patientId,${Unsafe(disease)}") + val key = (patientId, disease.toLowerCase) + storage.get(key) + } + + def contains(patientId: UuidId[Patient], + disease: String, + value: Set[LongId[Arm]]): Boolean = { + logger.debug(phi"contains(key=($patientId,${Unsafe(disease)}),value=$value") + get(patientId, disease.toLowerCase).contains(value) + } + + def remove(patientId: UuidId[Patient], disease: String): Unit = { + logger.debug(phi"remove($patientId,${Unsafe(disease)}") + val key = (patientId, disease.toLowerCase) + storage.remove(key) + } +} |