io.mosip.pms.device.authdevice.service.impl.FTPChipDetailServiceImpl Maven / Gradle / Ivy
package io.mosip.pms.device.authdevice.service.impl;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.kernel.core.util.EmptyCheckUtils;
import io.mosip.pms.common.constant.ApiAccessibleExceptionConstant;
import io.mosip.pms.common.dto.PageResponseDto;
import io.mosip.pms.common.entity.Partner;
import io.mosip.pms.common.exception.ApiAccessibleException;
import io.mosip.pms.common.exception.RequestException;
import io.mosip.pms.common.helper.SearchHelper;
import io.mosip.pms.common.repository.PartnerServiceRepository;
import io.mosip.pms.common.request.dto.RequestWrapper;
import io.mosip.pms.common.util.MapperUtils;
import io.mosip.pms.common.util.RestUtil;
import io.mosip.pms.device.authdevice.entity.FTPChipDetail;
import io.mosip.pms.device.authdevice.entity.FoundationalTrustProvider;
import io.mosip.pms.device.authdevice.repository.FTPChipDetailRepository;
import io.mosip.pms.device.authdevice.repository.FoundationalTrustProviderRepository;
import io.mosip.pms.device.authdevice.service.FtpChipDetailService;
import io.mosip.pms.device.constant.DeviceConstant;
import io.mosip.pms.device.constant.FoundationalTrustProviderErrorMessages;
import io.mosip.pms.device.request.dto.DeviceSearchDto;
import io.mosip.pms.device.request.dto.FtpCertificateRequestDto;
import io.mosip.pms.device.request.dto.FtpChipCertDownloadRequestDto;
import io.mosip.pms.device.request.dto.FtpChipCertificateRequestDto;
import io.mosip.pms.device.request.dto.FtpChipDetailDto;
import io.mosip.pms.device.request.dto.FtpChipDetailStatusDto;
import io.mosip.pms.device.request.dto.FtpChipDetailUpdateDto;
import io.mosip.pms.device.response.dto.FTPSearchResponseDto;
import io.mosip.pms.device.response.dto.FtpCertDownloadResponeDto;
import io.mosip.pms.device.response.dto.FtpCertificateResponseDto;
import io.mosip.pms.device.response.dto.IdDto;
import io.mosip.pms.device.util.AuditUtil;
import io.mosip.pms.device.util.DeviceUtil;
@Component
@Transactional
public class FTPChipDetailServiceImpl implements FtpChipDetailService {
@PersistenceContext(unitName = "authDeviceEntityManagerFactory")
private EntityManager entityManager;
@Autowired
SearchHelper searchHelper;
@Autowired
AuditUtil auditUtil;
@Autowired
RestUtil restUtil;
@Autowired
private Environment environment;
@Autowired
FTPChipDetailRepository ftpChipDetailRepository;
@Autowired
FoundationalTrustProviderRepository foundationalTrustProviderRepository;
@Autowired
PartnerServiceRepository partnerServiceRepository;
@Autowired
private ObjectMapper mapper;
private static final String ERRORS = "errors";
private static final String ERRORCODE = "errorCode";
private static final String ERRORMESSAGE = "message";
@Override
public IdDto createFtpChipDetails(FtpChipDetailDto chipDetails) {
Partner partnerFromDb = partnerServiceRepository.findByIdAndIsDeletedFalseorIsDeletedIsNullAndIsActiveTrue(chipDetails.getFtpProviderId());
if(partnerFromDb == null) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage());
}
FTPChipDetail uniqueChipDetail = ftpChipDetailRepository.findByUniqueKey(chipDetails.getFtpProviderId(),
chipDetails.getMake(), chipDetails.getModel());
if(uniqueChipDetail != null){
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorMessage());
}
FoundationalTrustProvider ftpProvider = foundationalTrustProviderRepository.findByIdAndIsActiveTrue(chipDetails.getFtpProviderId());
FoundationalTrustProvider entity = new FoundationalTrustProvider();
entity.setActive(true);
entity.setDeleted(false);
Authentication authN = SecurityContextHolder.getContext().getAuthentication();
if (!EmptyCheckUtils.isNullEmpty(authN)) {
entity.setCrBy(authN.getName());
}
entity.setCrDtimes(LocalDateTime.now());
entity.setId(partnerFromDb.getId());
if(ftpProvider == null) {
foundationalTrustProviderRepository.save(entity);
}
FTPChipDetail chipDetail = new FTPChipDetail();
chipDetail.setCrBy(authN.getName());
chipDetail.setActive(false);
chipDetail.setDeleted(false);
chipDetail.setCrDtimes(LocalDateTime.now());
chipDetail.setFtpProviderId(chipDetails.getFtpProviderId());
chipDetail.setFtpChipDetailId(DeviceUtil.generateId());
chipDetail.setMake(chipDetails.getMake());
chipDetail.setModel(chipDetails.getModel());
chipDetail.setPartnerOrganizationName(partnerFromDb.getName());
ftpChipDetailRepository.save(chipDetail);
IdDto response = new IdDto();
response.setId(chipDetail.getFtpChipDetailId());
return response;
}
@Override
public IdDto updateFtpChipDetails(FtpChipDetailUpdateDto chipDetails) {
Partner partnerFromDb = partnerServiceRepository.findByIdAndIsDeletedFalseorIsDeletedIsNullAndIsActiveTrue(chipDetails.getFtpProviderId());
if(partnerFromDb == null) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage());
}
Optional chipDetail = ftpChipDetailRepository.findById(chipDetails.getFtpChipDetailId());
if(chipDetail.isEmpty()) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage());
}
FTPChipDetail uniqueChipDetail = ftpChipDetailRepository.findByUniqueKey(chipDetails.getFtpProviderId(),
chipDetails.getMake(), chipDetails.getModel());
if(uniqueChipDetail != null && !chipDetail.get().getFtpChipDetailId().equals(uniqueChipDetail.getFtpChipDetailId())){
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_MAKE_MODEL_EXISTS.getErrorMessage());
}
Optional ftpProvider = foundationalTrustProviderRepository.findById(partnerFromDb.getId());
FoundationalTrustProvider entity = null;
Authentication authN = SecurityContextHolder.getContext().getAuthentication();
if(ftpProvider.isEmpty()) {
entity = new FoundationalTrustProvider();
entity.setCrDtimes(LocalDateTime.now());
entity.setId(partnerFromDb.getId());
//entity.setPartnerOrganizationName(partnerFromDb.getName());
entity.setActive(true);
entity.setCrBy(authN.getName());
}else {
entity = ftpProvider.get();
//entity.setPartnerOrganizationName(partnerFromDb.getName());
entity.setUpdDtimes(LocalDateTime.now());
entity.setUpdBy(authN.getName());
}
foundationalTrustProviderRepository.save(entity);
FTPChipDetail updateObject = chipDetail.get();
if (!EmptyCheckUtils.isNullEmpty(authN)) {
updateObject.setUpdBy(authN.getName());
}
updateObject.setUpdDtimes(LocalDateTime.now());
updateObject.setMake(chipDetails.getMake());
updateObject.setModel(chipDetails.getModel());
updateObject.setFtpProviderId(chipDetails.getFtpProviderId());
ftpChipDetailRepository.save(updateObject);
IdDto response = new IdDto();
response.setId(updateObject.getFtpChipDetailId());
return response;
}
@Override
public String updateFtpChipDetailStatus(FtpChipDetailStatusDto chipDetails) {
Optional chipDetail = ftpChipDetailRepository.findById(chipDetails.getFtpChipDetailId());
if(chipDetail.isEmpty()) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailStatusDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage());
}
FTPChipDetail updateObject = chipDetail.get();
Authentication authN = SecurityContextHolder.getContext().getAuthentication();
if (!EmptyCheckUtils.isNullEmpty(authN)) {
updateObject.setUpdBy(authN.getName());
}
updateObject.setUpdDtimes(LocalDateTime.now());
updateObject.setActive(chipDetails.getApprovalStatus());
ftpChipDetailRepository.save(updateObject);
return "Status updated successfully.";
}
@SuppressWarnings("unchecked")
@Override
public FtpCertificateResponseDto uploadPartnerCertificate(FtpChipCertificateRequestDto ftpChipCertRequestDto)
throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
Partner partnerFromDb = partnerServiceRepository.findByIdAndIsDeletedFalseorIsDeletedIsNullAndIsActiveTrue(ftpChipCertRequestDto.getFtpProviderId());
if(partnerFromDb == null) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_PROVIDER_NOT_EXISTS.getErrorMessage());
}
Optional chipDetail = ftpChipDetailRepository.findById(ftpChipCertRequestDto.getFtpChipDeatilId());
if(chipDetail.isEmpty()) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_NOT_EXISTS.getErrorMessage());
}
if(!chipDetail.get().getFtpProviderId().equals(ftpChipCertRequestDto.getFtpProviderId())) {
auditUtil.auditRequest(
String.format(
DeviceConstant.FAILURE_CREATE, FtpChipDetailUpdateDto.class.getCanonicalName()),
DeviceConstant.AUDIT_SYSTEM,
String.format(DeviceConstant.FAILURE_DESC,
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_PROVIDER_ID_NOT_MATCHED.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_PROVIDER_ID_NOT_MATCHED.getErrorMessage()),
"AUT-003");
throw new RequestException(FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_PROVIDER_ID_NOT_MATCHED.getErrorCode(),
FoundationalTrustProviderErrorMessages.FTP_CHIP_ID_PROVIDER_ID_NOT_MATCHED.getErrorMessage());
}
FtpCertificateRequestDto certRequest = new FtpCertificateRequestDto();
certRequest.setCertificateData(ftpChipCertRequestDto.getCertificateData());
certRequest.setOrganizationName(ftpChipCertRequestDto.getOrganizationName());
certRequest.setPartnerDomain(ftpChipCertRequestDto.getPartnerDomain());
certRequest.setPartnerId(chipDetail.get().getFtpChipDetailId());
certRequest.setPartnerType(partnerFromDb.getPartnerTypeCode());
RequestWrapper request = new RequestWrapper<>();
request.setRequest(certRequest);
Map uploadApiResponse = restUtil.postApi(environment.getProperty("pmp.partner.certificaticate.upload.rest.uri"), null, "", "",
MediaType.APPLICATION_JSON, request, Map.class);
FtpCertificateResponseDto responseObject = mapper.readValue(mapper.writeValueAsString(uploadApiResponse.get("response")), FtpCertificateResponseDto.class);
if(responseObject == null && uploadApiResponse.containsKey(ERRORS)) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy