Skip to content

Commit

Permalink
HLM-6196: updated @component in validators for client reference id va…
Browse files Browse the repository at this point in the history
…lidation
  • Loading branch information
kanishq-egov committed Jun 17, 2024
1 parent e9c0367 commit 6e915a0
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.household.HouseholdMember;
import org.egov.common.models.household.HouseholdMemberBulkRequest;
import org.egov.common.models.household.HouseholdMemberSearch;
import org.egov.common.validator.Validator;
import org.egov.household.repository.HouseholdMemberRepository;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -24,6 +27,9 @@
*
* @author kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class HmExistentEntityValidator implements Validator<HouseholdMemberBulkRequest, HouseholdMember> {

private final HouseholdMemberRepository householdMemberRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.household.Household;
import org.egov.common.models.household.HouseholdMember;
import org.egov.common.models.household.HouseholdMemberBulkRequest;
import org.egov.common.models.household.HouseholdMemberSearch;
import org.egov.common.models.household.HouseholdSearch;
import org.egov.common.validator.Validator;
import org.egov.household.repository.HouseholdMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static org.egov.common.utils.CommonUtils.checkNonExistentEntities;
import static org.egov.common.utils.CommonUtils.getIdFieldName;
import static org.egov.common.utils.CommonUtils.getIdToObjMap;
import static org.egov.common.utils.CommonUtils.getMethod;
import static org.egov.common.utils.CommonUtils.getObjClass;
Expand Down Expand Up @@ -73,7 +70,6 @@ public Map<HouseholdMember, List<Error>> validate(HouseholdMemberBulkRequest req
// Create a map of household members with their IDs as keys
Map<String, HouseholdMember> iMap = getIdToObjMap(householdMembers
.stream().filter(notHavingErrors()).collect(Collectors.toList()), idMethod);
// Check if the map is not empty

// Lists to store IDs and client reference IDs
List<String> idList = new ArrayList<>();
Expand All @@ -84,9 +80,9 @@ public Map<HouseholdMember, List<Error>> validate(HouseholdMemberBulkRequest req
clientReferenceIdList.add(householdMember.getClientReferenceId());
});

// Check if the map is not empty
if (!iMap.isEmpty()) {
// Extract IDs from the map
List<String> householdMemberIds = new ArrayList<>(iMap.keySet());

// Create a search object for querying existing entities
HouseholdMemberSearch householdMemberSearch = HouseholdMemberSearch.builder()
.clientReferenceId(clientReferenceIdList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -23,6 +26,9 @@
* This validator checks if the provided household entities already exist in the database based on their client reference IDs.
* @author kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class HExistentEntityValidator implements Validator<HouseholdBulkRequest, Household> {

private final HouseholdRepository householdRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.individual.Individual;
import org.egov.common.models.individual.IndividualBulkRequest;
import org.egov.common.models.individual.IndividualSearch;
import org.egov.common.validator.Validator;
import org.egov.individual.repository.IndividualRepository;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -23,6 +26,9 @@
* This validator checks if the provided individual entities already exist in the database based on their client reference IDs.
* @author kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class IExistentEntityValidator implements Validator<IndividualBulkRequest, Individual> {

private final IndividualRepository individualRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public Map<Individual, List<Error>> validate(IndividualBulkRequest request) {
});
// Check if the entity map is not empty
if (!eMap.isEmpty()) {
// Extract entity IDs
List<String> entityIds = new ArrayList<>(eMap.keySet());

// Create a search object for querying existing entities
IndividualSearch individualSearch = IndividualSearch.builder()
.id(idList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.egov.common.models.Error;
import org.egov.common.models.individual.Individual;
import org.egov.common.models.individual.IndividualBulkRequest;
import org.egov.common.models.individual.IndividualSearch;
import org.egov.common.validator.Validator;
import org.egov.individual.repository.IndividualRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -56,10 +57,30 @@ public Map<Individual, List<Error>> validate(IndividualBulkRequest request) {
Method idMethod = getMethod(GET_ID, objClass);
Map<String, Individual> iMap = getIdToObjMap(individuals
.stream().filter(notHavingErrors()).collect(Collectors.toList()), idMethod);
// 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 individual entities
individuals.forEach(individual -> {
idList.add(individual.getId());
clientReferenceIdList.add(individual.getClientReferenceId());
});
if (!iMap.isEmpty()) {
List<String> individualIds = new ArrayList<>(iMap.keySet());
List<Individual> existingIndividuals = individualRepository.findById(individualIds,
getIdFieldName(idMethod), false).getResponse();
// Create a search object for querying existing entities
IndividualSearch individualSearch = IndividualSearch.builder()
.id(idList)
.clientReferenceId(clientReferenceIdList)
.build();

List<Individual> existingIndividuals;
try {
// Query the repository to find existing entities
existingIndividuals = individualRepository.find(individualSearch, individuals.size(), 0,
individuals.get(0).getTenantId(), null, false).getResponse();
} catch (Exception e) {
// Handle query builder exception
throw new RuntimeException(e);
}
List<Individual> nonExistentIndividuals = checkNonExistentEntities(iMap,
existingIndividuals, idMethod);
nonExistentIndividuals.forEach(individual -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.project.BeneficiaryBulkRequest;
import org.egov.common.models.project.ProjectBeneficiary;
import org.egov.common.models.project.ProjectBeneficiarySearch;
import org.egov.common.validator.Validator;
import org.egov.project.repository.ProjectBeneficiaryRepository;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -24,6 +27,9 @@
*
* @author kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class PbExistentEntityValidator implements Validator<BeneficiaryBulkRequest, ProjectBeneficiary> {

private final ProjectBeneficiaryRepository projectBeneficiaryRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package org.egov.project.validator.beneficiary;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.project.BeneficiaryBulkRequest;
import org.egov.common.models.project.ProjectBeneficiary;
import org.egov.common.models.project.ProjectBeneficiarySearch;
import org.egov.common.validator.Validator;
import org.egov.project.repository.ProjectBeneficiaryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
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.checkNonExistentEntities;
import static org.egov.common.utils.CommonUtils.getIdFieldName;
import static org.egov.common.utils.CommonUtils.getIdToObjMap;
import static org.egov.common.utils.CommonUtils.getMethod;
import static org.egov.common.utils.CommonUtils.getObjClass;
Expand Down Expand Up @@ -49,10 +49,30 @@ public Map<ProjectBeneficiary, List<Error>> validate(BeneficiaryBulkRequest requ
Method idMethod = getMethod(GET_ID, objClass);
Map<String, ProjectBeneficiary> iMap = getIdToObjMap(projectBeneficiaries
.stream().filter(notHavingErrors()).collect(Collectors.toList()), idMethod);
// 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 Project Beneficiary entities
projectBeneficiaries.forEach(entity -> {
idList.add(entity.getId());
clientReferenceIdList.add(entity.getClientReferenceId());
});
if (!iMap.isEmpty()) {
List<String> beneficiaryIds = new ArrayList<>(iMap.keySet());
List<ProjectBeneficiary> existingProjectBeneficiaries = projectBeneficiaryRepository
.findById(beneficiaryIds, false, getIdFieldName(idMethod));
ProjectBeneficiarySearch projectBeneficiarySearch = ProjectBeneficiarySearch.builder()
.clientReferenceId(clientReferenceIdList)
.id(idList)
.build();

List<ProjectBeneficiary> existingProjectBeneficiaries;
try {
// Query the repository to find existing entities
existingProjectBeneficiaries = projectBeneficiaryRepository.find(projectBeneficiarySearch, projectBeneficiaries.size(), 0,
projectBeneficiaries.get(0).getTenantId(), null, false).getResponse();
} catch (Exception e) {
// Handle query builder exception
throw new RuntimeException(e);
}

List<ProjectBeneficiary> nonExistentIndividuals = checkNonExistentEntities(iMap,
existingProjectBeneficiaries, idMethod);
nonExistentIndividuals.forEach(projectBeneficiary -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.project.Task;
import org.egov.common.models.project.TaskBulkRequest;
import org.egov.common.models.project.TaskSearch;
import org.egov.common.validator.Validator;
import org.egov.project.repository.ProjectTaskRepository;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -24,6 +27,9 @@
*
* @author: kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class PtExistentEntityValidator implements Validator<TaskBulkRequest, Task> {

private final ProjectTaskRepository projectTaskRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package org.egov.project.validator.task;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.project.Task;
import org.egov.common.models.project.TaskBulkRequest;
import org.egov.common.models.project.TaskSearch;
import org.egov.common.validator.Validator;
import org.egov.project.repository.ProjectTaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.egov.common.utils.CommonUtils.checkNonExistentEntities;
import static org.egov.common.utils.CommonUtils.getIdFieldName;
import static org.egov.common.utils.CommonUtils.getIdMethod;
import static org.egov.common.utils.CommonUtils.getIdToObjMap;
import static org.egov.common.utils.CommonUtils.getMethod;
Expand Down Expand Up @@ -55,10 +55,29 @@ public Map<Task, List<Error>> validate(TaskBulkRequest request) {
Method idMethod = getMethod(GET_ID, objClass);
Map<String, Task> eMap = getIdToObjMap(entities
.stream().filter(notHavingErrors()).collect(Collectors.toList()), idMethod);
// 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 Project Task entities
entities.forEach(entity -> {
idList.add(entity.getId());
clientReferenceIdList.add(entity.getClientReferenceId());
});
if (!eMap.isEmpty()) {
List<String> entityIds = new ArrayList<>(eMap.keySet());
List<Task> existingEntities = projectTaskRepository.findById(entityIds,
getIdFieldName(idMethod), false).getResponse();
TaskSearch taskSearch = TaskSearch.builder()
.clientReferenceId(clientReferenceIdList)
.id(idList)
.build();

List<Task> existingEntities;
try {
// Query the repository to find existing entities
existingEntities = projectTaskRepository.find(taskSearch, entities.size(), 0,
entities.get(0).getTenantId(), null, false).getResponse();
} catch (Exception e) {
// Handle query builder exception
throw new RuntimeException(e);
}
List<Task> nonExistentEntities = checkNonExistentEntities(eMap,
existingEntities, idMethod);
nonExistentEntities.forEach(task -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.egov.common.models.Error;
import org.egov.common.models.referralmanagement.Referral;
import org.egov.common.models.referralmanagement.ReferralBulkRequest;
import org.egov.common.models.referralmanagement.ReferralSearch;
import org.egov.common.validator.Validator;
import org.egov.referralmanagement.repository.ReferralRepository;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import static org.egov.common.utils.CommonUtils.getIdFieldName;
Expand All @@ -23,6 +26,9 @@
* This validator checks if the provided referral entities already exist in the database based on their client reference IDs.
* @author kanishq-egov
*/
@Component
@Order(value = 1)
@Slf4j
public class RmExistentEntityValidator implements Validator<ReferralBulkRequest, Referral> {

private final ReferralRepository referralRepository;
Expand Down
Loading

0 comments on commit 6e915a0

Please sign in to comment.