From 4358f768899797ef34a9e4e858582d2a8dc93e94 Mon Sep 17 00:00:00 2001 From: Sree21 Date: Mon, 23 Jul 2018 18:31:16 +0530 Subject: [PATCH] Changed healthblock select query from (taluka, code) to (district, code) --- .../region/service/HealthBlockService.java | 3 ++ .../service/impl/HealthBlockServiceImpl.java | 30 +++++++++++++++++++ .../service/impl/LocationServiceImpl.java | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/region/src/main/java/org/motechproject/nms/region/service/HealthBlockService.java b/region/src/main/java/org/motechproject/nms/region/service/HealthBlockService.java index 7387bec12..869805959 100644 --- a/region/src/main/java/org/motechproject/nms/region/service/HealthBlockService.java +++ b/region/src/main/java/org/motechproject/nms/region/service/HealthBlockService.java @@ -10,6 +10,9 @@ public interface HealthBlockService { HealthBlock findByTalukaAndCode(Taluka taluka, Long code); + + HealthBlock findByDistrictAndCode(District district, Long code); + HealthBlock create(HealthBlock healthBlock); HealthBlock update(HealthBlock healthBlock); diff --git a/region/src/main/java/org/motechproject/nms/region/service/impl/HealthBlockServiceImpl.java b/region/src/main/java/org/motechproject/nms/region/service/impl/HealthBlockServiceImpl.java index 574f7c3da..c8a8e2602 100644 --- a/region/src/main/java/org/motechproject/nms/region/service/impl/HealthBlockServiceImpl.java +++ b/region/src/main/java/org/motechproject/nms/region/service/impl/HealthBlockServiceImpl.java @@ -81,6 +81,36 @@ public HealthBlock execute(Query query) { return healthBlockDataService.executeSQLQuery(queryExecution); } + @Override + public HealthBlock findByDistrictAndCode(final District district, final Long code) { + if (district == null) { return null; } + + SqlQueryExecution queryExecution = new SqlQueryExecution() { + + @Override + public String getSqlQuery() { + return "select * " + + "from nms_health_blocks " + + "where code = ? and district_id_OID = ?"; + } + + @Override + public HealthBlock execute(Query query) { + query.setClass(HealthBlock.class); + ForwardQueryResult fqr = (ForwardQueryResult) query.execute(code, district.getId()); + if (fqr.isEmpty()) { + return null; + } + if (fqr.size() == 1) { + return (HealthBlock) fqr.get(0); + } + throw new IllegalStateException("More than one row returned!"); + } + }; + + return healthBlockDataService.executeSQLQuery(queryExecution); + } + @Override public HealthBlock create(HealthBlock healthBlock) { return healthBlockDataService.create(healthBlock); diff --git a/region/src/main/java/org/motechproject/nms/region/service/impl/LocationServiceImpl.java b/region/src/main/java/org/motechproject/nms/region/service/impl/LocationServiceImpl.java index d8f9724bd..1e559cc70 100644 --- a/region/src/main/java/org/motechproject/nms/region/service/impl/LocationServiceImpl.java +++ b/region/src/main/java/org/motechproject/nms/region/service/impl/LocationServiceImpl.java @@ -227,7 +227,7 @@ public Map getLocations(Map map, boolean createI if (!isValidID(map, HEALTHBLOCK_ID)) { return locations; } - HealthBlock healthBlock = healthBlockService.findByTalukaAndCode(taluka, (Long) map.get(HEALTHBLOCK_ID)); + HealthBlock healthBlock = healthBlockService.findByDistrictAndCode(district, (Long) map.get(HEALTHBLOCK_ID)); if (healthBlock == null && createIfNotExists) { healthBlock = new HealthBlock(); healthBlock.addTaluka(taluka);