Skip to content

Commit

Permalink
code refactor and code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kanishq-egov committed Oct 19, 2023
1 parent 088ae72 commit 0a20eb6
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ReferralBulkRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("Referrals")
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ReferralBulkResponse {
@JsonProperty("ResponseInfo")
@NotNull
@Valid
private ResponseInfo responseInfo = null;
private ResponseInfo responseInfo;

@JsonProperty("Referrals")
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class ReferralRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("Referral")
@NotNull
@Valid
private Referral referral = null;
private Referral referral;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class ReferralResponse {
@JsonProperty("ResponseInfo")
@NotNull
@Valid
private ResponseInfo responseInfo = null;
private ResponseInfo responseInfo;

@JsonProperty("Referral")
@NotNull
@Valid
private Referral referral = null;
private Referral referral;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
@Builder
public class ReferralSearch {
@JsonProperty("id")
private List<String> id = null;
private List<String> id;

@JsonProperty("clientReferenceId")
private List<String> clientReferenceId = null;
private List<String> clientReferenceId;

@JsonProperty("projectBeneficiaryId")
private List<String> projectBeneficiaryId = null;
private List<String> projectBeneficiaryId;

@JsonProperty("projectBeneficiaryClientReferenceId")
private List<String> projectBeneficiaryClientReferenceId = null;
private List<String> projectBeneficiaryClientReferenceId;

@JsonProperty("sideEffectId")
private List<String> sideEffectId = null;
private List<String> sideEffectId;

@JsonProperty("sideEffectClientReferenceId")
private List<String> sideEffectClientReferenceId = null;
private List<String> sideEffectClientReferenceId;

@JsonProperty("referrerId")
private List<String> referrerId = null;
private List<String> referrerId;

@JsonProperty("recipientId")
private List<String> recipientId = null;
private List<String> recipientId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class ReferralSearchRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("Referral")
@Valid
private ReferralSearch referral = null;
private ReferralSearch referral;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SideEffectBulkRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("SideEffects")
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SideEffectBulkResponse {
@JsonProperty("ResponseInfo")
@NotNull
@Valid
private ResponseInfo responseInfo = null;
private ResponseInfo responseInfo;

@JsonProperty("SideEffects")
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class SideEffectRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("SideEffect")
@NotNull
@Valid
private SideEffect sideEffect = null;
private SideEffect sideEffect;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class SideEffectResponse {
@JsonProperty("ResponseInfo")
@NotNull
@Valid
private ResponseInfo responseInfo = null;
private ResponseInfo responseInfo;

@JsonProperty("SideEffect")
@NotNull
@Valid
private SideEffect sideEffect = null;
private SideEffect sideEffect;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
@Builder
public class SideEffectSearch {
@JsonProperty("id")
private List<String> id = null;
private List<String> id;

@JsonProperty("clientReferenceId")
private List<String> clientReferenceId = null;
private List<String> clientReferenceId;

@JsonProperty("taskId")
private String taskId = null;
private String taskId;

@JsonProperty("taskClientReferenceId")
private String taskClientReferenceId = null;
private String taskClientReferenceId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class SideEffectSearchRequest {
@JsonProperty("RequestInfo")
@NotNull
@Valid
private RequestInfo requestInfo = null;
private RequestInfo requestInfo;

@JsonProperty("SideEffect")
@Valid
private SideEffectSearch sideEffect = null;
private SideEffectSearch sideEffect;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.egov.referralmanagement.validator.RmNullIdValidator;
import org.egov.referralmanagement.validator.RmProjectBeneficiaryIdValidator;
import org.egov.referralmanagement.validator.RmRecipientIdValidator;
import org.egov.referralmanagement.validator.RmReferrerIdValidator;
import org.egov.referralmanagement.validator.RmSideEffectIdValidator;
import org.egov.referralmanagement.validator.RmUniqueEntityValidator;
import org.egov.tracer.model.CustomException;
Expand Down Expand Up @@ -56,11 +57,13 @@ public class ReferralManagementService {

private final Predicate<Validator<ReferralBulkRequest, Referral>> isApplicableForCreate = validator ->
validator.getClass().equals(RmProjectBeneficiaryIdValidator.class)
|| validator.getClass().equals(RmReferrerIdValidator.class)
|| validator.getClass().equals(RmRecipientIdValidator.class)
|| validator.getClass().equals(RmSideEffectIdValidator.class);

private final Predicate<Validator<ReferralBulkRequest, Referral>> isApplicableForUpdate = validator ->
validator.getClass().equals(RmProjectBeneficiaryIdValidator.class)
|| validator.getClass().equals(RmReferrerIdValidator.class)
|| validator.getClass().equals(RmRecipientIdValidator.class)
|| validator.getClass().equals(RmSideEffectIdValidator.class)
|| validator.getClass().equals(RmNullIdValidator.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
import java.util.List;
import java.util.stream.Collectors;

/**
* A Validation helper Util
*/
public class ValidatorUtil {

/**
* validate and remove valid identifiers from invalidStaffIds
* @param requestInfo
* @param userService
* @param staffIds
* @param invalidStaffIds
*/
public static void validateAndEnrichStaffIds(RequestInfo requestInfo, UserService userService,
List<String> staffIds, List<String> invalidStaffIds) {
if (!CollectionUtils.isEmpty(staffIds)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import org.egov.common.models.project.ProjectStaffSearchRequest;
import org.egov.common.models.referralmanagement.Referral;
import org.egov.common.models.referralmanagement.ReferralBulkRequest;
import org.egov.common.service.UserService;
import org.egov.common.validator.Validator;
import org.egov.referralmanagement.config.ReferralManagementConfiguration;
import org.egov.referralmanagement.service.FacilityService;
import org.egov.referralmanagement.util.ValidatorUtil;
import org.egov.tracer.model.CustomException;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
Expand All @@ -25,21 +28,22 @@
import static org.egov.common.utils.CommonUtils.notHavingErrors;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.ValidatorUtils.getErrorForNonExistentEntity;
import static org.egov.referralmanagement.Constants.STAFF;

/**
*
* Validate the referrer using user service
*/
@Component
@Order(value = 3)
@Slf4j
public class RmReferrerIdValidator implements Validator<ReferralBulkRequest, Referral> {

private final ServiceRequestClient serviceRequestClient;
private final ReferralManagementConfiguration referralManagementConfiguration;
private final FacilityService facilityService;
private final UserService userService;

public RmReferrerIdValidator(ServiceRequestClient serviceRequestClient, ReferralManagementConfiguration referralManagementConfiguration) {
this.serviceRequestClient = serviceRequestClient;
this.referralManagementConfiguration = referralManagementConfiguration;
public RmReferrerIdValidator(FacilityService facilityService, UserService userService) {
this.facilityService = facilityService;
this.userService = userService;
}

@Override
Expand All @@ -49,30 +53,18 @@ public Map<Referral, List<Error>> validate(ReferralBulkRequest request) {
List<Referral> entities = request.getReferrals();
Map<String, List<Referral>> tenantIdReferralMap = entities.stream().collect(Collectors.groupingBy(Referral::getTenantId));
tenantIdReferralMap.forEach((tenantId, referralList) -> {
List<ProjectStaff> existingProjectStaffList = new ArrayList<>();
final List<String> projectStaffUuidList = new ArrayList<>();
referralList.forEach(referral -> addIgnoreNull(projectStaffUuidList, referral.getReferrerId()));
ProjectStaffSearch projectStaffSearch = ProjectStaffSearch.builder()
.id(projectStaffUuidList.isEmpty() ? null : projectStaffUuidList)
.build();
List<String> invalidStaffIds = new ArrayList<>(projectStaffUuidList);
try {
ProjectStaffBulkResponse projectStaffBulkResponse = serviceRequestClient.fetchResult(
new StringBuilder(referralManagementConfiguration.getProjectHost()
+ referralManagementConfiguration.getProjectStaffSearchUrl()
+"?limit=" + entities.size()
+ "&offset=0&tenantId=" + tenantId),
ProjectStaffSearchRequest.builder().requestInfo(request.getRequestInfo()).projectStaff(projectStaffSearch).build(),
ProjectStaffBulkResponse.class
);
existingProjectStaffList = projectStaffBulkResponse.getProjectStaff();
ValidatorUtil.validateAndEnrichStaffIds(request.getRequestInfo(), userService, projectStaffUuidList, invalidStaffIds);
} catch (Exception e) {
throw new CustomException("Project Staff failed to fetch", "Exception : "+e.getMessage());
}
final List<String> existingProjectStaffUuids = new ArrayList<>();
existingProjectStaffList.forEach(projectStaff -> existingProjectStaffUuids.add(projectStaff.getId()));
List<Referral> invalidEntities = entities.stream().filter(notHavingErrors()).filter(entity ->
!existingProjectStaffUuids.contains(entity.getReferrerId())
invalidStaffIds.contains(entity.getReferrerId())
).collect(Collectors.toList());

invalidEntities.forEach(referral -> {
Error error = getErrorForNonExistentEntity();
populateErrorDetails(referral, error, errorDetailsMap);
Expand Down

0 comments on commit 0a20eb6

Please sign in to comment.