Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCMPRE-1254-Adding household type for communal living facility #1124

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions health-services/household/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-common</artifactId>
<version>1.0.18-SNAPSHOT</version>
<version>1.0.19-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.20-SNAPSHOT</version>
<version>1.0.24-dev-snapshot</version>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please sit with Yashita and get her enum changes merged to the same version of the library

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

She has added the same changes in data model package for apk

<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ public class HouseholdConfiguration {

@Value("${egov.boundary.search.url}")
private String boundarySearchUrl;

@Value("${household.type.same.validation}")
private boolean householdTypeSameValidation;

@Value("${household.type.community.creator.role}")
private String communityHouseholdCreatorRoleCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public SearchResponse<Household> find(HouseholdSearch searchObject, Integer limi
query = GenericQueryBuilder.generateQuery(query, whereFields).toString();
query = query.replace("id IN (:id)", "h.id IN (:id)");
query = query.replace("clientReferenceId IN (:clientReferenceId)", "h.clientReferenceId IN (:clientReferenceId)");
// To consider null values present in db as family if family parameter is passed
if (searchObject.getHouseholdType() != null && searchObject.getHouseholdType().equalsIgnoreCase("FAMILY")) {
query = query.replace("householdType=:householdType", "(householdType!='COMMUNITY' OR householdType IS NULL)");
}

if(CollectionUtils.isEmpty(whereFields)) {
query = query + " where h.tenantId=:tenantId ";
Expand Down Expand Up @@ -129,6 +133,10 @@ public SearchResponse<Household> findByRadius(HouseholdSearch searchObject, Inte
query = GenericQueryBuilder.generateQuery(query, whereFields).toString();
query = query.replace("id IN (:id)", "h.id IN (:id)");
query = query.replace("clientReferenceId IN (:clientReferenceId)", "h.clientReferenceId IN (:clientReferenceId)");
// To consider null values present in db as family if family parameter is passed
if (searchObject.getHouseholdType() != null && searchObject.getHouseholdType().equalsIgnoreCase("FAMILY")) {
query = query.replace("householdType=:householdType", "(householdType!='COMMUNITY' OR householdType IS NULL)");
}

if(CollectionUtils.isEmpty(whereFields)) {
query = query + " where h.tenantId=:tenantId ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import digit.models.coremodels.AuditDetails;
import org.egov.common.contract.models.AuditDetails;
import org.egov.common.models.core.AdditionalFields;
import org.egov.common.models.household.HouseholdMember;
import org.springframework.jdbc.core.RowMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import digit.models.coremodels.AuditDetails;
import org.egov.common.contract.models.AuditDetails;
import org.egov.common.models.core.AdditionalFields;
import org.egov.common.models.household.Address;
import org.egov.common.models.household.AddressType;
import org.egov.common.models.core.Boundary;
import org.egov.common.models.household.HouseHoldType;
import org.egov.common.models.household.Household;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
Expand All @@ -36,6 +37,7 @@ public Household mapRow(ResultSet resultSet, int i) throws SQLException {
Household household = Household.builder()
.id(resultSet.getString("id"))
.rowVersion(resultSet.getInt("rowVersion"))
.householdType(HouseHoldType.fromValue(resultSet.getString("householdType")))
.isDeleted(resultSet.getBoolean("isDeleted"))
.tenantId(resultSet.getString("tenantId"))
.memberCount(resultSet.getInt("numberOfMembers"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.egov.household.service;

import digit.models.coremodels.AuditDetails;
import org.egov.common.contract.models.AuditDetails;
import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.household.Address;
import org.egov.common.models.household.Household;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@
import org.egov.common.validator.Validator;
import org.egov.household.config.HouseholdConfiguration;
import org.egov.household.repository.HouseholdRepository;
import org.egov.household.validators.household.HExistentEntityValidator;
import org.egov.household.validators.household.HBoundaryValidator;
import org.egov.household.validators.household.HIsDeletedValidator;
import org.egov.household.validators.household.HNonExistentEntityValidator;
import org.egov.household.validators.household.HNullIdValidator;
import org.egov.household.validators.household.HRowVersionValidator;
import org.egov.household.validators.household.HUniqueEntityValidator;
import org.egov.household.validators.household.*;
import org.egov.common.models.household.HouseholdSearch;
import org.egov.tracer.model.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
Expand Down Expand Up @@ -62,15 +55,20 @@ public class HouseholdService {

private final Predicate<Validator<HouseholdBulkRequest, Household>> isApplicableForCreate = validator ->
validator.getClass().equals(HBoundaryValidator.class)
|| validator.getClass().equals(HExistentEntityValidator.class);
|| validator.getClass().equals(HExistentEntityValidator.class)
|| validator.getClass().equals(HCommunityValidator.class)
|| validator.getClass().equals(HCommunityTypeValidator.class);

private final Predicate<Validator<HouseholdBulkRequest, Household>> isApplicableForUpdate = validator ->
validator.getClass().equals(HNullIdValidator.class)
|| validator.getClass().equals(HBoundaryValidator.class)
|| validator.getClass().equals(HIsDeletedValidator.class)
|| validator.getClass().equals(HUniqueEntityValidator.class)
|| validator.getClass().equals(HNonExistentEntityValidator.class)
|| validator.getClass().equals(HRowVersionValidator.class);
|| validator.getClass().equals(HRowVersionValidator.class)
|| validator.getClass().equals(HCommunityValidator.class)
|| validator.getClass().equals(HCommunityTypeValidator.class)
|| validator.getClass().equals(HHouseholdTypeChangeValidator.class);

private final Predicate<Validator<HouseholdBulkRequest, Household>> isApplicableForDelete = validator ->
validator.getClass().equals(HNullIdValidator.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.egov.household.validators.household;

import org.egov.common.models.Error;
import org.egov.common.models.household.HouseHoldType;
import org.egov.common.models.household.Household;
import org.egov.common.models.household.HouseholdBulkRequest;
import org.egov.common.validator.Validator;
import org.egov.household.config.HouseholdConfiguration;
import org.egov.tracer.model.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.egov.common.utils.CommonUtils.populateErrorDetails;

public class HCommunityTypeValidator implements Validator<HouseholdBulkRequest, Household> {
private final HouseholdConfiguration configuration;

@Autowired
public HCommunityTypeValidator(HouseholdConfiguration configuration) {
this.configuration = configuration;
}
@Override
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) {
HashMap<Household, List<Error>> errorDetailsMap = new HashMap<>();
if (configuration.isHouseholdTypeSameValidation()) {
// validate if request contains households of different householdTypes
List<Household> communityHouseholds = request.getHouseholds()
.stream()
.filter(household -> household.getHouseholdType() != null &&
household.getHouseholdType().equals(HouseHoldType.COMMUNITY))
.toList();

if (!CollectionUtils.isEmpty(communityHouseholds) &&
request.getHouseholds().size() != communityHouseholds.size()) {
communityHouseholds.forEach(household -> {
Error error = Error.builder()
.errorMessage("Community and Family household cannot be in same request")
.errorCode("COMMUNITY_AND_FAMILY_HOUSEHOLD_IN_SAME_REQUEST")
.type(Error.ErrorType.NON_RECOVERABLE)
.exception(new CustomException("COMMUNITY_AND_FAMILY_HOUSEHOLD_IN_SAME_REQUEST", "Community and Family household cannot be in same request"))
.build();
// Populate error details for the household
populateErrorDetails(household, error, errorDetailsMap);
});
}
}
return errorDetailsMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.egov.household.validators.household;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.household.HouseHoldType;
import org.egov.common.models.household.Household;
import org.egov.common.models.household.HouseholdBulkRequest;
import org.egov.common.validator.Validator;
import org.egov.household.config.HouseholdConfiguration;
import org.egov.tracer.model.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.egov.common.utils.CommonUtils.populateErrorDetails;

@Component
@Order(value = 1)
@Slf4j
public class HCommunityValidator implements Validator<HouseholdBulkRequest, Household> {

private final HouseholdConfiguration configuration;

@Autowired
public HCommunityValidator(HouseholdConfiguration configuration) {
this.configuration = configuration;
}

@Override
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation should be added to check no normal household is there for community household create request and vice versa. and this should be enabled or disabled by config

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

HashMap<Household, List<Error>> errorDetailsMap = new HashMap<>();
List<Household> communityHouseholds = request.getHouseholds()
.stream()
.filter(household -> household.getHouseholdType() != null &&
household.getHouseholdType().equals(HouseHoldType.COMMUNITY))
.toList();

if (!CollectionUtils.isEmpty(communityHouseholds) &&
request.getRequestInfo().getUserInfo().getRoles()
.stream()
.noneMatch(role -> role.getCode().equals(configuration.getCommunityHouseholdCreatorRoleCode()))) {
communityHouseholds.forEach(household -> {
Error error = Error.builder()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error goes to error queue, make sure error is thrown in single api

.errorMessage("User doesn't have permission to create/update community household")
.errorCode("COMMUNITY_USER_ACCESS_DENIED")
.type(Error.ErrorType.NON_RECOVERABLE)
.exception(new CustomException("COMMUNITY_USER_ACCESS_DENIED", "User doesn't have permission to create/update community household"))
.build();
// Populate error details for the household
populateErrorDetails(household, error, errorDetailsMap);
});
}
return errorDetailsMap;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.egov.household.validators.household;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.household.Household;
import org.egov.common.models.household.HouseholdBulkRequest;
import org.egov.common.models.household.HouseholdSearch;
import org.egov.common.validator.Validator;
import org.egov.household.repository.HouseholdRepository;
import org.egov.tracer.model.CustomException;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.egov.common.utils.CommonUtils.*;
import static org.egov.common.utils.CommonUtils.notHavingErrors;

@Slf4j
@Component
@Order(value = 3)
public class HHouseholdTypeChangeValidator implements Validator<HouseholdBulkRequest, Household> {

private final HouseholdRepository householdRepository;

public HHouseholdTypeChangeValidator(HouseholdRepository householdRepository) {
this.householdRepository = householdRepository;
}

@Override
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) {
// Map to hold household entities and their error details
Map<Household, List<Error>> errorDetailsMap = new HashMap<>();
// Get the list of household entities from the request
List<Household> entities = request.getHouseholds();
// Map to store entities by their IDs
Map<String, Household> eMap = entities.stream().filter(notHavingErrors()).collect(Collectors.toMap(Household::getId, household -> household));
// Lists to store IDs and client reference IDs
List<String> idList = new ArrayList<>();
List<String> clientReferenceIdList = new ArrayList<>();
// Extract IDs and client reference IDs from household entities
entities.forEach(household -> {
idList.add(household.getId());
clientReferenceIdList.add(household.getClientReferenceId());
});
// Check if the entity map is not empty
if (!eMap.isEmpty()) {
// Create a search object for querying existing entities
HouseholdSearch householdSearch = HouseholdSearch.builder()
.clientReferenceId(clientReferenceIdList)
.id(idList)
.build();

List<Household> existingEntities;
try {
// Query the repository to find existing entities
existingEntities = householdRepository.find(householdSearch, entities.size(), 0,
entities.get(0).getTenantId(), null, false).getResponse();
} catch (Exception e) {
// Handle query builder exception
log.error("Search failed for Household with error: {}", e.getMessage(), e);
throw new CustomException("HOUSEHOLD_SEARCH_FAILED", "Search Failed for Household, " + e.getMessage());
}
// Check for non-existent entities
List<Household> entitiesWithHouseholdTypeChange = changeInHouseholdType(eMap,
existingEntities);
// Populate error details for non-existent entities
entitiesWithHouseholdTypeChange.forEach(entity -> {
Error error = Error.builder().errorMessage("Household Type change").errorCode("HOUSEHOLD_TYPE_CHANGE")
.type(Error.ErrorType.NON_RECOVERABLE)
.exception(new CustomException("HOUSEHOLD_TYPE_CHANGE", "Household Type change")).build();
populateErrorDetails(entity, error, errorDetailsMap);
});
}

return errorDetailsMap;
}


private List<Household> changeInHouseholdType(Map<String, Household> eMap,
List<Household> existingEntities) {
List<Household> entitiesWithHouseholdTypeChange = new ArrayList<>();

for (Household existingEntity : existingEntities) {
if (eMap.containsKey(existingEntity.getId())) {
if (!existingEntity.getHouseholdType().equals(eMap.get(existingEntity.getId()).getHouseholdType())) {
entitiesWithHouseholdTypeChange.add(eMap.get(existingEntity.getId()));
}
}
}
return entitiesWithHouseholdTypeChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ egov.individual.search.url=/individual/v1/_search
egov.boundary.host=http://localhost:8081
egov.boundary.search.url=/boundary-service/boundary/_search
egov.boundary.hierarchy=HCM-Moz-Hierarchy

#Community Household Type
household.type.same.validation=true
household.type.community.creator.role=COMMUNITY_CREATOR
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE HOUSEHOLD ADD COLUMN IF NOT EXISTS householdType character varying(64);
UPDATE HOUSEHOLD SET householdType = 'FAMILY' WHERE householdType IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ serviceMaps:
fromTopic: save-household-topic
isTransaction: true
queryMaps:
- query: INSERT INTO HOUSEHOLD(id, tenantId, clientReferenceId, numberOfMembers, addressId, additionalDetails, createdBy, lastModifiedBy, createdTime, lastModifiedTime, rowVersion, isDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
- query: INSERT INTO HOUSEHOLD(id, tenantId, clientReferenceId, householdType, numberOfMembers, addressId, additionalDetails, createdBy, lastModifiedBy, createdTime, lastModifiedTime, rowVersion, isDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
basePath: $.*
jsonMaps:
- jsonPath: $.*.id
- jsonPath: $.*.tenantId
- jsonPath: $.*.clientReferenceId
- jsonPath: $.*.householdType
- jsonPath: $.*.memberCount
- jsonPath: $.*.address.id
- jsonPath: $.*.additionalFields
Expand Down
2 changes: 1 addition & 1 deletion health-services/libraries/health-services-models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.22-SNAPSHOT</version>
<version>1.0.24-dev-snapshot</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down
Loading