Skip to content

Commit

Permalink
Cleaning up findAll API
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 21, 2019
1 parent 8867dce commit 6c1babc
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 47 deletions.
4 changes: 1 addition & 3 deletions src/main/scala/org/mbari/vars/annotation/api/APIStack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import java.time.format.DateTimeFormatter
import java.time.{Duration, Instant}
import java.util.UUID
import java.util.concurrent.TimeUnit
import java.util.function.{Consumer, Function}
import java.util.stream.Stream

import javax.servlet.http.HttpServletResponse
import org.mbari.vars.annotation.Constants
Expand All @@ -33,7 +31,7 @@ import org.slf4j.LoggerFactory

import scala.concurrent.duration.{Duration => SDuration}
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success, Try}
import scala.util.Try

/**
* All Api classes should mixin this trait. It defines the common traits used by all implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class ImagedMomentV1Api(controller: ImagedMomentController)(implicit val executo
}

get("/") {
val limit = params.getAs[Int]("limit").getOrElse(1000)
val offset = params.getAs[Int]("offset").getOrElse(0)
val limit = params.getAs[Int]("limit").orElse(Some(100))
val offset = params.getAs[Int]("offset").orElse(Some(0))
controller.findAll(limit, offset)
.map(_.asJava)
.map(toJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ class AnnotationController(daoFactory: BasicDAOFactory) {
endTimestamp: Instant,
limit: Option[Int] = None,
offset: Option[Int] = None): (Closeable, java.util.stream.Stream[Annotation]) = {
val dao = daoFactory.newImagedMomentDAO()
// val dao = daoFactory.newImagedMomentDAO()
// (() => dao.close(),
// dao.streamByVideoReferenceUUIDAndTimestamps(videoReferenceUuid, startTimestamp, endTimestamp, limit, offset)
// .flatMap(im => im.javaObservations.stream().map(obs => AnnotationImpl(obs))))
val dao = daoFactory.newObservationDAO()
(() => dao.close(),
dao.streamByVideoReferenceUUIDAndTimestamps(videoReferenceUuid, startTimestamp, endTimestamp, limit, offset)
.flatMap(im => im.javaObservations.stream().map(obs => AnnotationImpl(obs))))
.map(obs => AnnotationImpl(obs)))
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ trait BaseController[A <: PersistentObject, B <: DAO[A]] {
exec(fn)
}

def findAll()(implicit ec: ExecutionContext): Future[Iterable[A]] =
exec(d => d.findAll())
def findAll(limit: Option[Int] = None, offset: Option[Int] = None)(implicit ec: ExecutionContext): Future[Iterable[A]] =
exec(d => d.findAll(limit, offset))

def findByUUID(uuid: UUID)(implicit ec: ExecutionContext): Future[Option[A]] =
exec(d => d.findByUUID(uuid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ImagedMomentController(val daoFactory: BasicDAOFactory)

override def newDAO(): IMDAO = daoFactory.newImagedMomentDAO()

def findAll(limit: Int, offset: Int)(implicit ec: ExecutionContext): Future[Iterable[ImagedMoment]] =
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None)(implicit ec: ExecutionContext): Future[Iterable[ImagedMoment]] =
exec(d => d.findAll(limit, offset))

def findAllVideoReferenceUUIDs(limit: Option[Int] = None, offset: Option[Int] = None)(implicit ec: ExecutionContext): Future[Iterable[UUID]] =
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/org/mbari/vars/annotation/dao/DAO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ trait DAO[A <: PersistentObject] {
def delete(entity: A): Unit
def deleteByUUID(primaryKey: UUID): Unit
def findByUUID(primaryKey: UUID): Option[A]
def findAll(): Iterable[A]
def findAll(limit: Int, offset: Int): Iterable[A]
def findAll(limit: Option[Int] = None, offset:Option[Int] = None): Iterable[A]
def runTransaction[R](fn: this.type => R)(implicit ec: ExecutionContext): Future[R]
def close(): Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ class AssociationDAOImpl(entityManager: EntityManager)
}
}

override def findAll(): Iterable[AssociationImpl] =
findByNamedQuery("Association.findAll")

override def findAll(limit: Int, offset: Int): Iterable[AssociationImpl] =
findByNamedQuery("Association.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[AssociationImpl] =
findByNamedQuery("Association.findAll", limit = limit, offset = offset)

override def countByToConcept(toConcept: String): Int = {
//val query = entityManager.createNativeQuery("Association.countByToConcept")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ class CachedAncillaryDatumDAOImpl(entityManager: EntityManager)
override def asPersistentObject(datum: CachedAncillaryDatum): CachedAncillaryDatum =
CachedAncillaryDatumImpl(datum)

override def findAll(): Iterable[CachedAncillaryDatumImpl] =
findByNamedQuery("AncillaryDatum.findAll")

override def findAll(limit: Int, offset: Int): Iterable[CachedAncillaryDatumImpl] =
findByNamedQuery("AncillaryDatum.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[CachedAncillaryDatumImpl] =
findByNamedQuery("AncillaryDatum.findAll", limit = limit, offset = offset)

override def findByObservationUUID(observationUuid: UUID): Option[CachedAncillaryDatum] =
findByNamedQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ class CachedVideoReferenceInfoDAOImpl(entityManager: EntityManager)
override def findByVideoReferenceUUID(uuid: UUID): Option[CachedVideoReferenceInfoImpl] =
findByNamedQuery("VideoReferenceInfo.findByVideoReferenceUUID", Map("uuid" -> uuid)).headOption

override def findAll(): Iterable[CachedVideoReferenceInfoImpl] =
findByNamedQuery("VideoReferenceInfo.findAll")

override def findAll(limit: Int, offset: Int): Iterable[CachedVideoReferenceInfoImpl] =
findByNamedQuery("VideoReferenceInfo.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[CachedVideoReferenceInfoImpl] =
findByNamedQuery("VideoReferenceInfo.findAll", limit = limit, offset = offset)

override def findAllVideoReferenceUUIDs(): Iterable[UUID] =
fetchUsing("VideoReferenceInfo.findAllVideoReferenceUUIDs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ class ImageReferenceDAOImpl(entityManager: EntityManager)
imageReference
}

override def findAll(): Iterable[ImageReferenceImpl] =
findByNamedQuery("ImageReference.findAll")

override def findAll(limit: Int, offset: Int): Iterable[ImageReferenceImpl] =
findByNamedQuery("ImageReference.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[ImageReferenceImpl] =
findByNamedQuery("ImageReference.findAll", limit = limit, offset = offset)

override def findByURL(url: URL): Option[ImageReferenceImpl] =
findByNamedQuery("ImageReference.findByURL", Map("url" -> url)).headOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,9 @@ class ImagedMomentDAOImpl(entityManager: EntityManager)
// override def findByUUID(primaryKey: UUID): Option[ImagedMomentImpl] =
// findByNamedQuery("ImagedMoment.findByUUID", Map("uuid" -> primaryKey)).headOption

override def findAll(): Iterable[ImagedMomentImpl] =
findByNamedQuery("ImagedMoment.findAll")

override def findAll(limit: Int, offset: Int): Iterable[ImagedMomentImpl] =
findByNamedQuery("ImagedMoment.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[ImagedMomentImpl] =
findByNamedQuery("ImagedMoment.findAll", limit = limit, offset = offset)

override def findByVideoReferenceUUIDAndElapsedTime(uuid: UUID, elapsedTime: Duration): Option[ImagedMomentImpl] =
findByNamedQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import scala.collection.JavaConverters._
),
new NamedQuery(
name = "ImagedMoment.findByVideoReferenceUUIDAndTimestamps",
query = "SELECT i FROM ImagedMoment i WHERE i.videoReferenceUUID = :uuid AND i.recordedDate BETWEEN :start AND :end"
query = "SELECT i FROM ImagedMoment i WHERE i.videoReferenceUUID = :uuid AND i.recordedDate BETWEEN :start AND :end ORDER BY i.recordedDate"
),
new NamedQuery(
name = "ImagedMoment.findWithImageReferences",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class IndexDAOImpl(entityManager: EntityManager)

// --- These methods are deliberately overridden ---

override def findAll(): Iterable[IndexImpl] = ???

override def findAll(limit: Int, offset: Int): Iterable[IndexImpl] = ???
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[IndexImpl] = ???

override def deleteByUUID(primaryKey: UUID): Unit = ???

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ class ObservationDAOImpl(entityManager: EntityManager)
.filter(_ != null)
.map(_.toString)

override def findAll(): Iterable[ObservationImpl] =
findByNamedQuery("Observation.findAll")

override def findAll(limit: Int, offset: Int): Iterable[ObservationImpl] =
findByNamedQuery("Observation.findAll", limit = Some(limit), offset = Some(offset))
override def findAll(limit: Option[Int] = None, offset: Option[Int] = None): Iterable[ObservationImpl] =
findByNamedQuery("Observation.findAll", limit = limit, offset = offset)

override def findAllConceptsByVideoReferenceUUID(uuid: UUID): Seq[String] = {
val query = entityManager.createNamedQuery("Observation.findAllNamesByVideoReferenceUUID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import org.mbari.vars.annotation.model.{Association, ImagedMoment, Observation}
query = "SELECT o FROM Observation o LEFT JOIN o.imagedMoment i WHERE i.videoReferenceUUID = :uuid ORDER BY o.uuid"),
new NamedQuery(
name = "Observation.findByVideoReferenceUUIDAndTimestamps",
query = "SELECT o FROM Observation o LEFT JOIN o.imagedMoment i WHERE i.videoReferenceUUID = :uuid AND i.recordedDate BETWEEN :start AND :end ORDER BY o.uuid")))
query = "SELECT o FROM Observation o LEFT JOIN o.imagedMoment i WHERE i.videoReferenceUUID = :uuid AND i.recordedDate BETWEEN :start AND :end ORDER BY i.recordedDate")))
class ObservationImpl extends Observation with JPAPersistentObject {

@Expose(serialize = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class AnnotationV1ApiSpec extends WebApiStack {
new AnnotationV1Api(controller)
}

protected[this] override val gson = Constants.GSON_FOR_ANNOTATION

addServlet(annotationV1Api, "/v1/annotations")

var annotation: Annotation = _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CachedAncillaryDatumV1ApiSpec extends WebApiStack {
val controller = new CachedAncillaryDatumController(daoFactory)
new CachedAncillaryDatumV1Api(controller)
}
protected[this] override val gson = Constants.GSON

private[this] val path = "/v1/ancillarydata"
addServlet(datumV1Api, path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import scala.concurrent.duration.{Duration => SDuration}
*/
class IndexV1ApiSpec extends WebApiStack {

private[this] val startTimestamp = Instant.now()

private[this] val indexController = new IndexController(daoFactory)

private[this] val indexV1Api = new IndexV1Api(indexController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AnnotationControllerSpec extends FlatSpec with Matchers with BeforeAndAfte
private[this] val controller = new AnnotationController(daoFactory.asInstanceOf[BasicDAOFactory])
private[this] val timeout = SDuration(200, TimeUnit.SECONDS)
private[this] val recordedDate = Instant.now()
private[this] val log = LoggerFactory.getLogger(getClass)
// private[this] val log = LoggerFactory.getLogger(getClass)

def exec[R](fn: () => Future[R]): R = Await.result(fn.apply(), timeout)

Expand Down Expand Up @@ -103,6 +103,8 @@ class AnnotationControllerSpec extends FlatSpec with Matchers with BeforeAndAfte
recordedDate = Some(recordedDate.plusSeconds(i * 3600))))
}

xs.size should be (5)

val (closeable, stream) = controller.streamByVideoReferenceUUIDAndTimestamps(videoReferenceUuid,
recordedDate,
recordedDate.plusSeconds(4000))
Expand Down

0 comments on commit 6c1babc

Please sign in to comment.