diff options
author | kseniya <ktomskih@datamonsters.co> | 2017-09-20 17:58:19 +0700 |
---|---|---|
committer | kseniya <ktomskih@datamonsters.co> | 2017-09-20 17:58:19 +0700 |
commit | d82c93fef0fc0bb937220334f73c264fbb1082f2 (patch) | |
tree | 44e957d95b0d5a9c9edcbd84e9e005bc89d95264 /src/main/scala/xyz/driver/pdsuicommon/synchronization/utils | |
parent | b9f3ab180253c346664f4f38b418dc96f54bdc31 (diff) | |
download | rest-query-d82c93fef0fc0bb937220334f73c264fbb1082f2.tar.gz rest-query-d82c93fef0fc0bb937220334f73c264fbb1082f2.tar.bz2 rest-query-d82c93fef0fc0bb937220334f73c264fbb1082f2.zip |
Common code for synchronizers
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/synchronization/utils')
3 files changed, 102 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/FakeIdGen.scala b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/FakeIdGen.scala new file mode 100644 index 0000000..196aab1 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/FakeIdGen.scala @@ -0,0 +1,26 @@ +package xyz.driver.pdsuicommon.synchronization.utils + +import xyz.driver.pdsuicommon.synchronization.domain.FakeId + +/** + * Used to generate a fake id from an entity. + * A fake id is used in comparison between entities with different types, + * for example, RawTrial and Trial. + * + * @see FakeId + */ +trait FakeIdGen[-T] extends (T => FakeId) { + + def getFor(x: T): FakeId + + override def apply(x: T): FakeId = getFor(x) + +} + +object FakeIdGen { + + def create[T](f: T => FakeId) = new FakeIdGen[T] { + override def getFor(x: T): FakeId = f(x) + } + +} diff --git a/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/Refiner.scala b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/Refiner.scala new file mode 100644 index 0000000..768b889 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/Refiner.scala @@ -0,0 +1,12 @@ +package xyz.driver.pdsuicommon.synchronization.utils + +/** + * Allows to extract a data from the From entity to convert/update in to the To entity. + */ +trait Refiner[-From, To] { + + def refine(raw: From): To + + def refresh(orig: To, update: From): To + +} diff --git a/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/package.scala b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/package.scala new file mode 100644 index 0000000..1b30158 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/synchronization/utils/package.scala @@ -0,0 +1,64 @@ +package xyz.driver.pdsuicommon.synchronization + +import java.net.URL +import java.nio.ByteBuffer +import java.util.UUID + +import xyz.driver.pdsuicommon.domain.{LongId, UuidId} +import xyz.driver.pdsuicommon.http.HttpFetcher +import xyz.driver.pdsuicommon.json.JsonSerializer +import xyz.driver.pdsuicommon.synchronization.domain.FakeId + +import scala.collection.breakOut +import scala.concurrent.{ExecutionContext, Future} +import scala.io.Codec + +package object utils { + + type FakeIdMap[T] = Map[FakeId, T] + + object FakeIdMap { + + def empty[T]: FakeIdMap[T] = Map.empty + + def create[T](xs: Seq[T])(implicit fakeIdExtractor: FakeIdGen[T]): FakeIdMap[T] = { + xs.map({ x => + fakeIdExtractor.getFor(x) -> x + })(breakOut) + } + + } + + /** + * Requests domain objects from the repository using + * ids of fetched dictionary entities + * + * @param getList repository access function + * @param xs sequence of entity objects + * @param id function that extracts id from the entity + * @tparam Id Type of Id (for example [[LongId]], [[UuidId]]) + * @tparam K Type parameter for Id + * @tparam D Domain object type name + * @tparam E Dictionary entity object type name + */ + def domainFromEntities[K, D, E, Id[_]](getList: Set[Id[K]] => Seq[D], xs: Seq[E])(id: E => Id[K]): Seq[D] = { + getList(xs.map(x => id(x)).toSet) + } + + /** Version of [[domainFromEntities]] for LongId */ + def domainFromEntitiesLong[K, D, E](getList: Set[LongId[K]] => Seq[D], xs: Seq[E])(id: E => Long): Seq[D] = { + domainFromEntities(getList, xs)(e => LongId(id(e))) + } + + /** Version of [[domainFromEntities]] for UuidId */ + def domainFromEntitiesUUID[K, D, E](getList: Set[UuidId[K]] => Seq[D], xs: Seq[E])(id: E => UUID): Seq[D] = { + domainFromEntities(getList, xs)(e => UuidId(id(e))) + } + + def fetch[T](httpFetcher: HttpFetcher, url: URL)(implicit m: Manifest[T], ec: ExecutionContext): Future[T] = { + httpFetcher(url).map { rawContent => + val content = Codec.UTF8.decoder.decode(ByteBuffer.wrap(rawContent)).toString + JsonSerializer.deserialize[T](content) + } + } +} |