Skip to content

Commit

Permalink
Added count/find linkTemplates by toConcept
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed Dec 30, 2024
1 parent d62b450 commit 6a50300
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ trait LinkTemplateEndpointsSuite extends EndpointsSuite with DataInitializer wit
)
}

test("countByToConcept") {
val links = createLinkTemplates()
val link = links.head
runGet(
endpoints.countByToConceptImpl,
s"http://test.com/v1/linktemplates/toconcept/count/${link.toConcept}",
response =>
// println(response.body)
assertEquals(response.code, StatusCode.Ok)
val obtained = checkResponse[Long](response.body)
assertEquals(obtained, 1L)
)
}

test("findByToConcept") {
val links = createLinkTemplates()
val link = links.head
runGet(
endpoints.findByToConceptImpl,
s"http://test.com/v1/linktemplates/toconcept/${link.toConcept}",
response =>
// println(response.body)
assertEquals(response.code, StatusCode.Ok)
val obtained = checkResponse[Seq[ExtendedLink]](response.body)
assertEquals(obtained, Seq(link))
)
}

test("renameToConcept") {
val root = init(3, 10)
val descendants = root.getDescendants.asScala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ trait LinkTemplateServiceSuite extends DataInitializer with UserAuthMixin:
case Left(error) => fail(error.toString)
}

test("countByToConcept") {
val root = init(3, 10)
assert(root != null)
val descendants = root.getDescendants.asScala
val allLinkTemplates = descendants.flatMap(_.getConceptMetadata.getLinkTemplates.asScala).toSeq
for linkTemplate <- allLinkTemplates
do
val toConcept = linkTemplate.getToConcept
linkTemplateService.countByToConcept(toConcept) match
case Right(obtained) =>
val expected = allLinkTemplates.count(t => t.getToConcept == toConcept)
assertEquals(obtained, expected.toLong)
case Left(error) => fail(error.toString)
}

test("findByToConcept") {
val root = init(3, 10)
assert(root != null)
val descendants = root.getDescendants.asScala
val allLinkTemplates = descendants.flatMap(_.getConceptMetadata.getLinkTemplates.asScala).toSeq
for linkTemplate <- allLinkTemplates
do
val toConcept = linkTemplate.getToConcept
linkTemplateService.findByToConcept(toConcept) match
case Right(obtained) =>
val expected = allLinkTemplates.filter(t => t.getToConcept == toConcept).map(ExtendedLink.from)
assertEquals(obtained.sortBy(_.linkName), expected.sortBy(_.linkName))
case Left(error) => fail(error.toString)
}

