Skip to content

Commit

Permalink
avniproject/avni-product#1446 | Handle parent org concept search by n…
Browse files Browse the repository at this point in the history
…ame. Throw nullpointer if concept not found while saving observation.
  • Loading branch information
1t5j0y committed Dec 14, 2023
1 parent c1289d7 commit c395d05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@
@RepositoryRestResource(collectionResourceRel = "concept", path = "concept")
public interface ConceptRepository extends ReferenceDataRepository<Concept>, FindByLastModifiedDateTime<Concept> {
@QueryHints({@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_CACHEABLE, value = "true")})
@Query("select c from Concept c where c.name = ?1 and c.organisationId = ?2")
Concept findByNameAndOrganisationId(String name, Long organisationId);
@Query("select c from Concept c where c.name = ?1 and c.organisationId IN ?2")
Concept findByNameAndOrganisationId(String name, List<Long> organisationIds);

default List<Long> buildOrganisationIdList() {
List<Long> organisationIds = new ArrayList<>();
UserContext userContext = UserContextHolder.getUserContext();
organisationIds.add(userContext.getOrganisationId());
if (userContext.getOrganisation().getParentOrganisationId() != null) {
organisationIds.add(userContext.getOrganisation().getParentOrganisationId());
}
return organisationIds;
}

@Override
default Concept findByName(String name) {
return this.findByNameAndOrganisationId(name, UserContextHolder.getUserContext().getOrganisationId());
return this.findByNameAndOrganisationId(name, buildOrganisationIdList());
}

Page<Concept> findByIsVoidedFalseAndNameIgnoreCaseContaining(String name, Pageable pageable);
Expand All @@ -45,13 +55,7 @@ default Concept findByName(String name) {

@Override
default Concept findByUuid(String uuid) {
List<Long> organisationIds = new ArrayList<>();
UserContext userContext = UserContextHolder.getUserContext();
organisationIds.add(userContext.getOrganisationId());
if (userContext.getOrganisation().getParentOrganisationId() != null) {
organisationIds.add(userContext.getOrganisation().getParentOrganisationId());
}
return this.findByUuidAndOrganisationId(uuid, organisationIds);
return this.findByUuidAndOrganisationId(uuid, buildOrganisationIdList());
}

@Query("select c from Concept c where c.isVoided = false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public ObservationCollection createObservations(List<ObservationRequest> observa
} else {
concept = conceptRepository.findByUuid(observationRequest.getConceptUUID());
}
if (concept == null) {
throw new NullPointerException(String.format("Concept with uuid=%s/name=%s not found", observationRequest.getConceptUUID(), observationRequest.getConceptName()));
}
return new SimpleEntry<>(concept, observationRequest.getValue());
})
.filter(obsReqAsMap -> null != obsReqAsMap.getKey()
Expand Down

0 comments on commit c395d05

Please sign in to comment.