From 22c78a2fdc8283eb45731c3c9756fd76561b4568 Mon Sep 17 00:00:00 2001 From: hilpitome Date: Wed, 24 Jan 2024 08:52:03 +0300 Subject: [PATCH] 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) {