test("renameToConcept") {
val root = init(3, 10)
assert(root != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@Index(name = "idx_LinkTemplate_LUT", columnList = "LAST_UPDATED_TIME")})
@EntityListeners({ TransactionLogger.class, KeyNullifier.class })
@NamedQueries( {

@NamedQuery(name = "LinkTemplate.countByToConcept", query = "SELECT COUNT(v) FROM LinkTemplate v WHERE v.toConcept = :toConcept"),
@NamedQuery(name = "LinkTemplate.findById", query = "SELECT v FROM LinkTemplate v WHERE v.id = :id") ,
@NamedQuery(name = "LinkTemplate.findAll",
query = "SELECT l FROM LinkTemplate l") ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ public Collection<LinkTemplateEntity> findAll(int limit, int offset) {
return findByNamedQuery("LinkTemplate.findAll", limit, offset);
}

public Long countByToConcept(String toConcept) {
return countByNamedQuery("LinkTemplate.countByToConcept", Map.of("toConcept", toConcept));
}

public Collection<LinkTemplateEntity> findByToConcept(String toConcept) {
return findByNamedQuery("LinkTemplate.findByToConcept", Map.of("toConcept", toConcept));
}

public Collection<LinkTemplateEntity> findByToConcept(String toConcept, int limit, int offset) {
return findByNamedQuery("LinkTemplate.findByToConcept", Map.of("toConcept", toConcept), limit, offset);
}


public void validateToConcept(LinkTemplateEntity object) {
var conceptDAO = new ConceptRepository(entityManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public Long countByNamedQuery(String name) {
return ((Number) query.getSingleResult()).longValue();
}

public Long countByNamedQuery(String name, Map<String, Object> namedParams) {
debugLog(name, Map.of());
Query query = entityManager.createNamedQuery(name);
namedParams.forEach(query::setParameter);
return ((Number) query.getSingleResult()).longValue();
}

public <T> List<T> findByNamedQuery(String name,
Map<String, Object> namedParams) {
debugLog(name, namedParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LinkTemplateEndpoints(entityManagerFactory: EntityManagerFactory)(using jw
handleErrorsAsync(service.findByConcept(conceptName))
}

val findLinKTemplateByPrototype: Endpoint[Unit, Link, ErrorMsg, Seq[ExtendedLink], Any] = openEndpoint
val findLinkTemplateByPrototype: Endpoint[Unit, Link, ErrorMsg, Seq[ExtendedLink], Any] = openEndpoint
.post
.in(base / "prototype")
.in(jsonBody[Link])
Expand All @@ -63,10 +63,36 @@ class LinkTemplateEndpoints(entityManagerFactory: EntityManagerFactory)(using jw
.tag(tag)

val findLinkTemplateByPrototypeImpl: ServerEndpoint[Any, Future] =
findLinKTemplateByPrototype.serverLogic { link =>
findLinkTemplateByPrototype.serverLogic { link =>
handleErrorsAsync(service.findByPrototype(link))
}

val countByToConcept: Endpoint[Unit, String, ErrorMsg, Long, Any] = openEndpoint
.get
.in(base / "toconcept" / "count" / path[String]("toConcept"))
.out(jsonBody[Long])
.name("countLinkTemplatesByToConcept")
.description("Count all link templates by toConcept")
.tag(tag)

val countByToConceptImpl: ServerEndpoint[Any, Future] = countByToConcept
.serverLogic { toConcept =>
handleErrorsAsync(service.countByToConcept(toConcept))
}

val findByToConcept: Endpoint[Unit, String, ErrorMsg, Seq[ExtendedLink], Any] = openEndpoint
.get
.in(base / "toconcept" / path[String]("toConcept"))
.out(jsonBody[Seq[ExtendedLink]])
.name("findLinkTemplatesByToConcept")
.description("Find all link templates by toConcept")
.tag(tag)

val findByToConceptImpl: ServerEndpoint[Any, Future] = findByToConcept
.serverLogic { toConcept =>
handleErrorsAsync(service.findByToConcept(toConcept))
}

val renameToConcept: Endpoint[Option[String], LinkRenameToConceptRequest, ErrorMsg, LinkRenameToConceptResponse, Any] =
secureEndpoint
.put
Expand All @@ -82,6 +108,7 @@ class LinkTemplateEndpoints(entityManagerFactory: EntityManagerFactory)(using jw
.serverLogic { userAccount => request =>
handleErrorsAsync(service.renameToConcept(request.old, request.`new`, userAccount.username))
}


val createLinkTemplate: Endpoint[Option[String], LinkCreate, ErrorMsg, ExtendedLink, Any] = secureEndpoint
.post
Expand Down Expand Up @@ -137,7 +164,9 @@ class LinkTemplateEndpoints(entityManagerFactory: EntityManagerFactory)(using jw
override def all: List[Endpoint[?, ?, ?, ?, ?]] = List(
renameToConcept,
findLinkTemplateByConceptName,
findLinKTemplateByPrototype,
findLinkTemplateByPrototype,
countByToConcept,
findByToConcept,
createLinkTemplate,
updateLinkTemplate,
deleteLinkTemplate,
Expand All @@ -148,6 +177,8 @@ class LinkTemplateEndpoints(entityManagerFactory: EntityManagerFactory)(using jw
renameToConceptImpl,
findLinkTemplateByConceptNameImpl,
findLinkTemplateByPrototypeImpl,
countByToConceptImpl,
findByToConceptImpl,
createLinkTemplateImpl,
updateLinkTemplateImpl,
deleteLinkTemplateImpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ class LinkTemplateService(entityManagerFactory: EntityManagerFactory):
case None => throw ConceptNameNotFound(conceptName)
)

def countByToConcept(toConcept: String): Either[Throwable, Long] =
entityManagerFactory.transaction(entityManager =>
val repo = new LinkTemplateRepository(entityManager)
repo.countByToConcept(toConcept)
)

def findByToConcept(toConcept: String): Either[Throwable, Seq[ExtendedLink]] =
entityManagerFactory.transaction(entityManager =>
val repo = new LinkTemplateRepository(entityManager)
repo.findByToConcept(toConcept)
.asScala
.map(ExtendedLink.from)
.toSeq
)

def findByPrototype(link: Link): Either[Throwable, Seq[ExtendedLink]] =
entityManagerFactory.transaction(entityManager =>
val repo = new LinkTemplateRepository(entityManager)
Expand Down

0 comments on commit 6a50300

Please sign in to comment.