diff --git a/avni-server-api/src/main/java/org/avni/server/dao/EncounterSearchRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/EncounterSearchRepository.java index becb40b8d..4893a1ff0 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/EncounterSearchRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/EncounterSearchRepository.java @@ -44,8 +44,7 @@ public long getCount(EncounterSearchRequest searchRequest) { query.getParameters().forEach((name, value) -> { sql.setParameter(name, value); }); - BigInteger count = (BigInteger) sql.getSingleResult(); - return count.longValue(); + return (Long) sql.getSingleResult(); } finally { setRoleBackToUser(); } diff --git a/avni-server-api/src/main/java/org/avni/server/dao/SubjectSearchRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/SubjectSearchRepository.java index c89506392..7e6c79289 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/SubjectSearchRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/SubjectSearchRepository.java @@ -1,24 +1,20 @@ package org.avni.server.dao; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Query; +import jakarta.transaction.Transactional; import org.avni.server.dao.search.SearchBuilder; import org.avni.server.dao.search.SqlQuery; import org.avni.server.domain.SubjectType; import org.avni.server.web.request.webapp.search.SubjectSearchRequest; +import org.hibernate.query.sql.internal.NativeQueryImpl; +import org.hibernate.transform.AliasToEntityMapResultTransformer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Repository; import org.springframework.util.StringUtils; -import jakarta.persistence.EntityManager; - -import jakarta.persistence.*; - -import jakarta.persistence.Query; -import jakarta.transaction.Transactional; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,24 +31,6 @@ public SubjectSearchRepository(EntityManager entityManager, SubjectTypeRepositor this.subjectTypeRepository = subjectTypeRepository; } - private List> convertTuplesToMap(List tuples) { - List> result = new ArrayList<>(); - - tuples.forEach(object -> { - if (object instanceof Tuple single) { - Map tempMap = new HashMap<>(); - for (TupleElement key : single.getElements()) { - tempMap.put(key.getAlias(), single.get(key)); - } - result.add(tempMap); - } else { - throw new RuntimeException("Query should return instance of Tuple"); - } - }); - - return result; - } - @Transactional public List> search(SubjectSearchRequest searchRequest, SearchBuilder searchBuilder) { SubjectType subjectType = StringUtils.isEmpty(searchRequest.getSubjectType()) ? null : subjectTypeRepository.findByUuid(searchRequest.getSubjectType()); @@ -62,18 +40,19 @@ public List> search(SubjectSearchRequest searchRequest, Sear logger.debug("Executing query: " + query.getSql()); logger.debug("Parameters: " + query.getParameters()); Query sql = entityManager.createNativeQuery(query.getSql()); + NativeQueryImpl nativeQuery = (NativeQueryImpl) sql; + nativeQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE); query.getParameters().forEach((name, value) -> { sql.setParameter(name, value); }); - List resultList = sql.getResultList(); - return convertTuplesToMap(resultList); + return sql.getResultList(); } finally { setRoleBackToUser(); } } @Transactional - public BigInteger getTotalCount(SubjectSearchRequest searchRequest, SearchBuilder searchBuilder) { + public Long getTotalCount(SubjectSearchRequest searchRequest, SearchBuilder searchBuilder) { SubjectType subjectType = StringUtils.isEmpty(searchRequest.getSubjectType()) ? null : subjectTypeRepository.findByUuid(searchRequest.getSubjectType()); SqlQuery query = searchBuilder.getSQLCountQuery(searchRequest, subjectType); try { @@ -83,7 +62,7 @@ public BigInteger getTotalCount(SubjectSearchRequest searchRequest, SearchBuilde sql.setParameter(name, value); }); - return (BigInteger) sql.getSingleResult(); + return (Long) sql.getSingleResult(); } finally { setRoleBackToUser(); } diff --git a/avni-server-api/src/main/java/org/avni/server/service/IndividualSearchService.java b/avni-server-api/src/main/java/org/avni/server/service/IndividualSearchService.java index 9c68c4b9c..cf5f30dd3 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/IndividualSearchService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/IndividualSearchService.java @@ -8,8 +8,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.math.BigInteger; -import java.util.*; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -31,18 +33,18 @@ public LinkedHashMap search(SubjectSearchRequest subjectSearchRe logger.info("Searching for individuals"); List> searchResults = subjectSearchRepository.search(subjectSearchRequest, new SubjectSearchQueryBuilder()); logger.info("Found " + searchResults.size() + " individuals"); - BigInteger totalCount = subjectSearchRepository.getTotalCount(subjectSearchRequest, new SubjectSearchQueryBuilder()); + Long totalCount = subjectSearchRepository.getTotalCount(subjectSearchRequest, new SubjectSearchQueryBuilder()); logger.info("Total count of individuals: " + totalCount); return constructIndividual(searchResults, totalCount); } - private LinkedHashMap constructIndividual(List> individualList, BigInteger totalCount) { - LinkedHashMap recordsMap = new LinkedHashMap(); + private LinkedHashMap constructIndividual(List> individualList, Long totalCount) { + LinkedHashMap recordsMap = new LinkedHashMap<>(); List individualIds = individualList.stream() .map(individualRecord -> Long.valueOf((Integer) individualRecord.get("id"))) .collect(Collectors.toList()); List addressIds = individualList.stream() - .map(individualRecord -> ((BigInteger) individualRecord.get("addressId")).longValue()) + .map(individualRecord -> (Long) individualRecord.get("addressId")) .collect(Collectors.toList()); List searchSubjectEnrolledPrograms = !individualIds.isEmpty() ? @@ -58,7 +60,7 @@ private LinkedHashMap constructIndividual(List x.getId().equals(individualId)) .map(SearchSubjectEnrolledProgram::getProgram) .collect(Collectors.toList())); - individualRecord.put("addressLevel", titleLineages.get(((BigInteger) individualRecord.get("addressId")).longValue())); + individualRecord.put("addressLevel", titleLineages.get((Long) individualRecord.get("addressId"))); }).collect(Collectors.toList()); recordsMap.put("totalElements", totalCount); recordsMap.put("listOfRecords", listOfRecords); diff --git a/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java b/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java index 6cdf3f9c4..a569e0065 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java @@ -17,8 +17,6 @@ import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.stereotype.Service; -import jakarta.transaction.Transactional; -import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -106,7 +104,7 @@ public LinkedHashMap searchSubjects(SubjectSearchRequest subject List subjectIds = searchResults.stream().map(s -> Long.parseLong(s.get("id").toString())).collect(Collectors.toList()); List userSubjectAssignmentBySubjectIds = userSubjectAssignmentRepository.findUserSubjectAssignmentBySubject_IdIn(subjectIds); List addressIds = searchResults.stream() - .map(searchResult -> ((BigInteger) searchResult.get("addressId")).longValue()) + .map(searchResult -> (Long) searchResult.get("addressId")) .collect(Collectors.toList()); Map titleLineages = addressLevelService.getTitleLineages(addressIds); Map> groupedSubjects = userSubjectAssignmentBySubjectIds.stream() @@ -122,10 +120,10 @@ public LinkedHashMap searchSubjects(SubjectSearchRequest subject .orElseGet(Stream::empty) .map(uw -> pf.createProjection(UserWebProjection.class, uw)).collect(Collectors.toList()); searchResult.put("assignedUsers", userWebProjections); - searchResult.put("addressLevel", titleLineages.get(((BigInteger) searchResult.get("addressId")).longValue())); + searchResult.put("addressLevel", titleLineages.get((Long) searchResult.get("addressId"))); } - BigInteger totalCount = subjectSearchRepository.getTotalCount(subjectSearchRequest, new SubjectAssignmentSearchQueryBuilder()); + Long totalCount = subjectSearchRepository.getTotalCount(subjectSearchRequest, new SubjectAssignmentSearchQueryBuilder()); LinkedHashMap recordsMap = new LinkedHashMap<>(); recordsMap.put("totalElements", totalCount);