package net.gepafin.tendermanagement.dao; import net.gepafin.tendermanagement.entities.S3ConfigEntity; import net.gepafin.tendermanagement.enums.CompanyDocSourceTypeEnum; import net.gepafin.tendermanagement.enums.CompanyDocumentTypeEnum; import net.gepafin.tendermanagement.enums.DocOtherSourceTypeEnum; import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum; import net.gepafin.tendermanagement.repositories.S3ConfigRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class S3PathConfig { @Autowired S3ConfigRepository s3ConfigRepository; public String generateDocumentPath(DocumentSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId) { S3ConfigEntity config = getDocumentPath(type); return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId); } public String generateDocumentPathForOther(DocOtherSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId) { S3ConfigEntity config = getDocumentPathForOther(type); return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId); } public String generateDocumentPathForDelegationAndSignedDocument(DocOtherSourceTypeEnum type) { S3ConfigEntity config = getDocumentPathForOther(type); return config.getParentFolder() + "/" + config.getPath(); } private String buildS3Path(String pathTemplate, Long callId, Long applicationId, Long amendmentId,Long communicationId) { return pathTemplate .replace("{call_id}", callId != null && callId != 0L ? "call_" + callId : "") .replace("{application_id}", applicationId != null && applicationId != 0L ? "application_" + applicationId : "") .replace("{amendment_id}", amendmentId != null && amendmentId != 0L ? "amendment_" + amendmentId : "") .replace("{communication_id}", communicationId != null && communicationId != 0L ? "communication_" + communicationId : ""); } private S3ConfigEntity getDocumentPath(DocumentSourceTypeEnum type) { return s3ConfigRepository.getPathByType(type.name()).orElseThrow(() -> new IllegalArgumentException("No path configuration found for type: " + type)); } private S3ConfigEntity getDocumentPathForOther(DocOtherSourceTypeEnum type) { return s3ConfigRepository.getPathByType(type.name()).orElseThrow(() -> new IllegalArgumentException("No path configuration found for type: " + type)); } public String getBucketNameForOtherType(DocOtherSourceTypeEnum type){ return s3ConfigRepository.getBucketNameByType(type); } public String getBucketNameForCallAppType(DocumentSourceTypeEnum type){ return s3ConfigRepository.getBucketNameByType(type); } public String generateCompanyDocumentPath(CompanyDocumentTypeEnum type, Long companyId) { S3ConfigEntity config = getCompanyDocumentPath(type); return config.getParentFolder() + "/" + buildCompanyDocumentS3Path(config.getPath(), companyId); } private String buildCompanyDocumentS3Path(String pathTemplate, Long companyId) { return pathTemplate .replace("{company_id}", companyId != null && companyId != 0L ? "company_" + companyId : ""); } private S3ConfigEntity getCompanyDocumentPath(CompanyDocumentTypeEnum type) { return s3ConfigRepository.getPathByType(type.name()).orElseThrow(() -> new IllegalArgumentException("No path configuration found for type: " + type)); } private S3ConfigEntity getCompanyDocumentPathForOther(CompanyDocSourceTypeEnum type) { return s3ConfigRepository.getPathByType(type.name()).orElseThrow(() -> new IllegalArgumentException("No path configuration found for type: " + type)); } public String generateCompanyDocumentPathForOther(CompanyDocSourceTypeEnum type, Long companyId) { S3ConfigEntity config = getCompanyDocumentPathForOther(type); return config.getParentFolder() + "/" + buildCompanyDocumentS3Path(config.getPath(),companyId); } }