lundi 26 septembre 2022

Improving large switch case

I have an API (built with spring boot) which has a method that provides a template file on format .xlxs delivered on a response DTO object with a base64 inside of it.

    @GetMapping("/getLoadTemplate")
    public ResponseEntity<?> getLoadTemplate(@RequestParam(name = "type", defaultValue = "!") String type) {
        ResponseDTO responseDTO;
        if (notInTypes(type))
            return ResponseEntity.badRequest().body(badRequest("Tipo de archivo invalido."));
        try {
            responseDTO = loadQuotasService.getFileTemplate(type);
            return ResponseEntity.status(responseDTO.getStatus()).body(responseDTO);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(errorResponse(logger, e));
        }
    }

The request object is a simple string that can be on any of the types of the classes that have a xlxs template, so each class has it own LOAD_NAME value. Example public final static String LOAD_NAME = "POLITICAS_AFILIADO"

public static boolean notInTypes(String type){
        if(type == null) return true;
        String[] validTypes = {QuotaLoad.LOAD_NAME, QuotaModify.LOAD_NAME, QuotaState.LOAD_NAME,
                AffiliatePolitics.LOAD_NAME, AffiliateRisk.LOAD_NAME, BuyerEvaluation.LOAD_NAME,
                HabeasRegister.LOAD_NAME, QuotaBuyer.LOAD_NAME, ClintonList.LOAD_NAME, BuyerQualification.LOAD_NAME};
        return !Arrays.asList(validTypes).contains(type);
    }

PROBLEM This is the service method ... It's filled of repetitive code and a super big switch block. Will be difficult to mantainf

public ResponseDTO getFileTemplate(String type) {
        ASRFile template = null;
        switch (type) {
            case QuotaLoad.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(QuotaLoad.TEMPLATE, QuotaLoad.CATEGORY);
                break;
            case QuotaModify.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(QuotaModify.TEMPLATE, QuotaModify.CATEGORY);
                break;
            case QuotaState.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(QuotaState.TEMPLATE, QuotaState.CATEGORY);
                break;
            case AffiliatePolitics.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(AffiliatePolitics.TEMPLATE,
                        AffiliatePolitics.CATEGORY);
                break;
            case AffiliateRisk.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(AffiliateRisk.TEMPLATE,
                        AffiliateRisk.CATEGORY);
                break;
            case BuyerEvaluation.LOAD_NAME:
                template = fileRepository.findByFileOriginalNameAndCategory(BuyerEvaluation.TEMPLATE,
                        BuyerEvaluation.CATEGORY);
                break;
// more equal code ...

How can I impprove this repetitive code?

Aucun commentaire:

Enregistrer un commentaire