package net.gepafin.tendermanagement.dao; import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import net.gepafin.tendermanagement.entities.S3ConfigEntity; import net.gepafin.tendermanagement.model.request.S3ConfigReq; import net.gepafin.tendermanagement.model.response.S3ConfigBean; import net.gepafin.tendermanagement.repositories.S3ConfigRepository; import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException; import net.gepafin.tendermanagement.web.rest.api.errors.Status; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Optional; import static net.gepafin.tendermanagement.util.Utils.setIfUpdated; @Component public class S3ConfigDao { private static final Logger log = LoggerFactory.getLogger(S3ConfigDao.class); @Autowired S3ConfigRepository s3ConfigRepository; public S3ConfigBean addS3Path(S3ConfigReq s3PathConfigurationReq) { log.info("Adding s3 s3PathConfigurationReq structure with it's type.."); S3ConfigEntity s3PathConfigurationEntity = convertToS3pathEntity(s3PathConfigurationReq); s3PathConfigurationEntity = s3ConfigRepository.save(s3PathConfigurationEntity); log.info("Added s3 path config details {} to DB.", s3PathConfigurationEntity); return convertToS3pathBean(s3PathConfigurationEntity); } private S3ConfigEntity convertToS3pathEntity(S3ConfigReq s3PathConfigReq) { S3ConfigEntity s3PathConfigEntity = new S3ConfigEntity(); s3PathConfigEntity.setPath(s3PathConfigReq.getPath()); s3PathConfigEntity.setType(s3PathConfigReq.getType()); s3PathConfigEntity.setBucketName(s3PathConfigReq.getBucketName()); return s3PathConfigEntity; } private S3ConfigBean convertToS3pathBean(S3ConfigEntity s3PathConfigReq) { S3ConfigBean s3PathConfigBean = new S3ConfigBean(); s3PathConfigBean.setPath(s3PathConfigReq.getPath()); s3PathConfigBean.setType(s3PathConfigReq.getType()); s3PathConfigBean.setBucketName(s3PathConfigReq.getBucketName()); return s3PathConfigBean; } public Optional getS3PathByType(String type) { log.info("Fetching S3-Path structure by type: {}", type); Optional s3PathData = s3ConfigRepository.getPathByType(type); if (s3PathData == null) { log.error("No S3-Path found for type: {}", type); throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.S3_PATH_STRUCTURE_NOT_FOUND_BY_TYPE_MSG)); } log.info("Fetched S3-Path: {} for type: {}", s3PathData, type); return s3PathData; } public S3ConfigEntity deleteS3PathConfigById(Long id) { log.info("Checking s3-path associated with this id {} to delete....", id); S3ConfigEntity s3PathConfigData = s3ConfigRepository.findS3PathConfigurationById(id); if (s3PathConfigData == null) { log.error("No S3-Path found for id: {}", id); throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.S3_PATH_STRUCTURE_NOT_FOUND_BY_ID_MSG)); } else { log.info("Found s3-path associated with this id {} to delete.", id); s3ConfigRepository.deleteById(id); log.error("Deleted s3-path configuration successfully for id: {}", id); return s3PathConfigData; } } public S3ConfigBean updateS3PathConfiguration(S3ConfigReq s3PathConfigurationReq, Long id) { log.info("Updating S3-path Configuration."); S3ConfigEntity s3PathConfigDataExists = s3ConfigRepository.findS3PathConfigurationById(id); if (s3PathConfigDataExists == null) { log.error("No S3-Path found for id: {}", id); throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.S3_PATH_STRUCTURE_NOT_FOUND_BY_ID_MSG)); } else { Optional s3PathData = s3ConfigRepository.getPathByType(s3PathConfigurationReq.getType()); if(s3PathData != null){ log.error("S3-Path type already exist. {}", s3PathData); throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.S3_PATH_CONFIG_DUPLICATE_TYPE_ALREADY_EXIST)); } S3ConfigEntity s3PathConfigurationEntity = convertToS3pathEntity(s3PathConfigurationReq); setIfUpdated(s3PathConfigurationEntity::getPath, s3PathConfigurationEntity::setPath, s3PathConfigurationReq.getPath()); setIfUpdated(s3PathConfigurationEntity::getBucketName, s3PathConfigurationEntity::setBucketName, s3PathConfigurationReq.getBucketName()); setIfUpdated(s3PathConfigurationEntity::getType, s3PathConfigurationEntity::setType, s3PathConfigurationReq.getType()); // s3PathConfigurationEntity.setType(s3PathConfigurationReq.getType()); // s3PathConfigurationEntity.setPath(s3PathConfigurationReq.getPath()); // s3PathConfigurationEntity.setBucketName(s3PathConfigurationReq.getBucketName()); s3ConfigRepository.save(s3PathConfigurationEntity); log.info("Updated S3-path-configuration successfully."); return convertToS3pathBean(s3PathConfigurationEntity); } } }