Skip to content

Commit

Permalink
Added controller, service layer, validator and enrichment layer for p…
Browse files Browse the repository at this point in the history
…lan employee assignment
  • Loading branch information
tanishi-egov committed Sep 18, 2024
1 parent bb72eeb commit 293f1d8
Show file tree
Hide file tree
Showing 22 changed files with 938 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class Configuration {
@Value("${egov.mdms.search.endpoint}")
private String mdmsEndPoint;

//HRMS
@Value("${egov.hrms.host}")
private String hrmsHost;

@Value("${egov.hrms.search.endpoint}")
private String hrmsEndPoint;

//Persister Topic
@Value("${plan.configuration.create.topic}")
Expand All @@ -36,6 +42,12 @@ public class Configuration {
@Value("${plan.configuration.update.topic}")
private String planConfigUpdateTopic;

@Value("${plan.employee.assignment.create.topic}")
private String planEmployeeAssignmentCreateTopic;

@Value("${plan.employee.assignment.update.topic}")
private String planEmployeeAssignmentUpdateTopic;

@Value("${plan.create.topic}")
private String planCreateTopic;

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_DATA_FROM_HRMS = "Exception occurred while fetching employee from hrms: ";

public static final String RES_MSG_ID = "uief87324";
public static final String SUCCESSFUL = "successful";
public static final String FAILED = "failed";
Expand Down Expand Up @@ -55,6 +57,9 @@ public class ServiceConstants {
public static final String NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_CODE = "NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT";
public static final String NO_MDMS_DATA_FOUND_FOR_GIVEN_TENANT_MESSAGE = "Invalid or incorrect TenantId. No mdms data found for provided Tenant.";

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 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
@@ -0,0 +1,10 @@
package digit.repository;

import digit.web.models.PlanEmployeeAssignmentRequest;

public interface PlanEmployeeAssignmentRepository {

public void create(PlanEmployeeAssignmentRequest planEmployeeAssignmentRequest);

public void update(PlanEmployeeAssignmentRequest planEmployeeAssignmentRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package digit.repository.impl;

import digit.config.Configuration;
import digit.kafka.Producer;
import digit.repository.PlanEmployeeAssignmentRepository;
import digit.web.models.PlanEmployeeAssignmentRequest;
import org.springframework.stereotype.Repository;

@Repository
public class PlanEmployeeAssignmentImpl implements PlanEmployeeAssignmentRepository {

private Producer producer;

private Configuration config;

public PlanEmployeeAssignmentImpl(Producer producer, Configuration config)
{
this.producer = producer;
this.config = config;
}

/**
* Pushes a new plan employee assignment to persister kafka topic.
* @param planEmployeeAssignmentRequest The request containing the plan employee assignment details.
*/
@Override
public void create(PlanEmployeeAssignmentRequest planEmployeeAssignmentRequest) {
producer.push(config.getPlanEmployeeAssignmentCreateTopic(), planEmployeeAssignmentRequest);
}

/**
* Pushes an updated existing plan employee assignment to persister kafka topic.
* @param planEmployeeAssignmentRequest The request containing the updated plan employee assignment details.
*/
@Override
public void update(PlanEmployeeAssignmentRequest planEmployeeAssignmentRequest) {
producer.push(config.getPlanEmployeeAssignmentUpdateTopic(), planEmployeeAssignmentRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package digit.service;

import digit.config.Configuration;
import digit.kafka.Producer;
import digit.repository.PlanEmployeeAssignmentRepository;
import digit.service.enrichment.PlanEmployeeAssignmentEnricher;
import digit.service.validator.PlanEmployeeAssignmentValidator;
import digit.util.ResponseInfoFactory;
import digit.web.models.*;
import org.egov.common.utils.ResponseInfoUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
public class PlanEmployeeService {

Producer producer;

Configuration config;

ResponseInfoFactory responseInfoFactory;

PlanEmployeeAssignmentRepository repository;

PlanEmployeeAssignmentEnricher enricher;

PlanEmployeeAssignmentValidator validator;

public PlanEmployeeService(Producer producer, Configuration config, ResponseInfoFactory responseInfoFactory, PlanEmployeeAssignmentRepository repository, PlanEmployeeAssignmentEnricher enricher, PlanEmployeeAssignmentValidator validator)
{
this.producer = producer;
this.config = config;
this.responseInfoFactory = responseInfoFactory;
this.repository = repository;
this.enricher = enricher;
this.validator = validator;
}

/**
* Creates a new plan employee assignment based on the provided request.
* @param request The request containing the plan employee assignment details.
* @return The response containing the created plan employee assignment.
*/
public PlanEmployeeAssignmentResponse create(PlanEmployeeAssignmentRequest request) {

validator.validateCreate(request);
enricher.enrichCreate(request);
repository.create(request);

return PlanEmployeeAssignmentResponse.builder()
.planEmployeeAssignment(Collections.singletonList(request.getPlanEmployeeAssignment()))
.responseInfo(responseInfoFactory.createResponseInfoFromRequestInfo(request.getRequestInfo(), Boolean.TRUE))
.build();
}

/**
* Searches for plan employee assignment based on the provided search criteria.
* @param request The search request containing the criteria.
* @return A list of plan employee assignments that matches the search criteria.
*/
public PlanEmployeeAssignmentResponse search(PlanEmployeeAssignmentSearchRequest request) {

return PlanEmployeeAssignmentResponse.builder().
responseInfo(responseInfoFactory.createResponseInfoFromRequestInfo(request.getRequestInfo(), Boolean.TRUE))
.build();
}

/**
* Updates an existing plan employee assignment based on the provided request.
* @param request The request containing the updated plan employee assignment details.
* @return The response containing the updated plan employee assignment.
*/

public PlanEmployeeAssignmentResponse update(PlanEmployeeAssignmentRequest request) {

validator.validateUpdate(request);
enricher.enrichUpdate(request);
repository.update(request);

return PlanEmployeeAssignmentResponse.builder()
.responseInfo(ResponseInfoUtil.createResponseInfoFromRequestInfo(request.getRequestInfo(), Boolean.TRUE))
.planEmployeeAssignment(Collections.singletonList(request.getPlanEmployeeAssignment()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import digit.repository.PlanConfigurationRepository;
import digit.repository.PlanRepository;
import digit.util.MdmsUtil;
import digit.util.ServiceUtil;
import digit.web.models.*;
import org.egov.common.utils.MultiStateInstanceUtil;
import org.egov.tracer.model.CustomException;
Expand All @@ -27,11 +28,14 @@ public class PlanValidator {

private MultiStateInstanceUtil centralInstanceUtil;

public PlanValidator(PlanRepository planRepository, PlanConfigurationRepository planConfigurationRepository, MdmsUtil mdmsUtil, MultiStateInstanceUtil centralInstanceUtil) {
private ServiceUtil serviceUtil;

public PlanValidator(PlanRepository planRepository, PlanConfigurationRepository planConfigurationRepository, MdmsUtil mdmsUtil, MultiStateInstanceUtil centralInstanceUtil, ServiceUtil serviceUtil) {
this.planRepository = planRepository;
this.planConfigurationRepository = planConfigurationRepository;
this.mdmsUtil = mdmsUtil;
this.centralInstanceUtil = centralInstanceUtil;
this.serviceUtil = serviceUtil;
}

/**
Expand Down Expand Up @@ -163,10 +167,9 @@ private void validateActivities(PlanRequest request) {
*/
private void validatePlanConfigurationExistence(PlanRequest request) {
// If plan id provided is invalid, throw an exception
if(!ObjectUtils.isEmpty(request.getPlan().getPlanConfigurationId()) && CollectionUtils.isEmpty(planConfigurationRepository.search(PlanConfigurationSearchCriteria.builder()
.id(request.getPlan().getPlanConfigurationId())
.tenantId(request.getPlan().getTenantId())
.build()))) {
if(!ObjectUtils.isEmpty(request.getPlan().getPlanConfigurationId()) &&
CollectionUtils.isEmpty(serviceUtil.searchPlanConfigId(request.getPlan().getPlanConfigurationId(), request.getPlan().getTenantId())))
{
throw new CustomException(INVALID_PLAN_CONFIG_ID_CODE, INVALID_PLAN_CONFIG_ID_MESSAGE);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package digit.service.enrichment;

import digit.web.models.PlanEmployeeAssignment;
import digit.web.models.PlanEmployeeAssignmentRequest;
import org.egov.common.utils.UUIDEnrichmentUtil;
import org.springframework.stereotype.Component;

import static org.egov.common.utils.AuditDetailsEnrichmentUtil.prepareAuditDetails;

@Component
public class PlanEmployeeAssignmentEnricher {

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

// Generate id for Plan employee assignment body
UUIDEnrichmentUtil.enrichRandomUuid(planEmployeeAssignment, "id");

// Set Audit Details for Plan employee assignment
planEmployeeAssignment.setAuditDetails(prepareAuditDetails(planEmployeeAssignment.getAuditDetails(),
request.getRequestInfo(),
Boolean.TRUE));
}

/**
* 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)
{
PlanEmployeeAssignment planEmployeeAssignment = request.getPlanEmployeeAssignment();

// Set Audit Details for Plan employee assignment update
planEmployeeAssignment.setAuditDetails(prepareAuditDetails(planEmployeeAssignment.getAuditDetails(),
request.getRequestInfo(),
Boolean.FALSE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package digit.util;


import digit.config.Configuration;
import digit.web.models.hrms.*;
import digit.web.models.RequestInfoWrapper;
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.*;

import static digit.config.ServiceConstants.*;

@Slf4j
@Component
public class HrmsUtil {

private RestTemplate restTemplate;

private Configuration configs;

private RequestInfoWrapper requestInfoWrapper;

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

public List<Employee> 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}");

Map<String, String> uriParameters = new HashMap<>();
uriParameters.put("limit", configs.getDefaultLimit().toString());
uriParameters.put("tenantId", tenantId);
uriParameters.put("offset", configs.getDefaultOffset().toString());
uriParameters.put("employeeId", employeeId);

RequestInfoWrapper requestInfoWrapper = RequestInfoWrapper.builder().requestInfo(requestInfo).build();
EmployeeResponse employeeResponse = new EmployeeResponse();

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

return employeeResponse.getEmployees();
}


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

import digit.repository.PlanConfigurationRepository;
import digit.web.models.PlanConfiguration;
import digit.web.models.PlanConfigurationSearchCriteria;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class ServiceUtil {

private PlanConfigurationRepository planConfigurationRepository;

public ServiceUtil(PlanConfigurationRepository planConfigurationRepository)
{
this.planConfigurationRepository = planConfigurationRepository;
}

/**
* Validates the given input string against the provided regex pattern.
*
Expand All @@ -20,4 +31,20 @@ public Boolean validateStringAgainstRegex(String patternString, String inputStri
Matcher matcher = pattern.matcher(inputString);
return matcher.matches();
}

/**
* Searches the plan config based on the plan config id provided
* @param planConfigId the plan config id to validate
* @param tenantId the tenant id of the plan config
* @return list of planConfiguration for the provided plan config id
*/
public List<PlanConfiguration> searchPlanConfigId(String planConfigId, String tenantId)
{
List<PlanConfiguration> planConfigurations = planConfigurationRepository.search(PlanConfigurationSearchCriteria.builder()
.id(planConfigId)
.tenantId(tenantId)
.build());

return planConfigurations;
}
}
Loading

0 comments on commit 293f1d8

Please sign in to comment.