Skip to content

Commit

Permalink
Added pojos for project factory and hrms, and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishi-egov committed Sep 19, 2024
1 parent 293f1d8 commit 3188727
Show file tree
Hide file tree
Showing 16 changed files with 650 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public class Configuration {
@Value("${egov.hrms.search.endpoint}")
private String hrmsEndPoint;

//Project Factory
@Value("${egov.project.factory.host}")
private String projectFactoryHost;

@Value("${egov.project.factory.search.endpoint}")
private String projectFactorySearchEndPoint;

//Persister Topic
@Value("${plan.configuration.create.topic}")
private String planConfigCreateTopic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ServiceConstants {

public static final String ERROR_WHILE_FETCHING_FROM_MDMS = "Exception occurred while fetching category lists from mdms: ";

public static final String ERROR_WHILE_FETCHING_FROM_PROJECT_FACTORY = "Exception occurred while fetching campaign details from project factory: ";

public static final String ERROR_WHILE_FETCHING_DATA_FROM_HRMS = "Exception occurred while fetching employee from hrms: ";

public static final String RES_MSG_ID = "uief87324";
Expand Down Expand Up @@ -60,6 +62,12 @@ public class ServiceConstants {
public static final String NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID_CODE = "NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID";
public static final String NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID_MESSAGE = "Invalid or incorrect employee id. No hrms data found for provided employee id.";

public static final String NO_CAMPAIGN_DETAILS_FOUND_FOR_GIVEN_CAMPAIGN_ID_CODE = "NO_CAMPAIGN_DETAILS_FOUND_FOR_GIVEN_CAMPAIGN_ID";
public static final String NO_CAMPAIGN_DETAILS_FOUND_FOR_GIVEN_CAMPAIGN_ID_MESSAGE = "Invalid or incorrect campaign id. No campaign details found for provided campaign id.";

public static final String INVALID_EMPLOYEE_JURISDICTION_CODE = "INVALID_EMPLOYEE_JURISDICTION";
public static final String INVALID_EMPLOYEE_JURISDICTION_MESSAGE = "The provided employee's jurisdiction is invalid";

public static final String SEARCH_CRITERIA_EMPTY_CODE = "SEARCH_CRITERIA_EMPTY";
public static final String SEARCH_CRITERIA_EMPTY_MESSAGE = "Search criteria cannot be empty";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class PlanEmployeeAssignmentEnricher {

/**
* Enriches the PlanEmployeeAssignmentRequest with id and audit details.
*
* @param request The PlanEmployeeAssignmentRequest body to be enriched
*/
public void enrichCreate(PlanEmployeeAssignmentRequest request)
{
public void enrichCreate(PlanEmployeeAssignmentRequest request) {
PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();

// Generate id for Plan employee assignment body
Expand All @@ -29,10 +29,10 @@ public void enrichCreate(PlanEmployeeAssignmentRequest request)

/**
* Enriches the PlanEmployeeAssignmentRequest for updating an existing plan employee assignment with audit details.
*
* @param request The PlanEmployeeAssignmentRequest body to be enriched
*/
public void enrichUpdate(PlanEmployeeAssignmentRequest request)
{
public void enrichUpdate(PlanEmployeeAssignmentRequest request) {
PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();

// Set Audit Details for Plan employee assignment update
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package digit.service.validator;

import digit.util.CampaignUtil;
import digit.util.HrmsUtil;
import digit.util.ServiceUtil;
import digit.web.models.PlanConfiguration;
import digit.web.models.PlanEmployeeAssignment;
import digit.web.models.PlanEmployeeAssignmentRequest;
import digit.web.models.hrms.EmployeeResponse;
import digit.web.models.projectFactory.Boundary;
import digit.web.models.projectFactory.CampaignDetail;
import digit.web.models.projectFactory.CampaignResponse;
import lombok.extern.slf4j.Slf4j;
import org.egov.common.utils.MultiStateInstanceUtil;
import org.egov.tracer.model.CustomException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static digit.config.ServiceConstants.*;

@Slf4j
@Component
public class PlanEmployeeAssignmentValidator {

private MultiStateInstanceUtil centralInstanceUtil;

private HrmsUtil hrmsUtil;

private ServiceUtil serviceUtil;

private CampaignUtil campaignUtil;

public PlanEmployeeAssignmentValidator(MultiStateInstanceUtil centralInstanceUtil, HrmsUtil hrmsUtil, ServiceUtil serviceUtil, CampaignUtil campaignUtil) {
this.centralInstanceUtil = centralInstanceUtil;
this.hrmsUtil = hrmsUtil;
this.serviceUtil = serviceUtil;
this.campaignUtil = campaignUtil;
}


/**
* This method validates the create request for plan employee assignment.
*
* @param request The create request for plan employee assignment
*/
public void validateCreate(PlanEmployeeAssignmentRequest request) {
PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();
String rootTenantId = centralInstanceUtil.getStateLevelTenant(request.getPlanEmployeeAssignment().getTenantId());
List<PlanConfiguration> planConfigurations = serviceUtil.searchPlanConfigId(planEmployeeAssignment.getPlanConfigurationId(), rootTenantId);

// Validate if plan config id exists
validatePlanConfigId(planConfigurations);

// Validate if employee exists against hrms
validateEmployeeAgainstHRMS(request);

// Validate campaign id and employee jurisdiction
validateCampaignDetails(planConfigurations.get(0).getExecutionPlanId(), rootTenantId, request);
}


/**
* This method validates campaign id and employee's jurisdiction against project factory
*
* @param campaignId the campaign id corresponding to the plan config id provided in the request
* @param tenantId the tenant id provided in the request
* @param planEmployeeAssignmentRequest the plan employee assignment request provided
*/
private void validateCampaignDetails(String campaignId, String tenantId, PlanEmployeeAssignmentRequest planEmployeeAssignmentRequest) {

PlanEmployeeAssignment planEmployeeAssignment = planEmployeeAssignmentRequest.getPlanEmployeeAssignment();
CampaignResponse campaignResponse = campaignUtil.fetchCampaignData(planEmployeeAssignmentRequest.getRequestInfo(), campaignId, tenantId);

// Validate if campaign id exists against project factory
validateCampaignId(campaignResponse);

// Validate the provided jurisdiction for employee
validateEmployeeJurisdiction(campaignResponse.getCampaignDetails().get(0), planEmployeeAssignment);
}

/**
* This method validates if employee's jurisdiction exist in campaign details
*
* @param campaignDetail the campaign details for the corresponding campaign id
* @param planEmployeeAssignment the plan employee assignment provided in request
*/
private void validateEmployeeJurisdiction(CampaignDetail campaignDetail, PlanEmployeeAssignment planEmployeeAssignment) {

// Collect all boundary code for the campaign
Set<String> boundaryCode = campaignDetail.getBoundaries().stream()
.map(Boundary::getCode)
.collect(Collectors.toSet());

planEmployeeAssignment.getJurisdiction().stream()
.forEach(jurisdiction -> {
if (!boundaryCode.contains(jurisdiction)) {
throw new CustomException(INVALID_EMPLOYEE_JURISDICTION_CODE, INVALID_EMPLOYEE_JURISDICTION_MESSAGE);
}
});

}

/**
* This method validates if the campaign id provided in the request exists
*
* @param campaignResponse The campaign details response from project factory
*/
private void validateCampaignId(CampaignResponse campaignResponse) {

if (CollectionUtils.isEmpty(campaignResponse.getCampaignDetails())) {
throw new CustomException(NO_CAMPAIGN_DETAILS_FOUND_FOR_GIVEN_CAMPAIGN_ID_CODE, NO_CAMPAIGN_DETAILS_FOUND_FOR_GIVEN_CAMPAIGN_ID_MESSAGE);
}
}

/**
* This method validates if the employee provided in plan employee assignment request exist in hrms
*
* @param request The request for plan employee assignment
*/
private void validateEmployeeAgainstHRMS(PlanEmployeeAssignmentRequest request) {

PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();
EmployeeResponse employeeResponse = hrmsUtil.fetchHrmsData(request.getRequestInfo(), planEmployeeAssignment.getEmployeeId(), planEmployeeAssignment.getTenantId());

if (CollectionUtils.isEmpty(employeeResponse.getEmployees())) {
log.error(NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID_MESSAGE + " - " + planEmployeeAssignment.getEmployeeId());
throw new CustomException(NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID_CODE, NO_HRMS_DATA_FOUND_FOR_GIVEN_EMPLOYEE_ID_MESSAGE);
}
}


/**
* This method validates if the plan configuration id provided in the request exists
*
* @param planConfigurations The list of plan configuration for the provided plan config id
*/
private void validatePlanConfigId(List<PlanConfiguration> planConfigurations) {
if (CollectionUtils.isEmpty(planConfigurations)) {
throw new CustomException(INVALID_PLAN_CONFIG_ID_CODE, INVALID_PLAN_CONFIG_ID_MESSAGE);
}
}


/**
* This method validates the update request for plan employee assignment.
*
* @param request The update request for plan employee assignment.
*/
public void validateUpdate(PlanEmployeeAssignmentRequest request) {
PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();
String rootTenantId = centralInstanceUtil.getStateLevelTenant(request.getPlanEmployeeAssignment().getTenantId());
List<PlanConfiguration> planConfigurations = serviceUtil.searchPlanConfigId(planEmployeeAssignment.getPlanConfigurationId(), rootTenantId);

// Validate if Plan employee assignment exists
validatePlanEmployeeAssignment(planEmployeeAssignment);

// Validate if plan config id exists
validatePlanConfigId(planConfigurations);

// Validate if employee exists against hrms
validateEmployeeAgainstHRMS(request);

// Validate campaign id and employee jurisdiction
validateCampaignDetails(planConfigurations.get(0).getExecutionPlanId(), rootTenantId, request);

}

/**
* This method validates if the plan employee assignment id provided in the update request exists
*
* @param planEmployeeAssignment The plan employee assignment details from the request
*/
private void validatePlanEmployeeAssignment(PlanEmployeeAssignment planEmployeeAssignment) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package digit.util;

import digit.config.Configuration;
import digit.web.models.projectFactory.*;
import lombok.extern.slf4j.Slf4j;
import org.egov.common.contract.request.RequestInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

import static digit.config.ServiceConstants.*;

@Slf4j
@Component
public class CampaignUtil {

private RestTemplate restTemplate;

private Configuration configs;

public CampaignUtil(RestTemplate restTemplate, Configuration configs) {
this.restTemplate = restTemplate;
this.configs = configs;
}

public CampaignResponse fetchCampaignData(RequestInfo requestInfo, String campaignId, String tenantId) {
StringBuilder uri = new StringBuilder();
uri = uri.append(configs.getProjectFactoryHost()).append(configs.getProjectFactorySearchEndPoint());

CampaignSearchReq campaignSearchReq = getSearchReq(requestInfo, campaignId, tenantId);
CampaignResponse campaignResponse = new CampaignResponse();
try {
campaignResponse = restTemplate.postForObject(uri.toString(), campaignSearchReq, CampaignResponse.class);
} catch (Exception e) {
log.error(ERROR_WHILE_FETCHING_FROM_PROJECT_FACTORY, e);
}

return campaignResponse;
}

private CampaignSearchReq getSearchReq(RequestInfo requestInfo, String campaignId, String tenantId) {
Pagination pagination = Pagination.builder().limit(configs.getDefaultLimit()).offset(configs.getDefaultOffset()).build();
CampaignSearchCriteria searchCriteria = CampaignSearchCriteria.builder()
.ids(Collections.singletonList(campaignId))
.tenantId(tenantId)
.pagination(pagination)
.build();

return CampaignSearchReq.builder()
.requestInfo(requestInfo)
.campaignSearchCriteria(searchCriteria)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ public class HrmsUtil {

private Configuration configs;

private RequestInfoWrapper requestInfoWrapper;

public HrmsUtil(RestTemplate restTemplate, Configuration configs, RequestInfoWrapper requestInfoWrapper)
{
public HrmsUtil(RestTemplate restTemplate, Configuration configs) {
this.restTemplate = restTemplate;
this.configs = configs;
this.requestInfoWrapper = requestInfoWrapper;
}

public List<Employee> fetchHrmsData(RequestInfo requestInfo, String employeeId, String tenantId) {
/**
* This method fetches data from HRMS service for provided employeeId
*
* @param employeeId employee id provided in the request
* @param requestInfo request info from the request
* @param tenantId tenant id from the request
*/
public EmployeeResponse fetchHrmsData(RequestInfo requestInfo, String employeeId, String tenantId) {

StringBuilder uri = new StringBuilder();
uri.append(configs.getHrmsHost()).append(configs.getHrmsEndPoint()).append("?limit={limit}&tenantId={tenantId}&offset={offset}&ids={employeeId}");
Expand All @@ -46,12 +50,12 @@ public List<Employee> fetchHrmsData(RequestInfo requestInfo, String employeeId,
EmployeeResponse employeeResponse = new EmployeeResponse();

try {
employeeResponse = restTemplate.postForObject(uri.toString(), requestInfoWrapper, EmployeeResponse.class, uriParameters);
employeeResponse = restTemplate.postForObject(uri.toString(), requestInfoWrapper, EmployeeResponse.class, uriParameters);
} catch (Exception e) {
log.error(ERROR_WHILE_FETCHING_DATA_FROM_HRMS, e);
}

return employeeResponse.getEmployees();
return employeeResponse;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package digit.web.models.projectFactory;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Boundary
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Boundary {

@JsonProperty("code")
private String code;

@JsonProperty("type")
private String type;

@JsonProperty("isRoot")
private Boolean isRoot;

@JsonProperty("includeAllChildren")
private Boolean includeAllChildren;

@JsonProperty("parent")
private String parent;
}
Loading

0 comments on commit 3188727

Please sign in to comment.