From 7218a7e697576ec5e62363f3ac929b8b6a770904 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Thu, 26 Oct 2023 19:31:04 +0300 Subject: [PATCH 01/13] optimize generate unique id --- .../org/opensrp/repository/postgres/UniqueIdRepositoryImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensrp/repository/postgres/UniqueIdRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/UniqueIdRepositoryImpl.java index 4d531106e..a6cf83cb1 100644 --- a/src/main/java/org/opensrp/repository/postgres/UniqueIdRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/UniqueIdRepositoryImpl.java @@ -87,7 +87,7 @@ public UniqueId findByIdentifierSourceOrderByIdDesc(Long idSource) { example.createCriteria().andIdSourceEqualTo(idSource); example.setOrderByClause("id DESC"); - List uniqueIds = uniqueIdMapper.selectByExample(example); + List uniqueIds = uniqueIdMapper.selectMany(example, 0, 1); List convertedUniqueIds = convert(uniqueIds); return convertedUniqueIds != null && convertedUniqueIds.size() >= 1 ? convertedUniqueIds.get(0) : null; } From 736b9521c7f09143a0c608736e67c0777eebb8d8 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Thu, 26 Oct 2023 19:32:29 +0300 Subject: [PATCH 02/13] bump up version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a6fc02ebc..d6d6bab4f 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.7-SNAPSHOT + 2.14.8-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core From 5b23603c4cc82e080e2b76e203568f309e1def23 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Mon, 15 Jan 2024 19:29:39 +0300 Subject: [PATCH 03/13] generate tasks after adding or updating stock item --- .../opensrp/repository/PlanRepository.java | 2 ++ .../postgres/PlanRepositoryImpl.java | 11 ++++++- .../custom/CustomPlanMetadataMapper.java | 3 ++ .../custom/xml/CustomPlanMetadataMapper.xml | 28 +++++++++++++++++- .../org/opensrp/service/StockService.java | 29 ++++++++++++++++++- 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensrp/repository/PlanRepository.java b/src/main/java/org/opensrp/repository/PlanRepository.java index 1d8d7f41e..56d1ed975 100644 --- a/src/main/java/org/opensrp/repository/PlanRepository.java +++ b/src/main/java/org/opensrp/repository/PlanRepository.java @@ -14,6 +14,8 @@ public interface PlanRepository extends BaseRepository, PlanDao { List getPlansByServerVersionAndOperationalAreas(Long serverVersion, List operationalAreaIds, boolean experimental); + List getPlansByServerVersionAndOperationalAreasAndStatus(Long serverVersion, List operationalAreaIds, + boolean experimental, PlanDefinition.PlanStatus status); /** * This method searches for plans using a list of provided diff --git a/src/main/java/org/opensrp/repository/postgres/PlanRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/PlanRepositoryImpl.java index b0190f6ff..5919cc914 100644 --- a/src/main/java/org/opensrp/repository/postgres/PlanRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/PlanRepositoryImpl.java @@ -147,7 +147,16 @@ public List getPlansByServerVersionAndOperationalAreas(Long serv PlanExample planExample = new PlanExample(); planExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion).andDateDeletedIsNull().andExperimentalEqualTo(experimental); List plans = planMetadataMapper.selectMany(planExample, operationalAreaIds, 0, DEFAULT_FETCH_SIZE); - + return convert(plans); + } + + public List getPlansByServerVersionAndOperationalAreasAndStatus(Long serverVersion, List operationalAreaIds, boolean experimental, + PlanDefinition.PlanStatus status) { + PlanExample planExample = new PlanExample(); + planExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion).andDateDeletedIsNull() + .andExperimentalEqualTo(experimental); + List plans = planMetadataMapper.selectManyByStatus(planExample, operationalAreaIds, 0, DEFAULT_FETCH_SIZE, status.value()); + return convert(plans); } diff --git a/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomPlanMetadataMapper.java b/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomPlanMetadataMapper.java index d04fa1ef5..9c4c6f40d 100644 --- a/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomPlanMetadataMapper.java +++ b/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomPlanMetadataMapper.java @@ -10,5 +10,8 @@ public interface CustomPlanMetadataMapper extends PlanMetadataMapper { List selectMany(@Param("example") PlanExample planExample, @Param("operationalAreaIds") List operationalAreaIds, @Param("offset") int offset, @Param("limit") int limit); + + List selectManyByStatus(@Param("example") PlanExample planExample, @Param("operationalAreaIds") List operationalAreaIds, @Param("offset") int offset, + @Param("limit") int limit, @Param("status") String status); } diff --git a/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomPlanMetadataMapper.xml b/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomPlanMetadataMapper.xml index 7eb3dc98b..05f5fbd7b 100644 --- a/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomPlanMetadataMapper.xml +++ b/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomPlanMetadataMapper.xml @@ -60,4 +60,30 @@ LIMIT #{limit} OFFSET #{offset} - \ No newline at end of file + + + diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index 2797068df..8caa66c1f 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -8,13 +8,14 @@ import org.joda.time.DateTime; import org.smartregister.domain.Inventory; import org.smartregister.domain.ProductCatalogue; +import org.smartregister.domain.PhysicalLocation; +import org.smartregister.domain.PlanDefinition; import org.smartregister.domain.Stock; import org.opensrp.dto.CsvBulkImportDataSummary; import org.opensrp.dto.FailedRecordSummary; import org.opensrp.repository.StocksRepository; import org.opensrp.search.StockSearchBean; import org.opensrp.validator.InventoryDataValidator; -import org.smartregister.domain.PhysicalLocation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -48,6 +49,11 @@ public class StockService { private PhysicalLocationService physicalLocationService; private InventoryDataValidator inventoryDataValidator; + + @Autowired + private PlanService planService; + @Autowired + private TaskGenerator taskGenerator; private static Logger logger = LogManager.getLogger(StockService.class.toString()); @@ -177,6 +183,27 @@ public void addInventory(Inventory inventory, String userName) { return; } allStocks.add(stock); + // Go up the location tree to get the operational area. + //TODO Make process configurable + PhysicalLocation servicePoint = physicalLocationService.getLocation(stock.getLocationId(), false, false); + if(servicePoint == null || servicePoint.getProperties() == null || servicePoint.getProperties().getParentId() == null) return; + logger.info("Service Point %s"+servicePoint.getProperties().getName()); + + PhysicalLocation district = physicalLocationService.getLocation(servicePoint.getProperties().getParentId(), false, false); + if(district == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; + + logger.info("District %s"+district.getProperties().getName()); + String regionId = district.getProperties().getParentId(); + if(regionId==null) return; + + logger.info("RegionID %s"+regionId); + List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, + Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); + for (PlanDefinition plan : + plans) { + logger.info("processng tasks for planID "+plan.getIdentifier()); + taskGenerator.processPlanEvaluation(plan, null,userName); + } } public void updateInventory(Inventory inventory, String userName) { From e5adb3997f62c21cf3c99d3c0183c63c7821426b Mon Sep 17 00:00:00 2001 From: hilpitome Date: Tue, 16 Jan 2024 18:43:55 +0300 Subject: [PATCH 04/13] fix obtaining operational area --- pom.xml | 2 +- .../org/opensrp/service/StockService.java | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index a6fc02ebc..9807a93da 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.7-SNAPSHOT + 2.14.9-DEV1-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index 8caa66c1f..368123dff 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -3,9 +3,14 @@ import java.text.ParseException; import java.util.*; +import com.google.gson.Gson; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.joda.time.DateTime; +import org.json.JSONObject; +import org.opensrp.domain.postgres.Structure; +import org.opensrp.domain.postgres.StructureMetadataExample; +import org.opensrp.repository.postgres.mapper.custom.CustomStructureMetadataMapper; import org.smartregister.domain.Inventory; import org.smartregister.domain.ProductCatalogue; import org.smartregister.domain.PhysicalLocation; @@ -54,6 +59,9 @@ public class StockService { private PlanService planService; @Autowired private TaskGenerator taskGenerator; + + @Autowired + private CustomStructureMetadataMapper structureMetadataMapper; private static Logger logger = LogManager.getLogger(StockService.class.toString()); @@ -185,18 +193,29 @@ public void addInventory(Inventory inventory, String userName) { allStocks.add(stock); // Go up the location tree to get the operational area. //TODO Make process configurable - PhysicalLocation servicePoint = physicalLocationService.getLocation(stock.getLocationId(), false, false); - if(servicePoint == null || servicePoint.getProperties() == null || servicePoint.getProperties().getParentId() == null) return; - logger.info("Service Point %s"+servicePoint.getProperties().getName()); + logger.info("Init updating tasks after adding stock"); + StructureMetadataExample structureMetadataExample = new StructureMetadataExample(); + structureMetadataExample.createCriteria().andGeojsonIdEqualTo(stock.getId()); + Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); + PhysicalLocation servicePoint = (PhysicalLocation) structure.getJson(); + + if(structure.getJson() == null || servicePoint.getProperties().getParentId() == null) + return;; + logger.info("Fetching parent location for servicepoint "+servicePoint.getProperties().getUid()); + String servicePointParentId = servicePoint.getProperties().getParentId(); + + PhysicalLocation servicePointLocation = physicalLocationService.getLocation(servicePointParentId, false, false); + + if(servicePointLocation == null || servicePointLocation.getProperties() == null || servicePointLocation.getProperties().getParentId() == null) return; + logger.info("Service Point parent "+servicePointLocation.getProperties().getName()+" location id "+servicePointParentId); PhysicalLocation district = physicalLocationService.getLocation(servicePoint.getProperties().getParentId(), false, false); if(district == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; - logger.info("District %s"+district.getProperties().getName()); String regionId = district.getProperties().getParentId(); - if(regionId==null) return; + logger.info("District "+district.getProperties().getName()); + logger.info("RegionID "+regionId); - logger.info("RegionID %s"+regionId); List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); for (PlanDefinition plan : From 75214cbcdf3f62ca4e188d02b9fe96c6d41ad411 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 17 Jan 2024 11:42:10 +0300 Subject: [PATCH 05/13] ensure we can get region --- pom.xml | 2 +- .../org/opensrp/service/StockService.java | 24 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 9807a93da..6dc7bd9da 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.9-DEV1-SNAPSHOT + 2.14.9-DEV3-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index 368123dff..611ab3348 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -3,11 +3,9 @@ import java.text.ParseException; import java.util.*; -import com.google.gson.Gson; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.joda.time.DateTime; -import org.json.JSONObject; import org.opensrp.domain.postgres.Structure; import org.opensrp.domain.postgres.StructureMetadataExample; import org.opensrp.repository.postgres.mapper.custom.CustomStructureMetadataMapper; @@ -207,20 +205,28 @@ public void addInventory(Inventory inventory, String userName) { PhysicalLocation servicePointLocation = physicalLocationService.getLocation(servicePointParentId, false, false); if(servicePointLocation == null || servicePointLocation.getProperties() == null || servicePointLocation.getProperties().getParentId() == null) return; - logger.info("Service Point parent "+servicePointLocation.getProperties().getName()+" location id "+servicePointParentId); + logger.info("Service Point name "+servicePointLocation.getProperties().getName()+" location id "+servicePointLocation.getProperties().getUid()); - PhysicalLocation district = physicalLocationService.getLocation(servicePoint.getProperties().getParentId(), false, false); - if(district == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; + PhysicalLocation commune = physicalLocationService.getLocation(servicePoint.getProperties().getParentId(), false, false); + if(commune == null || commune.getProperties() == null || commune.getProperties().getParentId() == null) return; + logger.info("Commune name"+commune.getProperties().getName()+" commune id "+servicePoint.getProperties().getParentId()); - String regionId = district.getProperties().getParentId(); - logger.info("District "+district.getProperties().getName()); - logger.info("RegionID "+regionId); + String districtId = commune.getProperties().getParentId(); + PhysicalLocation district = physicalLocationService.getLocation(districtId, false, false); + + if(districtId == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; + logger.info("District name "+district.getProperties().getName() +" district id "+commune.getProperties().getParentId()); + PhysicalLocation region = physicalLocationService.getLocation(district.getProperties().getParentId(), false, false); + + if(region == null) return; + String regionId = district.getProperties().getParentId(); + logger.info("Region name "+region.getProperties().getName() +" region id "+regionId); List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); for (PlanDefinition plan : plans) { - logger.info("processng tasks for planID "+plan.getIdentifier()); + logger.info("Processing tasks for planID "+plan.getIdentifier()); taskGenerator.processPlanEvaluation(plan, null,userName); } } From 853c778af8555f09cc73f20ad9a95492655e7412 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Tue, 23 Jan 2024 18:04:46 +0300 Subject: [PATCH 06/13] regenerate tasks in operational area when service point is created --- .../service/PhysicalLocationService.java | 46 +++++++++++++++++++ .../org/opensrp/service/StockService.java | 6 +++ 2 files changed, 52 insertions(+) diff --git a/src/main/java/org/opensrp/service/PhysicalLocationService.java b/src/main/java/org/opensrp/service/PhysicalLocationService.java index 0306ddd1c..4c43cee72 100755 --- a/src/main/java/org/opensrp/service/PhysicalLocationService.java +++ b/src/main/java/org/opensrp/service/PhysicalLocationService.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import java.util.Collections; import javax.transaction.Transactional; @@ -25,10 +26,12 @@ import org.opensrp.domain.LocationDetail; import org.opensrp.domain.StructureCount; import org.opensrp.domain.StructureDetails; +import org.opensrp.domain.postgres.Structure; import org.opensrp.repository.LocationRepository; import org.opensrp.search.LocationSearchBean; import org.smartregister.domain.LocationProperty; import org.smartregister.domain.PhysicalLocation; +import org.smartregister.domain.PlanDefinition; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; @@ -40,6 +43,10 @@ @Service public class PhysicalLocationService { + @Autowired + PlanService planService; + @Autowired + TaskGenerator taskGenerator; private static Logger logger = LogManager.getLogger(PhysicalLocationService.class.toString()); private LocationRepository locationRepository; @@ -588,4 +595,43 @@ long countStructuresByProperties(List parentIds, Map pro public List findStructureIdsByProperties(List parentIds, Map properties, int limit){ return locationRepository.findStructureIdsByProperties(parentIds,properties,limit); } + + public void regenerateTasksForOperationalArea(Structure structure, String username){ + /* + Go up the location tree to get the operational area. + TODO Make process configurable + */ + assert structure != null; + PhysicalLocation servicePoint = (PhysicalLocation) structure.getJson(); + logger.info("Fetching parent location for servicepoint "+servicePoint.getProperties().getUid()); + String servicePointParentId = servicePoint.getProperties().getParentId(); + + PhysicalLocation servicePointLocation = getLocation(servicePointParentId, false, false); + + if(servicePointLocation == null || servicePointLocation.getProperties() == null || servicePointLocation.getProperties().getParentId() == null) return; + logger.info("Service Point name "+servicePointLocation.getProperties().getName()+" location id "+servicePointLocation.getProperties().getUid()); + + PhysicalLocation commune = getLocation(servicePoint.getProperties().getParentId(), false, false); + if(commune == null || commune.getProperties() == null || commune.getProperties().getParentId() == null) return; + logger.info("Commune name"+commune.getProperties().getName()+" commune id "+servicePoint.getProperties().getParentId()); + + String districtId = commune.getProperties().getParentId(); + PhysicalLocation district = getLocation(districtId, false, false); + + if(districtId == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; + logger.info("District name "+district.getProperties().getName() +" district id "+commune.getProperties().getParentId()); + + PhysicalLocation region = getLocation(district.getProperties().getParentId(), false, false); + + if(region == null) return; + String regionId = district.getProperties().getParentId(); + logger.info("Region name "+region.getProperties().getName() +" region id "+regionId); + List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, + Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); + for (PlanDefinition plan : + plans) { + logger.info("Processing tasks for planID "+plan.getIdentifier()); + taskGenerator.processPlanEvaluation(plan, null,username); + } + } } diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index 611ab3348..e43b0fe37 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -245,6 +245,12 @@ public void updateInventory(Inventory inventory, String userName) { stock.setId(existingStock.getId()); stock.setDateEdited(DateTime.now()); allStocks.update(stock); + + logger.info("Init updating tasks after adding stock"); + StructureMetadataExample structureMetadataExample = new StructureMetadataExample(); + structureMetadataExample.createCriteria().andGeojsonIdEqualTo(stock.getId()); + Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); + physicalLocationService.regenerateTasksForOperationalArea(structure, userName); } public Stock findByIdentifierAndServicePointId(String identifier, String locationId) { From 22c78a2fdc8283eb45731c3c9756fd76561b4568 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 24 Jan 2024 08:52:03 +0300 Subject: [PATCH 07/13] code refactor --- pom.xml | 2 +- .../repository/LocationRepository.java | 1 + .../StructureCreateOrUpdateEvent.java | 12 +++++ .../postgres/LocationRepositoryImpl.java | 5 ++ .../postgres/StructureListener.java | 7 +++ .../service/PhysicalLocationService.java | 45 +++++++++-------- .../org/opensrp/service/StockService.java | 48 +++---------------- 7 files changed, 55 insertions(+), 65 deletions(-) create mode 100644 src/main/java/org/opensrp/repository/StructureCreateOrUpdateEvent.java create mode 100644 src/main/java/org/opensrp/repository/postgres/StructureListener.java diff --git a/pom.xml b/pom.xml index 6dc7bd9da..161833d45 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.9-DEV3-SNAPSHOT + 2.14.9-DEV5-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/repository/LocationRepository.java b/src/main/java/org/opensrp/repository/LocationRepository.java index 101fc6959..d1b959333 100644 --- a/src/main/java/org/opensrp/repository/LocationRepository.java +++ b/src/main/java/org/opensrp/repository/LocationRepository.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.opensrp.domain.LocationDetail; import org.opensrp.domain.StructureCount; +import org.opensrp.repository.postgres.StructureListener; import org.smartregister.domain.PhysicalLocation; import org.opensrp.domain.StructureDetails; import org.opensrp.search.LocationSearchBean; diff --git a/src/main/java/org/opensrp/repository/StructureCreateOrUpdateEvent.java b/src/main/java/org/opensrp/repository/StructureCreateOrUpdateEvent.java new file mode 100644 index 000000000..6c349cf02 --- /dev/null +++ b/src/main/java/org/opensrp/repository/StructureCreateOrUpdateEvent.java @@ -0,0 +1,12 @@ +package org.opensrp.repository; + +import org.opensrp.domain.postgres.Structure; +import org.springframework.context.ApplicationEvent; + +public class StructureCreateOrUpdateEvent extends ApplicationEvent { + public StructureCreateOrUpdateEvent(Structure source) { + super(source); + } +} + + diff --git a/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java index 758824d05..c949b52ef 100644 --- a/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java @@ -46,6 +46,7 @@ import org.smartregister.domain.PhysicalLocation; import org.smartregister.domain.PhysicalLocationAndStocks; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -67,6 +68,8 @@ public class LocationRepositoryImpl extends BaseRepositoryImpl @Autowired private LocationTagService locationTagService; + @Autowired + private ApplicationEventPublisher applicationEventPublisher; @Override public PhysicalLocation get(String id) { return convert(locationMetadataMapper.findById(id, true, false)); @@ -162,6 +165,7 @@ private void addStructure(PhysicalLocation entity) { structureMetadataMapper.insertSelective(structureMetadata); + applicationEventPublisher.publishEvent(pgStructure); } @Override @@ -230,6 +234,7 @@ private void updateStructure(PhysicalLocation entity, Long id) { structureMetadata.setId(metadata.getId()); structureMetadata.setDateCreated(metadata.getDateCreated()); structureMetadataMapper.updateByPrimaryKey(structureMetadata); + applicationEventPublisher.publishEvent(pgStructure); } @Override diff --git a/src/main/java/org/opensrp/repository/postgres/StructureListener.java b/src/main/java/org/opensrp/repository/postgres/StructureListener.java new file mode 100644 index 000000000..50e3ab42c --- /dev/null +++ b/src/main/java/org/opensrp/repository/postgres/StructureListener.java @@ -0,0 +1,7 @@ +package org.opensrp.repository.postgres; + +import org.opensrp.domain.postgres.Structure; + +public interface StructureListener { + void onCreateOrUpdateStructure(Structure structure); +} diff --git a/src/main/java/org/opensrp/service/PhysicalLocationService.java b/src/main/java/org/opensrp/service/PhysicalLocationService.java index 4c43cee72..f7f548ae6 100755 --- a/src/main/java/org/opensrp/service/PhysicalLocationService.java +++ b/src/main/java/org/opensrp/service/PhysicalLocationService.java @@ -28,6 +28,7 @@ import org.opensrp.domain.StructureDetails; import org.opensrp.domain.postgres.Structure; import org.opensrp.repository.LocationRepository; +import org.opensrp.repository.StructureCreateOrUpdateEvent; import org.opensrp.search.LocationSearchBean; import org.smartregister.domain.LocationProperty; import org.smartregister.domain.PhysicalLocation; @@ -35,13 +36,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Service; import com.google.gson.JsonElement; import com.google.gson.JsonParser; @Service -public class PhysicalLocationService { +public class PhysicalLocationService implements ApplicationListener { @Autowired PlanService planService; @@ -596,42 +598,39 @@ public List findStructureIdsByProperties(List parentIds, Map plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, - Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); + Collections.singletonList(operationalAreaId), false, PlanDefinition.PlanStatus.ACTIVE); for (PlanDefinition plan : plans) { logger.info("Processing tasks for planID "+plan.getIdentifier()); taskGenerator.processPlanEvaluation(plan, null,username); } } + @Override + public void onApplicationEvent(StructureCreateOrUpdateEvent structureCreateOrUpdateEvent) { + Structure structure = (Structure) structureCreateOrUpdateEvent.getSource(); + logger.info("updating structure qw "+structure.getJson()); + regenerateTasksForOperationalArea(structure); + } } diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index e43b0fe37..5470f9fe1 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -189,48 +189,10 @@ public void addInventory(Inventory inventory, String userName) { return; } allStocks.add(stock); - // Go up the location tree to get the operational area. - //TODO Make process configurable - logger.info("Init updating tasks after adding stock"); - StructureMetadataExample structureMetadataExample = new StructureMetadataExample(); - structureMetadataExample.createCriteria().andGeojsonIdEqualTo(stock.getId()); - Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); - PhysicalLocation servicePoint = (PhysicalLocation) structure.getJson(); - - if(structure.getJson() == null || servicePoint.getProperties().getParentId() == null) - return;; - logger.info("Fetching parent location for servicepoint "+servicePoint.getProperties().getUid()); - String servicePointParentId = servicePoint.getProperties().getParentId(); - - PhysicalLocation servicePointLocation = physicalLocationService.getLocation(servicePointParentId, false, false); - - if(servicePointLocation == null || servicePointLocation.getProperties() == null || servicePointLocation.getProperties().getParentId() == null) return; - logger.info("Service Point name "+servicePointLocation.getProperties().getName()+" location id "+servicePointLocation.getProperties().getUid()); - - PhysicalLocation commune = physicalLocationService.getLocation(servicePoint.getProperties().getParentId(), false, false); - if(commune == null || commune.getProperties() == null || commune.getProperties().getParentId() == null) return; - logger.info("Commune name"+commune.getProperties().getName()+" commune id "+servicePoint.getProperties().getParentId()); + generateTasks(stock); - String districtId = commune.getProperties().getParentId(); - PhysicalLocation district = physicalLocationService.getLocation(districtId, false, false); - - if(districtId == null || district.getProperties() == null || district.getProperties().getParentId() == null) return; - logger.info("District name "+district.getProperties().getName() +" district id "+commune.getProperties().getParentId()); - - PhysicalLocation region = physicalLocationService.getLocation(district.getProperties().getParentId(), false, false); - - if(region == null) return; - String regionId = district.getProperties().getParentId(); - logger.info("Region name "+region.getProperties().getName() +" region id "+regionId); - List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, - Collections.singletonList(regionId), false, PlanDefinition.PlanStatus.ACTIVE); - for (PlanDefinition plan : - plans) { - logger.info("Processing tasks for planID "+plan.getIdentifier()); - taskGenerator.processPlanEvaluation(plan, null,userName); - } } - + public void updateInventory(Inventory inventory, String userName) { validateFields(inventory); if(inventory.getStockId() == null) { @@ -247,10 +209,14 @@ public void updateInventory(Inventory inventory, String userName) { allStocks.update(stock); logger.info("Init updating tasks after adding stock"); + generateTasks(stock); + } + + private void generateTasks(Stock stock) { StructureMetadataExample structureMetadataExample = new StructureMetadataExample(); structureMetadataExample.createCriteria().andGeojsonIdEqualTo(stock.getId()); Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); - physicalLocationService.regenerateTasksForOperationalArea(structure, userName); + physicalLocationService.regenerateTasksForOperationalArea(structure); } public Stock findByIdentifierAndServicePointId(String identifier, String locationId) { From 43ca92dadec32450dcb75f688787ac5ca6498bb0 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 24 Jan 2024 16:54:27 +0300 Subject: [PATCH 08/13] more code refactor --- .../org/opensrp/repository/postgres/StructureListener.java | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/main/java/org/opensrp/repository/postgres/StructureListener.java diff --git a/src/main/java/org/opensrp/repository/postgres/StructureListener.java b/src/main/java/org/opensrp/repository/postgres/StructureListener.java deleted file mode 100644 index 50e3ab42c..000000000 --- a/src/main/java/org/opensrp/repository/postgres/StructureListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.opensrp.repository.postgres; - -import org.opensrp.domain.postgres.Structure; - -public interface StructureListener { - void onCreateOrUpdateStructure(Structure structure); -} From bfa0ababd32092df35bdcb8e78b2787ade804f7c Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 24 Jan 2024 16:58:55 +0300 Subject: [PATCH 09/13] fix usage of event --- pom.xml | 2 +- .../repository/LocationRepository.java | 1 - .../postgres/LocationRepositoryImpl.java | 8 ++++--- .../service/PhysicalLocationService.java | 23 +++++++------------ .../org/opensrp/service/StockService.java | 1 - 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 161833d45..b35a427b5 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ opensrp-server-core jar - 2.14.9-DEV5-SNAPSHOT + 2.14.9-ALPHA-SNAPSHOT opensrp-server-core OpenSRP Server Core module https://github.com/OpenSRP/opensrp-server-core diff --git a/src/main/java/org/opensrp/repository/LocationRepository.java b/src/main/java/org/opensrp/repository/LocationRepository.java index d1b959333..101fc6959 100644 --- a/src/main/java/org/opensrp/repository/LocationRepository.java +++ b/src/main/java/org/opensrp/repository/LocationRepository.java @@ -9,7 +9,6 @@ import org.apache.commons.lang3.tuple.Pair; import org.opensrp.domain.LocationDetail; import org.opensrp.domain.StructureCount; -import org.opensrp.repository.postgres.StructureListener; import org.smartregister.domain.PhysicalLocation; import org.opensrp.domain.StructureDetails; import org.opensrp.search.LocationSearchBean; diff --git a/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java index c949b52ef..c4a415720 100644 --- a/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java +++ b/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java @@ -33,6 +33,7 @@ import org.opensrp.domain.postgres.StructureMetadata; import org.opensrp.domain.postgres.StructureMetadataExample; import org.opensrp.repository.LocationRepository; +import org.opensrp.repository.StructureCreateOrUpdateEvent; import org.opensrp.repository.postgres.mapper.custom.CustomLocationMapper; import org.opensrp.repository.postgres.mapper.custom.CustomLocationMetadataMapper; import org.opensrp.repository.postgres.mapper.custom.CustomStructureMapper; @@ -164,8 +165,8 @@ private void addStructure(PhysicalLocation entity) { StructureMetadata structureMetadata = createStructureMetadata(entity, pgStructure.getId()); structureMetadataMapper.insertSelective(structureMetadata); - - applicationEventPublisher.publishEvent(pgStructure); + StructureCreateOrUpdateEvent structureEvent = new StructureCreateOrUpdateEvent(pgStructure); + applicationEventPublisher.publishEvent(structureEvent); } @Override @@ -234,7 +235,8 @@ private void updateStructure(PhysicalLocation entity, Long id) { structureMetadata.setId(metadata.getId()); structureMetadata.setDateCreated(metadata.getDateCreated()); structureMetadataMapper.updateByPrimaryKey(structureMetadata); - applicationEventPublisher.publishEvent(pgStructure); + StructureCreateOrUpdateEvent structureEvent = new StructureCreateOrUpdateEvent(pgStructure); + applicationEventPublisher.publishEvent(structureEvent); } @Override diff --git a/src/main/java/org/opensrp/service/PhysicalLocationService.java b/src/main/java/org/opensrp/service/PhysicalLocationService.java index f7f548ae6..a28bca60d 100755 --- a/src/main/java/org/opensrp/service/PhysicalLocationService.java +++ b/src/main/java/org/opensrp/service/PhysicalLocationService.java @@ -28,7 +28,6 @@ import org.opensrp.domain.StructureDetails; import org.opensrp.domain.postgres.Structure; import org.opensrp.repository.LocationRepository; -import org.opensrp.repository.StructureCreateOrUpdateEvent; import org.opensrp.search.LocationSearchBean; import org.smartregister.domain.LocationProperty; import org.smartregister.domain.PhysicalLocation; @@ -36,14 +35,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; -import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Service; import com.google.gson.JsonElement; import com.google.gson.JsonParser; @Service -public class PhysicalLocationService implements ApplicationListener { +public class PhysicalLocationService { @Autowired PlanService planService; @@ -605,7 +603,7 @@ public void regenerateTasksForOperationalArea(Structure structure){ */ assert structure != null; PhysicalLocation servicePoint = (PhysicalLocation) structure.getJson(); - logger.info("Fetching parent location for service point "+servicePoint.getProperties().getUid()); + logger.info("Fetching parent location for service point "+servicePoint.getProperties().getParentId()); String currentLevelLocationId = servicePoint.getProperties().getParentId(); // get username from service point @@ -613,24 +611,19 @@ public void regenerateTasksForOperationalArea(Structure structure){ PhysicalLocation operationalArea = null; for (int i = 0; i <3 ; i++) { operationalArea = getLocation(currentLevelLocationId, false, false); + logger.info("Current operational area "+operationalArea.getProperties().getName() +" id "+operationalArea.getId()); + currentLevelLocationId = operationalArea.getProperties().getParentId(); + logger.info("parentId "+currentLevelLocationId); + } - - if(operationalArea == null) return; - String operationalAreaId = operationalArea.getProperties().getUid(); - logger.info("Generating tasks for operationalArea "+operationalArea.getProperties().getName() +"with id "+operationalAreaId); + logger.info("Generating tasks for operationalArea "+operationalArea.getProperties().getName() +"with id "+operationalArea.getId()); List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, - Collections.singletonList(operationalAreaId), false, PlanDefinition.PlanStatus.ACTIVE); + Collections.singletonList(operationalArea.getId()), false, PlanDefinition.PlanStatus.ACTIVE); for (PlanDefinition plan : plans) { logger.info("Processing tasks for planID "+plan.getIdentifier()); taskGenerator.processPlanEvaluation(plan, null,username); } } - @Override - public void onApplicationEvent(StructureCreateOrUpdateEvent structureCreateOrUpdateEvent) { - Structure structure = (Structure) structureCreateOrUpdateEvent.getSource(); - logger.info("updating structure qw "+structure.getJson()); - regenerateTasksForOperationalArea(structure); - } } diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index 5470f9fe1..ff2affbf4 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -12,7 +12,6 @@ import org.smartregister.domain.Inventory; import org.smartregister.domain.ProductCatalogue; import org.smartregister.domain.PhysicalLocation; -import org.smartregister.domain.PlanDefinition; import org.smartregister.domain.Stock; import org.opensrp.dto.CsvBulkImportDataSummary; import org.opensrp.dto.FailedRecordSummary; From ea60dc38ca543c8900719e75c90638c02727052b Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 31 Jan 2024 11:03:42 +0300 Subject: [PATCH 10/13] add release file --- .github/workflows/release.yml | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..5e929e618 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,42 @@ +# This workflow will build a Java project with Gradle +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle + +name: Snapshot Release + +on: + push: + tags: + - v[0-9]+.[0-9]+.[0-9]+-SNAPSHOT + - v[0-9]+.[0-9]+.[0-9]+-[0-9a-zA-Z]+-SNAPSHOT + - v[0-9]+.[0-9]+.[0-9]+-[0-9a-zA-Z]+-[0-9a-zA-Z]+-SNAPSHOT + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Cancel previous workflow runs + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} + + - uses: actions/checkout@v2 + with: + submodules: recursive + + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + + - name: Decode & Generate Settings.xml file + run: echo $SETTINGS_FILE | base64 -di > ~/.m2/settings.xml + env: + SETTINGS_FILE: ${{ secrets.SETTINGS_XML }} + + - name: Generate & upload library snapshot artifact JAR (Java Archive) file + run: mvn clean deploy -Dmaven.test.skip=true --no-transfer-progress + + - name: Github Release + uses: softprops/action-gh-release@v1 + with: + prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') }} From ace939f816b6944174eef07842b78a15caebbde1 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 31 Jan 2024 15:23:59 +0300 Subject: [PATCH 11/13] fix cyclic references --- .../service/PhysicalLocationService.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/opensrp/service/PhysicalLocationService.java b/src/main/java/org/opensrp/service/PhysicalLocationService.java index a28bca60d..79a6cd10b 100755 --- a/src/main/java/org/opensrp/service/PhysicalLocationService.java +++ b/src/main/java/org/opensrp/service/PhysicalLocationService.java @@ -28,13 +28,17 @@ import org.opensrp.domain.StructureDetails; import org.opensrp.domain.postgres.Structure; import org.opensrp.repository.LocationRepository; +import org.opensrp.repository.PlanRepository; +import org.opensrp.repository.postgres.PlanRepositoryImpl; import org.opensrp.search.LocationSearchBean; import org.smartregister.domain.LocationProperty; import org.smartregister.domain.PhysicalLocation; import org.smartregister.domain.PlanDefinition; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import com.google.gson.JsonElement; @@ -43,10 +47,13 @@ @Service public class PhysicalLocationService { - @Autowired - PlanService planService; @Autowired TaskGenerator taskGenerator; + + @Autowired + private ApplicationContext applicationContext; + @Value("#{opensrp['operational.area.levels.from.structure'] ?: 3}") + private int iterationsToOperationalArea; private static Logger logger = LogManager.getLogger(PhysicalLocationService.class.toString()); private LocationRepository locationRepository; @@ -57,7 +64,7 @@ public class PhysicalLocationService { public void setLocationRepository(LocationRepository locationRepository) { this.locationRepository = locationRepository; } - + public PhysicalLocation getLocation(String id, boolean returnGeometry, boolean includeInactive) { return locationRepository.get(id, returnGeometry, includeInactive); } @@ -606,19 +613,18 @@ public void regenerateTasksForOperationalArea(Structure structure){ logger.info("Fetching parent location for service point "+servicePoint.getProperties().getParentId()); String currentLevelLocationId = servicePoint.getProperties().getParentId(); // get username from service point - String username = servicePoint.getProperties().getUsername(); PhysicalLocation operationalArea = null; - for (int i = 0; i <3 ; i++) { + for (int i = 0; i < iterationsToOperationalArea ; i++) { operationalArea = getLocation(currentLevelLocationId, false, false); logger.info("Current operational area "+operationalArea.getProperties().getName() +" id "+operationalArea.getId()); currentLevelLocationId = operationalArea.getProperties().getParentId(); logger.info("parentId "+currentLevelLocationId); - } logger.info("Generating tasks for operationalArea "+operationalArea.getProperties().getName() +"with id "+operationalArea.getId()); - List plans = planService.getPlanRepository().getPlansByServerVersionAndOperationalAreasAndStatus(0L, + PlanRepository planRepository = applicationContext.getBean(PlanRepositoryImpl.class); + List plans = planRepository.getPlansByServerVersionAndOperationalAreasAndStatus(0L, Collections.singletonList(operationalArea.getId()), false, PlanDefinition.PlanStatus.ACTIVE); for (PlanDefinition plan : plans) { From 8f588706a3bbd9995238316ddf22a75c09afe15e Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 31 Jan 2024 16:22:22 +0300 Subject: [PATCH 12/13] fix failing StockServiceTests --- src/main/java/org/opensrp/service/StockService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensrp/service/StockService.java b/src/main/java/org/opensrp/service/StockService.java index ff2affbf4..af72005e7 100755 --- a/src/main/java/org/opensrp/service/StockService.java +++ b/src/main/java/org/opensrp/service/StockService.java @@ -214,8 +214,11 @@ public void updateInventory(Inventory inventory, String userName) { private void generateTasks(Stock stock) { StructureMetadataExample structureMetadataExample = new StructureMetadataExample(); structureMetadataExample.createCriteria().andGeojsonIdEqualTo(stock.getId()); - Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); - physicalLocationService.regenerateTasksForOperationalArea(structure); + // null check to keep tests from failing + if(structureMetadataMapper != null) { + Structure structure = structureMetadataMapper.findById(stock.getLocationId(), true); + physicalLocationService.regenerateTasksForOperationalArea(structure); + } } public Stock findByIdentifierAndServicePointId(String identifier, String locationId) { From 254736bc8850cdbeca961dae9b536f22e2ce263f Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 31 Jan 2024 17:04:31 +0300 Subject: [PATCH 13/13] update configs file --- configs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs b/configs index c3b715d4e..c9552e1cf 160000 --- a/configs +++ b/configs @@ -1 +1 @@ -Subproject commit c3b715d4eba79bca22ea7e87a633ebe8655d3c86 +Subproject commit c9552e1cfbc9438f7ee90416b8fd2e2e57989ed5