-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pojos for project factory and hrms, and validations
- Loading branch information
1 parent
293f1d8
commit 3188727
Showing
16 changed files
with
650 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
...s/plan-service/src/main/java/digit/service/validator/PlanEmployeeAssignmentValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
health-services/plan-service/src/main/java/digit/util/CampaignUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
health-services/plan-service/src/main/java/digit/web/models/projectFactory/Boundary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.