Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Jan 24, 2024
1 parent 853c778 commit 22c78a2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.14.9-DEV3-SNAPSHOT</version>
<version>2.14.9-DEV5-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check notice on line 12 in src/main/java/org/opensrp/repository/LocationRepository.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/opensrp/repository/LocationRepository.java#L12

Unused import - org.opensrp.repository.postgres.StructureListener.
import org.smartregister.domain.PhysicalLocation;
import org.opensrp.domain.StructureDetails;
import org.opensrp.search.LocationSearchBean;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -67,6 +68,8 @@ public class LocationRepositoryImpl extends BaseRepositoryImpl<PhysicalLocation>
@Autowired
private LocationTagService locationTagService;

@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Override
public PhysicalLocation get(String id) {
return convert(locationMetadataMapper.findById(id, true, false));
Expand Down Expand Up @@ -162,6 +165,7 @@ private void addStructure(PhysicalLocation entity) {

structureMetadataMapper.insertSelective(structureMetadata);

applicationEventPublisher.publishEvent(pgStructure);
}

@Override
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.opensrp.repository.postgres;

import org.opensrp.domain.postgres.Structure;

public interface StructureListener {
void onCreateOrUpdateStructure(Structure structure);
}
45 changes: 22 additions & 23 deletions src/main/java/org/opensrp/service/PhysicalLocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@
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;
import org.smartregister.domain.PlanDefinition;
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<StructureCreateOrUpdateEvent> {

@Autowired
PlanService planService;
Expand Down Expand Up @@ -596,42 +598,39 @@ public List<String> findStructureIdsByProperties(List<String> parentIds, Map<Str
return locationRepository.findStructureIdsByProperties(parentIds,properties,limit);
}

public void regenerateTasksForOperationalArea(Structure structure, String username){
public void regenerateTasksForOperationalArea(Structure structure){
/*
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();
logger.info("Fetching parent location for service point "+servicePoint.getProperties().getUid());
String currentLevelLocationId = servicePoint.getProperties().getParentId();
// get username from service point

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());
String username = servicePoint.getProperties().getUsername();
PhysicalLocation operationalArea = null;
for (int i = 0; i <3 ; i++) {
operationalArea = getLocation(currentLevelLocationId, false, false);
}

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);
if(operationalArea == null) return;
String operationalAreaId = operationalArea.getProperties().getUid();
logger.info("Generating tasks for operationalArea "+operationalArea.getProperties().getName() +"with id "+operationalAreaId);
List<PlanDefinition> 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);
}
}
48 changes: 7 additions & 41 deletions src/main/java/org/opensrp/service/StockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlanDefinition> 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 22c78a2

Please sign in to comment.