Skip to content

Commit

Permalink
Merge pull request #512 from opensrp/509-missing-task-gen
Browse files Browse the repository at this point in the history
Implement functionlaty to return missing tasks for plan
  • Loading branch information
Rkareko authored Dec 7, 2021
2 parents 8befb04 + 416bdbd commit e00f2b6
Show file tree
Hide file tree
Showing 30 changed files with 949 additions and 39 deletions.
4 changes: 2 additions & 2 deletions 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.12.14-SNAPSHOT</version>
<version>2.12.15-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand All @@ -23,7 +23,7 @@

<opensrp.api.version>1.0.6-SNAPSHOT</opensrp.api.version>
<opensrp.interface.version>2.0.1-SNAPSHOT</opensrp.interface.version>
<opensrp.plan.evaluator.version>1.6.4-SNAPSHOT</opensrp.plan.evaluator.version>
<opensrp.plan.evaluator.version>1.6.5-SNAPSHOT</opensrp.plan.evaluator.version>
<jackson.version>2.12.1</jackson.version>
<powermock.version>2.0.5</powermock.version>
<lombok.version>1.18.12</lombok.version>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/opensrp/domain/PlanTaskCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.opensrp.domain;

import java.io.Serializable;
import java.util.List;

public class PlanTaskCount implements Serializable {

private static final long serialVersionUID = 1L;

private String planIdentifier;

private List<TaskCount> taskCounts;

public String getPlanIdentifier() {
return planIdentifier;
}

public void setPlanIdentifier(String planIdentifier) {
this.planIdentifier = planIdentifier;
}

public List<TaskCount> getTaskCounts() {
return taskCounts;
}

public void setTaskCounts(List<TaskCount> taskCounts) {
this.taskCounts = taskCounts;
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/opensrp/domain/TaskCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.opensrp.domain;

import java.io.Serializable;

public class TaskCount implements Serializable {
private static final long serialVersionUID = 1L;

private String code;

private long actualCount;

private long expectedCount;

private long missingCount;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public long getActualCount() {
return actualCount;
}

public void setActualCount(long actualCount) {
this.actualCount = actualCount;
}

public long getExpectedCount() {
return expectedCount;
}

public void setExpectedCount(long expectedCount) {
this.expectedCount = expectedCount;
}

public long getMissingCount() {
return missingCount;
}

public void setMissingCount(long missingCount) {
this.missingCount = missingCount;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/opensrp/repository/ClientsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,7 @@ public interface ClientsRepository extends BaseRepository<Client>, ClientDao {
* @return
*/
List<Client> convert(List<org.opensrp.domain.postgres.Client> clients);

Long countFamilyMembersByLocation(List<String> locationIds, Integer ageLowerBound);

}
26 changes: 26 additions & 0 deletions src/main/java/org/opensrp/repository/LocationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,30 @@ List<PhysicalLocation> findStructuresByProperties(boolean returnGeometry, String
*/
List<PhysicalLocationAndStocks> findLocationAndStocksByJurisdiction(String jurisdictionId, Map<String, String> properties,
boolean returnGeometry, int limit);

/**
* This methods returns a count of jurisdictions using the parentId and location properties
* It returns the Geometry optionally if @param returnGeometry is set to true.
* @param parentIds list of the parent ids of the jurisdiction being searched. If empty search for ROOT location.
* @param properties map of location properties to filter with, each entry in map has property name and value
* @return count of jurisdictions matching the params
*/
long countLocationsByProperties(List<String> parentIds, Map<String, String> properties);

/**
* This methods returns a count of structures using the parentId and structure properties
* It returns the Geometry optionally if @param returnGeometry is set to true.
* @param parentIds list of the parent ids of the structure being searched. If empty search for ROOT location.
* @param properties map of location properties to filter with, each entry in map has property name and value
* @return count of jurisdictions matching the params
*/
long countStructuresByProperties(List<String> parentIds, Map<String, String> properties);

/**
* This methods searches for structures ids using the parentId and location properties
* @param parentIds list of the parent ids of the structure being searched
* @param properties map of location properties to filter with, each entry in map has property name and value
* @return structure ids matching the params
*/
List<String> findStructureIdsByProperties(List<String> parentIds, Map<String, String> properties,int limit);
}
10 changes: 10 additions & 0 deletions src/main/java/org/opensrp/repository/PlanRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,14 @@ public interface PlanRepository extends BaseRepository<PlanDefinition>, PlanDao
Long countPlansByIdentifiersAndServerVersion(List<String> planIdentifiers, Long serverVersion);

List<PlanDefinition> getAllPlans(PlanSearchBean planSearchBean);

/** Gets the plans using the plan identifiers filtered by date edited and status
* @param planIdentifiers the plan identifiers
* @param status status of the plan
* @param fromDate lower bound of when the plan was edited
* @param toDate upper bound of when the plan was edited
* @return plans with the identifiers filtered by date edited and status
*/
List<PlanDefinition> getPlansByIdentifiersAndStatusAndDateEdited(List<String> planIdentifiers, PlanDefinition.PlanStatus status,
Date fromDate, Date toDate);
}
13 changes: 12 additions & 1 deletion src/main/java/org/opensrp/repository/TaskRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ public interface TaskRepository extends BaseRepository<Task>, TaskDao {

int getTaskCount(TaskSearchBean searchBean);

}
/**
* This method returns a count of tasks with a particular code for a plan
*
* @param plan plan identifier for the task
* @param code the code for the task
* @param entityIds Ids for entities the tasks are generated against
* @param excludePlanTasks whether to exclude tasks from the current plan in the count
* @return count of tasks with a particular code for a plan
*/
Long countTasksByPlanAndCode(String plan, String code, List<String> entityIds, boolean excludePlanTasks);

}
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public List<Client> convert(List<org.opensrp.domain.postgres.Client> clients) {

return convertedClients;
}

private Client convert(org.opensrp.domain.postgres.Client client) {
if (client == null || client.getJson() == null || !(client.getJson() instanceof Client)) {
return null;
Expand Down Expand Up @@ -763,6 +763,20 @@ public List<Client> findByLocationIdExclusiveOfType(String locationId, String cl
return convert(clients);
}

@Override
public Long countFamilyMembersByLocation(List<String> locationIds, Integer ageLowerBound) {
ClientMetadataExample clientMetadataExample = new ClientMetadataExample();
Criteria criteria = clientMetadataExample.createCriteria();
criteria.andLocationIdIn(locationIds).andClientTypeIsNull();
if (ageLowerBound != null) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -5);
Date date = calendar.getTime();
criteria.andBirthDateLessThan(date);
}
return clientMetadataMapper.countMany(clientMetadataExample);
}

private List<Patient> convertToFHIR(List<Client> clients) {
return clients.stream().map(client -> ClientConverter.convertClientToPatientResource(client))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ public List<PhysicalLocationAndStocks> findLocationAndStocksByJurisdiction(Strin
returnGeometry, 0, limit));
}


private PhysicalLocationAndStocks convertToPhysicalLocationAndStock(LocationAndStock entity) {
if (entity == null || entity.getJson() == null || !(entity.getJson() instanceof PhysicalLocation)) {
return null;
Expand Down Expand Up @@ -1020,4 +1019,41 @@ private List<com.ibm.fhir.model.resource.Location> convertToFHIRLocation(List<Ph
return locations.stream().map(location -> LocationConverter.convertPhysicalLocationToLocationResource(location))
.collect(Collectors.toList());
}

@Override
public long countLocationsByProperties(List<String> parentIds, Map<String, String> properties) {
LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
if (parentIds != null && !parentIds.isEmpty()) {
locationMetadataExample.createCriteria().andParentIdIn(parentIds);
}
return locationMetadataMapper.countManyByProperties(locationMetadataExample, properties);
}

/**
* {@inheritDoc}
*/
@Override
public long countStructuresByProperties(List<String> parentIds, Map<String, String> properties) {
StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
if (parentIds != null && !parentIds.isEmpty()) {
structureMetadataExample.createCriteria().andParentIdIn(parentIds);
}
return structureMetadataMapper.countManyByProperties(structureMetadataExample, properties);
}


/**
* {@inheritDoc}
*/
@Override
public List<String> findStructureIdsByProperties(List<String> parentIds, Map<String, String> properties, int limit) {
int fetchLimit = limit > 0 ? limit : DEFAULT_FETCH_SIZE;
StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
if (parentIds != null && !parentIds.isEmpty()) {
structureMetadataExample.createCriteria().andParentIdIn(parentIds);
}
List<String> structureIds = structureMetadataMapper.selectManyIdsByProperties(structureMetadataExample, properties,
0, fetchLimit);
return structureIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.opensrp.util.Utils.isEmptyList;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -393,4 +394,35 @@ private Pair<Integer, Integer> getPageSizeAndOffset(PlanSearchBean planSearchBea
public PlanDefinition findPlanByIdentifier(String identifier) {
return get(identifier);
}

/**
* {@inheritDoc}
*/
@Override
public List<PlanDefinition> getPlansByIdentifiersAndStatusAndDateEdited(List<String> planIdentifiers, PlanDefinition.PlanStatus status, Date fromDate, Date toDate) {
if (toDate == null && fromDate == null && planIdentifiers != null) {
return getPlansByIdentifiersAndServerVersion(planIdentifiers,0l, false);
} else {
PlanExample planExample = new PlanExample();
PlanExample.Criteria criteria = planExample.createCriteria();
if (toDate == null && fromDate == null){
Calendar calendar = Calendar.getInstance();
Date localToDate = calendar.getTime();
calendar.add(Calendar.DATE, -1);
Date localFromDate = calendar.getTime();
criteria.andDateEditedBetween( localFromDate, localToDate);
} else if (fromDate == null) {
criteria.andDateEditedLessThanOrEqualTo(toDate);
} else if (toDate == null) {
criteria.andDateEditedGreaterThanOrEqualTo(fromDate);
} else {
criteria.andDateEditedBetween( fromDate, toDate);
}
criteria.andExperimentalEqualTo(false);

List<Plan> plans = planMapper.selectManyByStatus(planExample, status,0, Integer.MAX_VALUE);

return convert(plans);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,23 @@ public int getTaskCount(TaskSearchBean taskSearchBean) {
return taskMetadataMapper.selectTaskCount(taskSearchBean);
}

@Override
public Long countTasksByPlanAndCode(String plan, String code, List<String> entityIds, boolean excludePlanTasks) {
TaskMetadataExample taskMetadataExample = new TaskMetadataExample();
TaskMetadataExample.Criteria criteria = taskMetadataExample.createCriteria();
criteria.andCodeEqualTo(code);
if (entityIds != null && !entityIds.isEmpty()) {
criteria.andForEntityIn(entityIds);
}
if (excludePlanTasks){
criteria.andPlanIdentifierNotEqualTo(plan);
} else {
criteria.andPlanIdentifierEqualTo(plan);
}

return taskMetadataMapper.countByExample(taskMetadataExample);
}

private List<com.ibm.fhir.model.resource.Task> convertToFHIRTasks(List<Task> tasks) {
return tasks.stream().map(task -> TaskConverter.convertTasktoFihrResource(task)).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ List<Location> selectLocations(@Param("locationSearchBean") LocationSearchBean l
List<String> selectChildrenIds(@Param("locationId") String locationId);

Long countMany(@Param("example")LocationMetadataExample locationMetadataExample);

Long countManyByProperties(@Param("example") LocationMetadataExample locationMetadataExample,
@Param("properties") Map<String, String> properties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.opensrp.domain.postgres.PlanExample;
import org.opensrp.repository.postgres.mapper.PlanMapper;
import org.opensrp.search.PlanSearchBean;
import org.smartregister.domain.PlanDefinition;

/**
* Created by Vincent Karuri on 02/05/2019
Expand All @@ -32,4 +33,7 @@ List<Plan> selectPlansBySearchBean(@Param("searchBean") PlanSearchBean searchBea

int updateByPrimaryKeyAndGenerateServerVersion(Plan plan);

List<Plan> selectManyByStatus(@Param("example") PlanExample planExample, @Param("status")PlanDefinition.PlanStatus status, @Param("offset") int offset,
@Param("limit") int limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ List<LocationAndStock> findStructureAndStocksByJurisdiction(@Param("example") St
@Param("properties") Map<String, String> properties, @Param("geometry") boolean returnGeometry,
@Param("offset") int offset, @Param("limit") int limit);

long countManyByProperties(@Param("example") StructureMetadataExample locationMetadataExample,
@Param("properties") Map<String, String> properties);

List<String> selectManyIdsByProperties(@Param("example") StructureMetadataExample locationMetadataExample,
@Param("properties") Map<String, String> properties,
@Param("offset") int offset, @Param("limit") int limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,23 @@

</select>

<select id="countManyByProperties"
resultType="long">
select count(lm.id)

from core.location_metadata lm
join core.location l on lm.location_id = l.id
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="properties != null" >
<trim prefix="and " >
<foreach collection="properties" index="propertyName"
item="propertyValue" separator=" and ">
json -> 'properties' ->> #{propertyName,jdbcType=VARCHAR} = #{propertyValue,jdbcType=VARCHAR}
</foreach>
</trim>
</if>
</select>

</mapper>
Loading

0 comments on commit e00f2b6

Please sign in to comment.