io.mosip.pms.partner.manager.service.impl.PartnerManagementServiceImpl Maven / Gradle / Ivy
The newest version!
package io.mosip.pms.partner.manager.service.impl;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.transaction.Transactional;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.kernel.core.logger.spi.Logger;
import io.mosip.pms.common.constant.ApiAccessibleExceptionConstant;
import io.mosip.pms.common.constant.ConfigKeyConstants;
import io.mosip.pms.common.constant.EventType;
import io.mosip.pms.common.dto.APIKeyDataPublishDto;
import io.mosip.pms.common.dto.PartnerDataPublishDto;
import io.mosip.pms.common.dto.PolicyPublishDto;
import io.mosip.pms.common.dto.Type;
import io.mosip.pms.common.entity.AuthPolicy;
import io.mosip.pms.common.entity.BiometricExtractorProvider;
import io.mosip.pms.common.entity.MISPLicenseEntity;
import io.mosip.pms.common.entity.Partner;
import io.mosip.pms.common.entity.PartnerPolicy;
import io.mosip.pms.common.entity.PartnerPolicyRequest;
import io.mosip.pms.common.exception.ApiAccessibleException;
import io.mosip.pms.common.helper.WebSubPublisher;
import io.mosip.pms.common.repository.AuthPolicyRepository;
import io.mosip.pms.common.repository.BiometricExtractorProviderRepository;
import io.mosip.pms.common.repository.MispLicenseRepository;
import io.mosip.pms.common.repository.MispServiceRepository;
import io.mosip.pms.common.repository.PartnerPolicyRepository;
import io.mosip.pms.common.repository.PartnerPolicyRequestRepository;
import io.mosip.pms.common.repository.PartnerRepository;
import io.mosip.pms.common.repository.PolicyGroupRepository;
import io.mosip.pms.common.response.dto.NotificationDto;
import io.mosip.pms.common.service.NotificatonService;
import io.mosip.pms.common.util.MapperUtils;
import io.mosip.pms.common.util.PMSLogger;
import io.mosip.pms.common.util.RestUtil;
import io.mosip.pms.common.util.UserDetailUtil;
import io.mosip.pms.device.util.AuditUtil;
import io.mosip.pms.partner.constant.PartnerConstants;
import io.mosip.pms.partner.manager.constant.ErrorCode;
import io.mosip.pms.partner.manager.constant.PartnerManageEnum;
import io.mosip.pms.partner.manager.dto.StatusRequestDto;
import io.mosip.pms.partner.manager.dto.ApikeyRequests;
import io.mosip.pms.partner.manager.dto.PartnerAPIKeyToPolicyMappingsResponse;
import io.mosip.pms.partner.manager.dto.PartnerDetailsDto;
import io.mosip.pms.partner.manager.dto.PartnerDetailsResponse;
import io.mosip.pms.partner.manager.dto.PartnersPolicyMappingRequest;
import io.mosip.pms.partner.manager.dto.PartnersPolicyMappingResponse;
import io.mosip.pms.partner.manager.dto.RetrievePartnerDetailsResponse;
import io.mosip.pms.partner.manager.dto.RetrievePartnersDetails;
import io.mosip.pms.partner.manager.exception.PartnerManagerServiceException;
import io.mosip.pms.partner.manager.service.PartnerManagerService;
import io.mosip.pms.partner.request.dto.APIKeyGenerateRequestDto;
import io.mosip.pms.partner.request.dto.APIkeyStatusUpdateRequestDto;
import io.mosip.pms.partner.response.dto.APIKeyGenerateResponseDto;
import io.mosip.pms.partner.response.dto.PartnerCertDownloadResponeDto;
import io.mosip.pms.partner.util.PartnerUtil;
@Service
@Transactional
public class PartnerManagementServiceImpl implements PartnerManagerService {
private static final Logger LOGGER = PMSLogger.getLogger(PartnerManagementServiceImpl.class);
@Autowired
PartnerPolicyRepository partnerPolicyRepository;
@Autowired
PartnerRepository partnerRepository;
@Autowired
PartnerPolicyRequestRepository partnerPolicyRequestRepository;
@Autowired
PolicyGroupRepository policyGroupRepository;
@Autowired
BiometricExtractorProviderRepository extractorProviderRepository;
@Autowired
AuthPolicyRepository authPolicyRepository;
@Autowired
MispServiceRepository mispRepository;
@Autowired
MispLicenseRepository mispLicenseRepository;
@Autowired
private WebSubPublisher webSubPublisher;
@Autowired
private NotificatonService notificationService;
@Autowired
RestUtil restUtil;
@Autowired
AuditUtil auditUtil;
@Autowired
private Environment environment;
@Autowired
private ObjectMapper mapper;
@Value("${pmp.bioextractors.required.partner.types}")
private String biometricExtractorsRequiredPartnerTypes;
@Value("${mosip.pmp.partner.policy.expiry.period.indays}")
private int partnerPolicyExpiryInDays;
@Override
public PartnersPolicyMappingResponse updatePolicyAgainstApikey(PartnersPolicyMappingRequest request,
String partnerId, String partnerApikey) {
PartnerPolicy partnerPolicyFromDb = partnerPolicyRepository.findByPartnerIdAndPolicyIdAndApikey(partnerId,
request.getOldPolicyID(), partnerApikey);
if (partnerPolicyFromDb == null) {
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_APIKEY_POLICY_MAPPING_NOTEXISTS.getErrorCode(),
ErrorCode.PARTNER_APIKEY_POLICY_MAPPING_NOTEXISTS.getErrorMessage());
}
if (!partnerPolicyFromDb.getIsActive()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_APIKEY_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_APIKEY_NOT_ACTIVE_EXCEPTION.getErrorMessage());
}
AuthPolicy requestedPolicy = validateAndGetPolicyOfPolicyGroup(
partnerPolicyFromDb.getPartner().getPolicyGroupId(), request.getNewPolicyID());
PartnerPolicy updateObject = partnerPolicyFromDb;
updateObject.setUpdBy(getUser());
updateObject.setUpdDtimes(Timestamp.valueOf(LocalDateTime.now()));
updateObject.setPolicyId(requestedPolicy.getId());
partnerPolicyRepository.save(updateObject);
PartnersPolicyMappingResponse partnersPolicyMappingResponse = new PartnersPolicyMappingResponse();
partnersPolicyMappingResponse.setMessage("Given apikey updated with policy successfully. ");
notify(null, null, MapperUtils.mapKeyDataToPublishDto(updateObject), EventType.APIKEY_UPDATED);
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_SUCCESS, partnerId, "partnerId");
return partnersPolicyMappingResponse;
}
private AuthPolicy validateAndGetPolicyOfPolicyGroup(String policyGroupId, String policyId) {
AuthPolicy authPolicy = authPolicyRepository.findByPolicyGroupAndId(policyGroupId, policyId);
if (authPolicy == null) {
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_FAILURE, policyId, "policyId");
throw new PartnerManagerServiceException(ErrorCode.POLICY_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.POLICY_NOT_EXIST_EXCEPTION.getErrorMessage() + policyId);
}
if (!authPolicy.getIsActive()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_FAILURE, policyId, "policyId");
throw new PartnerManagerServiceException(ErrorCode.POLICY_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.POLICY_NOT_ACTIVE_EXCEPTION.getErrorMessage() + policyId);
}
if (!authPolicy.getPolicyGroup().getIsActive()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.API_KEY_MAPPING_FAILURE, policyId, "policyId");
throw new PartnerManagerServiceException(ErrorCode.POLICY_GROUP_NOT_ACTIVE.getErrorCode(),
ErrorCode.POLICY_GROUP_NOT_ACTIVE.getErrorMessage() + policyId);
}
return authPolicy;
}
@Override
public PartnersPolicyMappingResponse activateDeactivateAuthEKYCPartner(String partnerId,
StatusRequestDto request) {
Optional partnerFromDb = partnerRepository.findById(partnerId);
if (partnerFromDb.isEmpty()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_KYC_PARTNERS_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorMessage());
}
Partner updatePartnerObject = partnerFromDb.get();
updatePartnerObject.setUpdBy(getUser());
updatePartnerObject.setUpdDtimes(Timestamp.valueOf(LocalDateTime.now()));
PartnersPolicyMappingResponse response = new PartnersPolicyMappingResponse();
if (request.getStatus().equalsIgnoreCase(PartnerConstants.ACTIVE)) {
if(updatePartnerObject.getCertificateAlias() == null) {
throw new PartnerManagerServiceException(ErrorCode.CERTIFICATE_NOT_UPLOADED_EXCEPTION.getErrorCode(),
ErrorCode.CERTIFICATE_NOT_UPLOADED_EXCEPTION.getErrorMessage());
}
//if partner is not active, then only make partner active
if(!updatePartnerObject.getIsActive()) {
updatePartnerObject.setIsActive(true);
partnerRepository.save(updatePartnerObject);
notify(MapperUtils.mapDataToPublishDto(updatePartnerObject,getPartnerCertificate(updatePartnerObject.getCertificateAlias())), null, null, EventType.PARTNER_UPDATED);
sendNotifications(EventType.PARTNER_UPDATED, updatePartnerObject);
}
response.setMessage("Partner activated successfully");
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_KYC_PARTNERS_SUCCESS, partnerId, "partnerId");
return response;
}
if (request.getStatus().equalsIgnoreCase(PartnerConstants.DEACTIVE)) {
// if partner is active, then only make partner in-active
if(updatePartnerObject.getIsActive()) {
updatePartnerObject.setIsActive(false);
partnerRepository.save(updatePartnerObject);
// if partner is misp, then de-activate all licenses.
if(updatePartnerObject.getPartnerTypeCode().equalsIgnoreCase(environment.getProperty(ConfigKeyConstants.MISP_PARTNER_TYPE, "MISP_Partner"))) {
List activeLicenses = mispLicenseRepository.findByMispIdAndIsActive(updatePartnerObject.getId());
// assuming one misp partner will have only one active license key at any giving point of time
for(MISPLicenseEntity license : activeLicenses) {
license.setIsActive(false);
license.setUpdatedBy(getUser());
license.setUpdatedDateTime(LocalDateTime.now());
mispLicenseRepository.save(license);
Map data = new HashMap<>();
data.put("mispLicenseData", MapperUtils.mapDataToPublishDto(license));
notify(data, EventType.MISP_LICENSE_UPDATED);
}
}
notify(MapperUtils.mapDataToPublishDto(updatePartnerObject,getPartnerCertificate(updatePartnerObject.getCertificateAlias())), null, null, EventType.PARTNER_UPDATED);
sendNotifications(EventType.PARTNER_UPDATED, updatePartnerObject);
}
response.setMessage("Partner de-activated successfully");
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_KYC_PARTNERS_SUCCESS, partnerId, "partnerId");
return response;
}
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_KYC_PARTNERS_FAILURE, partnerId, "partnerId");
LOGGER.info(request.getStatus() + " : is Invalid Input Parameter, it should be (Active/De-Active)");
throw new PartnerManagerServiceException(ErrorCode.INVALID_STATUS_CODE_ACTIVE_DEACTIVE.getErrorCode(),
ErrorCode.INVALID_STATUS_CODE_ACTIVE_DEACTIVE.getErrorMessage());
}
@Override
public RetrievePartnerDetailsResponse getAllAuthEKYCPartnersForThePolicyGroup(Optional partnerType) {
RetrievePartnerDetailsResponse partnersResponse = new RetrievePartnerDetailsResponse();
List partners = new ArrayList();
Iterator partnerIterat = getPartnersByPartnerType(partnerType).iterator();
Partner partner = null;
while (partnerIterat.hasNext()) {
RetrievePartnersDetails retrievePartnersDetails = new RetrievePartnersDetails();
partner = partnerIterat.next();
retrievePartnersDetails.setPartnerID(partner.getId());
retrievePartnersDetails
.setStatus(partner.getIsActive() == true ? PartnerConstants.ACTIVE : PartnerConstants.DEACTIVE);
retrievePartnersDetails.setOrganizationName(partner.getName());
retrievePartnersDetails.setContactNumber(partner.getContactNo());
retrievePartnersDetails.setEmailId(partner.getEmailId());
retrievePartnersDetails.setAddress(partner.getAddress());
retrievePartnersDetails.setPartnerType(partner.getPartnerTypeCode());
partners.add(retrievePartnersDetails);
}
partnersResponse.setPartners(partners);
return partnersResponse;
}
/**
*
* @param partnerType
* @return
*/
@Override
public PartnerDetailsResponse getPartners(Optional partnerType) {
PartnerDetailsResponse partnersResponse = new PartnerDetailsResponse();
List partners = new ArrayList();
Iterator partnerIterat = getPartnersByPartnerType(partnerType).iterator();
Partner partner = null;
while (partnerIterat.hasNext()) {
PartnerDetailsDto retrievePartnersDetails = new PartnerDetailsDto();
partner = partnerIterat.next();
retrievePartnersDetails.setPartnerID(partner.getId());
retrievePartnersDetails
.setStatus(partner.getIsActive() == true ? PartnerConstants.ACTIVE : PartnerConstants.DEACTIVE);
retrievePartnersDetails.setOrganizationName(partner.getName());
retrievePartnersDetails.setContactNumber(partner.getContactNo());
retrievePartnersDetails.setEmailId(partner.getEmailId());
retrievePartnersDetails.setAddress(partner.getAddress());
retrievePartnersDetails.setPartnerType(partner.getPartnerTypeCode());
retrievePartnersDetails.setLogoUrl(partner.getLogoUrl());
retrievePartnersDetails.setAdditionalInfo(
partner.getAdditionalInfo() == null ? null : getValidJson(partner.getAdditionalInfo()));
partners.add(retrievePartnersDetails);
}
partnersResponse.setPartners(partners);
return partnersResponse;
}
private JsonNode getValidJson(String jsonInString) {
try {
return mapper.readTree(jsonInString);
} catch (IOException e) {
LOGGER.error("Given addtional info is not a valid json object ", e);
throw new PartnerManagerServiceException(ErrorCode.JSON_NOT_VALID.getErrorCode(),
ErrorCode.JSON_NOT_VALID.getErrorMessage());
}
}
/**
*
* @param partnerType
* @return
*/
private List getPartnersByPartnerType(Optional partnerType) {
List partnersFromDb = null;
if (partnerType.isPresent() && !partnerType.get().trim().isEmpty()) {
partnersFromDb = partnerRepository.findByPartnerType(partnerType.get());
} else {
partnersFromDb = partnerRepository.findAll();
}
if (partnersFromDb.isEmpty()) {
LOGGER.error("Partners not exists in database");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_DOES_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_DOES_NOT_EXIST_EXCEPTION.getErrorMessage());
}
return partnersFromDb;
}
@Override
public PartnerAPIKeyToPolicyMappingsResponse getPartnerAPIKeyToPolicyMapping(String partnerId,
String partnerAPIKey) {
PartnerPolicy partnerPolicyFromDb = partnerPolicyRepository.findByPartnerIdAndApikey(partnerId, partnerAPIKey);
if (partnerPolicyFromDb == null) {
LOGGER.error("Given apikey {} not mapped to partner {}", partnerAPIKey, partnerId);
throw new PartnerManagerServiceException(ErrorCode.PARTNER_API_KEY_NOT_MAPPED.getErrorCode(),
ErrorCode.PARTNER_API_KEY_NOT_MAPPED.getErrorMessage());
}
PartnerAPIKeyToPolicyMappingsResponse response = new PartnerAPIKeyToPolicyMappingsResponse();
response.setPartnerID(partnerId);
response.setPolicyId(partnerPolicyFromDb.getPolicyId());
return response;
}
@Override
public List getAllPartnerAPIKeyRequestsAsReceivedByPartnerManagers() {
List apikeyRequestsFromDb = partnerPolicyRequestRepository.findAll();
if (apikeyRequestsFromDb.isEmpty()) {
LOGGER.error("No apikey requests exists");
throw new PartnerManagerServiceException(ErrorCode.NO_PARTNER_API_KEY_REQUEST_EXCEPTION.getErrorCode(),
ErrorCode.NO_PARTNER_API_KEY_REQUEST_EXCEPTION.getErrorMessage());
}
List response = new ArrayList();
for (PartnerPolicyRequest partnerPolicyRequest : apikeyRequestsFromDb) {
ApikeyRequests apikeyRequests = new ApikeyRequests();
apikeyRequests.setApiKeyReqNo(partnerPolicyRequest.getId());
apikeyRequests.setOrganizationName(partnerPolicyRequest.getPartner().getName());
apikeyRequests.setPartnerID(partnerPolicyRequest.getPartner().getId());
apikeyRequests.setPolicyDesc(partnerPolicyRequest.getRequestDetail());
apikeyRequests.setPolicyId(partnerPolicyRequest.getPolicyId());
apikeyRequests.setStatus(partnerPolicyRequest.getStatusCode());
response.add(apikeyRequests);
}
return response;
}
@Override
public ApikeyRequests getTheRequestForPartnerAPIKeyToPolicyMappingsForGivenRequestId(String apiKeyReqId) {
Optional apikeyRequestsFromDb = partnerPolicyRequestRepository.findById(apiKeyReqId);
if (apikeyRequestsFromDb.isEmpty()) {
LOGGER.error("APIKey request is not exists with id {}", apiKeyReqId);
throw new PartnerManagerServiceException(ErrorCode.NO_PARTNER_API_KEY_REQUEST_EXCEPTION.getErrorCode(),
ErrorCode.NO_PARTNER_API_KEY_REQUEST_EXCEPTION.getErrorMessage());
}
ApikeyRequests apikeyRequest = new ApikeyRequests();
apikeyRequest.setApiKeyReqNo(apikeyRequestsFromDb.get().getId());
apikeyRequest.setOrganizationName(apikeyRequestsFromDb.get().getPartner().getName());
apikeyRequest.setPartnerID(apikeyRequestsFromDb.get().getPartner().getId());
apikeyRequest.setPolicyDesc(apikeyRequestsFromDb.get().getRequestDetail());
apikeyRequest.setPolicyId(apikeyRequestsFromDb.get().getPolicyId());
apikeyRequest.setStatus(apikeyRequestsFromDb.get().getStatusCode());
return apikeyRequest;
}
private PartnerPolicyRequest getValidApikeyRequestForStatusUpdate(String requestedApikey) {
Optional partnerPolicyRequestFromDb = partnerPolicyRequestRepository
.findById(requestedApikey);
if (partnerPolicyRequestFromDb.isEmpty()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.APPROVE_REJECT_PARTNER_API_FAILURE);
throw new PartnerManagerServiceException(ErrorCode.PARTNER_API_DOES_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_API_DOES_NOT_EXIST_EXCEPTION.getErrorMessage());
}
if (partnerPolicyRequestFromDb.get().getStatusCode().equalsIgnoreCase(PartnerConstants.APPROVED)) {
auditUtil.setAuditRequestDto(PartnerManageEnum.APPROVE_REJECT_PARTNER_API_FAILURE);
throw new PartnerManagerServiceException(ErrorCode.POLICY_REQUEST_ALREADY_APPROVED.getErrorCode(),
ErrorCode.POLICY_REQUEST_ALREADY_APPROVED.getErrorMessage());
}
if (partnerPolicyRequestFromDb.get().getStatusCode().equalsIgnoreCase(PartnerConstants.REJECTED)) {
auditUtil.setAuditRequestDto(PartnerManageEnum.APPROVE_REJECT_PARTNER_API_FAILURE);
throw new PartnerManagerServiceException(ErrorCode.POLICY_REQUEST_ALREADY_REJECTED.getErrorCode(),
ErrorCode.POLICY_REQUEST_ALREADY_REJECTED.getErrorMessage());
}
if (Arrays.stream(biometricExtractorsRequiredPartnerTypes.split(","))
.anyMatch(partnerPolicyRequestFromDb.get().getPartner().getPartnerTypeCode()::equalsIgnoreCase)) {
List extractorsFromDb = extractorProviderRepository.findByPartnerAndPolicyId(
partnerPolicyRequestFromDb.get().getPartner().getId(),
partnerPolicyRequestFromDb.get().getPolicyId());
if (extractorsFromDb.isEmpty()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.APPROVE_REJECT_PARTNER_API_FAILURE);
throw new PartnerManagerServiceException(ErrorCode.EXTRACTORS_NOT_PRESENT.getErrorCode(),
ErrorCode.EXTRACTORS_NOT_PRESENT.getErrorMessage());
}
}
return partnerPolicyRequestFromDb.get();
}
private JSONObject getPolicyObject(String policy) {
JSONParser parser = new JSONParser();
String error = null;
try {
return ((JSONObject) parser.parse(policy));
} catch (ParseException e) {
error = e.getMessage();
}
auditUtil.setAuditRequestDto(PartnerManageEnum.GET_POLICY_FAILURE);
throw new PartnerManagerServiceException(ErrorCode.POLICY_PARSING_ERROR.getErrorCode(),
ErrorCode.POLICY_PARSING_ERROR.getErrorMessage() + error);
}
@SuppressWarnings("unchecked")
private String getPartnerCertificate(String certificateAlias) {
Map pathsegments = new HashMap<>();
pathsegments.put("partnerCertId", certificateAlias);
Map getApiResponse = restUtil
.getApi(environment.getProperty("pmp.partner.certificaticate.get.rest.uri"), pathsegments, Map.class);
PartnerCertDownloadResponeDto responseObject = null;
try {
responseObject = mapper.readValue(mapper.writeValueAsString(getApiResponse.get("response")),
PartnerCertDownloadResponeDto.class);
} catch (IOException e) {
LOGGER.error("Error occured while parsing the response ", e);
throw new ApiAccessibleException(ApiAccessibleExceptionConstant.UNABLE_TO_PROCESS.getErrorCode(),
ApiAccessibleExceptionConstant.UNABLE_TO_PROCESS.getErrorMessage());
}
if (responseObject == null && getApiResponse.containsKey(PartnerConstants.ERRORS)) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy