Skip to content

Commit

Permalink
generate tasks after adding or updating stock item
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Jan 15, 2024
1 parent 9f8072c commit 5b23603
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/opensrp/repository/PlanRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public interface PlanRepository extends BaseRepository<PlanDefinition>, PlanDao {

List<PlanDefinition> getPlansByServerVersionAndOperationalAreas(Long serverVersion, List<String> operationalAreaIds, boolean experimental);
List<PlanDefinition> getPlansByServerVersionAndOperationalAreasAndStatus(Long serverVersion, List<String> operationalAreaIds,
boolean experimental, PlanDefinition.PlanStatus status);

/**
* This method searches for plans using a list of provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ public List<PlanDefinition> getPlansByServerVersionAndOperationalAreas(Long serv
PlanExample planExample = new PlanExample();
planExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion).andDateDeletedIsNull().andExperimentalEqualTo(experimental);
List<Plan> plans = planMetadataMapper.selectMany(planExample, operationalAreaIds, 0, DEFAULT_FETCH_SIZE);

return convert(plans);
}

public List<PlanDefinition> getPlansByServerVersionAndOperationalAreasAndStatus(Long serverVersion, List<String> operationalAreaIds, boolean experimental,
PlanDefinition.PlanStatus status) {
PlanExample planExample = new PlanExample();
planExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion).andDateDeletedIsNull()
.andExperimentalEqualTo(experimental);
List<Plan> plans = planMetadataMapper.selectManyByStatus(planExample, operationalAreaIds, 0, DEFAULT_FETCH_SIZE, status.value());

return convert(plans);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
public interface CustomPlanMetadataMapper extends PlanMetadataMapper {
List<Plan> selectMany(@Param("example") PlanExample planExample, @Param("operationalAreaIds") List<String> operationalAreaIds, @Param("offset") int offset,
@Param("limit") int limit);

List<Plan> selectManyByStatus(@Param("example") PlanExample planExample, @Param("operationalAreaIds") List<String> operationalAreaIds, @Param("offset") int offset,
@Param("limit") int limit, @Param("status") String status);

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,30 @@
LIMIT #{limit} OFFSET #{offset}
</select>

</mapper>
<select id="selectManyByStatus"
resultMap="org.opensrp.repository.postgres.mapper.PlanMapper.BaseResultMap">
select
<include refid="Base_Column_List" />
from core.plan p
<where>
<trim prefixOverrides="and">
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="operationalAreaIds != null">
and p.id in ( select plan_id from core.plan_metadata where
operational_area_id IN <foreach item="operationalAreaId" collection="operationalAreaIds" open="(" separator="," close=")">#{operationalAreaId}</foreach>
)
</if>
<if test="status != null">
and p.json->>'status' = #{status,jdbcType=VARCHAR}
</if>
</trim>
</where>
<if test="example.orderByClause != null">
order by pm.${example.orderByClause}
</if>
LIMIT #{limit} OFFSET #{offset}
</select>

</mapper>
29 changes: 28 additions & 1 deletion src/main/java/org/opensrp/service/StockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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<PlanDefinition> 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) {
Expand Down

0 comments on commit 5b23603

Please sign in to comment.